userbot.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. __MODULE__ = "Userbot"
  21. __HELP__ = """
  22. .alive - Send Alive Message.
  23. .py - Execute Python Code.
  24. .sh - Execute Shell Code.
  25. .approve | .disapprove - Approve Or Disapprove A User To PM You.
  26. .block | .unblock - Block Or Unblock A User.
  27. .create (b|s|c) Name - create [basic|super]group & channel
  28. """
  29. # Eval and Sh module from nana-remix
  30. m = None
  31. p = print
  32. r = None
  33. exec_time = exec_time
  34. arq = arq
  35. async def aexec(code, client, message):
  36. exec(
  37. "async def __aexec(client, message): "
  38. + "".join(f"\n {a}" for a in code.split("\n"))
  39. )
  40. return await locals()["__aexec"](client, message)
  41. async def edit_or_reply(msg: Message, **kwargs):
  42. func = msg.edit_text if msg.from_user.is_self else msg.reply
  43. spec = getfullargspec(func.__wrapped__).args
  44. return await func(
  45. **{k: v for k, v in kwargs.items() if k in spec}
  46. )
  47. @app2.on_message(
  48. filters.user(SUDOERS)
  49. & ~filters.forwarded
  50. & ~filters.via_bot
  51. & ~filters.edited
  52. & filters.command("py", prefixes=USERBOT_PREFIX)
  53. )
  54. async def executor(client, message: Message):
  55. global m, p, r
  56. try:
  57. cmd = message.text.split(" ", maxsplit=1)[1]
  58. except IndexError:
  59. return await message.delete()
  60. m = message
  61. p = print
  62. if m.reply_to_message:
  63. r = m.reply_to_message
  64. if r.reply_markup:
  65. if isinstance(r.reply_markup, ReplyKeyboardMarkup):
  66. return await m.edit("INSECURE!")
  67. old_stderr = sys.stderr
  68. old_stdout = sys.stdout
  69. redirected_output = sys.stdout = StringIO()
  70. redirected_error = sys.stderr = StringIO()
  71. stdout, stderr, exc = None, None, None
  72. try:
  73. await aexec(cmd, client, message)
  74. except Exception:
  75. exc = traceback.format_exc()
  76. stdout = redirected_output.getvalue()
  77. stderr = redirected_error.getvalue()
  78. sys.stdout = old_stdout
  79. sys.stderr = old_stderr
  80. evaluation = ""
  81. if exc:
  82. evaluation = exc
  83. elif stderr:
  84. evaluation = stderr
  85. elif stdout:
  86. evaluation = stdout
  87. else:
  88. evaluation = "Success"
  89. final_output = f"**INPUT:**\n```{escape(cmd)}```\n\n**OUTPUT**:\n```{escape(evaluation.strip())}```"
  90. if len(final_output) > 4096:
  91. filename = "output.txt"
  92. with open(filename, "w+", encoding="utf8") as out_file:
  93. out_file.write(str(evaluation.strip()))
  94. await message.reply_document(
  95. document=filename,
  96. caption=f"**INPUT:**\n`{escape(cmd[0:980])}`\n\n**OUTPUT:**\n`Attached Document`",
  97. quote=False,
  98. )
  99. await message.delete()
  100. os.remove(filename)
  101. else:
  102. await edit_or_reply(message, text=final_output)
  103. @app2.on_message(
  104. filters.user(SUDOERS)
  105. & ~filters.forwarded
  106. & ~filters.via_bot
  107. & ~filters.edited
  108. & filters.command("sh", prefixes=USERBOT_PREFIX),
  109. )
  110. async def shellrunner(client, message: Message):
  111. if len(message.command) < 2:
  112. return await edit_or_reply(
  113. message, text="**Usage:**\n/sh git pull"
  114. )
  115. if message.reply_to_message:
  116. r = message.reply_to_message
  117. if r.reply_markup:
  118. if isinstance(r.reply_markup, ReplyKeyboardMarkup):
  119. return await message.edit("INSECURE!")
  120. text = message.text.split(None, 1)[1]
  121. if "\n" in text:
  122. code = text.split("\n")
  123. output = ""
  124. for x in code:
  125. shell = re.split(
  126. """ (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x
  127. )
  128. try:
  129. process = subprocess.Popen(
  130. shell,
  131. stdout=subprocess.PIPE,
  132. stderr=subprocess.PIPE,
  133. )
  134. except Exception as err:
  135. print(err)
  136. await edit_or_reply(
  137. message,
  138. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{escape(err)}```",
  139. )
  140. output += f"**{code}**\n"
  141. output += process.stdout.read()[:-1].decode("utf-8")
  142. output += "\n"
  143. else:
  144. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  145. for a in range(len(shell)):
  146. shell[a] = shell[a].replace('"', "")
  147. try:
  148. process = subprocess.Popen(
  149. shell,
  150. stdout=subprocess.PIPE,
  151. stderr=subprocess.PIPE,
  152. )
  153. except Exception as err:
  154. print(err)
  155. exc_type, exc_obj, exc_tb = sys.exc_info()
  156. errors = traceback.format_exception(
  157. etype=exc_type,
  158. value=exc_obj,
  159. tb=exc_tb,
  160. )
  161. return await edit_or_reply(
  162. message,
  163. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{''.join(errors)}```",
  164. )
  165. output = process.stdout.read()[:-1].decode("utf-8")
  166. if str(output) == "\n":
  167. output = None
  168. if output:
  169. if len(output) > 4096:
  170. with open("output.txt", "w+") as file:
  171. file.write(output)
  172. await app2.send_document(
  173. message.chat.id,
  174. "output.txt",
  175. reply_to_message_id=message.message_id,
  176. caption=escape(text),
  177. )
  178. return os.remove("output.txt")
  179. await edit_or_reply(
  180. message,
  181. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT:**\n```{escape(output)}```",
  182. )
  183. else:
  184. await edit_or_reply(
  185. message,
  186. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT: **\n`No output`",
  187. )
  188. def shell(cmd: list) -> tuple:
  189. proc = subprocess.run(
  190. cmd,
  191. stdout=subprocess.PIPE,
  192. stderr=subprocess.PIPE,
  193. )
  194. stdout, stderr = proc.stdout, proc.stderr
  195. stdout = stdout.decode() if stdout else None
  196. stderr = stderr.decode() if stderr else None
  197. return stdout, stderr