diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2020-08-22 12:47:01 +0200 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2020-08-31 07:50:01 +0200 |
| commit | 80e40a3d2ca735db955807ad0605b43ca22e4e35 (patch) | |
| tree | 4d41d7b53565f833444d3520eb22eed3e8bf26f1 | |
| parent | 5d8beb271d9890241a6d61dd476fab26ca37ebbf (diff) | |
| download | focaccia-miasm-80e40a3d2ca735db955807ad0605b43ca22e4e35.tar.gz focaccia-miasm-80e40a3d2ca735db955807ad0605b43ca22e4e35.zip | |
Avoid generate default locationdb
77 files changed, 426 insertions, 321 deletions
diff --git a/README.md b/README.md index f80d02a3..c31bdc26 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ Import the shellcode thanks to the `Container` abstraction: ```pycon >>> from miasm.analysis.binary import Container ->>> c = Container.from_string(s) +>>> c = Container.from_string(s, loc_db) >>> c <miasm.analysis.binary.ContainerUnknown object at 0x7f34cefe6090> ``` @@ -182,7 +182,7 @@ Disassembling the shellcode at address `0`: ```pycon >>> from miasm.analysis.machine import Machine >>> machine = Machine('x86_32') ->>> mdis = machine.dis_engine(c.bin_stream) +>>> mdis = machine.dis_engine(c.bin_stream, loc_db=loc_db) >>> asmcfg = mdis.dis_multiblock(0) >>> for block in asmcfg.blocks: ... print(block.to_string(asmcfg.loc_db)) @@ -208,7 +208,7 @@ RET Initializing the Jit engine with a stack: ```pycon ->>> jitter = machine.jitter(jit_type='python') +>>> jitter = machine.jitter(loc_db, jit_type='python') >>> jitter.init_stack() ``` diff --git a/example/asm/shellcode.py b/example/asm/shellcode.py index 67c882e9..d33d6231 100755 --- a/example/asm/shellcode.py +++ b/example/asm/shellcode.py @@ -83,7 +83,7 @@ with open(args.source) as fstream: loc_db = LocationDB() -asmcfg, loc_db = parse_asm.parse_txt(machine.mn, attrib, source, loc_db) +asmcfg = parse_asm.parse_txt(machine.mn, attrib, source, loc_db) # Fix shellcode addrs loc_db.set_location_offset(loc_db.get_name_location("main"), addr_main) diff --git a/example/asm/simple.py b/example/asm/simple.py index 8f6aac92..6197556b 100644 --- a/example/asm/simple.py +++ b/example/asm/simple.py @@ -4,10 +4,12 @@ from pprint import pprint from miasm.arch.x86.arch import mn_x86 from miasm.core import parse_asm, asmblock - +from miasm.core.locationdb import LocationDB # Assemble code -asmcfg, loc_db = parse_asm.parse_txt(mn_x86, 32, ''' +loc_db = LocationDB() +asmcfg = parse_asm.parse_txt( + mn_x86, 32, ''' main: MOV EAX, 1 MOV EBX, 2 @@ -20,7 +22,9 @@ loop: ADD EAX, ECX JZ loop RET -''') +''', + loc_db +) # Set 'main' loc_key's offset loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) diff --git a/example/disasm/callback.py b/example/disasm/callback.py index 1498b11e..eb999738 100644 --- a/example/disasm/callback.py +++ b/example/disasm/callback.py @@ -2,6 +2,7 @@ from __future__ import print_function from miasm.analysis.binary import Container from miasm.analysis.machine import Machine from miasm.core.asmblock import AsmConstraint +from miasm.core.locationdb import LocationDB def cb_x86_callpop(mdis, cur_bloc, offset_to_dis): @@ -50,8 +51,9 @@ shellcode = ( # Instantiate a x86 32 bit architecture machine = Machine("x86_32") -cont = Container.from_string(shellcode) -mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) +loc_db = LocationDB() +cont = Container.from_string(shellcode, loc_db) +mdis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) print("Without callback:\n") asmcfg = mdis.dis_multiblock(0) diff --git a/example/disasm/dis_binary.py b/example/disasm/dis_binary.py index 37eabb14..af140f28 100644 --- a/example/disasm/dis_binary.py +++ b/example/disasm/dis_binary.py @@ -2,12 +2,14 @@ from __future__ import print_function import sys from miasm.analysis.binary import Container from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB fdesc = open(sys.argv[1], 'rb') +loc_db = LocationDB() # The Container will provide a *bin_stream*, bytes source for the disasm engine # It will prodive a view from a PE or an ELF. -cont = Container.from_stream(fdesc) +cont = Container.from_stream(fdesc, loc_db) # The Machine, instantiated with the detected architecture, will provide tools # (disassembler, etc.) to work with this architecture diff --git a/example/disasm/dis_binary_ir.py b/example/disasm/dis_binary_ir.py index ff7a0d36..3facd74b 100644 --- a/example/disasm/dis_binary_ir.py +++ b/example/disasm/dis_binary_ir.py @@ -3,14 +3,16 @@ import sys from future.utils import viewvalues from miasm.analysis.binary import Container from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB ##################################### # Common section from dis_binary.py # ##################################### fdesc = open(sys.argv[1], 'rb') +loc_db = LocationDB() -cont = Container.from_stream(fdesc) +cont = Container.from_stream(fdesc, loc_db) machine = Machine(cont.arch) diff --git a/example/disasm/dis_binary_ira.py b/example/disasm/dis_binary_ira.py index 3ecd5349..bfed3497 100644 --- a/example/disasm/dis_binary_ira.py +++ b/example/disasm/dis_binary_ira.py @@ -4,14 +4,16 @@ import sys from future.utils import viewvalues from miasm.analysis.binary import Container from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB ##################################### # Common section from dis_binary.py # ##################################### fdesc = open(sys.argv[1], 'rb') +loc_db = LocationDB() -cont = Container.from_stream(fdesc) +cont = Container.from_stream(fdesc, loc_db) machine = Machine(cont.arch) diff --git a/example/disasm/dis_x86_string.py b/example/disasm/dis_x86_string.py index 6d4e2c84..b944c6ad 100644 --- a/example/disasm/dis_x86_string.py +++ b/example/disasm/dis_x86_string.py @@ -1,16 +1,21 @@ from __future__ import print_function from miasm.analysis.binary import Container from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB # The Container will provide a *bin_stream*, bytes source for the disasm engine -cont = Container.from_string(b"\x83\xf8\x10\x74\x07\x89\xc6\x0f\x47\xc3\xeb\x08\x89\xc8\xe8\x31\x33\x22\x11\x40\xc3") +loc_db = LocationDB() +cont = Container.from_string( + b"\x83\xf8\x10\x74\x07\x89\xc6\x0f\x47\xc3\xeb\x08\x89\xc8\xe8\x31\x33\x22\x11\x40\xc3", + loc_db +) # Instantiate a x86 32 bit architecture machine = Machine("x86_32") # Instantiate a disassembler engine, using the previous bin_stream and its # associated location DB. -mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) +mdis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) # Run a recursive traversal disassembling from address 0 asmcfg = mdis.dis_multiblock(0) diff --git a/example/disasm/full.py b/example/disasm/full.py index 57263a6f..9e739109 100644 --- a/example/disasm/full.py +++ b/example/disasm/full.py @@ -15,6 +15,7 @@ from miasm.expression.simplifications import expr_simp from miasm.analysis.ssa import SSADiGraph from miasm.ir.ir import AssignBlock, IRBlock from miasm.analysis.simplifier import IRCFGSimplifierCommon, IRCFGSimplifierSSA +from miasm.core.locationdb import LocationDB log = logging.getLogger("dis") console_handler = logging.StreamHandler() @@ -75,13 +76,20 @@ args = parser.parse_args() if args.verbose: log_asmblock.setLevel(logging.DEBUG) +loc_db = LocationDB() log.info('Load binary') if args.rawbinary: - cont = Container.fallback_container(open(args.filename, "rb").read(), - vm=None, addr=args.base_address) + cont = Container.fallback_container( + open(args.filename, "rb").read(), + vm=None, addr=args.base_address, + loc_db=loc_db, + ) else: with open(args.filename, "rb") as fdesc: - cont = Container.from_stream(fdesc, addr=args.base_address) + cont = Container.from_stream( + fdesc, addr=args.base_address, + loc_db=loc_db, + ) default_addr = cont.entry_point bs = cont.bin_stream diff --git a/example/expression/access_c.py b/example/expression/access_c.py index c604a0bd..3cc8e6a2 100644 --- a/example/expression/access_c.py +++ b/example/expression/access_c.py @@ -55,6 +55,8 @@ from miasm.arch.x86.ctype import CTypeAMD64_unk from miasm.core.objc import ExprToAccessC, CHandler from miasm.core.objc import CTypesManagerNotPacked from miasm.core.ctypesmngr import CAstTypes, CTypePtr, CTypeStruct +from miasm.core.locationdb import LocationDB + def find_call(ircfg): """Returns (irb, index) which call""" @@ -116,6 +118,7 @@ class MyCHandler(CHandler): +loc_db = LocationDB() data = open(sys.argv[1], 'rb').read() # Digest C information text = """ @@ -143,12 +146,12 @@ cont = Container.fallback_container(data, None, addr=0) machine = Machine("x86_64") dis_engine, ira = machine.dis_engine, machine.ira -mdis = dis_engine(cont.bin_stream, loc_db=cont.loc_db) +mdis = dis_engine(cont.bin_stream, loc_db=loc_db) addr_head = 0 asmcfg = mdis.dis_multiblock(addr_head) -lbl_head = mdis.loc_db.get_offset_location(addr_head) +lbl_head = loc_db.get_offset_location(addr_head) -ir_arch_a = ira(mdis.loc_db) +ir_arch_a = ira(loc_db) ircfg = ir_arch_a.new_ircfg_from_asmcfg(asmcfg) open('graph_irflow.dot', 'w').write(ircfg.dot()) diff --git a/example/expression/asm_to_ir.py b/example/expression/asm_to_ir.py index 83eac728..edc23437 100644 --- a/example/expression/asm_to_ir.py +++ b/example/expression/asm_to_ir.py @@ -9,10 +9,13 @@ from miasm.expression.expression import * from miasm.core import asmblock from miasm.arch.x86.ira import ir_a_x86_32 from miasm.analysis.data_flow import DeadRemoval +from miasm.core.locationdb import LocationDB # First, asm code -asmcfg, loc_db = parse_asm.parse_txt(mn_x86, 32, ''' +loc_db = LocationDB() +asmcfg = parse_asm.parse_txt( + mn_x86, 32, ''' main: MOV EAX, 1 MOV EBX, 2 @@ -25,7 +28,9 @@ loop: ADD EAX, ECX JZ loop RET -''') +''', + loc_db +) loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) diff --git a/example/expression/constant_propagation.py b/example/expression/constant_propagation.py index 36a548c5..a5929eed 100644 --- a/example/expression/constant_propagation.py +++ b/example/expression/constant_propagation.py @@ -12,6 +12,8 @@ from miasm.analysis.cst_propag import propagate_cst_expr from miasm.analysis.data_flow import DeadRemoval, \ merge_blocks, remove_empty_assignblks from miasm.expression.simplifications import expr_simp +from miasm.core.locationdb import LocationDB + parser = ArgumentParser("Constant expression propagation") @@ -25,8 +27,9 @@ args = parser.parse_args() machine = Machine("x86_32") -cont = Container.from_stream(open(args.filename, 'rb')) -mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) +loc_db = LocationDB() +cont = Container.from_stream(open(args.filename, 'rb'), loc_db) +mdis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) ir_arch = machine.ira(mdis.loc_db) addr = int(args.address, 0) deadrm = DeadRemoval(ir_arch) diff --git a/example/expression/export_llvm.py b/example/expression/export_llvm.py index 241a907d..a4c65787 100644 --- a/example/expression/export_llvm.py +++ b/example/expression/export_llvm.py @@ -6,18 +6,19 @@ from miasm.analysis.machine import Machine from miasm.jitter.llvmconvert import LLVMType, LLVMContext_IRCompilation, LLVMFunction_IRCompilation from llvmlite import ir as llvm_ir from miasm.expression.simplifications import expr_simp_high_to_explicit +from miasm.core.locationdb import LocationDB parser = ArgumentParser("LLVM export example") parser.add_argument("target", help="Target binary") parser.add_argument("addr", help="Target address") parser.add_argument("--architecture", "-a", help="Force architecture") args = parser.parse_args() - +loc_db = LocationDB() # This part focus on obtaining an IRCFG to transform # -cont = Container.from_stream(open(args.target, 'rb')) +cont = Container.from_stream(open(args.target, 'rb'), loc_db) machine = Machine(args.architecture if args.architecture else cont.arch) -ir = machine.ir(cont.loc_db) -dis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) +ir = machine.ir(loc_db) +dis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) asmcfg = dis.dis_multiblock(int(args.addr, 0)) ircfg = ir.new_ircfg_from_asmcfg(asmcfg) ircfg.simplify(expr_simp_high_to_explicit) diff --git a/example/expression/graph_dataflow.py b/example/expression/graph_dataflow.py index e7386e9e..4b428df7 100644 --- a/example/expression/graph_dataflow.py +++ b/example/expression/graph_dataflow.py @@ -10,6 +10,7 @@ from miasm.analysis.data_analysis import intra_block_flow_raw, inter_block_flow from miasm.core.graph import DiGraph from miasm.ir.symbexec import SymbolicExecutionEngine from miasm.analysis.data_flow import DeadRemoval +from miasm.core.locationdb import LocationDB parser = ArgumentParser("Simple expression use for generating dataflow graph") @@ -126,19 +127,19 @@ def gen_block_data_flow_graph(ir_arch, ircfg, ad, block_flow_cb): ad = int(args.addr, 16) - +loc_db = LocationDB() print('disasm...') -cont = Container.from_stream(open(args.filename, 'rb')) +cont = Container.from_stream(open(args.filename, 'rb'), loc_db) machine = Machine("x86_32") -mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) +mdis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) mdis.follow_call = True asmcfg = mdis.dis_multiblock(ad) print('ok') print('generating dataflow graph for:') -ir_arch_analysis = machine.ira(mdis.loc_db) +ir_arch_analysis = machine.ira(loc_db) ircfg = ir_arch_analysis.new_ircfg_from_asmcfg(asmcfg) deadrm = DeadRemoval(ir_arch_analysis) diff --git a/example/expression/solve_condition_stp.py b/example/expression/solve_condition_stp.py index 2c654b77..3743bfad 100644 --- a/example/expression/solve_condition_stp.py +++ b/example/expression/solve_condition_stp.py @@ -15,6 +15,7 @@ from miasm.ir.symbexec import SymbolicExecutionEngine, get_block from miasm.expression.simplifications import expr_simp from miasm.core import parse_asm from miasm.ir.translators.translator import Translator +from miasm.core.locationdb import LocationDB machine = Machine("x86_32") @@ -79,24 +80,26 @@ def emul_symb(ir_arch, ircfg, mdis, states_todo, states_done): if __name__ == '__main__': - + loc_db = LocationDB() translator_smt2 = Translator.to_language("smt2") addr = int(options.address, 16) - cont = Container.from_stream(open(args[0], 'rb')) - mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) + cont = Container.from_stream(open(args[0], 'rb'), loc_db) + mdis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) ir_arch = machine.ir(mdis.loc_db) ircfg = ir_arch.new_ircfg() symbexec = SymbolicExecutionEngine(ir_arch) - asmcfg, loc_db = parse_asm.parse_txt(machine.mn, 32, ''' + asmcfg = parse_asm.parse_txt( + machine.mn, 32, ''' init: PUSH argv PUSH argc PUSH ret_addr ''', - loc_db=mdis.loc_db) + loc_db + ) argc_lbl = loc_db.get_name_location('argc') diff --git a/example/jitter/arm.py b/example/jitter/arm.py index daea2428..72fcbc49 100755 --- a/example/jitter/arm.py +++ b/example/jitter/arm.py @@ -5,6 +5,7 @@ import logging from pdb import pm from miasm.analysis.sandbox import Sandbox_Linux_arml +from miasm.core.locationdb import LocationDB # Get arguments parser = Sandbox_Linux_arml.parser(description="""Sandbox an elf binary with arm @@ -14,7 +15,8 @@ parser.add_argument('-v', "--verbose", help="verbose mode", action="store_true") options = parser.parse_args() # Prepare the sandbox -sb = Sandbox_Linux_arml(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_arml(loc_db, options.filename, options, globals()) # Handle 'verbose' option if options.verbose is True: diff --git a/example/jitter/arm_sc.py b/example/jitter/arm_sc.py index 9ff770ff..20118429 100755 --- a/example/jitter/arm_sc.py +++ b/example/jitter/arm_sc.py @@ -4,6 +4,7 @@ from miasm.core.utils import int_to_byte from miasm.analysis.sandbox import Sandbox_Linux_armb_str from miasm.analysis.sandbox import Sandbox_Linux_arml_str from miasm.loader.strpatchwork import StrPatchwork +from miasm.core.locationdb import LocationDB from pdb import pm @@ -23,7 +24,8 @@ elif options.endianness == 'l': else: raise ValueError("Bad endianness!") -sb = sandbox(options.filename, options, globals()) +loc_db = LocationDB() +sb = sandbox(loc_db, options.filename, options, globals()) if options.address is None: raise ValueError('invalid address') diff --git a/example/jitter/example_types.py b/example/jitter/example_types.py index 653adaf9..af44c6d8 100755 --- a/example/jitter/example_types.py +++ b/example/jitter/example_types.py @@ -11,6 +11,9 @@ from miasm.analysis.machine import Machine from miasm.core.types import MemStruct, Self, Void, Str, Array, Ptr, \ Num, Array, set_allocator from miasm.os_dep.common import heap +from miasm.core.locationdb import LocationDB + +loc_db = LocationDB() # Instantiate a heap my_heap = heap() @@ -154,7 +157,7 @@ print() # A random jitter # You can also use miasm.jitter.VmMngr.Vm(), but it does not happen in real # life scripts, so here is the usual way: -jitter = Machine("x86_32").jitter("python") +jitter = Machine("x86_32").jitter(loc_db, "python") vm = jitter.vm # Auto-allocated by my_heap. If you allocate memory at `addr`, diff --git a/example/jitter/mips32.py b/example/jitter/mips32.py index 4aeb576f..b84dc4e5 100755 --- a/example/jitter/mips32.py +++ b/example/jitter/mips32.py @@ -5,6 +5,8 @@ from argparse import ArgumentParser from miasm.analysis import debugging from miasm.jitter.csts import * from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB + parser = ArgumentParser( description="""Sandbox raw binary with mips32 engine @@ -34,8 +36,9 @@ def code_sentinelle(jitter): return True def jit_mips32_binary(args): + loc_db = LocationDB() filepath, entryp = args.binary, int(args.addr, 0) - myjit = machine.jitter(jit_type = args.jitter) + myjit = machine.jitter(loc_db, jit_type = args.jitter) myjit.init_stack() # Log level (if available with jitter engine) diff --git a/example/jitter/msp430.py b/example/jitter/msp430.py index 927fb47b..887985ba 100755 --- a/example/jitter/msp430.py +++ b/example/jitter/msp430.py @@ -5,6 +5,7 @@ from argparse import ArgumentParser from miasm.analysis import debugging from miasm.jitter.csts import * from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB parser = ArgumentParser( description="""Sandbox raw binary with msp430 engine @@ -29,8 +30,9 @@ parser.add_argument("addr", machine = Machine("msp430") def jit_msp430_binary(args): + loc_db = LocationDB() filepath, entryp = args.binary, int(args.addr, 0) - myjit = machine.jitter(jit_type = args.jitter) + myjit = machine.jitter(loc_db, jit_type = args.jitter) # Log level (if available with jitter engine) myjit.set_trace_log( diff --git a/example/jitter/run_with_linuxenv.py b/example/jitter/run_with_linuxenv.py index 9b17b172..9290e6a8 100644 --- a/example/jitter/run_with_linuxenv.py +++ b/example/jitter/run_with_linuxenv.py @@ -7,6 +7,7 @@ from miasm.loader import elf as elf_csts from miasm.os_dep.linux import environment, syscall from miasm.analysis.machine import Machine from miasm.analysis.binary import Container +from miasm.core.locationdb import LocationDB parser = ArgumentParser("Run an ELF in a Linux-like environment") parser.add_argument("target", help="Target ELF") @@ -23,8 +24,9 @@ args = parser.parse_args() if args.verbose: syscall.log.setLevel(logging.DEBUG) +loc_db = LocationDB() # Get corresponding interpreter and reloc address -cont_target_tmp = Container.from_stream(open(args.target, 'rb')) +cont_target_tmp = Container.from_stream(open(args.target, 'rb'), loc_db) ld_path = bytes(cont_target_tmp.executable.getsectionbyname(".interp").content).strip(b"\x00") if cont_target_tmp.executable.Ehdr.type in [elf_csts.ET_REL, elf_csts.ET_DYN]: elf_base_addr = 0x40000000 @@ -35,7 +37,7 @@ else: # Instantiate a jitter machine = Machine(cont_target_tmp.arch) -jitter = machine.jitter(args.jitter) +jitter = machine.jitter(loc_db, args.jitter) jitter.init_stack() # Get elements for the target architecture diff --git a/example/jitter/sandbox_call.py b/example/jitter/sandbox_call.py index 7d400b7d..6d24777a 100644 --- a/example/jitter/sandbox_call.py +++ b/example/jitter/sandbox_call.py @@ -5,16 +5,18 @@ from miasm.analysis.sandbox import Sandbox_Linux_arml from miasm.analysis.binary import Container from miasm.os_dep.linux_stdlib import linobjs from miasm.core.utils import hexdump +from miasm.core.locationdb import LocationDB # Parse arguments parser = Sandbox_Linux_arml.parser(description="ELF sandboxer") parser.add_argument("filename", help="ELF Filename") options = parser.parse_args() -sb = Sandbox_Linux_arml(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_arml(loc_db, options.filename, options, globals()) with open(options.filename, "rb") as fdesc: - cont = Container.from_stream(fdesc) + cont = Container.from_stream(fdesc, loc_db) loc_key = cont.loc_db.get_name_location("md5_starts") addr_to_call = cont.loc_db.get_location_offset(loc_key) diff --git a/example/jitter/sandbox_elf_aarch64l.py b/example/jitter/sandbox_elf_aarch64l.py index 472b2354..7ad91118 100644 --- a/example/jitter/sandbox_elf_aarch64l.py +++ b/example/jitter/sandbox_elf_aarch64l.py @@ -1,6 +1,7 @@ import logging from pdb import pm from miasm.analysis.sandbox import Sandbox_Linux_aarch64l +from miasm.core.locationdb import LocationDB from miasm.jitter.jitload import log_func # Insert here user defined methods @@ -11,7 +12,8 @@ parser.add_argument("filename", help="ELF Filename") options = parser.parse_args() # Create sandbox -sb = Sandbox_Linux_aarch64l(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_aarch64l(loc_db, options.filename, options, globals()) log_func.setLevel(logging.ERROR) diff --git a/example/jitter/sandbox_elf_ppc32.py b/example/jitter/sandbox_elf_ppc32.py index 829381fc..d59181de 100644 --- a/example/jitter/sandbox_elf_ppc32.py +++ b/example/jitter/sandbox_elf_ppc32.py @@ -1,6 +1,7 @@ import os from pdb import pm from miasm.analysis.sandbox import Sandbox_Linux_ppc32b +from miasm.core.locationdb import LocationDB from miasm.jitter.csts import * from miasm.jitter.jitload import log_func import logging @@ -13,7 +14,8 @@ parser.add_argument("filename", help="ELF Filename") options = parser.parse_args() # Create sandbox -sb = Sandbox_Linux_ppc32b(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_ppc32b(loc_db, options.filename, options, globals()) log_func.setLevel(logging.ERROR) sb.run() diff --git a/example/jitter/sandbox_pe_x86_32.py b/example/jitter/sandbox_pe_x86_32.py index 263fad94..de7af95d 100644 --- a/example/jitter/sandbox_pe_x86_32.py +++ b/example/jitter/sandbox_pe_x86_32.py @@ -1,6 +1,6 @@ from pdb import pm from miasm.analysis.sandbox import Sandbox_Win_x86_32 - +from miasm.core.locationdb import LocationDB # Insert here user defined methods # Parse arguments @@ -9,7 +9,8 @@ parser.add_argument("filename", help="PE Filename") options = parser.parse_args() # Create sandbox -sb = Sandbox_Win_x86_32(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Win_x86_32(loc_db, options.filename, options, globals()) # Run sb.run() diff --git a/example/jitter/sandbox_pe_x86_64.py b/example/jitter/sandbox_pe_x86_64.py index 4d8f00ce..a168c325 100644 --- a/example/jitter/sandbox_pe_x86_64.py +++ b/example/jitter/sandbox_pe_x86_64.py @@ -1,5 +1,6 @@ from pdb import pm from miasm.analysis.sandbox import Sandbox_Win_x86_64 +from miasm.core.locationdb import LocationDB # Insert here user defined methods @@ -9,7 +10,8 @@ parser.add_argument("filename", help="PE Filename") options = parser.parse_args() # Create sandbox -sb = Sandbox_Win_x86_64(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Win_x86_64(loc_db, options.filename, options, globals()) # Run sb.run() diff --git a/example/jitter/test_x86_32_seh.py b/example/jitter/test_x86_32_seh.py index d29d3a22..e2c354a0 100644 --- a/example/jitter/test_x86_32_seh.py +++ b/example/jitter/test_x86_32_seh.py @@ -1,6 +1,7 @@ import os from pdb import pm from miasm.analysis.sandbox import Sandbox_Win_x86_32 +from miasm.core.locationdb import LocationDB from miasm.os_dep import win_api_x86_32_seh from miasm.jitter.csts import * @@ -42,7 +43,8 @@ options.usesegm = True options.use_windows_structs = True # Create sandbox -sb = Sandbox_Win_x86_32(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Win_x86_32(loc_db, options.filename, options, globals()) # Install Windows SEH callbacks sb.jitter.add_exception_handler(EXCEPT_ACCESS_VIOL, deal_exception_access_violation) diff --git a/example/jitter/trace.py b/example/jitter/trace.py index 46b313c1..968626f4 100644 --- a/example/jitter/trace.py +++ b/example/jitter/trace.py @@ -14,6 +14,7 @@ from pdb import pm from miasm.analysis.sandbox import Sandbox_Linux_arml from miasm.jitter.emulatedsymbexec import EmulatedSymbExec from miasm.jitter.jitcore_python import JitCore_Python +from miasm.core.locationdb import LocationDB # Function called at each instruction instr_count = 0 @@ -45,7 +46,8 @@ JitCore_Python.SymbExecClass = ESETrackMemory # Create sandbox, forcing Python jitter options.jitter = "python" -sb = Sandbox_Linux_arml(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_arml(loc_db, options.filename, options, globals()) # Force jit one instr per call, and register our callback sb.jitter.jit.set_options(jit_maxline=1, max_exec_per_call=1) diff --git a/example/jitter/unpack_upx.py b/example/jitter/unpack_upx.py index 2527f0c4..59f7389a 100644 --- a/example/jitter/unpack_upx.py +++ b/example/jitter/unpack_upx.py @@ -3,6 +3,7 @@ import os import logging from miasm.analysis.sandbox import Sandbox_Win_x86_32 from miasm.jitter.loader.pe import vm2pe +from miasm.core.locationdb import LocationDB from miasm.os_dep.common import get_win_str_a @@ -41,8 +42,11 @@ parser.add_argument("--graph", options = parser.parse_args() options.load_hdr = True -sb = Sandbox_Win_x86_32(options.filename, options, globals(), - parse_reloc=False) +loc_db = LocationDB() +sb = Sandbox_Win_x86_32( + loc_db, options.filename, options, globals(), + parse_reloc=False +) if options.verbose is True: @@ -54,7 +58,7 @@ if options.verbose is True: print(sb.jitter.vm) # Ensure there is one and only one leave (for OEP discovering) -mdis = sb.machine.dis_engine(sb.jitter.bs) +mdis = sb.machine.dis_engine(sb.jitter.bs, loc_db=loc_db) mdis.dont_dis_nulstart_bloc = True asmcfg = mdis.dis_multiblock(sb.entry_point) diff --git a/example/jitter/x86_32.py b/example/jitter/x86_32.py index cee9241a..427cd021 100644 --- a/example/jitter/x86_32.py +++ b/example/jitter/x86_32.py @@ -1,6 +1,7 @@ from argparse import ArgumentParser from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB from pdb import pm @@ -16,8 +17,9 @@ def code_sentinelle(jitter): jitter.pc = 0 return True +loc_db = LocationDB() -myjit = Machine("x86_32").jitter(args.jitter) +myjit = Machine("x86_32").jitter(loc_db, args.jitter) myjit.init_stack() data = open(args.filename, 'rb').read() diff --git a/example/jitter/x86_64.py b/example/jitter/x86_64.py index 78d88c18..943f5624 100644 --- a/example/jitter/x86_64.py +++ b/example/jitter/x86_64.py @@ -2,6 +2,7 @@ from argparse import ArgumentParser from pdb import pm from miasm.jitter.csts import PAGE_READ, PAGE_WRITE, EXCEPT_SYSCALL from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB # Some syscalls often used by shellcodes @@ -76,8 +77,9 @@ if __name__ == "__main__": parser.add_argument("--verbose", "-v", action="store_true", help="Verbose mode") args = parser.parse_args() + loc_db = LocationDB() - myjit = Machine("x86_64").jitter(args.jitter) + myjit = Machine("x86_64").jitter(loc_db, args.jitter) myjit.init_stack() with open(args.filename, 'rb') as f: diff --git a/example/symbol_exec/depgraph.py b/example/symbol_exec/depgraph.py index c7b9017f..8285452e 100644 --- a/example/symbol_exec/depgraph.py +++ b/example/symbol_exec/depgraph.py @@ -10,6 +10,7 @@ from miasm.analysis.machine import Machine from miasm.analysis.binary import Container from miasm.analysis.depgraph import DependencyGraph from miasm.expression.expression import ExprMem, ExprId, ExprInt +from miasm.core.locationdb import LocationDB parser = ArgumentParser("Dependency grapher") parser.add_argument("filename", help="Binary to analyse") @@ -33,10 +34,10 @@ parser.add_argument("--json", help="Output solution in JSON", action="store_true") args = parser.parse_args() - +loc_db = LocationDB() # Get architecture with open(args.filename, "rb") as fstream: - cont = Container.from_stream(fstream) + cont = Container.from_stream(fstream, loc_db) arch = args.architecture if args.architecture else cont.arch machine = Machine(arch) @@ -50,8 +51,8 @@ for element in args.element: except KeyError: raise ValueError("Unknown element '%s'" % element) -mdis = machine.dis_engine(cont.bin_stream, dont_dis_nulstart_bloc=True) -ir_arch = machine.ira(mdis.loc_db) +mdis = machine.dis_engine(cont.bin_stream, dont_dis_nulstart_bloc=True, loc_db=loc_db) +ir_arch = machine.ira(loc_db) # Common argument forms init_ctx = {} diff --git a/example/symbol_exec/dse_crackme.py b/example/symbol_exec/dse_crackme.py index 82a7af08..e014ada2 100644 --- a/example/symbol_exec/dse_crackme.py +++ b/example/symbol_exec/dse_crackme.py @@ -21,6 +21,7 @@ from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.analysis.sandbox import Sandbox_Linux_x86_64 from miasm.expression.expression import * from miasm.os_dep.win_api_x86_32 import get_win_str_a +from miasm.core.locationdb import LocationDB is_win = platform.system() == "Windows" @@ -75,7 +76,8 @@ parser.add_argument("--strategy", options = parser.parse_args() options.mimic_env = True options.command_line = ["%s" % TEMP_FILE.name] -sb = Sandbox_Linux_x86_64(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_x86_64(loc_db, options.filename, options, globals()) # Init segment sb.jitter.ir_arch.do_stk_segm = True @@ -238,7 +240,7 @@ strategy = { "branch-cov": DSEPathConstraint.PRODUCE_SOLUTION_BRANCH_COV, "path-cov": DSEPathConstraint.PRODUCE_SOLUTION_PATH_COV, }[options.strategy] -dse = DSEPathConstraint(machine, produce_solution=strategy) +dse = DSEPathConstraint(machine, loc_db, produce_solution=strategy) # Attach to the jitter dse.attach(sb.jitter) diff --git a/example/symbol_exec/dse_strategies.py b/example/symbol_exec/dse_strategies.py index 3f968215..bcea2329 100644 --- a/example/symbol_exec/dse_strategies.py +++ b/example/symbol_exec/dse_strategies.py @@ -26,6 +26,7 @@ from miasm.analysis.machine import Machine from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.analysis.dse import DSEPathConstraint from miasm.expression.expression import ExprMem, ExprId, ExprInt, ExprAssign +from miasm.core.locationdb import LocationDB # Argument handling parser = ArgumentParser("DSE Example") @@ -41,10 +42,12 @@ strategy = { "path-cov": DSEPathConstraint.PRODUCE_SOLUTION_PATH_COV, }[args.strategy] +loc_db = LocationDB() + # Map the shellcode run_addr = 0x40000 machine = Machine("x86_32") -jitter = machine.jitter("python") +jitter = machine.jitter(loc_db, "python") with open(args.filename, "rb") as fdesc: jitter.vm.add_memory_page( run_addr, @@ -72,7 +75,7 @@ jitter.push_uint32_t(ret_addr) jitter.init_run(run_addr) # Init a DSE instance with a given strategy -dse = DSEPathConstraint(machine, produce_solution=strategy) +dse = DSEPathConstraint(machine, loc_db, produce_solution=strategy) dse.attach(jitter) # Concretize everything except the argument dse.update_state_from_concrete() diff --git a/miasm/analysis/binary.py b/miasm/analysis/binary.py index 36f3acb9..0548dc9d 100644 --- a/miasm/analysis/binary.py +++ b/miasm/analysis/binary.py @@ -3,7 +3,6 @@ import warnings from miasm.core.bin_stream import bin_stream_str, bin_stream_elf, bin_stream_pe from miasm.jitter.csts import PAGE_READ -from miasm.core.locationdb import LocationDB log = logging.getLogger("binary") @@ -35,15 +34,16 @@ class Container(object): fallback_container = None # Fallback container format @classmethod - def from_string(cls, data, *args, **kwargs): + def from_string(cls, data, loc_db, *args, **kwargs): """Instantiate a container and parse the binary @data: str containing the binary + @loc_db: LocationDB instance """ log.info('Load binary') # Try each available format for container_type in cls.available_container: try: - return container_type(data, *args, **kwargs) + return container_type(data, loc_db, *args, **kwargs) except ContainerSignatureException: continue except ContainerParsingException as error: @@ -51,7 +51,7 @@ class Container(object): # Fallback mode log.warning('Fallback to string input') - return cls.fallback_container(data, *args, **kwargs) + return cls.fallback_container(data, loc_db, *args, **kwargs) @classmethod def register_container(cls, container): @@ -79,17 +79,14 @@ class Container(object): """ raise NotImplementedError("Abstract method") - def __init__(self, data, loc_db=None, **kwargs): + def __init__(self, data, loc_db, **kwargs): "Alias for 'parse'" # Init attributes self._executable = None self._bin_stream = None self._entry_point = None self._arch = None - if loc_db is None: - self._loc_db = LocationDB() - else: - self._loc_db = loc_db + self._loc_db = loc_db # Launch parsing self.parse(data, **kwargs) diff --git a/miasm/analysis/depgraph.py b/miasm/analysis/depgraph.py index 964dcef4..0b370f61 100644 --- a/miasm/analysis/depgraph.py +++ b/miasm/analysis/depgraph.py @@ -7,7 +7,6 @@ from future.utils import viewitems from miasm.expression.expression import ExprInt, ExprLoc, ExprAssign, \ ExprWalk, canonize_to_exprloc from miasm.core.graph import DiGraph -from miasm.core.locationdb import LocationDB from miasm.expression.simplifications import expr_simp_explicit from miasm.ir.symbexec import SymbolicExecutionEngine from miasm.ir.ir import IRBlock, AssignBlock @@ -309,7 +308,7 @@ class DependencyResult(DependencyState): line_nb).assignblks # Eval the block - loc_db = LocationDB() + loc_db = ir_arch.loc_db temp_loc = loc_db.get_or_create_name_location("Temp") symb_exec = SymbolicExecutionEngine(ir_arch, ctx_init) symb_exec.eval_updt_irblock(IRBlock(temp_loc, assignblks), step=step) diff --git a/miasm/analysis/disasm_cb.py b/miasm/analysis/disasm_cb.py index f3480598..af47603b 100644 --- a/miasm/analysis/disasm_cb.py +++ b/miasm/analysis/disasm_cb.py @@ -11,8 +11,8 @@ from miasm.core.locationdb import LocationDB from miasm.core.utils import upck32 -def get_ira(mnemo, attrib): - arch = mnemo.name, attrib +def get_ira(arch, attrib): + arch = arch.name, attrib if arch == ("arm", "arm"): from miasm.arch.arm.ira import ir_a_arm_base as ira elif arch == ("x86", 32): @@ -20,20 +20,20 @@ def get_ira(mnemo, attrib): elif arch == ("x86", 64): from miasm.arch.x86.ira import ir_a_x86_64 as ira else: - raise ValueError('unknown architecture: %s' % mnemo.name) + raise ValueError('unknown architecture: %s' % arch.name) return ira -def arm_guess_subcall( - mnemo, attrib, pool_bin, cur_bloc, offsets_to_dis, loc_db): - ira = get_ira(mnemo, attrib) +def arm_guess_subcall(dis_engine, cur_block, offsets_to_dis): + arch = dis_engine.arch + loc_db = dis_engine.loc_db + ira = get_ira(arch, dis_engine.attrib) - sp = LocationDB() - ir_arch = ira(sp) + ir_arch = ira(loc_db) ircfg = ira.new_ircfg() print('###') - print(cur_bloc) - ir_arch.add_asmblock_to_ircfg(cur_bloc, ircfg) + print(cur_block) + ir_arch.add_asmblock_to_ircfg(cur_block, ircfg) to_add = set() for irblock in viewvalues(ircfg.blocks): @@ -43,14 +43,14 @@ def arm_guess_subcall( for e in exprs: if e.dst == ir_arch.pc: pc_val = e.src - if e.dst == mnemo.regs.LR: + if e.dst == arch.regs.LR: lr_val = e.src if pc_val is None or lr_val is None: continue if not isinstance(lr_val, ExprInt): continue - l = cur_bloc.lines[-1] + l = cur_block.lines[-1] if lr_val.arg != l.offset + l.l: continue l = loc_db.get_or_create_offset_location(int(lr_val)) @@ -60,20 +60,20 @@ def arm_guess_subcall( offsets_to_dis.add(int(lr_val)) for c in to_add: - cur_bloc.addto(c) + cur_block.addto(c) -def arm_guess_jump_table( - mnemo, attrib, pool_bin, cur_bloc, offsets_to_dis, loc_db): - ira = get_ira(mnemo, attrib) +def arm_guess_jump_table(dis_engine, cur_block, offsets_to_dis): + arch = dis_engine.arch + loc_db = dis_engine.loc_db + ira = get_ira(arch, dis_engine.attrib) jra = ExprId('jra') jrb = ExprId('jrb') - sp = LocationDB() - ir_arch = ira(sp) + ir_arch = ira(loc_db) ircfg = ira.new_ircfg() - ir_arch.add_asmblock_to_ircfg(cur_bloc, ircfg) + ir_arch.add_asmblock_to_ircfg(cur_block, ircfg) for irblock in viewvalues(ircfg.blocks): pc_val = None @@ -105,7 +105,7 @@ def arm_guess_jump_table( while i < max_table_entry: i += 1 try: - ad = upck32(pool_bin.getbytes(base_ad + 4 * i, 4)) + ad = upck32(dis_engine.bin_stream.getbytes(base_ad + 4 * i, 4)) except: break if abs(ad - base_ad) > max_diff_addr: @@ -117,12 +117,11 @@ def arm_guess_jump_table( offsets_to_dis.add(ad) l = loc_db.get_or_create_offset_location(ad) c = AsmConstraintTo(l) - cur_bloc.addto(c) + cur_block.addto(c) guess_funcs = [] -def guess_multi_cb( - mnemo, attrib, pool_bin, cur_bloc, offsets_to_dis, loc_db): +def guess_multi_cb(dis_engine, cur_block, offsets_to_dis): for f in guess_funcs: - f(mnemo, attrib, pool_bin, cur_bloc, offsets_to_dis, loc_db) + f(dis_engine, cur_block, offsets_to_dis) diff --git a/miasm/analysis/dse.py b/miasm/analysis/dse.py index 4d2655df..cfd13821 100644 --- a/miasm/analysis/dse.py +++ b/miasm/analysis/dse.py @@ -66,7 +66,6 @@ from miasm.expression.expression_helper import possible_values from miasm.ir.translators import Translator from miasm.analysis.expression_range import expr_range from miasm.analysis.modularintervals import ModularIntervals -from miasm.core.locationdb import LocationDB DriftInfo = namedtuple("DriftInfo", ["symbol", "computed", "expected"]) @@ -162,9 +161,9 @@ class DSEEngine(object): """ SYMB_ENGINE = ESETrackModif - def __init__(self, machine): + def __init__(self, machine, loc_db): self.machine = machine - self.loc_db = LocationDB() + self.loc_db = loc_db self.handler = {} # addr -> callback(DSEEngine instance) self.instrumentation = {} # addr -> callback(DSEEngine instance) self.addr_to_cacheblocks = {} # addr -> {label -> IRBlock} @@ -527,13 +526,13 @@ class DSEPathConstraint(DSEEngine): PRODUCE_SOLUTION_BRANCH_COV = 2 PRODUCE_SOLUTION_PATH_COV = 3 - def __init__(self, machine, produce_solution=PRODUCE_SOLUTION_CODE_COV, + def __init__(self, machine, loc_db, produce_solution=PRODUCE_SOLUTION_CODE_COV, known_solutions=None, **kwargs): """Init a DSEPathConstraint @machine: Machine of the targeted architecture instance @produce_solution: (optional) if set, new solutions will be computed""" - super(DSEPathConstraint, self).__init__(machine, **kwargs) + super(DSEPathConstraint, self).__init__(machine, loc_db, **kwargs) # Dependency check assert z3 is not None diff --git a/miasm/analysis/sandbox.py b/miasm/analysis/sandbox.py index 1449d7be..2c56e7ca 100644 --- a/miasm/analysis/sandbox.py +++ b/miasm/analysis/sandbox.py @@ -42,7 +42,7 @@ class Sandbox(object): classes = property(lambda x: x.__class__._classes_()) - def __init__(self, fname, options, custom_methods=None, **kwargs): + def __init__(self, loc_db, fname, options, custom_methods=None, **kwargs): """ Initialize a sandbox @fname: str file name @@ -54,8 +54,10 @@ class Sandbox(object): assert isinstance(fname, basestring) self.fname = fname self.options = options + self.loc_db = loc_db if custom_methods is None: custom_methods = {} + kwargs["loc_db"] = loc_db for cls in self.classes: if cls == Sandbox: continue @@ -171,9 +173,9 @@ class Arch(object): # Architecture name _ARCH_ = None - def __init__(self, **kwargs): + def __init__(self, loc_db, **kwargs): self.machine = Machine(self._ARCH_) - self.jitter = self.machine.jitter(self.options.jitter) + self.jitter = self.machine.jitter(loc_db, self.options.jitter) @classmethod def update_parser(cls, parser): @@ -384,8 +386,8 @@ class Arch_x86(Arch): STACK_SIZE = 0x10000 STACK_BASE = 0x130000 - def __init__(self, **kwargs): - super(Arch_x86, self).__init__(**kwargs) + def __init__(self, loc_db, **kwargs): + super(Arch_x86, self).__init__(loc_db, **kwargs) if self.options.usesegm: self.jitter.ir_arch.do_stk_segm = True @@ -417,8 +419,8 @@ class Arch_arml(Arch): STACK_SIZE = 0x100000 STACK_BASE = 0x100000 - def __init__(self, **kwargs): - super(Arch_arml, self).__init__(**kwargs) + def __init__(self, loc_db, **kwargs): + super(Arch_arml, self).__init__(loc_db, **kwargs) # Init stack self.jitter.stack_size = self.STACK_SIZE @@ -431,8 +433,8 @@ class Arch_armb(Arch): STACK_SIZE = 0x100000 STACK_BASE = 0x100000 - def __init__(self, **kwargs): - super(Arch_armb, self).__init__(**kwargs) + def __init__(self, loc_db, **kwargs): + super(Arch_armb, self).__init__(loc_db, **kwargs) # Init stack self.jitter.stack_size = self.STACK_SIZE @@ -445,8 +447,8 @@ class Arch_armtl(Arch): STACK_SIZE = 0x100000 STACK_BASE = 0x100000 - def __init__(self, **kwargs): - super(Arch_armtl, self).__init__(**kwargs) + def __init__(self, loc_db, **kwargs): + super(Arch_armtl, self).__init__(loc_db, **kwargs) # Init stack self.jitter.stack_size = self.STACK_SIZE @@ -459,8 +461,8 @@ class Arch_mips32b(Arch): STACK_SIZE = 0x100000 STACK_BASE = 0x100000 - def __init__(self, **kwargs): - super(Arch_mips32b, self).__init__(**kwargs) + def __init__(self, loc_db, **kwargs): + super(Arch_mips32b, self).__init__(loc_db, **kwargs) # Init stack self.jitter.stack_size = self.STACK_SIZE @@ -473,8 +475,8 @@ class Arch_aarch64l(Arch): STACK_SIZE = 0x100000 STACK_BASE = 0x100000 - def __init__(self, **kwargs): - super(Arch_aarch64l, self).__init__(**kwargs) + def __init__(self, loc_db, **kwargs): + super(Arch_aarch64l, self).__init__(loc_db, **kwargs) # Init stack self.jitter.stack_size = self.STACK_SIZE @@ -487,8 +489,8 @@ class Arch_aarch64b(Arch): STACK_SIZE = 0x100000 STACK_BASE = 0x100000 - def __init__(self, **kwargs): - super(Arch_aarch64b, self).__init__(**kwargs) + def __init__(self, loc_db, **kwargs): + super(Arch_aarch64b, self).__init__(loc_db, **kwargs) # Init stack self.jitter.stack_size = self.STACK_SIZE @@ -506,8 +508,8 @@ class Arch_ppc32b(Arch_ppc32): class Sandbox_Win_x86_32(Sandbox, Arch_x86_32, OS_Win): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # Pre-stack some arguments self.jitter.push_uint32_t(2) @@ -538,8 +540,8 @@ class Sandbox_Win_x86_32(Sandbox, Arch_x86_32, OS_Win): class Sandbox_Win_x86_64(Sandbox, Arch_x86_64, OS_Win): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # reserve stack for local reg for _ in range(0x4): @@ -574,8 +576,8 @@ class Sandbox_Win_x86_64(Sandbox, Arch_x86_64, OS_Win): class Sandbox_Linux_x86_32(Sandbox, Arch_x86_32, OS_Linux): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # Pre-stack some arguments if self.options.mimic_env: @@ -634,8 +636,8 @@ class Sandbox_Linux_x86_32(Sandbox, Arch_x86_32, OS_Linux): class Sandbox_Linux_x86_64(Sandbox, Arch_x86_64, OS_Linux): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # Pre-stack some arguments if self.options.mimic_env: @@ -693,8 +695,8 @@ class Sandbox_Linux_x86_64(Sandbox, Arch_x86_64, OS_Linux): class Sandbox_Linux_arml(Sandbox, Arch_arml, OS_Linux): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # Pre-stack some arguments if self.options.mimic_env: @@ -751,8 +753,8 @@ class Sandbox_Linux_arml(Sandbox, Arch_arml, OS_Linux): class Sandbox_Linux_armtl(Sandbox, Arch_armtl, OS_Linux): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # Pre-stack some arguments if self.options.mimic_env: @@ -810,8 +812,8 @@ class Sandbox_Linux_armtl(Sandbox, Arch_armtl, OS_Linux): class Sandbox_Linux_mips32b(Sandbox, Arch_mips32b, OS_Linux): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # Pre-stack some arguments if self.options.mimic_env: @@ -865,8 +867,8 @@ class Sandbox_Linux_mips32b(Sandbox, Arch_mips32b, OS_Linux): class Sandbox_Linux_armb_str(Sandbox, Arch_armb, OS_Linux_str): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) self.jitter.cpu.LR = self.CALL_FINISH_ADDR @@ -881,8 +883,8 @@ class Sandbox_Linux_armb_str(Sandbox, Arch_armb, OS_Linux_str): class Sandbox_Linux_arml_str(Sandbox, Arch_arml, OS_Linux_str): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) self.jitter.cpu.LR = self.CALL_FINISH_ADDR @@ -897,8 +899,8 @@ class Sandbox_Linux_arml_str(Sandbox, Arch_arml, OS_Linux_str): class Sandbox_Linux_aarch64l(Sandbox, Arch_aarch64l, OS_Linux): - def __init__(self, *args, **kwargs): - Sandbox.__init__(self, *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Sandbox.__init__(self, loc_db, *args, **kwargs) # Pre-stack some arguments if self.options.mimic_env: @@ -957,8 +959,8 @@ class Sandbox_Linux_ppc32b(Sandbox, Arch_ppc32b, OS_Linux): # The glue between the kernel and the ELF ABI on Linux/PowerPC is # implemented in glibc/sysdeps/powerpc/powerpc32/dl-start.S, so we # have to play the role of ld.so here. - def __init__(self, *args, **kwargs): - super(Sandbox_Linux_ppc32b, self).__init__(*args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + super(Sandbox_Linux_ppc32b, self).__init__(loc_db, *args, **kwargs) # Init stack self.jitter.stack_size = self.STACK_SIZE diff --git a/miasm/arch/aarch64/jit.py b/miasm/arch/aarch64/jit.py index e3ea77f7..52ef1ae7 100644 --- a/miasm/arch/aarch64/jit.py +++ b/miasm/arch/aarch64/jit.py @@ -2,7 +2,6 @@ from builtins import range import logging from miasm.jitter.jitload import Jitter, named_arguments -from miasm.core.locationdb import LocationDB from miasm.core.utils import pck64, upck64 from miasm.arch.aarch64.sem import ir_aarch64b, ir_aarch64l @@ -15,8 +14,8 @@ log.setLevel(logging.CRITICAL) class jitter_aarch64l(Jitter): max_reg_arg = 8 - def __init__(self, *args, **kwargs): - Jitter.__init__(self, ir_aarch64l(LocationDB()), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_aarch64l(loc_db), *args, **kwargs) self.vm.set_little_endian() def push_uint64_t(self, value): @@ -75,6 +74,6 @@ class jitter_aarch64l(Jitter): class jitter_aarch64b(jitter_aarch64l): - def __init__(self, *args, **kwargs): - Jitter.__init__(self, ir_aarch64b(LocationDB()), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_aarch64b(loc_db), *args, **kwargs) self.vm.set_big_endian() diff --git a/miasm/arch/arm/jit.py b/miasm/arch/arm/jit.py index ee4e5c96..b4b7e793 100644 --- a/miasm/arch/arm/jit.py +++ b/miasm/arch/arm/jit.py @@ -2,7 +2,6 @@ from builtins import range import logging from miasm.jitter.jitload import Jitter, named_arguments -from miasm.core.locationdb import LocationDB from miasm.core.utils import pck32, upck32 from miasm.arch.arm.sem import ir_armb, ir_arml, ir_armtl, ir_armtb, cond_dct_inv, tab_cond from miasm.jitter.codegen import CGen @@ -65,9 +64,8 @@ class arm_CGen(CGen): class jitter_arml(Jitter): C_Gen = arm_CGen - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_arml(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_arml(loc_db), *args, **kwargs) self.vm.set_little_endian() def push_uint32_t(self, value): @@ -133,16 +131,14 @@ class jitter_arml(Jitter): class jitter_armb(jitter_arml): C_Gen = arm_CGen - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_armb(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_armb(loc_db), *args, **kwargs) self.vm.set_big_endian() class jitter_armtl(jitter_arml): C_Gen = arm_CGen - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_armtl(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_armtl(loc_db), *args, **kwargs) self.vm.set_little_endian() diff --git a/miasm/arch/mep/jit.py b/miasm/arch/mep/jit.py index 08bf73db..cc1e56ac 100644 --- a/miasm/arch/mep/jit.py +++ b/miasm/arch/mep/jit.py @@ -3,7 +3,6 @@ # Note: inspiration from msp430/jit.py from miasm.jitter.jitload import Jitter -from miasm.core.locationdb import LocationDB from miasm.core.utils import * from miasm.jitter.codegen import CGen from miasm.ir.translators.C import TranslatorC @@ -77,9 +76,8 @@ class jitter_mepl(Jitter): C_Gen = mep_CGen - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_mepl(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_mepl(loc_db), *args, **kwargs) self.vm.set_little_endian() self.ir_arch.jit_pc = self.ir_arch.arch.regs.PC @@ -108,8 +106,7 @@ class jitter_mepl(Jitter): class jitter_mepb(jitter_mepl): - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_mepb(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_mepb(loc_db), *args, **kwargs) self.vm.set_big_endian() self.ir_arch.jit_pc = self.ir_arch.arch.regs.PC diff --git a/miasm/arch/mips32/jit.py b/miasm/arch/mips32/jit.py index 7cbc258b..1c2c182e 100644 --- a/miasm/arch/mips32/jit.py +++ b/miasm/arch/mips32/jit.py @@ -85,9 +85,8 @@ class jitter_mips32l(Jitter): C_Gen = mipsCGen - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_mips32l(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_mips32l(loc_db), *args, **kwargs) self.vm.set_little_endian() def push_uint32_t(self, value): @@ -145,7 +144,6 @@ class jitter_mips32l(Jitter): class jitter_mips32b(jitter_mips32l): - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_mips32b(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_mips32b(loc_db), *args, **kwargs) self.vm.set_big_endian() diff --git a/miasm/arch/msp430/jit.py b/miasm/arch/msp430/jit.py index faf00434..1212b338 100644 --- a/miasm/arch/msp430/jit.py +++ b/miasm/arch/msp430/jit.py @@ -13,9 +13,8 @@ log.setLevel(logging.CRITICAL) class jitter_msp430(Jitter): - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_msp430(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_msp430(loc_db), *args, **kwargs) self.vm.set_little_endian() def push_uint16_t(self, value): diff --git a/miasm/arch/ppc/jit.py b/miasm/arch/ppc/jit.py index 92147b04..ccdaab72 100644 --- a/miasm/arch/ppc/jit.py +++ b/miasm/arch/ppc/jit.py @@ -1,6 +1,5 @@ from builtins import range from miasm.jitter.jitload import Jitter, named_arguments -from miasm.core.locationdb import LocationDB from miasm.arch.ppc.sem import ir_ppc32b import struct @@ -15,8 +14,8 @@ log.setLevel(logging.CRITICAL) class jitter_ppc32b(Jitter): max_reg_arg = 8 - def __init__(self, *args, **kwargs): - super(jitter_ppc32b, self).__init__(ir_ppc32b(LocationDB()), + def __init__(self, loc_db, *args, **kwargs): + super(jitter_ppc32b, self).__init__(ir_ppc32b(loc_db), *args, **kwargs) self.vm.set_big_endian() diff --git a/miasm/arch/x86/jit.py b/miasm/arch/x86/jit.py index 0144c289..9113b9ad 100644 --- a/miasm/arch/x86/jit.py +++ b/miasm/arch/x86/jit.py @@ -4,7 +4,6 @@ import logging from miasm.jitter.jitload import Jitter, named_arguments from miasm.arch.x86.sem import ir_x86_16, ir_x86_32, ir_x86_64 from miasm.jitter.codegen import CGen -from miasm.core.locationdb import LocationDB from miasm.ir.translators.C import TranslatorC log = logging.getLogger('jit_x86') @@ -42,9 +41,8 @@ class jitter_x86_16(Jitter): C_Gen = x86_32_CGen - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_x86_16(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_x86_16(loc_db), *args, **kwargs) self.vm.set_little_endian() self.ir_arch.do_stk_segm = False self.orig_irbloc_fix_regs_for_mode = self.ir_arch.irbloc_fix_regs_for_mode @@ -74,9 +72,8 @@ class jitter_x86_32(Jitter): C_Gen = x86_32_CGen - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_x86_32(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_x86_32(loc_db), *args, **kwargs) self.vm.set_little_endian() self.ir_arch.do_stk_segm = False @@ -201,9 +198,8 @@ class jitter_x86_64(Jitter): args_regs_systemv = ['RDI', 'RSI', 'RDX', 'RCX', 'R8', 'R9'] args_regs_stdcall = ['RCX', 'RDX', 'R8', 'R9'] - def __init__(self, *args, **kwargs): - sp = LocationDB() - Jitter.__init__(self, ir_x86_64(sp), *args, **kwargs) + def __init__(self, loc_db, *args, **kwargs): + Jitter.__init__(self, ir_x86_64(loc_db), *args, **kwargs) self.vm.set_little_endian() self.ir_arch.do_stk_segm = False diff --git a/miasm/core/asmblock.py b/miasm/core/asmblock.py index 975f93c5..e293ffda 100644 --- a/miasm/core/asmblock.py +++ b/miasm/core/asmblock.py @@ -15,7 +15,6 @@ from miasm.expression.simplifications import expr_simp from miasm.core.utils import Disasm_Exception, pck from miasm.core.graph import DiGraph, DiGraphSimplifier, MatchGraphJoker from miasm.core.interval import interval -from miasm.core.locationdb import LocationDB log_asmblock = logging.getLogger("asmblock") @@ -81,11 +80,12 @@ class AsmConstraintTo(AsmConstraint): class AsmBlock(object): - def __init__(self, loc_key, alignment=1): + def __init__(self, loc_db, loc_key, alignment=1): assert isinstance(loc_key, LocKey) self.bto = set() self.lines = [] + self.loc_db = loc_db self._loc_key = loc_key self.alignment = alignment @@ -132,7 +132,7 @@ class AsmBlock(object): 'middle instruction? default middle') offsets.sort() return None - new_block = AsmBlock(loc_key) + new_block = AsmBlock(loc_db, loc_key) i = offsets.index(offset) self.lines, new_block.lines = self.lines[:i], self.lines[i:] @@ -263,12 +263,12 @@ class AsmBlockBad(AsmBlock): ERROR_IO: "IOError", } - def __init__(self, loc_key=None, alignment=1, errno=ERROR_UNKNOWN, *args, **kwargs): + def __init__(self, loc_db, loc_key=None, alignment=1, errno=ERROR_UNKNOWN, *args, **kwargs): """Instantiate an AsmBlock_bad. @loc_key, @alignment: same as AsmBlock.__init__ @errno: (optional) specify a error type associated with the block """ - super(AsmBlockBad, self).__init__(loc_key, alignment, *args, **kwargs) + super(AsmBlockBad, self).__init__(loc_db, loc_key, alignment, *args, **kwargs) self._errno = errno errno = property(lambda self: self._errno) @@ -307,7 +307,7 @@ class AsmCFG(DiGraph): AsmCFGPending = namedtuple("AsmCFGPending", ["waiter", "constraint"]) - def __init__(self, loc_db=None, *args, **kwargs): + def __init__(self, loc_db, *args, **kwargs): super(AsmCFG, self).__init__(*args, **kwargs) # Edges -> constraint self.edges2constraint = {} @@ -1196,7 +1196,7 @@ class disasmEngine(object): - dis_block_callback: callback after each new disassembled block """ - def __init__(self, arch, attrib, bin_stream, **kwargs): + def __init__(self, arch, attrib, bin_stream, loc_db, **kwargs): """Instantiate a new disassembly engine @arch: targeted architecture @attrib: architecture attribute @@ -1206,7 +1206,7 @@ class disasmEngine(object): self.arch = arch self.attrib = attrib self.bin_stream = bin_stream - self.loc_db = LocationDB() + self.loc_db = loc_db # Setup options self.dont_dis = [] @@ -1236,7 +1236,7 @@ class disasmEngine(object): offsets_to_dis = set() add_next_offset = False loc_key = self.loc_db.get_or_create_offset_location(offset) - cur_block = AsmBlock(loc_key) + cur_block = AsmBlock(self.loc_db, loc_key) log_asmblock.debug("dis at %X", int(offset)) while not in_delayslot or delayslot_count > 0: if in_delayslot: @@ -1246,7 +1246,7 @@ class disasmEngine(object): if not cur_block.lines: job_done.add(offset) # Block is empty -> bad block - cur_block = AsmBlockBad(loc_key, errno=AsmBlockBad.ERROR_FORBIDDEN) + cur_block = AsmBlockBad(self.loc_db, loc_key, errno=AsmBlockBad.ERROR_FORBIDDEN) else: # Block is not empty, stop the desassembly pass and add a # constraint to the next block @@ -1289,7 +1289,7 @@ class disasmEngine(object): if not cur_block.lines: job_done.add(offset) # Block is empty -> bad block - cur_block = AsmBlockBad(loc_key, errno=error) + cur_block = AsmBlockBad(self.loc_db, loc_key, errno=error) else: # Block is not empty, stop the desassembly pass and add a # constraint to the next block @@ -1303,7 +1303,7 @@ class disasmEngine(object): instr.b.count(b'\x00') == instr.l): log_asmblock.warning("reach nul instr at %X", int(off_i)) # Block is empty -> bad block - cur_block = AsmBlockBad(loc_key, errno=AsmBlockBad.ERROR_NULL_STARTING_BLOCK) + cur_block = AsmBlockBad(self.loc_db, loc_key, errno=AsmBlockBad.ERROR_NULL_STARTING_BLOCK) break # special case: flow graph modificator in delayslot diff --git a/miasm/core/cpu.py b/miasm/core/cpu.py index aee22c97..f509c9c9 100644 --- a/miasm/core/cpu.py +++ b/miasm/core/cpu.py @@ -16,7 +16,6 @@ import miasm.expression.expression as m2_expr from miasm.core.bin_stream import bin_stream, bin_stream_str from miasm.core.utils import Disasm_Exception from miasm.expression.simplifications import expr_simp -from miasm.core.locationdb import LocationDB from miasm.core.asm_ast import AstNode, AstInt, AstId, AstOp @@ -1016,17 +1015,15 @@ class instruction(object): def get_asm_next_offset(self, expr): return m2_expr.ExprInt(self.offset+self.l, expr.size) - def resolve_args_with_symbols(self, symbols=None): - if symbols is None: - symbols = LocationDB() + def resolve_args_with_symbols(self, loc_db): args_out = [] for expr in self.args: - # try to resolve symbols using symbols (0 for default value) + # try to resolve symbols using loc_db (0 for default value) loc_keys = m2_expr.get_expr_locs(expr) fixed_expr = {} for exprloc in loc_keys: loc_key = exprloc.loc_key - names = symbols.get_location_names(loc_key) + names = loc_db.get_location_names(loc_key) # special symbols if b'$' in names: fixed_expr[exprloc] = self.get_asm_offset(exprloc) @@ -1034,14 +1031,14 @@ class instruction(object): if b'_' in names: fixed_expr[exprloc] = self.get_asm_next_offset(exprloc) continue - arg_int = symbols.get_location_offset(loc_key) + arg_int = loc_db.get_location_offset(loc_key) if arg_int is not None: fixed_expr[exprloc] = m2_expr.ExprInt(arg_int, exprloc.size) continue if not names: raise ValueError('Unresolved symbol: %r' % exprloc) - offset = symbols.get_location_offset(loc_key) + offset = loc_db.get_location_offset(loc_key) if offset is None: raise ValueError( 'The offset of loc_key "%s" cannot be determined' % names @@ -1050,7 +1047,7 @@ class instruction(object): # Fix symbol with its offset size = exprloc.size if size is None: - default_size = self.get_symbol_size(exprloc, symbols) + default_size = self.get_symbol_size(exprloc, loc_db) size = default_size value = m2_expr.ExprInt(offset, size) fixed_expr[exprloc] = value @@ -1383,7 +1380,7 @@ class cls_mn(with_metaclass(metamn, object)): yield c @classmethod - def asm(cls, instr, symbols=None): + def asm(cls, instr, loc_db=None): """ Re asm instruction by searching mnemo using name and args. We then can modify args and get the hex of a modified instruction @@ -1392,7 +1389,7 @@ class cls_mn(with_metaclass(metamn, object)): clist = [x for x in clist] vals = [] candidates = [] - args = instr.resolve_args_with_symbols(symbols) + args = instr.resolve_args_with_symbols(loc_db) for cc in clist: diff --git a/miasm/core/parse_asm.py b/miasm/core/parse_asm.py index b742a2d2..509f0483 100644 --- a/miasm/core/parse_asm.py +++ b/miasm/core/parse_asm.py @@ -88,21 +88,16 @@ def asm_ast_to_expr_with_size(arg, loc_db, size): return ExprInt(arg.value, size) return None -def parse_txt(mnemo, attrib, txt, loc_db=None): - """Parse an assembly listing. Returns a couple (asmcfg, loc_db), where - asmcfg is an AsmCfg instance and loc_db the associated LocationDB +def parse_txt(mnemo, attrib, txt, loc_db): + """Parse an assembly listing. Returns an AsmCfg instance @mnemo: architecture used @attrib: architecture attribute @txt: assembly listing - @loc_db: (optional) the LocationDB instance used to handle labels - of the listing + @loc_db: the LocationDB instance used to handle labels of the listing """ - if loc_db is None: - loc_db = asmblock.LocationDB() - C_NEXT = asmblock.AsmConstraint.c_next C_TO = asmblock.AsmConstraint.c_to @@ -233,9 +228,9 @@ def parse_txt(mnemo, attrib, txt, loc_db=None): # First line must be a label. If it's not the case, generate # it. loc = guess_next_new_label(loc_db) - cur_block = asmblock.AsmBlock(loc, alignment=mnemo.alignment) + cur_block = asmblock.AsmBlock(loc_db, loc, alignment=mnemo.alignment) else: - cur_block = asmblock.AsmBlock(line, alignment=mnemo.alignment) + cur_block = asmblock.AsmBlock(loc_db, line, alignment=mnemo.alignment) i += 1 # Generate the current block asmcfg.add_block(cur_block) @@ -302,4 +297,4 @@ def parse_txt(mnemo, attrib, txt, loc_db=None): # Log block asmblock.log_asmblock.info(block) - return asmcfg, loc_db + return asmcfg diff --git a/miasm/ir/ir.py b/miasm/ir/ir.py index 3219b5fc..1da907ef 100644 --- a/miasm/ir/ir.py +++ b/miasm/ir/ir.py @@ -773,7 +773,7 @@ class IntermediateRepresentation(object): if loc_key is None: offset = getattr(instr, "offset", None) loc_key = self.loc_db.get_or_create_offset_location(offset) - block = AsmBlock(loc_key) + block = AsmBlock(self.loc_db, loc_key) block.lines = [instr] self.add_asmblock_to_ircfg(block, ircfg, gen_pc_updt) return loc_key diff --git a/miasm/loader/pe_init.py b/miasm/loader/pe_init.py index 7a8d2abd..3e0c660e 100644 --- a/miasm/loader/pe_init.py +++ b/miasm/loader/pe_init.py @@ -189,7 +189,8 @@ class PE(object): parse_resources=True, parse_delay=True, parse_reloc=True, - wsize=32): + wsize=32, + **kwargs): self._rva = ContectRva(self) self._virt = ContentVirtual(self) self.img_rva = StrPatchwork() diff --git a/test/analysis/dse.py b/test/analysis/dse.py index 7d5998f1..cd813d2c 100644 --- a/test/analysis/dse.py +++ b/test/analysis/dse.py @@ -10,6 +10,7 @@ from miasm.core.asmblock import asm_resolve_final from miasm.analysis.machine import Machine from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.analysis.dse import DSEEngine +from miasm.core.locationdb import LocationDB class DSETest(object): @@ -31,9 +32,10 @@ class DSETest(object): run_addr = 0x0 def __init__(self, jitter_engine): + self.loc_db = LocationDB() self.machine = Machine(self.arch_name) jitter = self.machine.jitter - self.myjit = jitter(jitter_engine) + self.myjit = jitter(self.loc_db, jitter_engine) self.myjit.init_stack() self.myjit.set_trace_log() @@ -52,7 +54,7 @@ class DSETest(object): self.myjit.cpu.ECX = 4 self.myjit.cpu.EDX = 5 - self.dse = DSEEngine(self.machine) + self.dse = DSEEngine(self.machine, self.loc_db) self.dse.attach(self.myjit) def __call__(self): @@ -71,17 +73,17 @@ class DSETest(object): def asm(self): mn_x86 = self.machine.mn - blocks, loc_db = parse_asm.parse_txt( + blocks = parse_asm.parse_txt( mn_x86, self.arch_attrib, self.TXT, - loc_db=self.myjit.ir_arch.loc_db + self.loc_db ) # fix shellcode addr - loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) + self.loc_db.set_location_offset(self.loc_db.get_name_location("main"), 0x0) output = StrPatchwork() - patches = asm_resolve_final(mn_x86, blocks, loc_db) + patches = asm_resolve_final(mn_x86, blocks, self.loc_db) for offset, raw in viewitems(patches): output[offset] = raw @@ -120,7 +122,7 @@ class DSEAttachInBreakpoint(DSETest): def bp_attach(self, jitter): """Attach a DSE in the current jitter""" - self.dse = DSEEngine(self.machine) + self.dse = DSEEngine(self.machine, self.loc_db) self.dse.attach(self.myjit) self.dse.update_state_from_concrete() self.dse.update_state({ diff --git a/test/arch/aarch64/unit/asm_test.py b/test/arch/aarch64/unit/asm_test.py index fe59f0d8..d6ed5223 100644 --- a/test/arch/aarch64/unit/asm_test.py +++ b/test/arch/aarch64/unit/asm_test.py @@ -10,12 +10,14 @@ from miasm.core import asmblock from miasm.loader.strpatchwork import StrPatchwork from miasm.analysis.machine import Machine from miasm.jitter.csts import * +from miasm.core.locationdb import LocationDB reg_and_id = dict(mn_aarch64.regs.all_regs_ids_byname) class Asm_Test(object): def __init__(self, jitter): - self.myjit = Machine("aarch64l").jitter(jitter) + self.loc_db = LocationDB() + self.myjit = Machine("aarch64l").jitter(self.loc_db, jitter) self.myjit.init_stack() def __call__(self): @@ -24,12 +26,11 @@ class Asm_Test(object): self.check() def asm(self): - blocks, loc_db = parse_asm.parse_txt(mn_aarch64, 'l', self.TXT, - loc_db = self.myjit.ir_arch.loc_db) + blocks = parse_asm.parse_txt(mn_aarch64, 'l', self.TXT, self.loc_db) # fix shellcode addr - loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) + self.loc_db.set_location_offset(self.loc_db.get_name_location("main"), 0x0) s = StrPatchwork() - patches = asmblock.asm_resolve_final(mn_aarch64, blocks, loc_db) + patches = asmblock.asm_resolve_final(mn_aarch64, blocks, self.loc_db) for offset, raw in viewitems(patches): s[offset] = raw diff --git a/test/arch/mep/jit/ut_helpers_jit.py b/test/arch/mep/jit/ut_helpers_jit.py index 0c756e39..e031f5a8 100644 --- a/test/arch/mep/jit/ut_helpers_jit.py +++ b/test/arch/mep/jit/ut_helpers_jit.py @@ -5,6 +5,7 @@ from __future__ import print_function from miasm.analysis.machine import Machine from miasm.jitter.csts import PAGE_READ, PAGE_WRITE +from miasm.core.locationdb import LocationDB def jit_instructions(mn_str): @@ -13,6 +14,7 @@ def jit_instructions(mn_str): # Get the miasm Machine machine = Machine("mepb") mn_mep = machine.mn() + loc_db = LocationDB() # Assemble the instructions asm = b"" @@ -22,7 +24,7 @@ def jit_instructions(mn_str): asm += mn_mep.asm(instr)[0] # Init the jitter and add the assembled instructions to memory - jitter = machine.jitter(jit_type="gcc") + jitter = machine.jitter(loc_db, jit_type="gcc") jitter.vm.add_memory_page(0, PAGE_READ | PAGE_WRITE, asm) # Set the breakpoint diff --git a/test/arch/mips32/unit/asm_test.py b/test/arch/mips32/unit/asm_test.py index 2dcaf6fc..164cc143 100644 --- a/test/arch/mips32/unit/asm_test.py +++ b/test/arch/mips32/unit/asm_test.py @@ -10,6 +10,7 @@ from miasm.core import asmblock from miasm.loader.strpatchwork import StrPatchwork from miasm.analysis.machine import Machine from miasm.jitter.csts import * +from miasm.core.locationdb import LocationDB reg_and_id = dict(mn_mips32.regs.all_regs_ids_byname) @@ -17,7 +18,8 @@ reg_and_id = dict(mn_mips32.regs.all_regs_ids_byname) class Asm_Test(object): def __init__(self, jitter): - self.myjit = Machine("mips32l").jitter(jitter) + self.loc_db = LocationDB() + self.myjit = Machine("mips32l").jitter(self.loc_db, jitter) self.myjit.init_stack() def __call__(self): @@ -26,12 +28,11 @@ class Asm_Test(object): self.check() def asm(self): - blocks, loc_db = parse_asm.parse_txt(mn_mips32, 'l', self.TXT, - loc_db=self.myjit.ir_arch.loc_db) + blocks = parse_asm.parse_txt(mn_mips32, 'l', self.TXT, self.loc_db) # fix shellcode addr - loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) + self.loc_db.set_location_offset(self.loc_db.get_name_location("main"), 0x0) s = StrPatchwork() - patches = asmblock.asm_resolve_final(mn_mips32, blocks, loc_db) + patches = asmblock.asm_resolve_final(mn_mips32, blocks, self.loc_db) for offset, raw in viewitems(patches): s[offset] = raw diff --git a/test/arch/x86/qemu/testqemu.py b/test/arch/x86/qemu/testqemu.py index 64312928..6a516ac3 100644 --- a/test/arch/x86/qemu/testqemu.py +++ b/test/arch/x86/qemu/testqemu.py @@ -13,6 +13,7 @@ except AttributeError: from miasm.analysis.sandbox import Sandbox_Linux_x86_32 from miasm.jitter.jitload import log_func from miasm.jitter.csts import PAGE_READ, PAGE_WRITE +from miasm.core.locationdb import LocationDB # Utils def parse_fmt(s): @@ -122,7 +123,8 @@ options = parser.parse_args() expected = open(options.expected) # Create sandbox -sb = Sandbox_Linux_x86_32(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_x86_32(loc_db, options.filename, options, globals()) try: addr = sb.elf.getsectionbyname(".symtab")[options.funcname].value except AttributeError: diff --git a/test/arch/x86/qemu/testqemu64.py b/test/arch/x86/qemu/testqemu64.py index 8d25ca68..b17abe66 100644 --- a/test/arch/x86/qemu/testqemu64.py +++ b/test/arch/x86/qemu/testqemu64.py @@ -13,6 +13,7 @@ except AttributeError: from miasm.analysis.sandbox import Sandbox_Linux_x86_64 from miasm.jitter.jitload import log_func from miasm.jitter.csts import PAGE_READ, PAGE_WRITE +from miasm.core.locationdb import LocationDB # Utils def parse_fmt(s): @@ -118,7 +119,8 @@ options = parser.parse_args() expected = open(options.expected) # Create sandbox -sb = Sandbox_Linux_x86_64(options.filename, options, globals()) +loc_db = LocationDB() +sb = Sandbox_Linux_x86_64(loc_db, options.filename, options, globals()) try: addr = sb.elf.getsectionbyname(".symtab")[options.funcname].value except AttributeError: diff --git a/test/arch/x86/sem.py b/test/arch/x86/sem.py index 10980b05..3d40d44c 100755 --- a/test/arch/x86/sem.py +++ b/test/arch/x86/sem.py @@ -56,7 +56,8 @@ def compute(ir, mode, asm, inputstate={}, debug=False): def compute_txt(ir, mode, txt, inputstate={}, debug=False): - asmcfg, loc_db = parse_asm.parse_txt(mn, mode, txt) + loc_db = LocationDB() + asmcfg = parse_asm.parse_txt(mn, mode, txt, loc_db) loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) patches = asmblock.asm_resolve_final(mn, asmcfg, loc_db) ir_arch = ir(loc_db) diff --git a/test/arch/x86/unit/access_xmm.py b/test/arch/x86/unit/access_xmm.py index 65248b2e..c7ecfb1e 100644 --- a/test/arch/x86/unit/access_xmm.py +++ b/test/arch/x86/unit/access_xmm.py @@ -2,9 +2,11 @@ """Test getter and setter for XMM registers (128 bits)""" from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB # Jitter engine doesn't matter, use the always available 'python' one -myjit = Machine("x86_32").jitter("python") +loc_db = LocationDB() +myjit = Machine("x86_32").jitter(loc_db, "python") # Test basic access (get) assert myjit.cpu.XMM0 == 0 diff --git a/test/arch/x86/unit/asm_test.py b/test/arch/x86/unit/asm_test.py index 6e7c55e2..d3ecf660 100644 --- a/test/arch/x86/unit/asm_test.py +++ b/test/arch/x86/unit/asm_test.py @@ -11,15 +11,16 @@ from miasm.expression.expression import * from miasm.core import asmblock from miasm.loader.strpatchwork import StrPatchwork from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB from miasm.jitter.csts import * reg_and_id = dict(mn_x86.regs.all_regs_ids_byname) - class Asm_Test(object): run_addr = 0x0 def __init__(self, jitter_engine): - self.myjit = Machine(self.arch_name).jitter(jitter_engine) + self.loc_db = LocationDB() + self.myjit = Machine(self.arch_name).jitter(self.loc_db, jitter_engine) self.myjit.init_stack() def test_init(self): @@ -44,12 +45,11 @@ class Asm_Test(object): assert(self.myjit.pc == self.ret_addr) def asm(self): - blocks, loc_db = parse_asm.parse_txt(mn_x86, self.arch_attrib, self.TXT, - loc_db = self.myjit.ir_arch.loc_db) + blocks = parse_asm.parse_txt(mn_x86, self.arch_attrib, self.TXT, self.loc_db) # fix shellcode addr - loc_db.set_location_offset(loc_db.get_name_location("main"), 0x0) + self.loc_db.set_location_offset(self.loc_db.get_name_location("main"), 0x0) s = StrPatchwork() - patches = asmblock.asm_resolve_final(mn_x86, blocks, loc_db) + patches = asmblock.asm_resolve_final(mn_x86, blocks, self.loc_db) for offset, raw in viewitems(patches): s[offset] = raw @@ -77,7 +77,8 @@ class Asm_Test_16(Asm_Test): ret_addr = 0x1337 def __init__(self, jitter_engine): - self.myjit = Machine(self.arch_name).jitter(jitter_engine) + self.loc_db = LocationDB() + self.myjit = Machine(self.arch_name).jitter(self.loc_db, jitter_engine) self.myjit.stack_base = 0x1000 self.myjit.stack_size = 0x1000 self.myjit.init_stack() diff --git a/test/arch/x86/unit/mn_pushpop.py b/test/arch/x86/unit/mn_pushpop.py index 6dc37b74..c13f5989 100755 --- a/test/arch/x86/unit/mn_pushpop.py +++ b/test/arch/x86/unit/mn_pushpop.py @@ -21,7 +21,7 @@ class Test_PUSHAD_32(Asm_Test_32): MYSTRING = "test pushad 32" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -48,7 +48,7 @@ class Test_PUSHA_32(Asm_Test_32): MYSTRING = "test pusha 32" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -75,7 +75,7 @@ class Test_PUSHA_16(Asm_Test_16): MYSTRING = "test pusha 16" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -102,7 +102,7 @@ class Test_PUSHAD_16(Asm_Test_16): MYSTRING = "test pushad 16" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -129,7 +129,7 @@ class Test_PUSH_mode32_32(Asm_Test_32): MYSTRING = "test push mode32 32" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -152,7 +152,7 @@ class Test_PUSH_mode32_16(Asm_Test_32): MYSTRING = "test push mode32 16" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -175,7 +175,7 @@ class Test_PUSH_mode16_16(Asm_Test_16): MYSTRING = "test push mode16 16" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -198,7 +198,7 @@ class Test_PUSH_mode16_32(Asm_Test_16): MYSTRING = "test push mode16 32" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): init_regs(self) @@ -221,7 +221,7 @@ class Test_POP_mode32_32(Asm_Test_32): MYSTRING = "test pop mode32 32" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): self.value = 0x11223344 @@ -243,7 +243,7 @@ class Test_POP_mode32_16(Asm_Test_32): MYSTRING = "test pop mode32 16" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): self.value = 0x1122 @@ -265,7 +265,7 @@ class Test_POP_mode16_16(Asm_Test_16): MYSTRING = "test pop mode16 16" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): self.value = 0x1122 @@ -287,7 +287,7 @@ class Test_POP_mode16_32(Asm_Test_16): MYSTRING = "test pop mode16 32" def prepare(self): - self.myjit.ir_arch.loc_db.add_location("lbl_ret", self.ret_addr) + self.loc_db.add_location("lbl_ret", self.ret_addr) def test_init(self): self.value = 0x11223344 diff --git a/test/arch/x86/unit/test_asm_x86_64.py b/test/arch/x86/unit/test_asm_x86_64.py index e23f9a19..e1d99ec1 100644 --- a/test/arch/x86/unit/test_asm_x86_64.py +++ b/test/arch/x86/unit/test_asm_x86_64.py @@ -2,11 +2,13 @@ from miasm.core import asmblock from miasm.arch.x86 import arch from miasm.core import parse_asm from miasm.core.interval import interval +from miasm.core.locationdb import LocationDB my_mn = arch.mn_x86 +loc_db = LocationDB() - -asmcfg, loc_db = parse_asm.parse_txt(my_mn, 64, r''' +asmcfg = parse_asm.parse_txt( + my_mn, 64, r''' main: PUSH RBP MOV RBP, RSP @@ -18,7 +20,9 @@ end: POP RBP RET -''') +''', + loc_db +) loc_db.set_location_offset(loc_db.get_name_location("main"), 0x100001000) dst_interval = interval([(0x100001000, 0x100002000)]) diff --git a/test/core/asmblock.py b/test/core/asmblock.py index 47f92569..fa66f75b 100644 --- a/test/core/asmblock.py +++ b/test/core/asmblock.py @@ -12,14 +12,16 @@ from miasm.core.asmblock import AsmCFG, AsmConstraint, AsmBlock, \ bbl_simplifier from miasm.core.graph import DiGraphSimplifier, MatchGraphJoker from miasm.expression.expression import ExprId +from miasm.core.locationdb import LocationDB # Initial data: from 'samples/simple_test.bin' data = decode_hex("5589e583ec10837d08007509c745fc01100000eb73837d08017709c745fc02100000eb64837d08057709c745fc03100000eb55837d080774138b450801c083f80e7509c745fc04100000eb3c8b450801c083f80e7509c745fc05100000eb298b450883e03085c07409c745fc06100000eb16837d08427509c745fc07100000eb07c745fc081000008b45fcc9c3") -cont = Container.from_string(data) +loc_db = LocationDB() +cont = Container.from_string(data, loc_db) # Test Disasm engine machine = Machine("x86_32") -mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) +mdis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) ## Disassembly of one block first_block = mdis.dis_block(0) assert len(first_block.lines) == 5 @@ -111,8 +113,8 @@ open("graph2.dot", "w").write(asmcfg.dot()) # Test helper methods ## loc_key_to_block should always be updated assert asmcfg.loc_key_to_block(first_block.loc_key) == first_block -testlabel = mdis.loc_db.get_or_create_name_location("testlabel") -my_block = AsmBlock(testlabel) +testlabel = loc_db.get_or_create_name_location("testlabel") +my_block = AsmBlock(loc_db, testlabel) asmcfg.add_block(my_block) assert len(asmcfg) == 3 assert asmcfg.loc_key_to_block(first_block.loc_key) == first_block @@ -122,8 +124,8 @@ assert asmcfg.loc_key_to_block(my_block.loc_key) == my_block assert len(list(asmcfg.get_bad_blocks())) == 0 assert len(list(asmcfg.get_bad_blocks_predecessors())) == 0 ### Add a bad block, not linked -testlabel_bad = mdis.loc_db.get_or_create_name_location("testlabel_bad") -my_bad_block = AsmBlockBad(testlabel_bad) +testlabel_bad = loc_db.get_or_create_name_location("testlabel_bad") +my_bad_block = AsmBlockBad(loc_db, testlabel_bad) asmcfg.add_block(my_bad_block) assert list(asmcfg.get_bad_blocks()) == [my_bad_block] assert len(list(asmcfg.get_bad_blocks_predecessors())) == 0 @@ -141,8 +143,8 @@ assert len(list(asmcfg.get_bad_blocks_predecessors(strict=True))) == 0 ## Sanity check asmcfg.sanity_check() ### Next on itself -testlabel_nextitself = mdis.loc_db.get_or_create_name_location("testlabel_nextitself") -my_block_ni = AsmBlock(testlabel_nextitself) +testlabel_nextitself = loc_db.get_or_create_name_location("testlabel_nextitself") +my_block_ni = AsmBlock(loc_db, testlabel_nextitself) my_block_ni.bto.add(AsmConstraintNext(my_block_ni.loc_key)) asmcfg.add_block(my_block_ni) error_raised = False @@ -155,13 +157,13 @@ assert error_raised asmcfg.del_block(my_block_ni) asmcfg.sanity_check() ### Multiple next on the same node -testlabel_target = mdis.loc_db.get_or_create_name_location("testlabel_target") -my_block_target = AsmBlock(testlabel_target) +testlabel_target = loc_db.get_or_create_name_location("testlabel_target") +my_block_target = AsmBlock(loc_db, testlabel_target) asmcfg.add_block(my_block_target) -testlabel_src1 = mdis.loc_db.get_or_create_name_location("testlabel_src1") -testlabel_src2 = mdis.loc_db.get_or_create_name_location("testlabel_src2") -my_block_src1 = AsmBlock(testlabel_src1) -my_block_src2 = AsmBlock(testlabel_src2) +testlabel_src1 = loc_db.get_or_create_name_location("testlabel_src1") +testlabel_src2 = loc_db.get_or_create_name_location("testlabel_src2") +my_block_src1 = AsmBlock(loc_db, testlabel_src1) +my_block_src2 = AsmBlock(loc_db, testlabel_src2) my_block_src1.bto.add(AsmConstraintNext(my_block_target.loc_key)) asmcfg.add_block(my_block_src1) ### OK for now @@ -190,10 +192,10 @@ assert asmcfg.loc_key_to_block(my_block_src1.loc_key).max_size == 0 ## Check pendings ### Create a pending element -testlabel_pend_src = mdis.loc_db.get_or_create_name_location("testlabel_pend_src") -testlabel_pend_dst = mdis.loc_db.get_or_create_name_location("testlabel_pend_dst") -my_block_src = AsmBlock(testlabel_pend_src) -my_block_dst = AsmBlock(testlabel_pend_dst) +testlabel_pend_src = loc_db.get_or_create_name_location("testlabel_pend_src") +testlabel_pend_dst = loc_db.get_or_create_name_location("testlabel_pend_dst") +my_block_src = AsmBlock(loc_db, testlabel_pend_src) +my_block_dst = AsmBlock(loc_db, testlabel_pend_dst) my_block_src.bto.add(AsmConstraintTo(my_block_dst.loc_key)) asmcfg.add_block(my_block_src) ### Check resulting state @@ -220,8 +222,8 @@ asmcfg.sanity_check() # Test block_merge data2 = decode_hex("31c0eb0c31c9750c31d2eb0c31ffebf831dbebf031edebfc31f6ebf031e4c3") -cont2 = Container.from_string(data2) -mdis = machine.dis_engine(cont2.bin_stream, loc_db=cont2.loc_db) +cont2 = Container.from_string(data2, loc_db) +mdis = machine.dis_engine(cont2.bin_stream, loc_db=loc_db) ## Elements to merge asmcfg = mdis.dis_multiblock(0) ## Block alone @@ -253,7 +255,7 @@ assert len(entry_block.lines) == 4 assert list(map(str, entry_block.lines)) == ['XOR EAX, EAX', 'XOR EBX, EBX', 'XOR ECX, ECX', - 'JNZ loc_key_3'] + 'JNZ loc_key_27'] assert len(asmcfg.successors(entry_block.loc_key)) == 2 assert len(entry_block.bto) == 2 nextb = asmcfg.loc_key_to_block(next((cons.loc_key for cons in entry_block.bto @@ -264,11 +266,11 @@ assert len(nextb.lines) == 4 assert list(map(str, nextb.lines)) == ['XOR EDX, EDX', 'XOR ESI, ESI', 'XOR EDI, EDI', - 'JMP loc_key_4'] + 'JMP loc_key_28'] assert asmcfg.successors(nextb.loc_key) == [nextb.loc_key] assert len(tob.lines) == 2 assert list(map(str, tob.lines)) == ['XOR EBP, EBP', - 'JMP loc_key_3'] + 'JMP loc_key_27'] assert asmcfg.successors(tob.loc_key) == [tob.loc_key] # Check split_block @@ -278,7 +280,7 @@ mdis.apply_splitting(asmcfg) assert asmcfg_bef == asmcfg open("graph5.dot", "w").write(asmcfg.dot()) ## Create conditions for a block split -inside_firstbbl = mdis.loc_db.get_offset_location(4) +inside_firstbbl = loc_db.get_offset_location(4) tob.bto.add(AsmConstraintTo(inside_firstbbl)) asmcfg.rebuild_edges() assert len(asmcfg.pendings) == 1 @@ -295,7 +297,7 @@ lbl_newb = asmcfg.successors(entry_block.loc_key)[0] newb = asmcfg.loc_key_to_block(lbl_newb) assert len(newb.lines) == 2 assert list(map(str, newb.lines)) == ['XOR ECX, ECX', - 'JNZ loc_key_3'] + 'JNZ loc_key_27'] preds = asmcfg.predecessors(lbl_newb) assert len(preds) == 2 assert entry_block.loc_key in preds @@ -306,8 +308,8 @@ assert asmcfg.edges2constraint[(tob.loc_key, lbl_newb)] == AsmConstraint.c_to # Check double block split data = decode_hex("74097405b8020000007405b803000000b804000000c3") -cont = Container.from_string(data) -mdis = machine.dis_engine(cont.bin_stream, loc_db=cont.loc_db) +cont = Container.from_string(data, loc_db) +mdis = machine.dis_engine(cont.bin_stream, loc_db=loc_db) asmcfg = mdis.dis_multiblock(0) ## Check resulting disasm assert len(asmcfg.nodes()) == 6 @@ -328,10 +330,10 @@ solutions = list(matcher.match(asmcfg)) assert len(solutions) == 1 solution = solutions.pop() for jbbl, label in viewitems(solution): - offset = mdis.loc_db.get_location_offset(label) + offset = loc_db.get_location_offset(label) assert offset == int(jbbl._name, 16) -loc_key_dum = mdis.loc_db.get_or_create_name_location("dummy_loc") +loc_key_dum = loc_db.get_or_create_name_location("dummy_loc") asmcfg.add_node(loc_key_dum) error_raised = False try: diff --git a/test/core/parse_asm.py b/test/core/parse_asm.py index ab2bca4a..fdf1fbb8 100755 --- a/test/core/parse_asm.py +++ b/test/core/parse_asm.py @@ -3,6 +3,7 @@ from builtins import range import unittest +from miasm.core.locationdb import LocationDB class TestParseAsm(unittest.TestCase): @@ -10,6 +11,7 @@ class TestParseAsm(unittest.TestCase): def test_ParseTxt(self): from miasm.arch.x86.arch import mn_x86 from miasm.core.parse_asm import parse_txt + loc_db = LocationDB() ASM0 = ''' ; @@ -33,13 +35,14 @@ class TestParseAsm(unittest.TestCase): ASM1 = ''' .XXX ''' - self.assertTrue(parse_txt(mn_x86, 32, ASM0)) - self.assertRaises(ValueError, parse_txt, mn_x86, 32, ASM1) + self.assertTrue(parse_txt(mn_x86, 32, ASM0, loc_db)) + self.assertRaises(ValueError, parse_txt, mn_x86, 32, ASM1, loc_db) def test_DirectiveDontSplit(self): from miasm.arch.x86.arch import mn_x86 from miasm.core.parse_asm import parse_txt from miasm.core.asmblock import asm_resolve_final + loc_db = LocationDB() ASM0 = ''' lbl0: @@ -65,10 +68,8 @@ class TestParseAsm(unittest.TestCase): .string "toto" ''' - asmcfg, loc_db = parse_txt(mn_x86, 32, ASM0) - patches = asm_resolve_final(mn_x86, - asmcfg, - loc_db) + asmcfg = parse_txt(mn_x86, 32, ASM0, loc_db) + patches = asm_resolve_final(mn_x86, asmcfg, loc_db) lbls = [] for i in range(6): lbls.append(loc_db.get_name_location('lbl%d' % i)) @@ -87,6 +88,7 @@ class TestParseAsm(unittest.TestCase): def test_DirectiveSplit(self): from miasm.arch.x86.arch import mn_x86 from miasm.core.parse_asm import parse_txt + loc_db = LocationDB() ASM0 = ''' lbl0: @@ -96,7 +98,7 @@ class TestParseAsm(unittest.TestCase): RET ''' - asmcfg, loc_db = parse_txt(mn_x86, 32, ASM0) + asmcfg = parse_txt(mn_x86, 32, ASM0, loc_db) lbls = [] for i in range(2): lbls.append(loc_db.get_name_location('lbl%d' % i)) diff --git a/test/core/test_types.py b/test/core/test_types.py index 13a7824e..b903b809 100755 --- a/test/core/test_types.py +++ b/test/core/test_types.py @@ -14,6 +14,7 @@ from miasm.core.types import MemStruct, Num, Ptr, Str, \ set_allocator, MemUnion, Struct from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.os_dep.common import heap +from miasm.core.locationdb import LocationDB # Two structures with some fields class OtherStruct(MemStruct): @@ -35,7 +36,8 @@ class MyStruct(MemStruct): ("i", Ptr("I", Num("I"))), ] -jitter = Machine("x86_32").jitter("python") +loc_db = LocationDB() +jitter = Machine("x86_32").jitter(loc_db, "python") jitter.init_stack() addr = 0x1000 size = 0x1000 diff --git a/test/jitter/bad_block.py b/test/jitter/bad_block.py index 256d2388..e7484c9e 100644 --- a/test/jitter/bad_block.py +++ b/test/jitter/bad_block.py @@ -2,6 +2,7 @@ import sys from miasm.core.utils import decode_hex from miasm.jitter.csts import PAGE_READ, PAGE_WRITE, EXCEPT_UNK_MNEMO from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB def code_sentinelle(jitter): jitter.run = False @@ -9,7 +10,8 @@ def code_sentinelle(jitter): return True machine = Machine("x86_32") -jitter = machine.jitter(sys.argv[1]) +loc_db = LocationDB() +jitter = machine.jitter(loc_db, sys.argv[1]) jitter.init_stack() diff --git a/test/jitter/jit_options.py b/test/jitter/jit_options.py index 74808330..f1258323 100644 --- a/test/jitter/jit_options.py +++ b/test/jitter/jit_options.py @@ -5,6 +5,7 @@ import sys from miasm.core.utils import decode_hex from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB from pdb import pm # Shellcode @@ -21,16 +22,17 @@ from pdb import pm data = decode_hex("b810000000bb0100000083e8010f44cb75f8c3") run_addr = 0x40000000 +loc_db = LocationDB() def code_sentinelle(jitter): jitter.run = False jitter.pc = 0 return True -def init_jitter(): +def init_jitter(loc_db): global data, run_addr # Create jitter - myjit = Machine("x86_32").jitter(sys.argv[1]) + myjit = Machine("x86_32").jitter(loc_db, sys.argv[1]) myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, data) @@ -44,7 +46,7 @@ def init_jitter(): # Test 'max_exec_per_call' print("[+] First run, to jit blocks") -myjit = init_jitter() +myjit = init_jitter(loc_db) myjit.init_run(run_addr) myjit.continue_run() @@ -78,7 +80,7 @@ assert myjit.cpu.EAX >= 0xA # Test 'jit_maxline' print("[+] Run instr one by one") -myjit = init_jitter() +myjit = init_jitter(loc_db) myjit.jit.options["jit_maxline"] = 1 myjit.jit.options["max_exec_per_call"] = 1 diff --git a/test/jitter/jitcore.py b/test/jitter/jitcore.py index 75360542..1e009d9a 100644 --- a/test/jitter/jitcore.py +++ b/test/jitter/jitcore.py @@ -1,8 +1,10 @@ import sys +from miasm.core.locationdb import LocationDB from miasm.analysis.machine import Machine machine = Machine("x86_64") -jitter = machine.jitter(sys.argv[1]) +loc_db = LocationDB() +jitter = machine.jitter(loc_db, sys.argv[1]) jitter.cpu.RAX = 16565615892967251934 assert jitter.cpu.RAX == 16565615892967251934 diff --git a/test/jitter/jitload.py b/test/jitter/jitload.py index 3038c21c..7a72d1d5 100644 --- a/test/jitter/jitload.py +++ b/test/jitter/jitload.py @@ -5,12 +5,15 @@ from miasm.core.utils import decode_hex, encode_hex from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.analysis.machine import Machine from miasm.expression.expression import ExprId, ExprAssign, ExprInt, ExprMem +from miasm.core.locationdb import LocationDB + # Initial data: from 'example/samples/x86_32_sc.bin' data = decode_hex("8d49048d5b0180f90174058d5bffeb038d5b0189d8c3") +loc_db = LocationDB() # Init jitter -myjit = Machine("x86_32").jitter(sys.argv[1]) +myjit = Machine("x86_32").jitter(loc_db, sys.argv[1]) myjit.init_stack() run_addr = 0x40000000 diff --git a/test/jitter/jmp_out_mem.py b/test/jitter/jmp_out_mem.py index 2b064f73..3d01aacc 100644 --- a/test/jitter/jmp_out_mem.py +++ b/test/jitter/jmp_out_mem.py @@ -2,6 +2,8 @@ import sys from miasm.core.utils import decode_hex from miasm.jitter.csts import PAGE_READ, PAGE_WRITE, EXCEPT_ACCESS_VIOL from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB + def code_sentinelle(jitter): jitter.run = False @@ -10,7 +12,8 @@ def code_sentinelle(jitter): machine = Machine("x86_32") -jitter = machine.jitter(sys.argv[1]) +loc_db = LocationDB() +jitter = machine.jitter(loc_db, sys.argv[1]) jitter.init_stack() diff --git a/test/jitter/mem_breakpoint.py b/test/jitter/mem_breakpoint.py index 502d3d2b..8a5d69c5 100644 --- a/test/jitter/mem_breakpoint.py +++ b/test/jitter/mem_breakpoint.py @@ -5,6 +5,7 @@ from miasm.core.utils import decode_hex from miasm.analysis.machine import Machine from miasm.jitter.csts import PAGE_READ, PAGE_WRITE, \ EXCEPT_BREAKPOINT_MEMORY, EXCEPT_ACCESS_VIOL +from miasm.core.locationdb import LocationDB def mem_breakpoint_handler(jitter): print("======") @@ -35,7 +36,8 @@ def mem_breakpoint_handler(jitter): return True machine = Machine("aarch64l") -jitter = machine.jitter(sys.argv[1]) +loc_db = LocationDB() +jitter = machine.jitter(loc_db, sys.argv[1]) start_addr = 0xFFFFFF8008080000 end_addr = start_addr + 0x8000000 diff --git a/test/jitter/test_post_instr.py b/test/jitter/test_post_instr.py index 52274a46..16e51830 100644 --- a/test/jitter/test_post_instr.py +++ b/test/jitter/test_post_instr.py @@ -5,9 +5,11 @@ from miasm.core.utils import decode_hex from miasm.analysis.machine import Machine from miasm.jitter.csts import PAGE_READ, PAGE_WRITE, \ EXCEPT_BREAKPOINT_MEMORY, EXCEPT_ACCESS_VIOL +from miasm.core.locationdb import LocationDB machine = Machine("x86_32") -jitter = machine.jitter(sys.argv[1]) +loc_db = LocationDB() +jitter = machine.jitter(loc_db, sys.argv[1]) # Prepare stack and reset memory accesses to avoid an exception jitter.vm.add_memory_page(0x10000, PAGE_READ|PAGE_WRITE, b"\x00"*0x1000, "stack") diff --git a/test/jitter/vm_mngr.py b/test/jitter/vm_mngr.py index 0fec1734..404d20fe 100644 --- a/test/jitter/vm_mngr.py +++ b/test/jitter/vm_mngr.py @@ -1,8 +1,10 @@ import sys from miasm.jitter.csts import PAGE_READ, PAGE_WRITE from miasm.analysis.machine import Machine +from miasm.core.locationdb import LocationDB -myjit = Machine("x86_32").jitter(sys.argv[1]) +loc_db = LocationDB() +myjit = Machine("x86_32").jitter(loc_db, sys.argv[1]) base_addr = 0x13371337 page_size = 0x1000 diff --git a/test/os_dep/common.py b/test/os_dep/common.py index 749d71a7..ca7b8bc1 100755 --- a/test/os_dep/common.py +++ b/test/os_dep/common.py @@ -8,10 +8,12 @@ from miasm.analysis.machine import Machine import miasm.os_dep.common as commonapi from miasm.core.utils import pck32 from miasm.jitter.csts import PAGE_READ, PAGE_WRITE +from miasm.core.locationdb import LocationDB machine = Machine("x86_32") -jit = machine.jitter() +loc_db = LocationDB() +jit = machine.jitter(loc_db) jit.init_stack() class TestCommonAPI(unittest.TestCase): diff --git a/test/os_dep/linux/stdlib.py b/test/os_dep/linux/stdlib.py index a0432e65..c57499c7 100755 --- a/test/os_dep/linux/stdlib.py +++ b/test/os_dep/linux/stdlib.py @@ -7,10 +7,12 @@ from miasm.analysis.machine import Machine import miasm.os_dep.linux_stdlib as stdlib from miasm.core.utils import pck32 from miasm.jitter.csts import PAGE_READ, PAGE_WRITE +from miasm.core.locationdb import LocationDB machine = Machine("x86_32") -jit = machine.jitter() +loc_db = LocationDB() +jit = machine.jitter(loc_db) jit.init_stack() heap = stdlib.linobjs.heap diff --git a/test/os_dep/linux/test_env.py b/test/os_dep/linux/test_env.py index 13351a84..5bf3d2a0 100644 --- a/test/os_dep/linux/test_env.py +++ b/test/os_dep/linux/test_env.py @@ -5,6 +5,7 @@ from pdb import pm from miasm.analysis.binary import Container from miasm.analysis.sandbox import Sandbox_Linux_x86_32, Sandbox_Linux_x86_64,\ Sandbox_Linux_arml, Sandbox_Linux_aarch64l +from miasm.core.locationdb import LocationDB if len(sys.argv) < 2: print("Usage: %s <arch> ..." % sys.argv[0]) @@ -29,7 +30,8 @@ parser.add_argument("filename", help="ELF Filename") options = parser.parse_args(sys.argv[2:]) # Create sandbox -sb = sandbox(options.filename, options, globals()) +loc_db = LocationDB() +sb = sandbox(loc_db, options.filename, options, globals()) # Run sb.run() diff --git a/test/os_dep/win_api_x86_32.py b/test/os_dep/win_api_x86_32.py index 6d1c9835..19a3a9f7 100755 --- a/test/os_dep/win_api_x86_32.py +++ b/test/os_dep/win_api_x86_32.py @@ -9,10 +9,12 @@ import miasm.os_dep.win_api_x86_32 as winapi from miasm.os_dep.win_api_x86_32 import get_win_str_a, get_win_str_w from miasm.core.utils import pck32 from miasm.jitter.csts import PAGE_READ, PAGE_WRITE +from miasm.core.locationdb import LocationDB machine = Machine("x86_32") -jit = machine.jitter() +loc_db = LocationDB() +jit = machine.jitter(loc_db) jit.init_stack() heap = winapi.winobjs.heap |