Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
common.py
Go to the documentation of this file.
1 import os
2 
3 from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
4 
5 BASE_SB_PATH = "file_sb"
6 
7 
8 def get_str_ansi(jitter, ad_str, max_char=None):
9  l = 0
10  tmp = ad_str
11  while ((max_char is None or l < max_char) and
12  jitter.vm.get_mem(tmp, 1) != "\x00"):
13  tmp += 1
14  l += 1
15  return jitter.vm.get_mem(ad_str, l)
16 
17 
18 def get_str_unic(jitter, ad_str, max_char=None):
19  l = 0
20  tmp = ad_str
21  while ((max_char is None or l < max_char) and
22  jitter.vm.get_mem(tmp, 2) != "\x00\x00"):
23  tmp += 2
24  l += 2
25  s = jitter.vm.get_mem(ad_str, l)
26  # TODO: real unicode decoding
27  s = s[::2]
28  return s
29 
30 
31 def set_str_ansi(s):
32  return s + "\x00"
33 
34 
35 def set_str_unic(s):
36  # TODO: real unicode encoding
37  return "\x00".join(list(s)) + '\x00' * 3
38 
39 
40 class heap(object):
41  "Light heap simulation"
42 
43  addr = 0x20000000
44  align = 0x1000
45  size = 32
46  mask = (1 << size) - 1
47 
48  def next_addr(self, size):
49  """
50  @size: the size to allocate
51  return the future checnk address
52  """
53  ret = self.addr
54  self.addr = (self.addr + size + self.align - 1)
55  self.addr &= self.mask ^ (self.align - 1)
56  return ret
57 
58  def alloc(self, jitter, size):
59  """
60  @jitter: a jitter instance
61  @size: the size to allocate
62  """
63 
64  addr = self.next_addr(size)
65  jitter.vm.add_memory_page(addr, PAGE_READ | PAGE_WRITE, "\x00" * size)
66  return addr
67 
68 
70  """Convert a Windows path to a valid filename within the sandbox
71  base directory.
72 
73  """
74  path = [elt for elt in path.lower().replace('/', '_').split('\\') if elt]
75  return os.path.join(BASE_SB_PATH, *path)
76 
77 
78 def unix_to_sbpath(path):
79  """Convert a POSIX path to a valid filename within the sandbox
80  base directory.
81 
82  """
83  path = [elt for elt in path.split('/') if elt]
84  return os.path.join(BASE_SB_PATH, *path)