about summary refs log tree commit diff stats
path: root/miasm2/os_dep/common.py
diff options
context:
space:
mode:
authorAdrien Guinet <adrien@guinet.me>2017-10-31 08:16:17 +0100
committerCamille Mougey <commial@gmail.com>2017-10-31 08:16:17 +0100
commit33dccf7012673882bef35b9afd9fb986881a8168 (patch)
treed3f31c219a4ed5227c3a08d0003bc4b9a4494e37 /miasm2/os_dep/common.py
parent1e1b3282704700328d23a96d4da402715f554f9e (diff)
downloadmiasm-33dccf7012673882bef35b9afd9fb986881a8168.tar.gz
miasm-33dccf7012673882bef35b9afd9fb986881a8168.zip
Various Win32 API additions/fixes (#616)
Various Win32 API additions/fixes

* add a get_size method to Miasm heap object, which allows the
implementation of mscvrt_realloc
* add the concept of "current directory", with the default value being
arbitrary set to "c:\tmp", which allows the implementation of
{Get,Set}CurrentDirecrtory
* various other methods implemented:

 - advapi32_RegCloseKey
 - advapi32_RegCreateKeyW
 - advapi32_RegSetValueExA
 - advapi32_RegSetValueExW
 - kernel32_GetProcessHeap
 - msvcrt_delete
 - msvcrt_fprintf
 - msvcrt_fwrite
 - msvcrt__mbscpy
 - msvcrt_new
 - msvcrt_realloc
 - msvcrt_sprintf
 - msvcrt_srand
 - msvcrt_strrchr
 - msvcrt_swprintf
 - msvcrt_wcscat
 - msvcrt_wcscmp
 - msvcrt_wcscpy
 - msvcrt__wcsicmp
 - msvcrt_wcslen
 - msvcrt_wcsncpy
 - msvcrt__wcsnicmp
 - msvcrt_wcsrchr
Diffstat (limited to '')
-rw-r--r--miasm2/os_dep/common.py54
1 files changed, 53 insertions, 1 deletions
diff --git a/miasm2/os_dep/common.py b/miasm2/os_dep/common.py
index ecb6d821..7e46b276 100644
--- a/miasm2/os_dep/common.py
+++ b/miasm2/os_dep/common.py
@@ -2,6 +2,7 @@ import os
 
 from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
 from miasm2.core.utils import get_caller_name
+from miasm2.core.utils import pck64, upck64
 
 BASE_SB_PATH = "file_sb"
 
@@ -73,10 +74,31 @@ class heap(object):
             combination of them); default is PAGE_READ|PAGE_WRITE
         """
         addr = self.next_addr(size)
-        vm.add_memory_page(addr, perm, "\x00" * size,
+        vm.add_memory_page(addr, perm, "\x00" * (size),
                            "Heap alloc by %s" % get_caller_name(2))
         return addr
 
+    def get_size(self, vm, ptr):
+        """
+        @vm: a VmMngr instance
+        @size: ptr to get the size of the associated allocation.
+        
+        `ptr` can be the base address of a previous allocation, or an address
+        within the allocated range. The size of the whole allocation is always
+        returned, regardless ptr is the base address or not.
+        """
+	assert vm.is_mapped(ptr, 1)
+	data = vm.get_all_memory()
+	ptr_page = data.get(ptr, None)
+	if ptr_page is None:
+	    for address, page_info in data.iteritems():
+		if address <= ptr < address + page_info["size"]:
+		    ptr_page = page_info
+		    break
+	    else:
+		raise RuntimeError("Must never happen (unmapped but mark as mapped by API)")
+	return ptr_page["size"]
+
 
 def windows_to_sbpath(path):
     """Convert a Windows path to a valid filename within the sandbox
@@ -94,3 +116,33 @@ def unix_to_sbpath(path):
     """
     path = [elt for elt in path.split('/') if elt]
     return os.path.join(BASE_SB_PATH, *path)
+
+def get_fmt_args(fmt, cur_arg, get_str, get_arg_n):
+    output = ""
+    idx = 0
+    fmt = get_str(fmt)
+    while True:
+        if idx == len(fmt):
+            break
+        char = fmt[idx]
+        idx += 1
+        if char == '%':
+            token = '%'
+            while True:
+                char = fmt[idx]
+                idx += 1
+                token += char
+                if char.lower() in '%cdfsux':
+                    break
+            if char == '%':
+                output += char
+                continue
+            if token.endswith('s'):
+                addr = get_arg_n(cur_arg)
+                arg = get_str(addr)
+            else:
+                arg = get_arg_n(cur_arg)
+            char = token % arg
+            cur_arg += 1
+        output += char
+    return output