forked from n1nj4sec/memorpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_generic.py
312 lines (237 loc) · 8.91 KB
/
test_generic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import logging
from ctypes import (
c_uint16, c_uint32, c_int64, c_uint64, c_float, c_double,
create_string_buffer, c_void_p, addressof,
c_longlong, c_ulonglong
)
from time import sleep
from os import getpid
from sys import exit, platform, version_info
from multiprocessing import Process as MProcess
from memorpy import (
MemWorker, Process,
PROT_READ, PROT_WRITE, PROT_EXEC
)
from memorpy.utils import expected_type_size
if version_info.major > 2:
MATH_TYPES = (float, int)
else:
MATH_TYPES = (float, int, long)
VALUE0_DATA = 0xBEEF, 'ushort'
VALUE1_DATA = 0xDEADBEEF, 'uint'
VALUE2_DATA = -0xB15B00B5, 'longlong'
VALUE3_DATA = 0xB15B00B5DEADBEEF, 'ulonglong'
VALUE4_DATA = -31337.0, 'float'
VALUE5_DATA = -1337.899999912341, 'double'
STRING1_TERM = b'Hello bytes world!'
STRING2_TERM = 'Hello unicode world!'
STRING1_DATA = STRING1_TERM * 32, 'bytes'
STRING2_DATA = STRING2_TERM * 32, 'unicode'
VALUES = (
VALUE0_DATA, VALUE1_DATA, VALUE2_DATA, VALUE3_DATA, VALUE4_DATA,
VALUE5_DATA, STRING1_DATA, STRING2_DATA
)
def cow():
STRING1 = create_string_buffer(STRING1_DATA[0])
STRING2_DATA_ENCODED_UNICODE = STRING2_DATA[0].encode('utf-16le')
STRING2 = create_string_buffer(
STRING2_DATA_ENCODED_UNICODE, len(STRING2_DATA_ENCODED_UNICODE))
VALUE0 = c_uint16(VALUE0_DATA[0])
VALUE1 = c_uint32(VALUE1_DATA[0])
VALUE2 = c_longlong(VALUE2_DATA[0])
VALUE3 = c_ulonglong(VALUE3_DATA[0])
VALUE4 = c_float(VALUE4_DATA[0])
VALUE5 = c_double(VALUE5_DATA[0])
VALUE6 = c_void_p(addressof(VALUE2))
print('Current pid={} String1: 0x{:X} String2: 0x{:X}'.format(
getpid(), addressof(STRING1), addressof(STRING2)))
while True:
sleep(10)
STORAGES = (
VALUE0, VALUE1, VALUE2, VALUE3, VALUE4, VALUE5, VALUE6,
STRING1, STRING2
)
assert(STORAGES)
def setup():
process = MProcess(target=cow)
process.start()
assert(process.pid)
assert(process.pid != getpid())
logging.info('Wait a bit, as we are not using locks')
sleep(5)
logging.info('Lets go')
return process
def search_value(worker, value, search_type):
logging.info('Search constant %s as type %s', value, search_type)
for item in worker.mem_search(value, search_type, protec=PROT_WRITE):
found_value = worker[item]
if found_value != value:
# Something changed
logging.info(
'Found candidate: at %s (read => %s (type=%s))',
hex(item.address), repr(found_value), type(found_value)
)
continue
logging.info(
'Found: at %s (read => %s (type=%s))',
hex(item.address), repr(found_value), type(found_value)
)
return item
logging.error('Failed to find %s type %s', value, search_type)
return None
def test(process, prefer_lock):
worker = MemWorker(process.pid, prefer_lock=prefer_lock)
logging.info('Pid: %d', process.pid)
logging.info('Iterating exectuable regions')
with worker:
for address, size in worker.process.iter_region(protec=PROT_EXEC):
logging.info(
'Region: 0x%X size=%d', address, size)
logging.info('Iterating RW regions')
with worker:
for address, size in worker.process.iter_region(
protec=PROT_WRITE | PROT_READ):
logging.info(
'Region: 0x%X size=%d', address, size)
logging.info('Search for values')
values_table = {}
with worker:
for (value, value_type) in VALUES:
address = search_value(worker, value, value_type)
if address is None:
return False
if search_value(worker, value, None) is None:
return False
values_table[value] = address, value_type
logging.info('Test address dereference')
with worker:
found_2x_deref = False
for address in worker.mem_search(*VALUE2_DATA):
for address_of_address in worker.mem_search(address):
try:
deref1 = worker[address_of_address]
except (IOError, OSError):
continue
deref1.type = VALUE2_DATA[1]
deref1.size = expected_type_size(deref1.type)
try:
value = worker[deref1]
except (IOError, OSError):
continue
if value == VALUE2_DATA[0]:
found_2x_deref = True
break
if not found_2x_deref:
return False
logging.info('Search by regex')
with worker:
found_bytes_re = False
for _, address in worker.mem_search(
br'Hello\s+([^\s]+)\s+world!', 're'):
try:
data = worker[address]
if data == STRING1_TERM:
found_bytes_re = True
break
else:
logging.info(
'Candidate: %s => %s (!= %s)',
address, data, STRING1_TERM
)
except (IOError, OSError):
continue
if not found_bytes_re:
logging.info('Failed to find regex')
return False
logging.info('Change values')
with worker:
for value, (address, _) in values_table.items():
if isinstance(value, bytes):
new_value = b'\xAB' * len(value)
elif isinstance(value, MATH_TYPES):
new_value = value + 1
if address.type == 'unicode':
continue
worker[address] = new_value
current_value = worker[address]
if current_value != new_value:
if isinstance(value, bytes):
logging.error(
'Failed to change value %s to %s: '
'Read: %s (address %s)',
repr(value), repr(new_value),
repr(current_value), address
)
else:
logging.error(
'Failed to change value 0x%X to 0x%X: '
'Read: 0x%X (address %s)',
value, new_value, current_value, address
)
return False
# Set old value back
worker[address] = value
return True
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
logging.info('Testing at platform %s', repr(platform))
current_process_name = None
for process in Process.list():
if process.pid == getpid():
current_process_name = process.name
logging.info(
'Current process: pid=%d name=%s',
process['pid'], process['name']
)
break
assert(current_process_name is not None)
try:
current_process = Process(getpid())
except ValueError:
current_process = None
assert(current_process is None)
py_process = Process.pid_from_name(current_process_name)
logging.info('Search for any python proces: %s', py_process)
assert(py_process is not None)
logging.info('Start child process')
process = setup()
try:
if not test(process, False):
exit('Failed to pass tests with prefer_lock=False')
if not test(process, True):
exit('Failed to pass tests with prefer_lock=True')
if platform.startswith('linux'):
from memorpy import LinProcess
if LinProcess.PROCESS_VM_SUPPORT:
logging.info('Disabling VM Support')
LinProcess.PROCESS_VM_SUPPORT = False
if not test(process, False):
exit(
'Failed to pass tests with prefer_lock=False '
'without process vm'
)
if not test(process, True):
exit(
'Failed to pass tests with prefer_lock=True'
'without process vm'
)
else:
logging.info('Unable to test with process vm support')
if LinProcess.LARGE_FILE_SUPPORT:
logging.info('Disabling Large file Support')
LinProcess.LARGE_FILE_SUPPORT = False
if not test(process, False):
exit(
'Failed to pass tests with prefer_lock=False '
'without large file support'
)
if not test(process, True):
exit(
'Failed to pass tests with prefer_lock=True '
'without large file support'
)
else:
logging.info('Unable to test with large file vm support')
finally:
process.terminate()
logging.info('Completed successfully')