about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorserpilliere <serpilliere@users.noreply.github.com>2023-04-23 21:03:15 +0200
committerGitHub <noreply@github.com>2023-04-23 21:03:15 +0200
commit20aff863fa196e0886f69e26315dc268e1b31f18 (patch)
tree2406b50f4454ec1c755c8d43e2553865c42e64be
parentb4569e454eb085958f37acd739f680cca76c6028 (diff)
parent17552f09b91acd44dbc03cce1d8d2f94c20af3d9 (diff)
downloadmiasm-20aff863fa196e0886f69e26315dc268e1b31f18.tar.gz
miasm-20aff863fa196e0886f69e26315dc268e1b31f18.zip
Merge pull request #1446 from cea-sec/example-symbexec
Example: add a basic symbol_exec example
-rw-r--r--example/symbol_exec/symbol_exec.py58
-rwxr-xr-xtest/test_all.py1
2 files changed, 59 insertions, 0 deletions
diff --git a/example/symbol_exec/symbol_exec.py b/example/symbol_exec/symbol_exec.py
new file mode 100644
index 00000000..1d3f4576
--- /dev/null
+++ b/example/symbol_exec/symbol_exec.py
@@ -0,0 +1,58 @@
+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)
+
+# no address -> entry point
+# 0xXXXXXX -> use this address
+# symbol -> resolve then use
+if options.address is None:
+    addr = cont.entry_point
+else:
+    try:
+        addr = int(options.address, 0)
+    except ValueError:
+        addr = loc_db.get_name_offset(options.address)
+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)
diff --git a/test/test_all.py b/test/test_all.py
index 2d078bf1..591f3d8e 100755
--- a/test/test_all.py
+++ b/test/test_all.py
@@ -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",