From 096e0a678badd6c508e8f1c2d0e02d4f0efc21a8 Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Fri, 22 Jan 2016 13:03:30 +0100 Subject: Test/depgraph: autopep --- example/symbol_exec/depgraph.py | 57 +++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'example/symbol_exec/depgraph.py') diff --git a/example/symbol_exec/depgraph.py b/example/symbol_exec/depgraph.py index a870b275..c0eeb134 100644 --- a/example/symbol_exec/depgraph.py +++ b/example/symbol_exec/depgraph.py @@ -12,18 +12,18 @@ parser.add_argument("func_addr", help="Function address") parser.add_argument("target_addr", help="Address to start") parser.add_argument("element", nargs="+", help="Elements to track") parser.add_argument("-m", "--architecture", - help="Architecture (%s)" % Machine.available_machine()) + help="Architecture (%s)" % Machine.available_machine()) parser.add_argument("-i", "--implicit", help="Use implicit tracking", - action="store_true") + action="store_true") parser.add_argument("--unfollow-mem", help="Stop on memory statements", - action="store_true") + action="store_true") parser.add_argument("--unfollow-call", help="Stop on call statements", - action="store_true") + action="store_true") parser.add_argument("--do-not-simplify", help="Do not simplify expressions", - action="store_true") + action="store_true") parser.add_argument("--rename-args", help="Rename common arguments (@32[ESP_init] -> Arg1)", - action="store_true") + action="store_true") args = parser.parse_args() # Get architecture @@ -38,9 +38,9 @@ elements = set() regs = machine.mn.regs.all_regs_ids_byname for element in args.element: try: - elements.add(regs[element.upper()]) + elements.add(regs[element.upper()]) except KeyError: - raise ValueError("Unknown element '%s'" % element) + raise ValueError("Unknown element '%s'" % element) mdis = machine.dis_engine(cont.bin_stream, dont_dis_nulstart_bloc=True) ir_arch = machine.ira(mdis.symbol_pool) @@ -63,9 +63,9 @@ for block in blocks: # Get the instance dg = DependencyGraph(ir_arch, implicit=args.implicit, - apply_simp=not(args.do_not_simplify), - follow_mem=not(args.unfollow_mem), - follow_call=not(args.unfollow_call)) + apply_simp=not args.do_not_simplify, + follow_mem=not args.unfollow_mem, + follow_call=not args.unfollow_call) # Build information target_addr = int(args.target_addr, 0) @@ -73,23 +73,24 @@ current_block = list(ir_arch.getby_offset(target_addr))[0] line_nb = 0 for line_nb, line in enumerate(current_block.lines): if line.offset == target_addr: - break + break # Enumerate solutions for sol_nb, sol in enumerate(dg.get(current_block.label, elements, line_nb, set())): - fname = "sol_%d.dot" % sol_nb - with open(fname, "w") as fdesc: - fdesc.write(sol.graph.dot()) - result = ", ".join("%s: %s" % (k, v) - for k, v in sol.emul(ctx=init_ctx).iteritems()) - print "Solution %d: %s -> %s" % (sol_nb, - result, - fname) - if args.implicit: - sat = sol.is_satisfiable - constraints = "" - if sat: - constraints = {} - for element in sol.constraints: - constraints[element] = hex(sol.constraints[element].as_long()) - print "\tSatisfiability: %s %s" % (sat, constraints) + fname = "sol_%d.dot" % sol_nb + with open(fname, "w") as fdesc: + fdesc.write(sol.graph.dot()) + result = ", ".join("%s: %s" % (k, v) + for k, v in sol.emul(ctx=init_ctx).iteritems()) + print "Solution %d: %s -> %s" % (sol_nb, + result, + fname) + if args.implicit: + sat = sol.is_satisfiable + constraints = "" + if sat: + constraints = {} + for element in sol.constraints: + constraints[element] = hex( + sol.constraints[element].as_long()) + print "\tSatisfiability: %s %s" % (sat, constraints) -- cgit 1.4.1 From 02935e7796b6fa185feb439315bcf7bb31015af9 Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Thu, 17 Mar 2016 08:56:30 +0100 Subject: Example/dg: fix print solutions --- example/symbol_exec/depgraph.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'example/symbol_exec/depgraph.py') diff --git a/example/symbol_exec/depgraph.py b/example/symbol_exec/depgraph.py index c0eeb134..d30b1ef0 100644 --- a/example/symbol_exec/depgraph.py +++ b/example/symbol_exec/depgraph.py @@ -91,6 +91,9 @@ for sol_nb, sol in enumerate(dg.get(current_block.label, elements, line_nb, set( if sat: constraints = {} for element in sol.constraints: - constraints[element] = hex( - sol.constraints[element].as_long()) + try: + result = hex(sol.constraints[element].as_long()) + except AttributeError: + result = str(sol.constraints[element]) + constraints[element] = result print "\tSatisfiability: %s %s" % (sat, constraints) -- cgit 1.4.1 From 7ab305601b1f274967187bb8182ce47e94ee0dd8 Mon Sep 17 00:00:00 2001 From: Fabrice Desclaux Date: Mon, 21 Mar 2016 13:11:21 +0100 Subject: Example/Depgraph: add JSON output --- example/symbol_exec/depgraph.py | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'example/symbol_exec/depgraph.py') diff --git a/example/symbol_exec/depgraph.py b/example/symbol_exec/depgraph.py index d30b1ef0..48758ad0 100644 --- a/example/symbol_exec/depgraph.py +++ b/example/symbol_exec/depgraph.py @@ -1,5 +1,6 @@ from argparse import ArgumentParser from pdb import pm +import json from miasm2.analysis.machine import Machine from miasm2.analysis.binary import Container @@ -24,6 +25,9 @@ parser.add_argument("--do-not-simplify", help="Do not simplify expressions", parser.add_argument("--rename-args", help="Rename common arguments (@32[ESP_init] -> Arg1)", action="store_true") +parser.add_argument("--json", + help="Output solution in JSON", + action="store_true") args = parser.parse_args() # Get architecture @@ -76,24 +80,43 @@ for line_nb, line in enumerate(current_block.lines): break # Enumerate solutions +json_solutions = [] for sol_nb, sol in enumerate(dg.get(current_block.label, elements, line_nb, set())): fname = "sol_%d.dot" % sol_nb with open(fname, "w") as fdesc: fdesc.write(sol.graph.dot()) - result = ", ".join("%s: %s" % (k, v) - for k, v in sol.emul(ctx=init_ctx).iteritems()) - print "Solution %d: %s -> %s" % (sol_nb, - result, - fname) + + results = sol.emul(ctx=init_ctx) + tokens = {str(k): str(v) for k, v in results.iteritems()} + if not args.json: + result = ", ".join("=".join(x) for x in tokens.iteritems()) + print "Solution %d: %s -> %s" % (sol_nb, + result, + fname) + if sol.has_loop: + print '\tLoop involved' + if args.implicit: sat = sol.is_satisfiable - constraints = "" + constraints = {} if sat: - constraints = {} for element in sol.constraints: try: result = hex(sol.constraints[element].as_long()) except AttributeError: result = str(sol.constraints[element]) constraints[element] = result - print "\tSatisfiability: %s %s" % (sat, constraints) + if args.json: + tokens["satisfiability"] = sat + tokens["constraints"] = {str(k): str(v) + for k, v in constraints.iteritems()} + else: + print "\tSatisfiability: %s %s" % (sat, constraints) + + if args.json: + tokens["has_loop"] = sol.has_loop + json_solutions.append(tokens) + + +if args.json: + print json.dumps(json_solutions) -- cgit 1.4.1