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 CONTEXT structure for i386.
33 """
34
35 __revision__ = "$Id: context_i386.py 1299 2013-12-20 09:30:55Z qvasimodo $"
36
37 from defines import *
38 from version import ARCH_I386
39
40
41
42 _all = None
43 _all = set(vars().keys())
44
45
46
47
48
49
50
51 EXCEPTION_READ_FAULT = 0
52 EXCEPTION_WRITE_FAULT = 1
53 EXCEPTION_EXECUTE_FAULT = 8
54
55 CONTEXT_i386 = 0x00010000
56 CONTEXT_i486 = 0x00010000
57
58 CONTEXT_CONTROL = (CONTEXT_i386 | 0x00000001L)
59 CONTEXT_INTEGER = (CONTEXT_i386 | 0x00000002L)
60 CONTEXT_SEGMENTS = (CONTEXT_i386 | 0x00000004L)
61 CONTEXT_FLOATING_POINT = (CONTEXT_i386 | 0x00000008L)
62 CONTEXT_DEBUG_REGISTERS = (CONTEXT_i386 | 0x00000010L)
63 CONTEXT_EXTENDED_REGISTERS = (CONTEXT_i386 | 0x00000020L)
64
65 CONTEXT_FULL = (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS)
66
67 CONTEXT_ALL = (CONTEXT_CONTROL | CONTEXT_INTEGER | CONTEXT_SEGMENTS | \
68 CONTEXT_FLOATING_POINT | CONTEXT_DEBUG_REGISTERS | \
69 CONTEXT_EXTENDED_REGISTERS)
70
71 SIZE_OF_80387_REGISTERS = 80
72 MAXIMUM_SUPPORTED_EXTENSION = 512
86 _pack_ = 1
87 _fields_ = [
88 ('ControlWord', DWORD),
89 ('StatusWord', DWORD),
90 ('TagWord', DWORD),
91 ('ErrorOffset', DWORD),
92 ('ErrorSelector', DWORD),
93 ('DataOffset', DWORD),
94 ('DataSelector', DWORD),
95 ('RegisterArea', BYTE * SIZE_OF_80387_REGISTERS),
96 ('Cr0NpxState', DWORD),
97 ]
98
99 _integer_members = ('ControlWord', 'StatusWord', 'TagWord', 'ErrorOffset', 'ErrorSelector', 'DataOffset', 'DataSelector', 'Cr0NpxState')
100
101 @classmethod
113
123
124 PFLOATING_SAVE_AREA = POINTER(FLOATING_SAVE_AREA)
125 LPFLOATING_SAVE_AREA = PFLOATING_SAVE_AREA
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 -class CONTEXT(Structure):
155 arch = ARCH_I386
156
157 _pack_ = 1
158
159
160
161
162
163
164
165
166
167 _fields_ = [
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 ('ContextFlags', DWORD),
186
187
188
189
190
191 ('Dr0', DWORD),
192 ('Dr1', DWORD),
193 ('Dr2', DWORD),
194 ('Dr3', DWORD),
195 ('Dr6', DWORD),
196 ('Dr7', DWORD),
197
198
199
200
201 ('FloatSave', FLOATING_SAVE_AREA),
202
203
204
205
206 ('SegGs', DWORD),
207 ('SegFs', DWORD),
208 ('SegEs', DWORD),
209 ('SegDs', DWORD),
210
211
212
213
214 ('Edi', DWORD),
215 ('Esi', DWORD),
216 ('Ebx', DWORD),
217 ('Edx', DWORD),
218 ('Ecx', DWORD),
219 ('Eax', DWORD),
220
221
222
223
224 ('Ebp', DWORD),
225 ('Eip', DWORD),
226 ('SegCs', DWORD),
227 ('EFlags', DWORD),
228 ('Esp', DWORD),
229 ('SegSs', DWORD),
230
231
232
233
234
235 ('ExtendedRegisters', BYTE * MAXIMUM_SUPPORTED_EXTENSION),
236 ]
237
238 _ctx_debug = ('Dr0', 'Dr1', 'Dr2', 'Dr3', 'Dr6', 'Dr7')
239 _ctx_segs = ('SegGs', 'SegFs', 'SegEs', 'SegDs', )
240 _ctx_int = ('Edi', 'Esi', 'Ebx', 'Edx', 'Ecx', 'Eax')
241 _ctx_ctrl = ('Ebp', 'Eip', 'SegCs', 'EFlags', 'Esp', 'SegSs')
242
243 @classmethod
244 - def from_dict(cls, ctx):
245 'Instance a new structure from a Python dictionary.'
246 ctx = Context(ctx)
247 s = cls()
248 ContextFlags = ctx['ContextFlags']
249 setattr(s, 'ContextFlags', ContextFlags)
250 if (ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS:
251 for key in s._ctx_debug:
252 setattr(s, key, ctx[key])
253 if (ContextFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT:
254 fsa = ctx['FloatSave']
255 s.FloatSave = FLOATING_SAVE_AREA.from_dict(fsa)
256 if (ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS:
257 for key in s._ctx_segs:
258 setattr(s, key, ctx[key])
259 if (ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER:
260 for key in s._ctx_int:
261 setattr(s, key, ctx[key])
262 if (ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL:
263 for key in s._ctx_ctrl:
264 setattr(s, key, ctx[key])
265 if (ContextFlags & CONTEXT_EXTENDED_REGISTERS) == CONTEXT_EXTENDED_REGISTERS:
266 er = ctx['ExtendedRegisters']
267 for index in xrange(0, MAXIMUM_SUPPORTED_EXTENSION):
268 s.ExtendedRegisters[index] = er[index]
269 return s
270
272 'Convert a structure into a Python native type.'
273 ctx = Context()
274 ContextFlags = self.ContextFlags
275 ctx['ContextFlags'] = ContextFlags
276 if (ContextFlags & CONTEXT_DEBUG_REGISTERS) == CONTEXT_DEBUG_REGISTERS:
277 for key in self._ctx_debug:
278 ctx[key] = getattr(self, key)
279 if (ContextFlags & CONTEXT_FLOATING_POINT) == CONTEXT_FLOATING_POINT:
280 ctx['FloatSave'] = self.FloatSave.to_dict()
281 if (ContextFlags & CONTEXT_SEGMENTS) == CONTEXT_SEGMENTS:
282 for key in self._ctx_segs:
283 ctx[key] = getattr(self, key)
284 if (ContextFlags & CONTEXT_INTEGER) == CONTEXT_INTEGER:
285 for key in self._ctx_int:
286 ctx[key] = getattr(self, key)
287 if (ContextFlags & CONTEXT_CONTROL) == CONTEXT_CONTROL:
288 for key in self._ctx_ctrl:
289 ctx[key] = getattr(self, key)
290 if (ContextFlags & CONTEXT_EXTENDED_REGISTERS) == CONTEXT_EXTENDED_REGISTERS:
291 er = [ self.ExtendedRegisters[index] for index in xrange(0, MAXIMUM_SUPPORTED_EXTENSION) ]
292 er = tuple(er)
293 ctx['ExtendedRegisters'] = er
294 return ctx
295
296 PCONTEXT = POINTER(CONTEXT)
297 LPCONTEXT = PCONTEXT
298
299 -class Context(dict):
300 """
301 Register context dictionary for the i386 architecture.
302 """
303
304 arch = CONTEXT.arch
305
306 - def __get_pc(self):
308 - def __set_pc(self, value):
310 pc = property(__get_pc, __set_pc)
311
312 - def __get_sp(self):
314 - def __set_sp(self, value):
316 sp = property(__get_sp, __set_sp)
317
318 - def __get_fp(self):
320 - def __set_fp(self, value):
322 fp = property(__get_fp, __set_fp)
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 -class _LDT_ENTRY_BYTES_(Structure):
353 _pack_ = 1
354 _fields_ = [
355 ('BaseMid', BYTE),
356 ('Flags1', BYTE),
357 ('Flags2', BYTE),
358 ('BaseHi', BYTE),
359 ]
360
361 -class _LDT_ENTRY_BITS_(Structure):
362 _pack_ = 1
363 _fields_ = [
364 ('BaseMid', DWORD, 8),
365 ('Type', DWORD, 5),
366 ('Dpl', DWORD, 2),
367 ('Pres', DWORD, 1),
368 ('LimitHi', DWORD, 4),
369 ('Sys', DWORD, 1),
370 ('Reserved_0', DWORD, 1),
371 ('Default_Big', DWORD, 1),
372 ('Granularity', DWORD, 1),
373 ('BaseHi', DWORD, 8),
374 ]
375
377 _pack_ = 1
378 _fields_ = [
379 ('Bytes', _LDT_ENTRY_BYTES_),
380 ('Bits', _LDT_ENTRY_BITS_),
381 ]
382
383 -class LDT_ENTRY(Structure):
384 _pack_ = 1
385 _fields_ = [
386 ('LimitLow', WORD),
387 ('BaseLow', WORD),
388 ('HighWord', _LDT_ENTRY_HIGHWORD_),
389 ]
390
391 PLDT_ENTRY = POINTER(LDT_ENTRY)
392 LPLDT_ENTRY = PLDT_ENTRY
393
394
395
396
397
398
399
400
401 -def GetThreadSelectorEntry(hThread, dwSelector):
402 _GetThreadSelectorEntry = windll.kernel32.GetThreadSelectorEntry
403 _GetThreadSelectorEntry.argtypes = [HANDLE, DWORD, LPLDT_ENTRY]
404 _GetThreadSelectorEntry.restype = bool
405 _GetThreadSelectorEntry.errcheck = RaiseIfZero
406
407 ldt = LDT_ENTRY()
408 _GetThreadSelectorEntry(hThread, dwSelector, byref(ldt))
409 return ldt
410
411
412
413
414
415 -def GetThreadContext(hThread, ContextFlags = None, raw = False):
416 _GetThreadContext = windll.kernel32.GetThreadContext
417 _GetThreadContext.argtypes = [HANDLE, LPCONTEXT]
418 _GetThreadContext.restype = bool
419 _GetThreadContext.errcheck = RaiseIfZero
420
421 if ContextFlags is None:
422 ContextFlags = CONTEXT_ALL | CONTEXT_i386
423 Context = CONTEXT()
424 Context.ContextFlags = ContextFlags
425 _GetThreadContext(hThread, byref(Context))
426 if raw:
427 return Context
428 return Context.to_dict()
429
430
431
432
433
434 -def SetThreadContext(hThread, lpContext):
435 _SetThreadContext = windll.kernel32.SetThreadContext
436 _SetThreadContext.argtypes = [HANDLE, LPCONTEXT]
437 _SetThreadContext.restype = bool
438 _SetThreadContext.errcheck = RaiseIfZero
439
440 if isinstance(lpContext, dict):
441 lpContext = CONTEXT.from_dict(lpContext)
442 _SetThreadContext(hThread, byref(lpContext))
443
444
445
446 _all = set(vars().keys()).difference(_all)
447 __all__ = [_x for _x in _all if not _x.startswith('_')]
448 __all__.sort()
449
450