def headers(orc_path): orc = orc_parser.parse(orc_path) print("ORC Header:", orc_path) print("Symbols:", len(orc.symbols)) print("Relocations:", len(orc.relocations)) print("Sections:", len(orc.sections)) print("Segments:", len(orc.segments)) print("Contents:", len(orc.data))
def disassemble(orc_path, segment_index): orc = orc_parser.parse(orc_path) segment = orc.segments[segment_index] base = segment.offset.int_value() program = "" for i in range(segment.size.int_value()): b1 = orc.data[base + i] program += b1.bin_str(padded=True) bs = BitStream(program) instruction = disassembler.disassemble_instruction(bs) while instruction: print(instruction.human()) instruction = disassembler.disassemble_instruction(bs)
def reset_app(): global machine machine = Machine() orc = orc_parser.parse("chall2.exe") segment = orc.segments[1] base = segment.base.int_value() offset = segment.offset.int_value() data = orc.data for i in range(segment.size.int_value()): machine.memory.write_byte(Word.from_int(base + i), data[offset + i]) machine.registers[Register.pc] = Word.from_int(base)
def segments(orc_path): orc = orc_parser.parse(orc_path) print("ORC Segments:", orc_path) for i, segment in enumerate(orc.segments): print(i, segment)
def sections(orc_path): orc = orc_parser.parse(orc_path) print("ORC Sections:", orc_path) for i, section in enumerate(orc.sections): print(i, section)
def relocations(orc_path): orc = orc_parser.parse(orc_path) print("ORC Relocations:", orc_path) for i, relocation in enumerate(orc.relocations): print(i, relocation)
def symbols(orc_path): orc = orc_parser.parse(orc_path) print("ORC Symbols:", orc_path) for i, symbol in enumerate(orc.symbols): print(i, symbol)
def debug(orc_path): orc = orc_parser.parse(orc_path) breakpoint()
def run(filename, break_, input_): orc = orc_parser.parse(filename) machine = emulator.Machine() if input_: machine.stdin = list(input_) for segment in orc.segments: for i in range(segment.size.int_value()): memory_location = Word.from_int(segment.base.int_value() + i) data_location = segment.offset.int_value() + i data_value = orc.data[data_location] machine.memory.write_byte(memory_location, data_value) machine.registers[Register.pc] = orc.entry_point instruction = machine.next_instruction() while instruction: print(machine.registers[Register.pc].hex_str(human=True), instruction.human()) if machine.registers[Register.pc].int_value() == break_: command = input("> ").strip() while command: if command == "debug": breakpoint() elif command == "continue": break elif command == "step": machine.run(instruction) instruction = machine.next_instruction() if instruction: print(instruction.human()) elif command == "registers": for register in Register: print( f"{register.name}: {machine.registers[register].hex_str()}" ) elif command.startswith("print"): _, variable = command.split(" ") if variable.startswith("0x"): address_hex = variable address = hex_parser.int_from_by7e_hex(address_hex) word = machine.memory.read_word(address) else: register_name = variable register = Register[register_name] word = machine.registers[register] print(word) elif command.startswith("memory"): _, address_hex = command.split(" ") address = hex_parser.int_from_by7e_hex(address_hex) for section in range(4): section_address = address + section * 4 word = machine.memory.read_word(section_address) formatted_address = Word.from_int( section_address).hex_str(human=True) print(formatted_address, word.hex_str()) elif command.startswith("disassemble"): _, address_hex = command.split(" ") address = hex_parser.int_from_by7e_hex(address_hex) for fake_address, fake_instruction in machine.glob_instructions( address): formatted_address = Word.from_int( fake_address).hex_str(human=True) print(formatted_address, fake_instruction.human()) elif command == "web": import app app.routes.override_app(machine) app.app.run() elif command == "help": print(help_str) else: print("Unknown command:", command) command = input("> ").strip() machine.run(instruction) instruction = machine.next_instruction() # print("".join([ str(char) for char in machine.stdout ])) print("".join([chr(char.int_value) for char in machine.stdout]))