diff options
| author | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-09-22 19:32:18 +0200 |
|---|---|---|
| committer | Fabrice Desclaux <fabrice.desclaux@cea.fr> | 2019-09-22 22:02:51 +0200 |
| commit | 523507835ed6789a9489120023b539f6ae82eb18 (patch) | |
| tree | 84a479a7ab6324f651d406226aef72afca89f7c9 | |
| parent | 1902c27e796277ab495afe3d39bf17d882eda062 (diff) | |
| download | miasm-523507835ed6789a9489120023b539f6ae82eb18.tar.gz miasm-523507835ed6789a9489120023b539f6ae82eb18.zip | |
Fix get_str_ansi: return str
get_str_ansi and get_str_unic now returns both *str* object: As get_str_unic decodes the string, get_str_ansi should do the same.
| -rw-r--r-- | example/symbol_exec/dse_crackme.py | 2 | ||||
| -rw-r--r-- | miasm/jitter/jitload.py | 6 | ||||
| -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 | 2 | ||||
| -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 |
10 files changed, 51 insertions, 57 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/jitter/jitload.py b/miasm/jitter/jitload.py index ebecb103..9fcb0b0a 100644 --- a/miasm/jitter/jitload.py +++ b/miasm/jitter/jitload.py @@ -456,10 +456,10 @@ 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, string): """Set an unicode string in memory""" 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 89af729a..5ef1b845 100644 --- a/miasm/os_dep/win_api_x86_32.py +++ b/miasm/os_dep/win_api_x86_32.py @@ -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/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): |