diff options
| author | serpilliere <serpilliere@users.noreply.github.com> | 2019-09-23 08:17:49 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-23 08:17:49 +0200 |
| commit | e37e481c7bdae0dbbeb5e07d0f943b4b3840f465 (patch) | |
| tree | 84a479a7ab6324f651d406226aef72afca89f7c9 | |
| parent | 5a039ca675a60d6de201cb0b89291ee2bd2304c4 (diff) | |
| parent | 523507835ed6789a9489120023b539f6ae82eb18 (diff) | |
| download | miasm-e37e481c7bdae0dbbeb5e07d0f943b4b3840f465.tar.gz miasm-e37e481c7bdae0dbbeb5e07d0f943b4b3840f465.zip | |
Merge pull request #1065 from serpilliere/fix_bytes_dll_names
Fix bytes dllname
| -rw-r--r-- | example/symbol_exec/dse_crackme.py | 2 | ||||
| -rw-r--r-- | miasm/analysis/sandbox.py | 4 | ||||
| -rw-r--r-- | miasm/jitter/jitload.py | 13 | ||||
| -rw-r--r-- | miasm/jitter/loader/pe.py | 8 | ||||
| -rw-r--r-- | miasm/os_dep/common.py | 14 | ||||
| -rw-r--r-- | miasm/os_dep/linux/syscall.py | 2 | ||||
| -rw-r--r-- | miasm/os_dep/linux_stdlib.py | 10 | ||||
| -rw-r--r-- | miasm/os_dep/win_api_x86_32.py | 6 | ||||
| -rw-r--r-- | miasm/os_dep/win_api_x86_32_seh.py | 6 | ||||
| -rw-r--r-- | test/arch/x86/qemu/testqemu.py | 36 | ||||
| -rw-r--r-- | test/arch/x86/qemu/testqemu64.py | 32 | ||||
| -rwxr-xr-x | test/os_dep/linux/stdlib.py | 2 | ||||
| -rwxr-xr-x | test/os_dep/win_api_x86_32.py | 2 |
13 files changed, 70 insertions, 67 deletions
diff --git a/example/symbol_exec/dse_crackme.py b/example/symbol_exec/dse_crackme.py index 90774dc3..be9f4490 100644 --- a/example/symbol_exec/dse_crackme.py +++ b/example/symbol_exec/dse_crackme.py @@ -280,7 +280,7 @@ while todo: sb.run() except FinishOn as finish_info: print(finish_info.string) - if finish_info.string == b"OK": + if finish_info.string == "OK": # Stop if the expected result is found found = True break diff --git a/miasm/analysis/sandbox.py b/miasm/analysis/sandbox.py index e5595071..b8aaf788 100644 --- a/miasm/analysis/sandbox.py +++ b/miasm/analysis/sandbox.py @@ -51,6 +51,8 @@ class Sandbox(object): """ # Initialize + if not isinstance(fname, bytes): + fname = fname.encode('utf8') self.fname = fname self.options = options if custom_methods is None: @@ -185,7 +187,7 @@ class OS_Win(OS): "ole32.dll", "urlmon.dll", "ws2_32.dll", 'advapi32.dll', "psapi.dll", ] - modules_path = "win_dll" + modules_path = b"win_dll" def __init__(self, custom_methods, *args, **kwargs): from miasm.jitter.loader.pe import vm_load_pe, vm_load_pe_libs,\ diff --git a/miasm/jitter/jitload.py b/miasm/jitter/jitload.py index 0d8ab722..9fcb0b0a 100644 --- a/miasm/jitter/jitload.py +++ b/miasm/jitter/jitload.py @@ -439,7 +439,8 @@ class Jitter(object): self.vm.get_mem(tmp, 1) != b"\x00"): tmp += 1 l += 1 - return self.vm.get_mem(addr, l) + value = self.vm.get_mem(addr, l) + return value.decode('utf8') def get_str_unic(self, addr, max_char=None): """Get unicode str from vm. @@ -455,14 +456,14 @@ class Jitter(object): s = s.decode("utf-16le") return s - def set_str_ansi(self, addr, s): + def set_str_ansi(self, addr, string): """Set an ansi string in memory""" - s = s + b"\x00" - self.vm.set_mem(addr, s) + string = (string + "\x00").encode('utf8') + self.vm.set_mem(addr, string) - def set_str_unic(self, addr, s): + def set_str_unic(self, addr, string): """Set an unicode string in memory""" - s = b"\x00".join(list(s)) + b'\x00' * 3 + s = (string + "\x00").encode('utf-16le') self.vm.set_mem(addr, s) @staticmethod diff --git a/miasm/jitter/loader/pe.py b/miasm/jitter/loader/pe.py index a82b79f6..c779f508 100644 --- a/miasm/jitter/loader/pe.py +++ b/miasm/jitter/loader/pe.py @@ -266,8 +266,12 @@ def vm_load_pe_libs(vm, libs_name, libs, lib_path_base, **kargs): Return a dictionary Filename -> PE instances Extra arguments are passed to vm_load_pe_lib """ - return {fname: vm_load_pe_lib(vm, fname, libs, lib_path_base, **kargs) - for fname in libs_name} + out = {} + for fname in libs_name: + if not isinstance(fname, bytes): + fname = fname.encode('utf8') + out[fname] = vm_load_pe_lib(vm, fname, libs, lib_path_base, **kargs) + return out def vm_fix_imports_pe_libs(lib_imgs, libs, lib_path_base, diff --git a/miasm/os_dep/common.py b/miasm/os_dep/common.py index 87602b3c..0b4d7e11 100644 --- a/miasm/os_dep/common.py +++ b/miasm/os_dep/common.py @@ -130,16 +130,10 @@ def unix_to_sbpath(path): def get_fmt_args(fmt, cur_arg, get_str, get_arg_n): idx = 0 fmt = get_str(fmt) - if isinstance(fmt, bytes): - chars_format = b'%cdfsuxX' - char_percent = b'%' - char_string = b's' - output = b"" - else: - chars_format = u'%cdfsuxX' - char_percent = u'%' - char_string = u's' - output = u"" + chars_format = '%cdfsuxX' + char_percent = '%' + char_string = 's' + output = "" while True: if idx == len(fmt): diff --git a/miasm/os_dep/linux/syscall.py b/miasm/os_dep/linux/syscall.py index 353d61cf..7fede9f1 100644 --- a/miasm/os_dep/linux/syscall.py +++ b/miasm/os_dep/linux/syscall.py @@ -528,7 +528,7 @@ def sys_x86_64_getdents(jitter, linux_env): d_reclen = 8 * 2 + 2 + 1 + len(name) + 1 d_off = cur_len + d_reclen entry = struct.pack("QqH", d_ino, d_off, d_reclen) + \ - name + b"\x00" + struct.pack("B", d_type) + name.encode("utf8") + b"\x00" + struct.pack("B", d_type) assert len(entry) == d_reclen return entry diff --git a/miasm/os_dep/linux_stdlib.py b/miasm/os_dep/linux_stdlib.py index b2836881..3fa5b02e 100644 --- a/miasm/os_dep/linux_stdlib.py +++ b/miasm/os_dep/linux_stdlib.py @@ -153,7 +153,7 @@ def xxx_snprintf(jitter): output = get_fmt_args(jitter, fmt, cur_arg) output = output[:size - 1] ret = len(output) - jitter.vm.set_mem(args.string, output + b'\x00') + jitter.vm.set_mem(args.string, (output + '\x00').encode('utf8')) return jitter.func_ret_systemv(ret_addr, ret) @@ -162,7 +162,7 @@ def xxx_sprintf(jitter): cur_arg, fmt = 2, args.fmt output = get_fmt_args(jitter, fmt, cur_arg) ret = len(output) - jitter.vm.set_mem(args.string, output + b'\x00') + jitter.vm.set_mem(args.string, (output + '\x00').encode('utf8')) return jitter.func_ret_systemv(ret_addr, ret) @@ -171,14 +171,14 @@ def xxx_printf(jitter): cur_arg, fmt = 1, args.fmt output = get_fmt_args(jitter, fmt, cur_arg) ret = len(output) - stdout.write(output) + stdout.write(output.encode('utf8')) return jitter.func_ret_systemv(ret_addr, ret) def xxx_strcpy(jitter): ret_ad, args = jitter.func_args_systemv(["dst", "src"]) - str_src = jitter.get_str_ansi(args.src) + b'\x00' - jitter.vm.set_mem(args.dst, str_src) + str_src = jitter.get_str_ansi(args.src) + '\x00' + jitter.vm.set_mem(args.dst, str_src.encode('utf8')) jitter.func_ret_systemv(ret_ad, args.dst) diff --git a/miasm/os_dep/win_api_x86_32.py b/miasm/os_dep/win_api_x86_32.py index ebf40cb0..5ef1b845 100644 --- a/miasm/os_dep/win_api_x86_32.py +++ b/miasm/os_dep/win_api_x86_32.py @@ -774,7 +774,7 @@ def kernel32_VirtualAlloc(jitter): if args.lpvoid == 0: alloc_addr = winobjs.heap.next_addr(args.dwsize) jitter.vm.add_memory_page( - alloc_addr, ACCESS_DICT[args.flprotect], "\x00" * args.dwsize, + alloc_addr, ACCESS_DICT[args.flprotect], b"\x00" * args.dwsize, "Alloc in %s ret 0x%X" % (whoami(), ret_ad)) else: all_mem = jitter.vm.get_all_memory() @@ -785,7 +785,7 @@ def kernel32_VirtualAlloc(jitter): alloc_addr = winobjs.heap.next_addr(args.dwsize) # alloc_addr = args.lpvoid jitter.vm.add_memory_page( - alloc_addr, ACCESS_DICT[args.flprotect], "\x00" * args.dwsize, + alloc_addr, ACCESS_DICT[args.flprotect], b"\x00" * args.dwsize, "Alloc in %s ret 0x%X" % (whoami(), ret_ad)) log.info('VirtualAlloc addr: 0x%x', alloc_addr) @@ -2070,7 +2070,7 @@ def msvcrt_sprintf(jitter): ret_ad, args, output = msvcrt_sprintf_str(jitter, jitter.get_str_ansi) ret = len(output) log.info("sprintf() = '%s'" % (output)) - jitter.vm.set_mem(args.string, output + b'\x00') + jitter.vm.set_mem(args.string, (output + '\x00').encode('utf8')) return jitter.func_ret_cdecl(ret_ad, ret) def msvcrt_swprintf(jitter): diff --git a/miasm/os_dep/win_api_x86_32_seh.py b/miasm/os_dep/win_api_x86_32_seh.py index 40f15480..d1be9ad2 100644 --- a/miasm/os_dep/win_api_x86_32_seh.py +++ b/miasm/os_dep/win_api_x86_32_seh.py @@ -253,9 +253,9 @@ def create_modules_chain(jitter, name2module): fname) continue addr = base_addr + i * 0x1000 - bpath = fname.replace('/', '\\') + bpath = fname.replace(b'/', b'\\') bname_str = os.path.split(fname)[1].lower() - bname_unicode = bname_str.encode("utf-16le") + bname_unicode = bname_str.decode('utf8').encode("utf-16le") log.info("Add module %x %r", pe_obj.NThdr.ImageBase, bname_str) modules_info.add(bname_str, pe_obj, addr) @@ -287,6 +287,8 @@ def create_modules_chain(jitter, name2module): "Module name %r" % bname_str ) + if isinstance(bpath, bytes): + bpath = bpath.decode('utf8') bpath_unicode = bpath.encode('utf-16le') jitter.vm.add_memory_page( addr + offset_path, diff --git a/test/arch/x86/qemu/testqemu.py b/test/arch/x86/qemu/testqemu.py index 99d6e6c1..594a826b 100644 --- a/test/arch/x86/qemu/testqemu.py +++ b/test/arch/x86/qemu/testqemu.py @@ -16,24 +16,24 @@ from miasm.jitter.csts import PAGE_READ, PAGE_WRITE # Utils def parse_fmt(s): - fmt = s[:]+b"\x00" + fmt = s[:]+"\x00" out = [] i = 0 while i < len(fmt): c = fmt[i:i+1] - if c != b"%": + if c != "%": i+=1 continue - if fmt[i+1:i+2] == b"%": + if fmt[i+1:i+2] == "%": i+=2 continue j = 0 i+=1 - while fmt[i+j:i+j+1] in b"0123456789$.-": + while fmt[i+j:i+j+1] in "0123456789$.-": j+=1 - if fmt[i+j:i+j+1] in [b'l']: + if fmt[i+j:i+j+1] in ['l']: j +=1 - if fmt[i+j:i+j+1] == b"h": + if fmt[i+j:i+j+1] == "h": x = fmt[i+j:i+j+2] else: x = fmt[i+j:i+j+1] @@ -50,8 +50,8 @@ def xxx___printf_chk(jitter): raise RuntimeError("Not implemented") fmt = jitter.get_str_ansi(args.format) # Manage llx - fmt = fmt.replace(b"llx", b"lx") - fmt = fmt.replace(b"%016lx", b"%016z") + fmt = fmt.replace("llx", "lx") + fmt = fmt.replace("%016lx", "%016z") fmt_a = parse_fmt(fmt) esp = jitter.cpu.ESP @@ -59,15 +59,15 @@ def xxx___printf_chk(jitter): i = 0 for x in fmt_a: a = jitter.vm.get_u32(esp + 8 + 4*i) - if x == b"s": + if x == "s": a = jitter.get_str_ansi(a) - elif x in (b"x", b'X', b"d"): + elif x in ("x", 'X', "d"): pass - elif x.lower() in (b"f", b"l"): + elif x.lower() in ("f", "l"): a2 = jitter.vm.get_u32(esp + 8 + 4*(i+1)) a = struct.unpack("d", struct.pack("Q", a2 << 32 | a))[0] i += 1 - elif x.lower() == b'z': + elif x.lower() == 'z': a2 = jitter.vm.get_u32(esp + 8 + 4*(i+1)) a = a2 << 32 | a i += 1 @@ -75,22 +75,22 @@ def xxx___printf_chk(jitter): raise RuntimeError("Not implemented format") args.append(a) i += 1 - fmt = fmt.replace(b"%016z", b"%016lx") + fmt = fmt.replace("%016z", "%016lx") output = fmt%(tuple(args)) # NaN bad repr in Python - output = output.replace(b"nan", b"-nan") + output = output.replace("nan", "-nan") - if b"\n" not in output: + if "\n" not in output: raise RuntimeError("Format must end with a \\n") # Check with expected result line = next(expected) - if output != line.encode(): + if output != line: print("Expected:", line) print("Obtained:", output) raise RuntimeError("Bad semantic") - stdout.write(b"[%d] %s" % (nb_tests, output)) + stdout.write(b"[%d] %s" % (nb_tests, output.encode('utf8'))) nb_tests += 1 jitter.func_ret_systemv(ret_ad, 0) @@ -105,7 +105,7 @@ def xxx_puts(jitter): output = jitter.get_str_ansi(args.target) # Check with expected result line = next(expected) - if output != line.rstrip().encode(): + if output != line.rstrip(): print("Expected:", line) print("Obtained:", output) raise RuntimeError("Bad semantic") diff --git a/test/arch/x86/qemu/testqemu64.py b/test/arch/x86/qemu/testqemu64.py index 24193d40..636cb6a9 100644 --- a/test/arch/x86/qemu/testqemu64.py +++ b/test/arch/x86/qemu/testqemu64.py @@ -16,24 +16,24 @@ from miasm.jitter.csts import PAGE_READ, PAGE_WRITE # Utils def parse_fmt(s): - fmt = s[:]+b"\x00" + fmt = s[:]+"\x00" out = [] i = 0 while i < len(fmt): c = fmt[i:i+1] - if c != b"%": + if c != "%": i+=1 continue - if fmt[i+1:i+2] == b"%": + if fmt[i+1:i+2] == "%": i+=2 continue j = 0 i+=1 - while fmt[i+j:i+j+1] in b"0123456789$.-": + while fmt[i+j:i+j+1] in "0123456789$.-": j+=1 - if fmt[i+j:i+j+1] in [b'l']: + if fmt[i+j:i+j+1] in ['l']: j +=1 - if fmt[i+j:i+j+1] == b"h": + if fmt[i+j:i+j+1] == "h": x = fmt[i+j:i+j+2] else: x = fmt[i+j:i+j+1] @@ -50,8 +50,8 @@ def xxx___printf_chk(jitter): raise RuntimeError("Not implemented") fmt = jitter.get_str_ansi(args.format) # Manage llx - fmt = fmt.replace(b"llx", b"lx") - fmt = fmt.replace(b"%016lx", b"%016z") + fmt = fmt.replace("llx", "lx") + fmt = fmt.replace("%016lx", "%016z") fmt_a = parse_fmt(fmt) args = [] @@ -59,11 +59,11 @@ def xxx___printf_chk(jitter): for x in fmt_a: a = jitter.get_arg_n_systemv(2 + i) - if x == b"s": + if x == "s": a = jitter.get_str_ansi(a) - elif x in (b"x", b'X', b'd', b'z', b'Z'): + elif x in ("x", 'X', 'd', 'z', 'Z'): pass - elif x.lower() in (b"f","l"): + elif x.lower() in ("f","l"): a = struct.unpack("d", struct.pack("Q", a))[0] i += 1 else: @@ -71,22 +71,22 @@ def xxx___printf_chk(jitter): args.append(a) i += 1 - fmt = fmt.replace(b"%016z", b"%016lx") + fmt = fmt.replace("%016z", "%016lx") output = fmt%(tuple(args)) # NaN bad repr in Python - output = output.replace(b"nan", b"-nan") + output = output.replace("nan", "-nan") - if b"\n" not in output: + if "\n" not in output: raise RuntimeError("Format must end with a \\n") # Check with expected result line = next(expected) - if output != line.encode(): + if output != line: print("Expected:", line) print("Obtained:", output) raise RuntimeError("Bad semantic") - stdout.write(b"[%d] %s" % (nb_tests, output)) + stdout.write(b"[%d] %s" % (nb_tests, output.encode('utf8'))) nb_tests += 1 jitter.func_ret_systemv(ret_ad, 0) diff --git a/test/os_dep/linux/stdlib.py b/test/os_dep/linux/stdlib.py index a205002b..ef890625 100755 --- a/test/os_dep/linux/stdlib.py +++ b/test/os_dep/linux/stdlib.py @@ -34,7 +34,7 @@ class TestLinuxStdlib(unittest.TestCase): jit.push_uint32_t(0) # ret_ad stdlib.xxx_sprintf(jit) ret = jit.get_str_ansi(buf) - self.assertEqual(ret, b"'coucou' 1111") + self.assertEqual(ret, "'coucou' 1111") if __name__ == '__main__': diff --git a/test/os_dep/win_api_x86_32.py b/test/os_dep/win_api_x86_32.py index a7d88f90..f759c6af 100755 --- a/test/os_dep/win_api_x86_32.py +++ b/test/os_dep/win_api_x86_32.py @@ -43,7 +43,7 @@ class TestWinAPI(unittest.TestCase): jit.push_uint32_t(0) # ret_ad winapi.msvcrt_sprintf(jit) ret = jit.get_str_ansi(buf) - self.assertEqual(ret, b"'coucou' 1111") + self.assertEqual(ret, "'coucou' 1111") def test_msvcrt_swprintf(self): |