Skip to content

Instantly share code, notes, and snippets.

@icecr4ck
Last active August 9, 2021 06:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save icecr4ck/b4a91af47eaa84946f3cd982cf5f05a0 to your computer and use it in GitHub Desktop.
Save icecr4ck/b4a91af47eaa84946f3cd982cf5f05a0 to your computer and use it in GitHub Desktop.
Basic script to extract assembly CFG with Miasm
import sys
from miasm.analysis.machine import Machine
from miasm.analysis.binary import Container
#def cb_example(cur_bloc, loc_db, offsets_to_dis, *args, **kwargs):
#if len(cur_bloc.lines) < 1:
#return
cont = Container.from_stream(open(sys.argv[1], 'rb'))
machine = Machine('x86_64') # change architecture if needed
mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db)
# follow_call: recursively disassemble CALL destinations
#mdis.follow_call = True
# dont_dis: stop the current disassembly branch if reached
#mdis.dont_dis = [0x1337BEEF]
# dont_dis: force a basic block end if reached with a next constraint on its successor
#mdis.split_dis = [0x1337BEEF]
# dontdis_retcall: stop on CALL return addresses
#mdis.dont_dis_retcall_funcs = {0x1337BEEF}
# dont_dis_nulstart_bloc: stop if a block begin with a few \x00
#mdis.dont_dis_nulstart_bloc = True
# dis_block_callback: callback after each new disassembled block
#mdis.dis_block_callback = cb_example
# lines_wd: maximum block's size (in number of instruction)
#mdis.lines_wd = 1
# blocs_wd: maximum number of distinct disassembled block
#mdis.blocks_wd = 10
asmcfg = mdis.dis_multiblock(cont.entry_point)
open('bin_cfg.dot', 'w').write(asmcfg.dot())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment