Skip to content

Commit

Permalink
Example: add a basic symbol_exec example
Browse files Browse the repository at this point in the history
  • Loading branch information
commial committed Apr 23, 2023
1 parent 00d4fd4 commit 41f3c6a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions example/symbol_exec/symbol_exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import print_function
from argparse import ArgumentParser

from future.utils import viewvalues
from miasm.analysis.binary import Container
from miasm.analysis.machine import Machine
from miasm.core.locationdb import LocationDB
from miasm.ir.symbexec import SymbolicExecutionEngine

parser = ArgumentParser("Simple SymbolicExecution demonstrator")
parser.add_argument("target_binary", help="Target binary path")
parser.add_argument("--address", help="Starting address for emulation. If not set, use the entrypoint")
parser.add_argument("--steps", help="Log emulation state after each instruction", action="store_true")
options = parser.parse_args()

###################################################################
# Common section from example/disam/dis_binary_lift_model_call.py #
###################################################################

fdesc = open(options.target_binary, 'rb')
loc_db = LocationDB()

cont = Container.from_stream(fdesc, loc_db)

machine = Machine(cont.arch)

mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db)

addr = cont.entry_point if options.address is None else int(options.address, 0)
asmcfg = mdis.dis_multiblock(addr)

lifter = machine.lifter_model_call(mdis.loc_db)
ircfg = lifter.new_ircfg_from_asmcfg(asmcfg)

#####################################
# End common section #
#####################################

# Instantiate a Symbolic Execution engine with default value for registers
symb = SymbolicExecutionEngine(lifter)

# Emulate until the next address cannot be resolved (`ret`, unresolved condition, etc.)
cur_addr = symb.run_at(ircfg, addr, step=options.steps)

# Modified elements
print('Modified registers:')
symb.dump(mems=False)
print('Modified memory (should be empty):')
symb.dump(ids=False)
1 change: 1 addition & 0 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ class ExampleSymbolExec(Example):


testset += ExampleSymbolExec(["single_instr.py"])
testset += ExampleSymbolExec(["symbol_exec.py", "--steps", Example.get_sample("box_upx.exe")])
for options, nb_sol, tag in [([], 8, []),
(["-i", "--rename-args"], 12, [TAGS["z3"]])]:
testset += ExampleSymbolExec(["depgraph.py",
Expand Down

0 comments on commit 41f3c6a

Please sign in to comment.