diff options
Diffstat (limited to 'example/disasm')
| -rw-r--r-- | example/disasm/callback.py | 27 | ||||
| -rw-r--r-- | example/disasm/file.py | 4 | ||||
| -rw-r--r-- | example/disasm/full.py | 63 | ||||
| -rw-r--r-- | example/disasm/function.py | 6 | ||||
| -rw-r--r-- | example/disasm/single_instr.py | 6 |
5 files changed, 58 insertions, 48 deletions
diff --git a/example/disasm/callback.py b/example/disasm/callback.py index a9bef20b..b9a09c09 100644 --- a/example/disasm/callback.py +++ b/example/disasm/callback.py @@ -1,9 +1,9 @@ from miasm2.core.bin_stream import bin_stream_str -from miasm2.core.asmblock import AsmLabel, AsmConstraint, expr_is_label +from miasm2.core.asmblock import AsmConstraint from miasm2.arch.x86.disasm import dis_x86_32, cb_x86_funcs -def cb_x86_callpop(cur_bloc, symbol_pool, *args, **kwargs): +def cb_x86_callpop(cur_bloc, loc_db, *args, **kwargs): """ 1000: call 1005 1005: pop @@ -21,12 +21,15 @@ def cb_x86_callpop(cur_bloc, symbol_pool, *args, **kwargs): last_instr = cur_bloc.lines[-1] if last_instr.name != 'CALL': return - ## The destination must be a label + ## The destination must be a location dst = last_instr.args[0] - if not expr_is_label(dst): + if not dst.is_loc(): return + + loc_key = dst.loc_key + offset = loc_db.get_location_offset(loc_key) ## The destination must be the next instruction - if dst.name.offset != last_instr.offset + last_instr.l: + if offset != last_instr.offset + last_instr.l: return # Update instruction instance @@ -34,7 +37,7 @@ def cb_x86_callpop(cur_bloc, symbol_pool, *args, **kwargs): # Update next blocks to process in the disassembly engine cur_bloc.bto.clear() - cur_bloc.add_cst(dst.name.offset, AsmConstraint.c_next, symbol_pool) + cur_bloc.add_cst(loc_key, AsmConstraint.c_next) # Prepare a tiny shellcode @@ -46,8 +49,8 @@ bin_stream = bin_stream_str(shellcode) mdis = dis_x86_32(bin_stream) print "Without callback:\n" -blocks = mdis.dis_multiblock(0) -print "\n".join(str(block) for block in blocks) +asmcfg = mdis.dis_multiblock(0) +print "\n".join(str(block) for block in asmcfg.blocks) # Enable callback cb_x86_funcs.append(cb_x86_callpop) @@ -56,9 +59,9 @@ cb_x86_funcs.append(cb_x86_callpop) print "=" * 40 print "With callback:\n" -blocks_after = mdis.dis_multiblock(0) -print "\n".join(str(block) for block in blocks_after) +asmcfg_after = mdis.dis_multiblock(0) +print "\n".join(str(block) for block in asmcfg_after.blocks) # Ensure the callback has been called -assert blocks.heads()[0].lines[0].name == "CALL" -assert blocks_after.heads()[0].lines[0].name == "PUSH" +assert asmcfg.loc_key_to_block(asmcfg.heads()[0]).lines[0].name == "CALL" +assert asmcfg_after.loc_key_to_block(asmcfg_after.heads()[0]).lines[0].name == "PUSH" diff --git a/example/disasm/file.py b/example/disasm/file.py index 88ba6162..196e1b1a 100644 --- a/example/disasm/file.py +++ b/example/disasm/file.py @@ -13,6 +13,6 @@ cont = Container.from_stream(open(sys.argv[1])) mdis = dis_x86_32(cont.bin_stream) # Inform the engine to avoid disassembling null instructions mdis.dont_dis_nulstart_bloc = True -blocks = mdis.dis_multiblock(addr) +asmcfg = mdis.dis_multiblock(addr) -open('graph.dot', 'w').write(blocks.dot()) +open('graph.dot', 'w').write(asmcfg.dot()) diff --git a/example/disasm/full.py b/example/disasm/full.py index 84c856e1..cfbfc80c 100644 --- a/example/disasm/full.py +++ b/example/disasm/full.py @@ -3,7 +3,7 @@ from argparse import ArgumentParser from pdb import pm from miasm2.analysis.binary import Container -from miasm2.core.asmblock import log_asmblock, AsmLabel, AsmCFG +from miasm2.core.asmblock import log_asmblock, AsmCFG from miasm2.expression.expression import ExprId from miasm2.core.interval import interval from miasm2.analysis.machine import Machine @@ -85,7 +85,7 @@ mn, dis_engine = machine.mn, machine.dis_engine ira, ir = machine.ira, machine.ir log.info('ok') -mdis = dis_engine(bs, symbol_pool=cont.symbol_pool) +mdis = dis_engine(bs, loc_db=cont.loc_db) # configure disasm engine mdis.dontdis_retcall = args.dontdis_retcall mdis.blocs_wd = args.blockwatchdog @@ -99,7 +99,9 @@ for addr in args.address: addrs.append(int(addr, 0)) except ValueError: # Second chance, try with symbol - addrs.append(mdis.symbol_pool.getby_name(addr).offset) + loc_key = mdis.loc_db.get_name_location(addr) + offset = mdis.loc_db.get_location_offset(loc_key) + addrs.append(offset) if len(addrs) == 0 and default_addr is not None: addrs.append(default_addr) @@ -121,27 +123,28 @@ while not finish and todo: if ad in done: continue done.add(ad) - allblocks = mdis.dis_multiblock(ad) + asmcfg = mdis.dis_multiblock(ad) log.info('func ok %.16x (%d)' % (ad, len(all_funcs))) all_funcs.add(ad) - all_funcs_blocks[ad] = allblocks - for block in allblocks: + all_funcs_blocks[ad] = asmcfg + for block in asmcfg.blocks: for l in block.lines: done_interval += interval([(l.offset, l.offset + l.l)]) if args.funcswatchdog is not None: args.funcswatchdog -= 1 if args.recurfunctions: - for block in allblocks: + for block in asmcfg.blocks: instr = block.get_subcall_instr() if not instr: continue - for dest in instr.getdstflow(mdis.symbol_pool): - if not (isinstance(dest, ExprId) and isinstance(dest.name, AsmLabel)): + for dest in instr.getdstflow(mdis.loc_db): + if not dest.is_loc(): continue - todo.append((mdis, instr, dest.name.offset)) + offset = mdis.loc_db.get_location_offset(dest.loc_key) + todo.append((mdis, instr, offset)) if args.funcswatchdog is not None and args.funcswatchdog <= 0: finish = True @@ -155,13 +158,13 @@ while not finish and todo: # Generate dotty graph -all_blocks = AsmCFG() +all_asmcfg = AsmCFG(mdis.loc_db) for blocks in all_funcs_blocks.values(): - all_blocks += blocks + all_asmcfg += blocks log.info('generate graph file') -open('graph_execflow.dot', 'w').write(all_blocks.dot(offset=True)) +open('graph_execflow.dot', 'w').write(all_asmcfg.dot(offset=True)) log.info('generate intervals') @@ -186,15 +189,19 @@ log.info('total lines %s' % total_l) if args.gen_ir: log.info("generating IR and IR analysis") - ir_arch = ir(mdis.symbol_pool) - ir_arch_a = ira(mdis.symbol_pool) + ir_arch = ir(mdis.loc_db) + ir_arch_a = ira(mdis.loc_db) + + ircfg = ir_arch.new_ircfg() + ircfg_a = ir_arch.new_ircfg() + ir_arch.blocks = {} ir_arch_a.blocks = {} - for ad, all_block in all_funcs_blocks.items(): + for ad, asmcfg in all_funcs_blocks.items(): log.info("generating IR... %x" % ad) - for block in all_block: - ir_arch_a.add_block(block) - ir_arch.add_block(block) + for block in asmcfg.blocks: + ir_arch.add_asmblock_to_ircfg(block, ircfg) + ir_arch_a.add_asmblock_to_ircfg(block, ircfg_a) log.info("Print blocks (without analyse)") for label, block in ir_arch.blocks.iteritems(): @@ -207,25 +214,25 @@ if args.gen_ir: print block if args.simplify > 0: - dead_simp(ir_arch_a) + dead_simp(ir_arch_a, ircfg_a) if args.defuse: reachings = ReachingDefinitions(ir_arch_a) open('graph_defuse.dot', 'w').write(DiGraphDefUse(reachings).dot()) - out = ir_arch_a.graph.dot() + out = ircfg.dot() open('graph_irflow.dot', 'w').write(out) - out = ir_arch.graph.dot() + out = ircfg_a.dot() open('graph_irflow_raw.dot', 'w').write(out) if args.simplify > 1: - ir_arch_a.simplify(expr_simp) + ircfg_a.simplify(expr_simp) modified = True while modified: modified = False - modified |= dead_simp(ir_arch_a) - modified |= ir_arch_a.remove_empty_assignblks() - modified |= ir_arch_a.remove_jmp_blocks() - modified |= ir_arch_a.merge_blocks() + modified |= dead_simp(ir_arch_a, ircfg_a) + modified |= ircfg_a.remove_empty_assignblks() + modified |= ircfg_a.remove_jmp_blocks() + modified |= ircfg_a.merge_blocks() - open('graph_irflow_reduced.dot', 'w').write(ir_arch_a.graph.dot()) + open('graph_irflow_reduced.dot', 'w').write(ircfg_a.dot()) diff --git a/example/disasm/function.py b/example/disasm/function.py index 89f65abb..10495dbc 100644 --- a/example/disasm/function.py +++ b/example/disasm/function.py @@ -8,9 +8,9 @@ from miasm2.arch.x86.disasm import dis_x86_32 # RET shellcode = '\xb8\xef\xbe7\x13\xb9\x04\x00\x00\x00\xc1\xc0\x08\xe2\xfb\xc3' mdis = dis_x86_32(shellcode) -blocks = mdis.dis_multiblock(0) +asmcfg = mdis.dis_multiblock(0) -for block in blocks: +for block in asmcfg.blocks: print block -open('graph.dot', 'w').write(blocks.dot()) +open('graph.dot', 'w').write(asmcfg.dot()) diff --git a/example/disasm/single_instr.py b/example/disasm/single_instr.py index 59b81de7..d17e303f 100644 --- a/example/disasm/single_instr.py +++ b/example/disasm/single_instr.py @@ -1,9 +1,9 @@ from miasm2.arch.x86.arch import mn_x86 from miasm2.arch.x86.regs import EDX -from miasm2.core.asmblock import AsmSymbolPool +from miasm2.core.locationdb import LocationDB -symbol_pool = AsmSymbolPool() -l = mn_x86.fromstring('MOV EAX, EBX', symbol_pool, 32) +loc_db = LocationDB() +l = mn_x86.fromstring('MOV EAX, EBX', loc_db, 32) print "instruction:", l print "arg:", l.args[0] x = mn_x86.asm(l) |