Miasm2
 All Classes Namespaces Files Functions Variables Typedefs Properties Macros
linux_stdlib.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 
4 from sys import stdout
5 from string import printable
6 
7 from miasm2.os_dep.common import \
8  heap, set_str_ansi, set_str_unic, get_str_ansi, get_str_unic
9 
10 
12 
13  base_addr = 0x20000000
14  align_addr = 0x1000
15  def __init__(self):
16  self.alloc_ad = self.base_addr
17  self.alloc_align = self.align_addr
18  self.heap = heap()
19 
20 linobjs = c_linobjs()
21 
22 
23 def xxx_isprint(jitter):
24  '''
25  #include <ctype.h>
26  int isprint(int c);
27 
28  checks for any printable character including space.
29  '''
30  ret_addr, args = jitter.func_args_stdcall(['c'])
31  ret = 1 if chr(args.c & 0xFF) in printable else 0
32  return jitter.func_ret_stdcall(ret_addr, ret)
33 
34 
35 def xxx_memcpy(jitter):
36  '''
37  #include <string.h>
38  void *memcpy(void *dest, const void *src, size_t n);
39 
40  copies n bytes from memory area src to memory area dest.
41  '''
42  ret_addr, args = jitter.func_args_stdcall(['dest', 'src', 'n'])
43  jitter.vm.set_mem(args.dest, jitter.vm.get_mem(args.src, args.n))
44  return jitter.func_ret_stdcall(ret_addr, args.dest)
45 
46 
47 def xxx_memset(jitter):
48  '''
49  #include <string.h>
50  void *memset(void *s, int c, size_t n);
51 
52  fills the first n bytes of the memory area pointed to by s with the constant
53  byte c.'''
54 
55  ret_addr, args = jitter.func_args_stdcall(['dest', 'c', 'n'])
56  jitter.vm.set_mem(args.dest, chr(args.c & 0xFF) * args.n)
57  return jitter.func_ret_stdcall(ret_addr, args.dest)
58 
59 
60 def xxx_puts(jitter):
61  '''
62  #include <stdio.h>
63  int puts(const char *s);
64 
65  writes the string s and a trailing newline to stdout.
66  '''
67  ret_addr, args = jitter.func_args_stdcall(['s'])
68  index = args.s
69  char = jitter.vm.get_mem(index, 1)
70  while char != '\x00':
71  stdout.write(char)
72  index += 1
73  char = jitter.vm.get_mem(index, 1)
74  stdout.write('\n')
75  return jitter.func_ret_stdcall(ret_addr, 1)
76 
77 
78 def get_fmt_args(jitter, fmt, cur_arg):
79  output = ""
80  while True:
81  char = jitter.vm.get_mem(fmt, 1)
82  fmt += 1
83  if char == '\x00':
84  break
85  if char == '%':
86  token = '%'
87  while True:
88  char = jitter.vm.get_mem(fmt, 1)
89  fmt += 1
90  token += char
91  if char.lower() in '%cdfsux':
92  break
93  if token.endswith('s'):
94  arg = jitter.get_str_ansi(jitter.get_arg_n_stdcall(cur_arg))
95  else:
96  arg = jitter.get_arg_n_stdcall(cur_arg)
97  char = token % arg
98  cur_arg += 1
99  output += char
100  return output
101 
102 
103 def xxx_snprintf(jitter):
104  ret_addr, args = jitter.func_args_stdcall(['string', 'size', 'fmt'])
105  cur_arg, fmt = 3, args.fmt
106  size = args.size if args.size else 1
107  output = get_fmt_args(jitter, fmt, cur_arg)
108  output = output[:size - 1]
109  ret = len(output)
110  jitter.vm.set_mem(args.string, output + '\x00')
111  return jitter.func_ret_stdcall(ret_addr, ret)
112 
113 
114 def xxx_sprintf(jitter):
115  ret_addr, args = jitter.func_args_stdcall(['string', 'fmt'])
116  cur_arg, fmt = 2, args.fmt
117  output = get_fmt_args(jitter, fmt, cur_arg)
118  ret = len(output)
119  jitter.vm.set_mem(args.string, output + '\x00')
120  return jitter.func_ret_stdcall(ret_addr, ret)
121 
122 
123 def xxx_printf(jitter):
124  ret_addr, args = jitter.func_args_stdcall(['fmt'])
125  cur_arg, fmt = 1, args.fmt
126  output = get_fmt_args(jitter, fmt, cur_arg)
127  ret = len(output)
128  print output,
129  return jitter.func_ret_stdcall(ret_addr, ret)
130 
131 
132 def xxx_strcpy(jitter):
133  ret_ad, args = jitter.func_args_stdcall(["dst", "src"])
134  str_src = jitter.get_str_ansi(args.src) + '\x00'
135  jitter.vm.set_mem(args.dst, str_src)
136  jitter.func_ret_stdcall(ret_ad, args.dst)
137 
138 
139 def xxx_strlen(jitter):
140  ret_ad, args = jitter.func_args_stdcall(["src"])
141  str_src = jitter.get_str_ansi(args.src)
142  jitter.func_ret_stdcall(ret_ad, len(str_src))
143 
144 
145 def xxx_malloc(jitter):
146  ret_ad, args = jitter.func_args_stdcall(["msize"])
147  addr = linobjs.heap.alloc(jitter, args.msize)
148  jitter.func_ret_stdcall(ret_ad, addr)
149 
150 
151 def xxx_free(jitter):
152  ret_ad, args = jitter.func_args_stdcall(["ptr"])
153  jitter.func_ret_stdcall(ret_ad, 0)
154 
155 
156 def xxx_strcmp(jitter):
157  ret_ad, args = jitter.func_args_stdcall(["ptr_str1", "ptr_str2"])
158  s1 = get_str_ansi(jitter, args.ptr_str1)
159  s2 = get_str_ansi(jitter, args.ptr_str2)
160  jitter.func_ret_stdcall(ret_ad, cmp(s1, s2))
161 
162 
163 def xxx_strncmp(jitter):
164  ret_ad, args = jitter.func_args_stdcall(["ptr_str1", "ptr_str2", "size"])
165  s1 = get_str_ansi(jitter, args.ptr_str1, args.size)
166  s2 = get_str_ansi(jitter, args.ptr_str2, args.size)
167  jitter.func_ret_stdcall(ret_ad, cmp(s1, s2))