-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathPyOS.py
More file actions
401 lines (313 loc) · 13.1 KB
/
PyOS.py
File metadata and controls
401 lines (313 loc) · 13.1 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python
########################################################################
#
# PyEmu: scriptable x86 emulator
#
# Cody Pierce - cpierce@tippingpoint.com - 2007
#
# License: None
#
########################################################################
import sys, struct
sys.path.append("lib")
from ctypes import *
'''
PyWindows:
This is a class to handle any Win32 related system structures. To the
best of my abilities I have tried to load any relevant pieces of
information. This is not complete but enough for SEH and selector
implementation. The class includes the PEB, TEB/TIB, LDT.
'''
class PyWindows:
DEBUG = 0
def __init__(self):
# We initialize a PEB structure
self.PEB = self.__PEB()
self.THREADS = []
# This holds any libraries we may be interested in
self.libraries = {}
#
# initialize: called from the emulator to set up the environment
#
def initialize(self, emu, stackbase, stacklimit, heapbase, heaplimit):
self.PEB.ProcessHeap = heapbase
# Grab the next TEB address
if not len(self.THREADS):
# Our first Thread
tebaddress = self.PEB.Address - 0x1000
else:
# Get last TEB address
tebaddress = self.THREADS[-1].TEB.Address - 0x1000
# Create a new thread
self.add_thread()
self.THREADS[-1].load_teb(emu, tebaddress)
self.THREADS[-1].load_stack(emu, stackbase, stacklimit)
self.THREADS[-1].load_exceptions(emu)
return True
def set_debug(self, level):
self.DEBUG = level
#
# add_thread: Handles creating a new thread/TEB/TIB and stores it
# in the class THREAD list
#
def add_thread(self):
# Instantiate a object with the TIB information
new_thread = self.__THREAD()
self.THREADS.append(new_thread)
return True
#
# add_library: Handles addition of libraries to the OS. This lets us
# call any user handlers when we call into this function
#
def add_library(self, dllname, function):
for library in self.libraries:
if function == self.libraries[library]['name']:
return True
handle = windll.kernel32.LoadLibraryA(dllname)
address = windll.kernel32.GetProcAddress(handle, function)
windll.kernel32.FreeLibrary(handle)
#if self.DEBUG >= 1:
print "[*] Adding library %s address 0x%08x" % (function, address)
self.libraries[address] = {'dll': dllname, 'address': address, 'name': function}
return True
#
# get_library_address: Returns the address for the specified library
#
def get_library_address(self, function):
for key in self.libraries.keys():
if function == self.libraries[key]['name']:
return key
return False
#
# get_selector: Responsible for looking up the LDT offset requested
# Currently this is not very robust, in time it wll be
# cleaned up.
#
def get_selector(self, selector):
return self.THREADS[-1].LDT.get_selector(selector)
#
# set_selector: Responsible for updating a selector values base address
#
def set_selector(self, selector, value):
return self.THREADS[-1].LDT.set_selector(selector, value)
'''
__PEB:
The PEB class implementation. This is not complete as stated
above. It should contain a few values for fun.
'''
class __PEB:
def __init__(self):
self.Address = 0x00000000
# *not complete*
self.InheritedAddressSpace = 0x0
self.ReadImageFileExecOptions = 0x0
self.BeingDebugged = 0x0
self.ImageBaseAddress = 0x00000000
self.Ldr = self.__LDR()
self.SubSystemData = 0x00000000
self.ProcessHeap = 0x00000000
self.ProcessParameters = 0x00000000
self.WindowTitle = ""
self.ImageFile = ""
self.CommandLine = ""
self.DllPath = ""
self.Environment = 0x00000000
#
# get_packed: Packs all our values, since we are not using any
# nothing much goes on
#
def get_packed(self):
packeddata = ""
# NULL it out for now
packeddata = "\x00" * 0x20c
return packeddata
'''
__LDR:
A LDR_DATA class for the future! and beyond!
'''
class __LDR:
def __init__(self):
self.Address = 0x0000000
# I need to add the LDR_DATA elements
#
# get_packed: Packs all our values, since we are not using any
# nothing much goes on
#
def get_packed(self):
packeddata = ""
# NULL it out for now
packeddata = "\x00" * 0x28
return packeddata
'''
__THREAD:
A THREAD class which contains our TEB and LDT. This has been
implemented to allow access to the SEH and selectors of the
current thread. This is not complete and simply a "good enough"
implementation.
'''
class __THREAD:
def __init__(self):
# Initialize a TEB and LDT
self.TEB = self.__TEB()
self.LDT = self.__LDT()
#
# load_teb: Handles getting memory for the TEB and setting values
#
def load_teb(self, emu, address):
self.TEB.Address = address
emu.memory.allocate_page(self.TEB.Address)
# Store our TEB in actual memory
emu.set_memory(self.TEB.Address, self.TEB.get_packed())
return True
#
# load_stack: Responsible for setting TIB stack values
#
def load_stack(self, emu, stackbase, stacklimit):
# Fetch a page into cache for our stack
emu.memory.allocate_page(stacklimit)
self.TEB.TIB.StackBase = stackbase
self.TEB.TIB.StackLimit = stacklimit
return True
#
# load_exceptions: Responsible for setting TIB exception values
#
def load_exceptions(self, emu):
# A generic address to get us started
self.TEB.TIB.ExceptionList = self.TEB.TIB.StackBase - 0x24
# Init a LDT entry four our TIB selector
selector = self.LDT.get_selector(0x3b)
selector.base = self.TEB.TIB.ExceptionList
# Store the exception entry in actual memory
emu.set_memory(self.TEB.TIB.ExceptionList, 0xffffffff)
emu.set_memory(self.TEB.TIB.ExceptionList + 0x4, 0xdeadbeef)
return True
'''
__TEB:
A class to represent the TEB. The only thing we are
currently interested in is the TIB so the rest of the TEB
is faked
'''
class __TEB:
def __init__(self):
self.Address = 0x00000000
# Initialize a new TIB (this is offset 0x0 in _TEB
self.TIB = self.__TIB()
# Rest of the TEB would go here
#
# get_packed: Packs all our values, since we are not using any
# besides the TIB we do that then pad the rest
#
def get_packed(self):
packeddata = ""
# Pack and pad
packeddata = self.TIB.get_packed()
packeddata += "\x00" * (0xfb6 - len(packeddata))
return packeddata
'''
__TIB:
A class to store the import TIB information. This includes
our stack boundaries and seh chain
'''
class __TIB:
def __init__(self):
self.ExceptionList = 0x00000000
self.StackBase = 0x00000000
self.StackLimit = 0x00000000
self.SubSystemTib = 0x00000000
self.FiberData = 0x00000000
self.ArbitraryUserPointer = 0x00000000
self.Self = 0x00000000
#
# get_packed: Packs all our values, im sure there is a
# much better way to do this.
#
def get_packed(self):
packeddata = ""
packeddata = struct.pack("<L", self.ExceptionList)
packeddata += struct.pack("<L", self.StackBase)
packeddata += struct.pack("<L", self.StackLimit)
packeddata += struct.pack("<L", self.SubSystemTib)
packeddata += struct.pack("<L", self.FiberData)
packeddata += struct.pack("<L", self.ArbitraryUserPointer)
packeddata += struct.pack("<L", self.Self)
return packeddata
'''
__EXCEPTION_RECORD:
A class for handling exception records. Currently
this is not used, but I like to type.
'''
class __EXCEPTION_RECORD:
def __init__(self):
self.Next = 0x00000000
self.Handler = 0x00000000
def get_packed(self):
packeddata = ""
packeddata = struct.pack("<L", self.Next)
packeddata += struct.pack("<L", self.Handler)
return packeddata
'''
__LDT:
A local descriptor table per thread. This is to allow us to
access the TIB selector for exception handling.
'''
class __LDT:
def __init__(self):
self.entries = {}
# Load our initial entries
self.init_entries()
#
# init_entries: Loads selectors for various segments and the TIB
#
def init_entries(self):
# Code, Data, TIB
self.entries[0x0000] = self.__LDT_ENTRY(0x0000, 0x0000000, 0x00000000)
self.entries[0x001b] = self.__LDT_ENTRY(0x001b, 0x0000000, 0xffffffff)
self.entries[0x0023] = self.__LDT_ENTRY(0x0023, 0x0000000, 0xffffffff)
self.entries[0x0038] = self.__LDT_ENTRY(0x0038, 0x0000000, 0x00000fff)
self.entries[0x003b] = self.__LDT_ENTRY(0x003b, 0x0000000, 0x00000fff)
#
# get_selector: Returns the requested selector
#
def get_selector(self, selector):
if selector not in self.entries:
print "[!] Couldnt get selector[%x]" % selector
return False
return self.entries[selector]
#
# set_selector: Sets the requested selector
#
def set_selector(self, selector, value):
if selector not in self.entries:
print "[!] Couldnt get selector[%x]" % selector
return False
self.entries[selector].base = value
return True
'''
__LDT_ENTRY:
A class for storing the each LDT entries information
including the selector offset, base, and limit. We dont
have support for getting at the other information yet
'''
class __LDT_ENTRY:
def __init__(self, selector, base, limit):
self.selector = selector
self.base = base
self.limit = limit
'''
PyLinux:
A class to handle process setup in Linux. I dont have time to add
this however I like at least mentioning it is possible. PyEmu will
choose this based on the internal os.name method.
'''
class PyLinux:
DEBUG = 0
def __init__(self):
pass
#
# initialize: called from the emulator to set up the environment this
# wont do anything...yet :(
#
def initialize(self, emu, stackbase, stacklimit, heapbase, heaplimit):
return True
def set_debug(self, level):
self.DEBUG = level