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/asm/shellcode.py | |
| 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/asm/shellcode.py')
| -rwxr-xr-x | example/asm/shellcode.py | 71 |
1 files changed, 45 insertions, 26 deletions
diff --git a/example/asm/shellcode.py b/example/asm/shellcode.py index 9be5b517..67c882e9 100755 --- a/example/asm/shellcode.py +++ b/example/asm/shellcode.py @@ -1,14 +1,17 @@ #! /usr/bin/env python2 +from __future__ import print_function from argparse import ArgumentParser from pdb import pm -from elfesteem import pe_init -from elfesteem.strpatchwork import StrPatchwork +from future.utils import viewitems +from miasm.loader import pe_init +from miasm.loader.strpatchwork import StrPatchwork -from miasm2.core import parse_asm, asmblock -from miasm2.analysis.machine import Machine -from miasm2.core.interval import interval -from miasm2.core.locationdb import LocationDB +from miasm.core import parse_asm, asmblock +from miasm.analysis.machine import Machine +from miasm.core.interval import interval +from miasm.core.locationdb import LocationDB +from miasm.core.utils import iterbytes, int_to_byte parser = ArgumentParser("Multi-arch (32 bits) assembler") parser.add_argument('architecture', help="architecture: " + @@ -41,8 +44,17 @@ if args.PE: pe = pe_init.PE(wsize=size) s_text = pe.SHList.add_section(name="text", addr=0x1000, rawsize=0x1000) s_iat = pe.SHList.add_section(name="iat", rawsize=0x100) - new_dll = [({"name": "USER32.dll", - "firstthunk": s_iat.addr}, ["MessageBoxA"])] + new_dll = [ + ( + { + "name": "USER32.dll", + "firstthunk": s_iat.addr + }, + [ + "MessageBoxA" + ] + ) + ] pe.DirImport.add_dlldesc(new_dll) s_myimp = pe.SHList.add_section(name="myimp", rawsize=len(pe.DirImport)) pe.DirImport.set_rva(s_myimp.addr) @@ -51,8 +63,11 @@ if args.PE: addr_main = pe.rva2virt(s_text.addr) virt = pe.virt output = pe - dst_interval = interval([(pe.rva2virt(s_text.addr), - pe.rva2virt(s_text.addr + s_text.size))]) + dst_interval = interval( + [ + (pe.rva2virt(s_text.addr), pe.rva2virt(s_text.addr + s_text.size)) + ] + ) else: st = StrPatchwork() @@ -74,20 +89,26 @@ asmcfg, loc_db = parse_asm.parse_txt(machine.mn, attrib, source, loc_db) loc_db.set_location_offset(loc_db.get_name_location("main"), addr_main) if args.PE: - loc_db.set_location_offset(loc_db.get_or_create_name_location("MessageBoxA"), - pe.DirImport.get_funcvirt('USER32.dll', - 'MessageBoxA')) + loc_db.set_location_offset( + loc_db.get_or_create_name_location("MessageBoxA"), + pe.DirImport.get_funcvirt( + 'USER32.dll', + 'MessageBoxA' + ) + ) # Print and graph firsts blocks before patching it for block in asmcfg.blocks: - print block + print(block) open("graph.dot", "w").write(asmcfg.dot()) # Apply patches -patches = asmblock.asm_resolve_final(machine.mn, - asmcfg, - loc_db, - dst_interval) +patches = asmblock.asm_resolve_final( + machine.mn, + asmcfg, + loc_db, + dst_interval +) if args.encrypt: # Encrypt code loc_start = loc_db.get_or_create_name_location(args.encrypt[0]) @@ -95,20 +116,18 @@ if args.encrypt: ad_start = loc_db.get_location_offset(loc_start) ad_stop = loc_db.get_location_offset(loc_stop) - new_patches = dict(patches) - for ad, val in patches.items(): + for ad, val in list(viewitems(patches)): if ad_start <= ad < ad_stop: - new_patches[ad] = "".join([chr(ord(x) ^ 0x42) for x in val]) - patches = new_patches + patches[ad] = b"".join(int_to_byte(ord(x) ^ 0x42) for x in iterbytes(val)) -print patches +print(patches) if isinstance(virt, StrPatchwork): - for offset, raw in patches.items(): + for offset, raw in viewitems(patches): virt[offset] = raw else: - for offset, raw in patches.items(): + for offset, raw in viewitems(patches): virt.set(offset, raw) # Produce output -open(args.output, 'wb').write(str(output)) +open(args.output, 'wb').write(bytes(output)) |