def __init__(self, host, port, timeout=None): """Create a socket Create a new socket and establish a connection to the host. Args: host (str): The host name or ip address of the server port (int): The port number Returns: remote: ``Socket`` instance. """ self.host = host self.port = port self.timeout = timeout # Create a new socket self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Establish a connection try: self.sock.connect((self.host, self.port)) log.info("Successfully connected to {0}:{1}".format( self.host, self.port)) except ConnectionRefusedError as e: log.warning("Connection to {0}:{1} refused".format( self.host, self.port))
def pause(): """ This allows you to pause the program and then attach it to debugger. Note: There is a bug that it only attachs to `gdb` if it is spawned by the root itself. """ log.info("Paused [Press any key to continue]") input('') return
def recv_thread(): while not go.is_set(): try: cur = self.recv(timeout=0.05) if cur: print(cur.decode("latin"), end='') except EOFError: log.info('Got EOF while reading in interactive') break
def close(self): """Close the socket Close the socket. This method is called from the destructor. """ if self.proc: self.proc.kill() self.proc = None log.info("close: '{0}' killed".format(self.fpath))
def close(self): """Close the socket Close the socket. This method is called from the destructor. """ if self.sock: self.sock.close() self.sock = None log.info("Connection to {0}:{1} closed".format( self.host, self.port))
def _poll(self): # Perform polling on running process to # see if it's alive or not if self.proc is None: return False self.proc.poll() returncode = self.proc.returncode if returncode is not None: log.info("Process '{}' stopped with exit code {} (PID={})".format( self.fpath, returncode, self.proc.pid)) self.proc = None return returncode
def send(self, data, timeout=None): """Send raw data Send raw data through the socket Args: data (bytes) : Data to send timeout (int): Timeout (in second) """ self._settimeout(timeout) if isinstance(data, str): data = str2bytes(data) try: self.proc.stdin.write(data) self.proc.stdin.flush() except IOError: log.info("Broken pipe") return data
def initialize(self): """ Initializing the ELF class by loading the information about of the sections, plt, got and function addresses """ log.info("Analyzing {}".format(self.path)) self._pie = 'DYN' in self.header.e_type self.arch = self.get_machine_arch().lower() if self._pie: self.base = 0 else: self.base = min(filter(bool, (s.header.p_vaddr for s in self.iter_segments()))) self.__section = self.init_sections() self.__got = self.init_got() self.__plt = self.init_plt() self.__symbol, self.__function = self.init_symbols()
def interactive(self, timeout=None): """Interactive mode """ log.info("Switching to Interactive mode.") go = threading.Event() def recv_thread(): while not go.is_set(): try: cur = self.recv(timeout=0.05) if cur: print(cur.decode("latin"), end='') except EOFError: log.info('Got EOF while reading in interactive') break t = threading.Thread(target=recv_thread) t.daemon = True t.start() try: while not go.is_set(): time.sleep(0.05) data = input("\033[1m\033[36m$\033[0m ") if data: try: self.sendline(data) except EOFError: go.set() log.info('Got EOF while sending in interactive') else: log.info("Got EOF while reading in interactive") go.set() except KeyboardInterrupt: log.info('Interrupted') go.set() while t.is_alive(): t.join(timeout=0.1)
def __init__(self, args, env=None, cwd=None, timeout=None, stdin=PIPE, stdout=PTY, stderr=STDOUT, preexec_fn=lambda: None, raw=True, closed_fds=True): """ Create a process instance and pipe it for `Tube` Args: args (list): The arguments to pass env (list) : The environment variables """ super(process, self).__init__() if isinstance(args, list): self.args = args self.fpath = self.args[0] else: self.args = [args] self.fpath = self.args[0] self.env = env self.timeout = timeout self.cwd = cwd self.raw = raw self.reservoir = b'' self.temp_timeout = None self.proc = None self.preexec_fn = preexec_fn if stderr is STDOUT: stderr = stdout handles = (stdin, stdout, stderr) self.pty = handles.index(PTY) if PTY in handles else None stdin, stdout, stderr, master, slave = self._handles(*handles) try: self.proc = subprocess.Popen(self.args, cwd=self.cwd, env=self.env, shell=False, stdout=stdout, stdin=stdin, stderr=stderr, preexec_fn=self.__preexec_fn) except FileNotFoundError: log.error("{} not found.".format(self.fpath)) return if self.pty is not None: if stdin is slave: self.proc.stdin = os.fdopen(os.dup(master), 'r+b', 0) if stdout is slave: self.proc.stdout = os.fdopen(os.dup(master), 'r+b', 0) if stderr is slave: self.proc.stderr = os.fdopen(os.dup(master), 'r+b', 0) os.close(master) os.close(slave) fd = self.proc.stdout.fileno() fl = fcntl.fcntl(fd, fcntl.F_GETFL) fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK) log.info("Successfully started process. PID - {}".format( self.proc.pid))