def init_jitter(loc_db): global data, run_addr # Create jitter myjit = Machine("x86_32").jitter(loc_db, sys.argv[1]) myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, data) # Init jitter myjit.init_stack() myjit.set_trace_log() myjit.push_uint32_t(0x1337beef) myjit.add_breakpoint(0x1337beef, code_sentinelle) return myjit
def init_jitter(): global data, run_addr # Create jitter myjit = Machine("x86_32").jitter(sys.argv[1]) myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, data) # Init jitter myjit.init_stack() myjit.set_trace_log() myjit.push_uint32_t(0x1337beef) myjit.add_breakpoint(0x1337beef, code_sentinelle) return myjit
class Asm_Test_16(Asm_Test): arch_name = "x86_16" arch_attrib = 16 ret_addr = 0x1337 def __init__(self, jitter_engine): self.myjit = Machine(self.arch_name).jitter(jitter_engine) self.myjit.stack_base = 0x1000 self.myjit.stack_size = 0x1000 self.myjit.init_stack() def init_machine(self): self.myjit.vm.add_memory_page(self.run_addr, PAGE_READ | PAGE_WRITE, self.assembly) self.myjit.push_uint16_t(self.ret_addr) self.myjit.add_breakpoint(self.ret_addr, lambda x:False)
class Asm_Test_16(Asm_Test): arch_name = "x86_16" arch_attrib = 16 ret_addr = 0x1337 def __init__(self, jitter_engine): self.myjit = Machine(self.arch_name).jitter(jitter_engine) self.myjit.stack_base = 0x1000 self.myjit.stack_size = 0x1000 self.myjit.init_stack() def init_machine(self): self.myjit.vm.add_memory_page(self.run_addr, PAGE_READ | PAGE_WRITE, self.assembly) self.myjit.push_uint16_t(self.ret_addr) self.myjit.add_breakpoint(self.ret_addr, lambda x: False)
class Asm_Test(object): run_addr = 0x0 def __init__(self, jitter_engine): self.myjit = Machine(self.arch_name).jitter(jitter_engine) self.myjit.init_stack() def test_init(self): pass def prepare(self): pass def __call__(self): self.prepare() self.asm() self.init_machine() self.test_init() self.run() self.check() def run(self): self.myjit.init_run(self.run_addr) self.myjit.continue_run() assert (self.myjit.pc == self.ret_addr) def asm(self): blocks, loc_db = parse_asm.parse_txt(mn_x86, self.arch_attrib, self.TXT, loc_db=self.myjit.ir_arch.loc_db) # fix shellcode addr loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) s = StrPatchwork() patches = asmblock.asm_resolve_final(mn_x86, blocks, loc_db) for offset, raw in viewitems(patches): s[offset] = raw s = bytes(s) self.assembly = s def check(self): raise NotImplementedError('abstract method')
class Asm_Test(object): run_addr = 0x0 def __init__(self, jitter_engine): self.myjit = Machine(self.arch_name).jitter(jitter_engine) self.myjit.init_stack() def test_init(self): pass def prepare(self): pass def __call__(self): self.prepare() self.asm() self.init_machine() self.test_init() self.run() self.check() def run(self): self.myjit.init_run(self.run_addr) self.myjit.continue_run() assert(self.myjit.pc == self.ret_addr) def asm(self): blocks, loc_db = parse_asm.parse_txt(mn_x86, self.arch_attrib, self.TXT, loc_db = self.myjit.ir_arch.loc_db) # fix shellcode addr loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) s = StrPatchwork() patches = asmblock.asm_resolve_final(mn_x86, blocks, loc_db) for offset, raw in viewitems(patches): s[offset] = raw s = bytes(s) self.assembly = s def check(self): raise NotImplementedError('abstract method')
class Asm_Test(object): def __init__(self, jitter): self.myjit = Machine("mips32l").jitter(jitter) self.myjit.init_stack() def __call__(self): self.asm() self.run() self.check() def asm(self): blocks, loc_db = parse_asm.parse_txt(mn_mips32, 'l', self.TXT, loc_db=self.myjit.ir_arch.loc_db) # fix shellcode addr loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) s = StrPatchwork() patches = asmblock.asm_resolve_final(mn_mips32, blocks, loc_db) for offset, raw in viewitems(patches): s[offset] = raw s = bytes(s) self.assembly = s def run(self): run_addr = 0 self.myjit.vm.add_memory_page( run_addr, PAGE_READ | PAGE_WRITE, self.assembly) self.myjit.cpu.RA = 0x1337beef self.myjit.add_breakpoint(0x1337beef, lambda x: False) self.myjit.init_run(run_addr) self.myjit.continue_run() assert(self.myjit.pc == 0x1337beef) def check(self): raise NotImplementedError('abstract method')
def get_str(jit, addr): data = jit.vm.get_mem(addr, 10) return data[:data.find(b'\x00')].decode('utf-8') def exception_int(jitter): print("SYSCALL {}".format(jitter.cpu.EAX)) jitter.cpu.set_exception(0) return True if __name__ == '__main__': parser = ArgumentParser(description="x86 64 basic Jitter") parser.add_argument("filename", help="x86 64 shellcode filename") parser.add_argument("-j", "--jitter", help="Jitter engine", default="python") args = parser.parse_args() myjit = Machine("x86_64").jitter(args.jitter) myjit.init_stack() data = open(args.filename, 'rb').read() run_addr = 0x40000000 myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, data) #myjit.set_trace_log() myjit.add_exception_handler(EXCEPT_SYSCALL, exception_int) myjit.run(run_addr)
class MyStruct(MemStruct): fields = [ # Number field: just struct.pack fields with one value ("num", Num("I")), ("flags", Num("B")), # This field is a pointer to another struct, it has a numeric # value (mystruct.other.val) and can be dereferenced to get an # OtherStruct instance (mystruct.other.deref) ("other", Ptr("I", OtherStruct)), # Ptr to a variable length String ("s", Ptr("I", Str())), ("i", Ptr("I", Num("I"))), ] jitter = Machine("x86_32").jitter("python") jitter.init_stack() addr = 0x1000 size = 0x1000 addr_str = 0x1100 addr_str2 = 0x1200 addr_str3 = 0x1300 # Initialize all mem with 0xaa jitter.vm.add_memory_page(addr, PAGE_READ | PAGE_WRITE, b"\xaa"*size) # MemStruct tests ## Creation # Use manual allocation with explicit addr for the first example mstruct = MyStruct(jitter.vm, addr) ## Fields are read from the virtual memory assert mstruct.num == 0xaaaaaaaa
from pdb import pm parser = ArgumentParser(description="x86 32 basic Jitter") parser.add_argument("filename", help="x86 32 shellcode filename") parser.add_argument("-j", "--jitter", help="Jitter engine (default is 'gcc')", default="gcc") args = parser.parse_args() def code_sentinelle(jitter): jitter.run = False jitter.pc = 0 return True myjit = Machine("x86_32").jitter(args.jitter) myjit.init_stack() data = open(args.filename, 'rb').read() run_addr = 0x40000000 myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, data) myjit.set_trace_log() myjit.push_uint32_t(0x1337beef) myjit.add_breakpoint(0x1337beef, code_sentinelle) myjit.init_run(run_addr) myjit.continue_run()
# Number field: just struct.pack fields with one value ("num", Num("I")), ("flags", Num("B")), # This field is a pointer to another struct, it has a numeric # value (mystruct.other.val) and can be dereferenced to get an # OtherStruct instance (mystruct.other.deref) ("other", Ptr("I", OtherStruct)), # Ptr to a variable length String ("s", Ptr("I", Str())), ("i", Ptr("I", Num("I"))), ] loc_db = LocationDB() jitter = Machine("x86_32").jitter(loc_db, "python") jitter.init_stack() addr = 0x1000 size = 0x1000 addr_str = 0x1100 addr_str2 = 0x1200 addr_str3 = 0x1300 # Initialize all mem with 0xaa jitter.vm.add_memory_page(addr, PAGE_READ | PAGE_WRITE, b"\xaa" * size) # MemStruct tests ## Creation # Use manual allocation with explicit addr for the first example mstruct = MyStruct(jitter.vm, addr) ## Fields are read from the virtual memory assert mstruct.num == 0xaaaaaaaa assert mstruct.flags == 0xaa