userbot.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. """
  2. CREDITS:
  3. EVAL AND SH FUNCTION IN THIS MODULE IS WRITTEN BY @Pokurt.
  4. SOURCE:
  5. https://github.com/pokurt/Nana-Remix/blob/master/nana/plugins/devs.py
  6. """
  7. import os
  8. import re
  9. import subprocess
  10. import sys
  11. import traceback
  12. from html import escape
  13. from inspect import getfullargspec
  14. from io import StringIO
  15. from pyrogram import filters
  16. from pyrogram.types import Message, ReplyKeyboardMarkup
  17. from wbb import app # don't remove
  18. from wbb import SUDOERS, USERBOT_PREFIX, app2, arq
  19. from wbb.core.decorators.misc import exec_time
  20. # Eval and Sh module from nana-remix
  21. m = None
  22. p = print
  23. r = None
  24. exec_time = exec_time
  25. arq = arq
  26. async def aexec(code, client, message):
  27. exec(
  28. "async def __aexec(client, message): "
  29. + "".join(f"\n {a}" for a in code.split("\n"))
  30. )
  31. return await locals()["__aexec"](client, message)
  32. async def edit_or_reply(msg: Message, **kwargs):
  33. func = msg.edit_text if msg.from_user.is_self else msg.reply
  34. spec = getfullargspec(func.__wrapped__).args
  35. return await func(
  36. **{k: v for k, v in kwargs.items() if k in spec}
  37. )
  38. @app2.on_message(
  39. filters.user(SUDOERS)
  40. & ~filters.forwarded
  41. & ~filters.via_bot
  42. & ~filters.edited
  43. & filters.command("py", prefixes=USERBOT_PREFIX)
  44. )
  45. async def executor(client, message: Message):
  46. global m, p, r
  47. try:
  48. cmd = message.text.split(" ", maxsplit=1)[1]
  49. except IndexError:
  50. return await message.delete()
  51. m = message
  52. p = print
  53. if m.reply_to_message:
  54. r = m.reply_to_message
  55. if r.reply_markup:
  56. if isinstance(r.reply_markup, ReplyKeyboardMarkup):
  57. return await m.edit("INSECURE!")
  58. old_stderr = sys.stderr
  59. old_stdout = sys.stdout
  60. redirected_output = sys.stdout = StringIO()
  61. redirected_error = sys.stderr = StringIO()
  62. stdout, stderr, exc = None, None, None
  63. try:
  64. await aexec(cmd, client, message)
  65. except Exception:
  66. exc = traceback.format_exc()
  67. stdout = redirected_output.getvalue()
  68. stderr = redirected_error.getvalue()
  69. sys.stdout = old_stdout
  70. sys.stderr = old_stderr
  71. evaluation = ""
  72. if exc:
  73. evaluation = exc
  74. elif stderr:
  75. evaluation = stderr
  76. elif stdout:
  77. evaluation = stdout
  78. else:
  79. evaluation = "Success"
  80. final_output = f"**INPUT:**\n```{escape(cmd)}```\n\n**OUTPUT**:\n```{escape(evaluation.strip())}```"
  81. if len(final_output) > 4096:
  82. filename = "output.txt"
  83. with open(filename, "w+", encoding="utf8") as out_file:
  84. out_file.write(str(evaluation.strip()))
  85. await message.reply_document(
  86. document=filename,
  87. caption=f"**INPUT:**\n`{escape(cmd[0:980])}`\n\n**OUTPUT:**\n`Attached Document`",
  88. quote=False,
  89. )
  90. await message.delete()
  91. os.remove(filename)
  92. else:
  93. await edit_or_reply(message, text=final_output)
  94. @app2.on_message(
  95. filters.user(SUDOERS)
  96. & ~filters.forwarded
  97. & ~filters.via_bot
  98. & ~filters.edited
  99. & filters.command("sh", prefixes=USERBOT_PREFIX),
  100. )
  101. async def shellrunner(client, message: Message):
  102. if len(message.command) < 2:
  103. return await edit_or_reply(
  104. message, text="**Usage:**\n/sh git pull"
  105. )
  106. if message.reply_to_message:
  107. r = message.reply_to_message
  108. if r.reply_markup:
  109. if isinstance(r.reply_markup, ReplyKeyboardMarkup):
  110. return await message.edit("INSECURE!")
  111. text = message.text.split(None, 1)[1]
  112. if "\n" in text:
  113. code = text.split("\n")
  114. output = ""
  115. for x in code:
  116. shell = re.split(
  117. """ (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x
  118. )
  119. try:
  120. process = subprocess.Popen(
  121. shell,
  122. stdout=subprocess.PIPE,
  123. stderr=subprocess.PIPE,
  124. )
  125. except Exception as err:
  126. print(err)
  127. await edit_or_reply(
  128. message,
  129. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{escape(err)}```",
  130. )
  131. output += f"**{code}**\n"
  132. output += process.stdout.read()[:-1].decode("utf-8")
  133. output += "\n"
  134. else:
  135. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  136. for a in range(len(shell)):
  137. shell[a] = shell[a].replace('"', "")
  138. try:
  139. process = subprocess.Popen(
  140. shell,
  141. stdout=subprocess.PIPE,
  142. stderr=subprocess.PIPE,
  143. )
  144. except Exception as err:
  145. print(err)
  146. exc_type, exc_obj, exc_tb = sys.exc_info()
  147. errors = traceback.format_exception(
  148. etype=exc_type,
  149. value=exc_obj,
  150. tb=exc_tb,
  151. )
  152. return await edit_or_reply(
  153. message,
  154. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{''.join(errors)}```",
  155. )
  156. output = process.stdout.read()[:-1].decode("utf-8")
  157. if str(output) == "\n":
  158. output = None
  159. if output:
  160. if len(output) > 4096:
  161. with open("output.txt", "w+") as file:
  162. file.write(output)
  163. await app2.send_document(
  164. message.chat.id,
  165. "output.txt",
  166. reply_to_message_id=message.message_id,
  167. caption=escape(text),
  168. )
  169. return os.remove("output.txt")
  170. await edit_or_reply(
  171. message,
  172. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT:**\n```{escape(output)}```",
  173. )
  174. else:
  175. await edit_or_reply(
  176. message,
  177. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT: **\n`No output`",
  178. )
  179. def shell(cmd: list) -> tuple:
  180. proc = subprocess.run(
  181. cmd,
  182. stdout=subprocess.PIPE,
  183. stderr=subprocess.PIPE,
  184. )
  185. stdout, stderr = proc.stdout, proc.stderr
  186. stdout = stdout.decode() if stdout else None
  187. stderr = stderr.decode() if stderr else None
  188. return stdout, stderr