def run(self): """Runs the interface and waits for user input commands.""" completer = WordCompleter([ 'value', 'type', 'show', 'name', 'slice', 'size', 'dump', 'clear', 'back' ]) history = FileHistory(self._polym_path + '/.finterface_history') session = PromptSession(history=history) while True: try: command = session.prompt( HTML("<bold>PH:cap/t%d/%s/<red>%s</red> > </bold>" % (self._tindex, self._lname, self._f.name)), completer=completer, complete_style=CompleteStyle.READLINE_LIKE, auto_suggest=AutoSuggestFromHistory(), enable_history_search=True) except KeyboardInterrupt: self.exit_program() continue try: command = command.rstrip().split(" ") if command[0] in self.EXIT: self.exit_program() elif command[0] in self.RET: break elif command[0] in ['v', 'value']: self._value(command) elif command[0] in ['s', 'show']: self._show(command) elif command[0] == 'name': print(self._f.name, '\n') elif command[0] == "slice": print(self._f.slice, '\n') elif command[0] == "type": self._type(command) elif command[0] == "size": print(self._f.size, '\n') elif command[0] in ['d', 'dump']: Interface.color_dump(self._f.pkt_raw, self._f.slice.start, self._f.slice.stop) elif command[0] == "clear": Interface._clear() elif command[0] == "": continue else: Interface._wrong_command() except SystemExit: raise except Exception as e: Interface._print_error( "Exception: Error processing the previous command. More info:" ) print(e)
def run(self): """Runs the interface and waits for user input commands.""" completer = WordCompleter([ 'value', 'type', 'show', 'name', 'slice', 'custom', 'size', 'dump', 'clear', 'back' ]) history = FileHistory(self._polym_path + '/.finterface_history') while True: try: command = prompt( HTML("<bold>PH:cap/t%d/%s/<red>%s</red> > </bold>" % (self._tindex, self._lname, self._f.name)), history=history, completer=completer, complete_style=CompleteStyle.READLINE_LIKE, auto_suggest=AutoSuggestFromHistory(), enable_history_search=True) except KeyboardInterrupt: self.exit_program() continue command = command.split(" ") if command[0] in self.EXIT: self.exit_program() elif command[0] in self.RET: break elif command[0] in ['t', 'type']: self._type(command) elif command[0] in ['v', 'value']: self._value(command) elif command[0] in ['s', 'show']: self._show(command) elif command[0] == 'name': self._name(command) elif command[0] == "slice": print(self._f.slice, '\n') elif command[0] == "custom": self._custom(command) elif command[0] == "size": print(self._f.size, '\n') elif command[0] in ['d', 'dump']: Interface.color_dump(self._f.raw, self._f.slice.start, self._f.slice.stop) elif command[0] == "clear": Interface._clear() elif command[0] == "": continue else: Interface._wrong_command()
def _dump(self, command): """Dumps the layer bytes in different formats.""" if len(command) == 1: Interface.color_dump(self._l.raw, self._l.slice.start) return # Parsing the arguments cp = CommandParser(LayerInterface._dump_opts()) args = cp.parse(command) if not args: Interface._argument_error() return if args["-hex"]: Interface.color_dump(self._l.raw, self._l.slice.start) elif args["-b"]: print(str(self._l.raw[self._l.slice.start:]), '\n') elif args["-hexstr"]: d = hexdump.dump(self._l.raw).split(" ") print(" ".join(d[self._l.slice.start:]), '\n') elif args["-h"]: Interface.print_help(LayerInterface._dump_help())
def _field(self, command): """Manages the access an creation of `TField`.""" if len(command) == 1: Interface.print_help(LayerInterface._field_help()) elif len(command) == 2 and command[1].lower() in self._l.fieldnames(): fi = FieldInterface(self._l.getfield(command[1].lower()), self._tindex, self._l.name, self._poisoner) fi.run() else: cp = CommandParser(LayerInterface._field_opts()) args = cp.parse(command) if not args: Interface._argument_error() return # Print the help if args["-h"]: Interface.print_help(LayerInterface._field_help()) # Adds a new field elif args["-a"]: Interface.color_dump(self._l.raw, self._l.slice.start) start = input("Start byte of the custom field: ") end = input("End byte of the custom field: ") if not start.isdecimal() or not end.isdecimal(): Interface._print_error( "The start or end byte is not a number") return else: fslice = slice(int(start), int(end)) fvalue = self._l.raw[fslice] fsize = len(fvalue) new_field = TField(name=args["-a"], value=fvalue, tslice=str(fslice).encode().hex(), custom=True, size=fsize, raw=self._l.raw.hex(), layer=self._l) # Set the type ftype = input("Field type [int/str/bytes/hex]: ") if ftype == "int": new_field.to_int() elif ftype == "str": new_field.to_str() elif ftype == "hex": new_field.to_hex() elif ftype == "bytes": new_field.to_bytes() # Add the field to the layer self._l.addfield(new_field) Interface._print_info("Field %s added to the layer" % args['-a']) # Deletes a field from the layer elif args["-d"]: del_field = self._l.getfield(args["-d"]) if del_field: self._l.delfield(del_field) Interface._print_info("Field %s deleted from the layer" % args["-d"]) else: Interface._print_error("The field %s is not in the layer" % args["-d"])