background shell

This commit is contained in:
Roman Hergenreder 2022-01-17 13:06:45 +01:00
parent 391c4c6c52
commit c652d2876b

@ -16,9 +16,11 @@ class ShellListener:
self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind_addr = addr self.bind_addr = addr
self.port = port self.port = port
self.verbose = False
self.on_message = None self.on_message = None
self.listen_thread = None self.listen_thread = None
self.connection = None self.connection = None
self.on_connect = None
def startBackground(self): def startBackground(self):
self.listen_thread = threading.Thread(target=self.start) self.listen_thread = threading.Thread(target=self.start)
@ -27,16 +29,23 @@ class ShellListener:
def start(self): def start(self):
self.running = True self.running = True
self.listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.listen_socket.bind((self.bind_addr, self.port)) self.listen_socket.bind((self.bind_addr, self.port))
self.listen_socket.listen() self.listen_socket.listen()
while self.running: while self.running:
self.connection, addr = self.listen_socket.accept() self.connection, addr = self.listen_socket.accept()
with self.connection: with self.connection:
print("[+] Got connection:", addr) print("[+] Got connection:", addr)
if self.on_connect:
self.on_connect(addr)
while self.running: while self.running:
data = self.connection.recv(1024) data = self.connection.recv(1024)
if not data: if not data:
break break
if self.verbose:
print("< ", data)
if self.on_message: if self.on_message:
self.on_message(data) self.on_message(data)
@ -52,6 +61,10 @@ class ShellListener:
if self.connection: if self.connection:
if isinstance(data, str): if isinstance(data, str):
data = data.encode() data = data.encode()
if self.verbose:
print("> ", data)
self.connection.sendall(data) self.connection.sendall(data)
def sendline(self, data): def sendline(self, data):
@ -69,6 +82,11 @@ class ShellListener:
while self.running and self.connection is not None: while self.running and self.connection is not None:
self.sendline(input()) self.sendline(input())
def wait(self):
while self.running and self.connection is None:
time.sleep(0.1)
return self.running
def generatePayload(type, local_address, port, index=None): def generatePayload(type, local_address, port, index=None):
commands = [] commands = []