diff options
| author | Camille Mougey <commial@gmail.com> | 2019-03-07 14:37:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-07 14:37:07 +0100 |
| commit | 4c2320b46250a8d6f8774e1218544b72a154cd8e (patch) | |
| tree | b67e7b072439f84109bd39dad8ed7f3f135224f8 /example/jitter | |
| parent | eab809932871f91d6f4aa770fc321af9e156e0f5 (diff) | |
| parent | 26c1075723a02984da6d3bc7423c5c0c43082dc3 (diff) | |
| download | miasm-4c2320b46250a8d6f8774e1218544b72a154cd8e.tar.gz miasm-4c2320b46250a8d6f8774e1218544b72a154cd8e.zip | |
Merge pull request #990 from serpilliere/support_python2_python3
Support python2 python3
Diffstat (limited to 'example/jitter')
| -rwxr-xr-x | example/jitter/arm.py | 5 | ||||
| -rwxr-xr-x | example/jitter/arm_sc.py | 13 | ||||
| -rwxr-xr-x | example/jitter/example_types.py | 64 | ||||
| -rwxr-xr-x | example/jitter/mips32.py | 15 | ||||
| -rwxr-xr-x | example/jitter/msp430.py | 15 | ||||
| -rw-r--r-- | example/jitter/run_with_linuxenv.py | 51 | ||||
| -rw-r--r-- | example/jitter/sandbox_call.py | 8 | ||||
| -rw-r--r-- | example/jitter/sandbox_elf_aarch64l.py | 4 | ||||
| -rw-r--r-- | example/jitter/sandbox_elf_ppc32.py | 12 | ||||
| -rw-r--r-- | example/jitter/sandbox_pe_x86_32.py | 2 | ||||
| -rw-r--r-- | example/jitter/sandbox_pe_x86_64.py | 2 | ||||
| -rw-r--r-- | example/jitter/test_x86_32_seh.py | 6 | ||||
| -rw-r--r-- | example/jitter/trace.py | 14 | ||||
| -rw-r--r-- | example/jitter/unpack_upx.py | 18 | ||||
| -rw-r--r-- | example/jitter/x86_32.py | 6 |
15 files changed, 128 insertions, 107 deletions
diff --git a/example/jitter/arm.py b/example/jitter/arm.py index e475abeb..daea2428 100755 --- a/example/jitter/arm.py +++ b/example/jitter/arm.py @@ -1,9 +1,10 @@ #! /usr/bin/env python2 #-*- coding:utf-8 -*- +from __future__ import print_function import logging from pdb import pm -from miasm2.analysis.sandbox import Sandbox_Linux_arml +from miasm.analysis.sandbox import Sandbox_Linux_arml # Get arguments parser = Sandbox_Linux_arml.parser(description="""Sandbox an elf binary with arm @@ -22,7 +23,7 @@ else: logging.basicConfig(level=logging.WARNING) if options.verbose is True: - print sb.jitter.vm + print(sb.jitter.vm) # Run the code sb.run() diff --git a/example/jitter/arm_sc.py b/example/jitter/arm_sc.py index 7720ad68..9ff770ff 100755 --- a/example/jitter/arm_sc.py +++ b/example/jitter/arm_sc.py @@ -1,8 +1,9 @@ #! /usr/bin/env python2 #-*- coding:utf-8 -*- -from miasm2.analysis.sandbox import Sandbox_Linux_armb_str -from miasm2.analysis.sandbox import Sandbox_Linux_arml_str -from elfesteem.strpatchwork import StrPatchwork +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 pdb import pm @@ -35,8 +36,8 @@ stop = sb.jitter.cpu.R1 s = sb.jitter.vm.get_mem(start, stop-start) s = StrPatchwork(s) for i, c in enumerate(s): - s[i] = chr(ord(c)^0x11) -s = str(s) -assert(s == "test string\x00") + s[i] = int_to_byte(ord(c)^0x11) +s = bytes(s) +assert(s == b"test string\x00") diff --git a/example/jitter/example_types.py b/example/jitter/example_types.py index bcf9bf70..653adaf9 100755 --- a/example/jitter/example_types.py +++ b/example/jitter/example_types.py @@ -1,14 +1,16 @@ #! /usr/bin/env python2 -"""This script is just a short example of common usages for miasm2.core.types. +"""This script is just a short example of common usages for miasm.core.types. For a more complete view of what is possible, tests/core/types.py covers most of the module possibilities, and the module doc gives useful information as well. """ +from __future__ import print_function -from miasm2.analysis.machine import Machine -from miasm2.core.types import MemStruct, Self, Void, Str, Array, Ptr, \ +from miasm.core.utils import iterbytes +from miasm.analysis.machine import Machine +from miasm.core.types import MemStruct, Self, Void, Str, Array, Ptr, \ Num, Array, set_allocator -from miasm2.os_dep.common import heap +from miasm.os_dep.common import heap # Instantiate a heap my_heap = heap() @@ -145,12 +147,12 @@ class DataStr(MemStruct): ] -print "This script demonstrates a LinkedList implementation using the types " -print "module in the first part, and how to play with some casts in the second." -print +print("This script demonstrates a LinkedList implementation using the types ") +print("module in the first part, and how to play with some casts in the second.") +print() # A random jitter -# You can also use miasm2.jitter.VmMngr.Vm(), but it does not happen in real +# 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") vm = jitter.vm @@ -172,17 +174,17 @@ assert link.size == 3 # If you get it directly from the VM, it is updated as well raw_size = vm.get_mem(link.get_addr("size"), link.get_type() .get_field_type("size").size) -assert raw_size == '\x03\x00\x00\x00' +assert raw_size == b'\x03\x00\x00\x00' -print "The linked list just built:" -print repr(link), '\n' +print("The linked list just built:") +print(repr(link), '\n') -print "Its uninitialized data elements:" +print("Its uninitialized data elements:") for data in link: # __iter__ returns MemVoids here, just cast them to the real data type real_data = data.cast(DataArray) - print repr(real_data) -print + print(repr(real_data)) +print() # Now let's play with one data data = link.pop(DataArray) @@ -196,9 +198,9 @@ assert data.arrayptr.deref == data.array # Let's say that it is a DataStr: datastr = data.cast(DataStr) -print "First element casted to DataStr:" -print repr(datastr) -print +print("First element casted to DataStr:") +print(repr(datastr)) +print() # data and datastr really share the same memory: data.val1 = 0x34 @@ -212,29 +214,29 @@ memstr = datastr.data.deref # Note that memstr is Str("utf16") memstr.val = 'Miams' -print "Cast data.array to MemStr and set the string value:" -print repr(memstr) -print +print("Cast data.array to MemStr and set the string value:") +print(repr(memstr)) +print() # If you followed, memstr and data.array point to the same object, so: -raw_miams = '\x00'.join('Miams') + '\x00'*3 -raw_miams_array = [ord(c) for c in raw_miams] +raw_miams = 'Miams'.encode('utf-16le') + b'\x00'*2 +raw_miams_array = [ord(c) for c in iterbytes(raw_miams)] assert list(data.array)[:len(raw_miams_array)] == raw_miams_array assert data.array.cast(Str("utf16")) == memstr # Default is "ansi" assert data.array.cast(Str()) != memstr assert data.array.cast(Str("utf16")).val == memstr.val -print "See that the original array has been modified:" -print repr(data) -print +print("See that the original array has been modified:") +print(repr(data)) +print() # Some type manipulation examples, for example let's construct an argv for # a program: # Let's say that we have two arguments, +1 for the program name and +1 for the # final null ptr in argv, the array has 4 elements: argv_t = Array(Ptr("<I", Str()), 4) -print "3 arguments argv type:", argv_t +print("3 arguments argv type:", argv_t) # alloc argv somewhere argv = argv_t.lval(vm) @@ -249,10 +251,10 @@ argv[3].val = 0 # If you changed your mind on the second arg, you could do: argv[2].deref.val = "42" -print "An argv instance:", repr(argv) -print "argv values:", repr([val.deref.val for val in argv[:-1]]) -print +print("An argv instance:", repr(argv)) +print("argv values:", repr([val.deref.val for val in argv[:-1]])) +print() -print "See test/core/types.py and the miasm2.core.types module doc for " -print "more information." +print("See test/core/types.py and the miasm.core.types module doc for ") +print("more information.") diff --git a/example/jitter/mips32.py b/example/jitter/mips32.py index 70181a2a..4aeb576f 100755 --- a/example/jitter/mips32.py +++ b/example/jitter/mips32.py @@ -1,9 +1,10 @@ #! /usr/bin/env python2 #-*- coding:utf-8 -*- +from __future__ import print_function from argparse import ArgumentParser -from miasm2.analysis import debugging -from miasm2.jitter.csts import * -from miasm2.analysis.machine import Machine +from miasm.analysis import debugging +from miasm.jitter.csts import * +from miasm.analysis.machine import Machine parser = ArgumentParser( description="""Sandbox raw binary with mips32 engine @@ -44,12 +45,16 @@ def jit_mips32_binary(args): trace_new_blocks=args.log_newbloc ) - myjit.vm.add_memory_page(0, PAGE_READ | PAGE_WRITE, open(filepath).read()) + myjit.vm.add_memory_page( + 0, + PAGE_READ | PAGE_WRITE, + open(filepath, 'rb').read() + ) myjit.add_breakpoint(0x1337BEEF, code_sentinelle) # for stack - myjit.vm.add_memory_page(0xF000, PAGE_READ | PAGE_WRITE, "\x00"*0x1000) + myjit.vm.add_memory_page(0xF000, PAGE_READ | PAGE_WRITE, b"\x00"*0x1000) myjit.cpu.SP = 0xF800 diff --git a/example/jitter/msp430.py b/example/jitter/msp430.py index 36e45421..972a1fdc 100755 --- a/example/jitter/msp430.py +++ b/example/jitter/msp430.py @@ -1,9 +1,10 @@ #! /usr/bin/env python2 #-*- coding:utf-8 -*- +from __future__ import print_function from argparse import ArgumentParser -from miasm2.analysis import debugging -from miasm2.jitter.csts import * -from miasm2.analysis.machine import Machine +from miasm.analysis import debugging +from miasm.jitter.csts import * +from miasm.analysis.machine import Machine parser = ArgumentParser( description="""Sandbox raw binary with msp430 engine @@ -39,12 +40,16 @@ def jit_msp430_binary(args): trace_new_blocks=args.log_newbloc ) - myjit.vm.add_memory_page(0, PAGE_READ | PAGE_WRITE, open(filepath, "rb").read()) + myjit.vm.add_memory_page( + 0, + PAGE_READ | PAGE_WRITE, + open(filepath, "rb").read() + ) myjit.add_breakpoint(0x1337, lambda _: exit(0)) # for stack - myjit.vm.add_memory_page(0xF000, PAGE_READ | PAGE_WRITE, "\x00"*0x1000) + myjit.vm.add_memory_page(0xF000, PAGE_READ | PAGE_WRITE, b"\x00"*0x1000) myjit.cpu.SP = 0xF800 diff --git a/example/jitter/run_with_linuxenv.py b/example/jitter/run_with_linuxenv.py index f4900a96..f981d2dd 100644 --- a/example/jitter/run_with_linuxenv.py +++ b/example/jitter/run_with_linuxenv.py @@ -2,11 +2,11 @@ from argparse import ArgumentParser import logging import re -from elfesteem import elf as elf_csts +from miasm.loader import elf as elf_csts -from miasm2.os_dep.linux import environment, syscall -from miasm2.analysis.machine import Machine -from miasm2.analysis.binary import Container +from miasm.os_dep.linux import environment, syscall +from miasm.analysis.machine import Machine +from miasm.analysis.binary import Container parser = ArgumentParser("Run an ELF in a Linux-like environment") parser.add_argument("target", help="Target ELF") @@ -24,8 +24,8 @@ if args.verbose: syscall.log.setLevel(logging.DEBUG) # Get corresponding interpreter and reloc address -cont_target_tmp = Container.from_stream(open(args.target)) -ld_path = str(cont_target_tmp.executable.getsectionbyname(".interp").content).strip("\x00") +cont_target_tmp = Container.from_stream(open(args.target, 'rb')) +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 elif cont_target_tmp.executable.Ehdr.type == elf_csts.ET_EXEC: @@ -52,29 +52,38 @@ else: # Load the interpreter in memory, applying relocation linux_env = LinuxEnvironment() -linux_env.filesystem.passthrough.append(re.compile(args.passthrough)) +linux_env.filesystem.passthrough.append(re.compile(args.passthrough.encode())) ld_path = linux_env.filesystem.resolve_path(ld_path) -cont_ld = Container.from_stream(open(ld_path), - vm=jitter.vm, - addr=0x80000000, - apply_reloc=True) +cont_ld = Container.from_stream( + open(ld_path, "rb"), + vm=jitter.vm, + addr=0x80000000, + apply_reloc=True +) # Load the target ELF in memory, without applying reloc loc_db = cont_ld.loc_db -cont_target = Container.from_stream(open(args.target), vm=jitter.vm, - loc_db=loc_db, - addr=elf_base_addr, - apply_reloc=False) +cont_target = Container.from_stream( + open(args.target, "rb"), + vm=jitter.vm, + loc_db=loc_db, + addr=elf_base_addr, + apply_reloc=False +) # PHDR containing the PH header -elf_phdr_header = [ph32.ph for ph32 in cont_target.executable.ph - if ph32.ph.type == elf_csts.PT_PHDR][0] +elf_phdr_header = next( + ph32.ph for ph32 in cont_target.executable.ph + if ph32.ph.type == elf_csts.PT_PHDR +) # Prepare the desired environment -argv = [args.target] + args.extra_args +argv = [args.target.encode()] + [arg.encode() for arg in args.extra_args] if args.flags: argv += ["-%s" % args.flags] -envp = {"PATH": "/usr/local/bin", "USER": linux_env.user_name} -auxv = environment.AuxVec(elf_base_addr + elf_phdr_header.vaddr, - cont_target.entry_point, linux_env) +envp = {b"PATH": b"/usr/local/bin", b"USER": linux_env.user_name} +auxv = environment.AuxVec( + elf_base_addr + elf_phdr_header.vaddr, + cont_target.entry_point, linux_env +) prepare_loader(jitter, argv, envp, auxv, linux_env) syscall.enable_syscall_handling(jitter, linux_env, syscall_callbacks) diff --git a/example/jitter/sandbox_call.py b/example/jitter/sandbox_call.py index 3eb0b86e..7d400b7d 100644 --- a/example/jitter/sandbox_call.py +++ b/example/jitter/sandbox_call.py @@ -1,10 +1,10 @@ """This example illustrate the Sandbox.call API, for direct call of a given function""" -from miasm2.analysis.sandbox import Sandbox_Linux_arml -from miasm2.analysis.binary import Container -from miasm2.os_dep.linux_stdlib import linobjs -from miasm2.core.utils import hexdump +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 # Parse arguments parser = Sandbox_Linux_arml.parser(description="ELF sandboxer") diff --git a/example/jitter/sandbox_elf_aarch64l.py b/example/jitter/sandbox_elf_aarch64l.py index 0f028876..472b2354 100644 --- a/example/jitter/sandbox_elf_aarch64l.py +++ b/example/jitter/sandbox_elf_aarch64l.py @@ -1,7 +1,7 @@ import logging from pdb import pm -from miasm2.analysis.sandbox import Sandbox_Linux_aarch64l -from miasm2.jitter.jitload import log_func +from miasm.analysis.sandbox import Sandbox_Linux_aarch64l +from miasm.jitter.jitload import log_func # Insert here user defined methods diff --git a/example/jitter/sandbox_elf_ppc32.py b/example/jitter/sandbox_elf_ppc32.py index c5960e8e..829381fc 100644 --- a/example/jitter/sandbox_elf_ppc32.py +++ b/example/jitter/sandbox_elf_ppc32.py @@ -1,16 +1,10 @@ import os from pdb import pm -from miasm2.analysis.sandbox import Sandbox_Linux_ppc32b -from miasm2.jitter.csts import * -from miasm2.jitter.jitload import log_func +from miasm.analysis.sandbox import Sandbox_Linux_ppc32b +from miasm.jitter.csts import * +from miasm.jitter.jitload import log_func import logging - -# Python auto completion -filename = os.environ.get('PYTHONSTARTUP') -if filename and os.path.isfile(filename): - execfile(filename) - # Insert here user defined methods # Parse arguments diff --git a/example/jitter/sandbox_pe_x86_32.py b/example/jitter/sandbox_pe_x86_32.py index 3a627b19..263fad94 100644 --- a/example/jitter/sandbox_pe_x86_32.py +++ b/example/jitter/sandbox_pe_x86_32.py @@ -1,5 +1,5 @@ from pdb import pm -from miasm2.analysis.sandbox import Sandbox_Win_x86_32 +from miasm.analysis.sandbox import Sandbox_Win_x86_32 # Insert here user defined methods diff --git a/example/jitter/sandbox_pe_x86_64.py b/example/jitter/sandbox_pe_x86_64.py index 773c54b9..4d8f00ce 100644 --- a/example/jitter/sandbox_pe_x86_64.py +++ b/example/jitter/sandbox_pe_x86_64.py @@ -1,5 +1,5 @@ from pdb import pm -from miasm2.analysis.sandbox import Sandbox_Win_x86_64 +from miasm.analysis.sandbox import Sandbox_Win_x86_64 # Insert here user defined methods diff --git a/example/jitter/test_x86_32_seh.py b/example/jitter/test_x86_32_seh.py index e7f8cff4..595b9586 100644 --- a/example/jitter/test_x86_32_seh.py +++ b/example/jitter/test_x86_32_seh.py @@ -1,8 +1,8 @@ import os from pdb import pm -from miasm2.analysis.sandbox import Sandbox_Win_x86_32 -from miasm2.os_dep import win_api_x86_32_seh -from miasm2.jitter.csts import * +from miasm.analysis.sandbox import Sandbox_Win_x86_32 +from miasm.os_dep import win_api_x86_32_seh +from miasm.jitter.csts import * def deal_exception_access_violation(jitter): jitter.pc = win_api_x86_32_seh.fake_seh_handler(jitter, win_api_x86_32_seh.EXCEPTION_ACCESS_VIOLATION) diff --git a/example/jitter/trace.py b/example/jitter/trace.py index e1683450..46b313c1 100644 --- a/example/jitter/trace.py +++ b/example/jitter/trace.py @@ -6,12 +6,14 @@ This example demonstrates two instrumentation possibility: Note: for better performance, one can also extend Codegen to produce instrumentation at the C / LLVM level """ +from __future__ import print_function + import os import time from pdb import pm -from miasm2.analysis.sandbox import Sandbox_Linux_arml -from miasm2.jitter.emulatedsymbexec import EmulatedSymbExec -from miasm2.jitter.jitcore_python import JitCore_Python +from miasm.analysis.sandbox import Sandbox_Linux_arml +from miasm.jitter.emulatedsymbexec import EmulatedSymbExec +from miasm.jitter.jitcore_python import JitCore_Python # Function called at each instruction instr_count = 0 @@ -26,11 +28,11 @@ class ESETrackMemory(EmulatedSymbExec): def mem_read(self, expr_mem): value = super(ESETrackMemory, self).mem_read(expr_mem) - print "Read %s: %s" % (expr_mem, value) + print("Read %s: %s" % (expr_mem, value)) return value def mem_write(self, dest, data): - print "Write %s: %s" % (dest, data) + print("Write %s: %s" % (dest, data)) return super(ESETrackMemory, self).mem_write(dest, data) # Parse arguments @@ -55,4 +57,4 @@ sb.run() stop_time = time.time() assert sb.jitter.run is False -print "Instr speed: %02.f / sec" % (instr_count / (stop_time - start_time)) +print("Instr speed: %02.f / sec" % (instr_count / (stop_time - start_time))) diff --git a/example/jitter/unpack_upx.py b/example/jitter/unpack_upx.py index 6bcef1ab..3b8125f4 100644 --- a/example/jitter/unpack_upx.py +++ b/example/jitter/unpack_upx.py @@ -1,8 +1,9 @@ +from __future__ import print_function import os import logging from pdb import pm -from elfesteem import pe -from miasm2.analysis.sandbox import Sandbox_Win_x86_32 +from miasm.loader import pe +from miasm.analysis.sandbox import Sandbox_Win_x86_32 # User defined methods @@ -12,12 +13,12 @@ def kernel32_GetProcAddress(jitter): # When the function is called, EBX is a pointer to the destination buffer dst_ad = jitter.cpu.EBX - logging.info('EBX ' + hex(dst_ad)) + logging.error('EBX ' + hex(dst_ad)) # Handle ordinal imports fname = (args.fname if args.fname < 0x10000 else jitter.get_str_ansi(args.fname)) - logging.info(fname) + logging.error(fname) # Get the generated address of the library, and store it in memory to # dst_ad @@ -38,6 +39,7 @@ parser.add_argument("--graph", action="store_true") options = parser.parse_args() options.load_hdr = True + sb = Sandbox_Win_x86_32(options.filename, options, globals(), parse_reloc=False) @@ -48,7 +50,7 @@ else: logging.basicConfig(level=logging.WARNING) if options.verbose is True: - print sb.jitter.vm + print(sb.jitter.vm) # Ensure there is one and only one leave (for OEP discovering) mdis = sb.machine.dis_engine(sb.jitter.bs) @@ -70,7 +72,7 @@ if options.graph is True: if options.verbose is True: - print sb.jitter.vm + print(sb.jitter.vm) def update_binary(jitter): @@ -91,7 +93,7 @@ sb.jitter.add_breakpoint(end_offset, update_binary) sb.run() # Rebuild PE -# Alternative solution: miasm2.jitter.loader.pe.vm2pe(sb.jitter, out_fname, +# Alternative solution: miasm.jitter.loader.pe.vm2pe(sb.jitter, out_fname, # libs=sb.libs, e_orig=sb.pe) new_dll = [] @@ -114,4 +116,4 @@ sb.pe.NThdr.optentries[pe.DIRECTORY_ENTRY_DELAY_IMPORT].rva = 0 bname, fname = os.path.split(options.filename) fname = os.path.join(bname, fname.replace('.', '_')) -open(fname + '_unupx.bin', 'w').write(str(sb.pe)) +open(fname + '_unupx.bin', 'wb').write(bytes(sb.pe)) diff --git a/example/jitter/x86_32.py b/example/jitter/x86_32.py index 5272f732..c2273b69 100644 --- a/example/jitter/x86_32.py +++ b/example/jitter/x86_32.py @@ -1,6 +1,6 @@ from argparse import ArgumentParser -from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE -from miasm2.analysis.machine import Machine +from miasm.jitter.csts import PAGE_READ, PAGE_WRITE +from miasm.analysis.machine import Machine from pdb import pm @@ -20,7 +20,7 @@ def code_sentinelle(jitter): myjit = Machine("x86_32").jitter(args.jitter) myjit.init_stack() -data = open(args.filename).read() +data = open(args.filename, 'rb').read() run_addr = 0x40000000 myjit.vm.add_memory_page(run_addr, PAGE_READ | PAGE_WRITE, data) |