|
Package Libs ::
Module immlib
|
|
1
2 """
3 Immunity Debugger API for python
4
5 (c) Immunity, Inc. 2004-2007
6
7
8 U{Immunity Inc.<http://www.immunityinc.com>} Debugger API for python
9
10
11 """
12
13 __VERSION__ = '1.3'
14
15
16 import debugger
17 import immutils
18 import string
19 import time
20 import struct
21 import pickle
22 import cPickle
23 import libheap
24
25 from libhook import *
26 from libevent import *
27 from debugtypes import *
28 from libanalyze import *
29 from librecognition import FunctionRecognition
30 from libcontrolflow import ControlFlowAnalysis
31
32
33 BpKeys = {"VK_F2": 0x71, "VK_F4" : 0x73}
34 BpFlags = {"TY_STOPAN": 0x80L, "TY_SET": 0x100L, "TY_ACTIVE": 0x200L, "TY_DISABLED":0x400,\
35 "TY_ONESHOT": 0x800L, "TY_TEMP":0x1000L, "TY_KEEPCODE":0x2000L, "TY_KEEPCOND": 0x4000L,\
36 "TY_NOUPDATE":0x8000, "TY_RTRACE": 0x10000}
37
38
39
40 HB_FREE=0
41 HB_CODE=1
42 HB_ACCESS=2
43 HB_WRITE=3
44 HB_IO=4
45 HB_ONESHOT=5
46 HB_STOPAN=6
47 HB_TEMP=7
48
49 DebugerStatus = { "NONE":0, "STOPPED":1, "EVENT":2, "RUNNING": 3, "FINISHED":4, "CLOSING":5 }
50
51 Register = { "EAX" : 0 , "ECX" : 1, "EDX": 2, "EBX": 3, "ESP": 4, "EBP": 5, "ESI": 6, "EDI": 7, "EIP":8}
52
53 PageFlags = {0x1 : " ",0x2: "R ", 0x4:"RW ", 0x8: "RW COW", 0x10: " E",\
54 0x20: "R E", 0x40: "RWE", 0x80: "RWE COW"}
55
56 ImmFonts = {"fixed": 0, "terminal6": 1, "fixedsys":2, "courier":3, "lucida":4, "font5": 5,\
57 "font6": 6, "font7":7, "main": 8, "sys": 9, "info": 10}
58
59
60
61 BpMemFlags = {"R": 0x1, "W":0x2, "S":0x1000L}
62
63 MemoryProtection = { "PAGE_EXECUTE" :0x10, "PAGE_EXECUTE_READ" :0x20 , "PAGE_EXECUTE_READWRITE": 0x40,\
64 "PAGE_EXECUTE_WRITECOPY":0x80, "PAGE_NOACCESS":0x01, "PAGE_READONLY":0x02,\
65 "PAGE_READWRITE":0x04, "PAGE_WRITECOPY": 0x08 }
66
67
68
69 IgnoreSingleStep = {"DISABLE" : 0 , "FORCE" : 1 , "CONTINUE" : 2}
70
71
72
73
74
75
76
77
78 jmpTypeFlags = {"JUMP":0,\
79 "JUMP_COND":1,\
80 "JUMP_SWITCH":2,\
81 "CALL":3,\
82 "CALL_INTER":4}
83
84
85 NM_NONAME=0x00
86 NM_MODSEARCH=0xFD
87 NM_ANYNAME=0xFF
88
89 NM_PLUGCMD=0x30
90 NM_LABEL=0x31
91 NM_EXPORT=0x32
92 NM_IMPORT=0x33
93 NM_LIBRARY=0x34
94 NM_CONST=0x35
95 NM_COMMENT=0x36
96 NM_LIBCOMM=0x37
97 NM_BREAK=0x38
98 NM_ARG=0x39
99 NM_ANALYSE=0x3A
100 NM_BREAKEXPR=0x3B
101 NM_BREAKEXPL=0x3C
102 NM_ASSUME=0x3D
103 NM_STRUCT=0x3E
104 NM_CASE=0x3F
105
106 NM_INSPECT=0x40
107 NM_WATCH=0x41
108 NM_ASM=0x42
109 NM_FINDASM=0x43
110 NM_LASTWATCH=0x48
111 NM_SOURCE=0x49
112 NM_REFTXT=0x4A
113 NM_GOTO=0x4B
114 NM_GOTODUMP=0x4C
115 NM_TRPAUSE=0x4D
116
117 NM_DLLPARMS=0x50
118
119 NM_DEBUG=0x80
120 NM_IMPLIB=0x81
121 NM_IMPNAME=0x82
122 NM_FONT=0x83
123 NM_SCHEME=0x84
124 NM_GOTOSTACK=0x85
125 NM_HILITE=0x86
126
127 NM_IMCALL=0xFE
128
129
130 import UserDict
131
132
135 UserDict.IterableUserDict.__init__(self)
137 for k in self.data.keys():
138 yield self.data[k]
139
140
141 ImmDrawColors = {"Black":0,"Maroon":128,"Green":32768,"Olive":32896,"Navy":8388608,"Purple":8388736,"Teal":8421376,\
142 "Gray":8421504,"Silver":12632256,"Red":255,"Lime":65280,"Yellow":65535,"Blue":16711680,"Fuchsia":16711935,\
143 "Aqua":16776960,"LightGray":12632256,"DarkGray":8421504,"White":16777215,"MoneyGreen":12639424,\
144 "SkyBlue":15780518,"Cream":15793151,"MedGray":10789024,"red":255,"darkgreen":32768}
145
146
147
148
149
150
174
176 self.Symbols = DictTypes()
177 self.Handles = DictTypes()
178 self.Threads = DictTypes()
179 self.MemoryPages = DictTypes()
180 self.Modules = DictTypes()
181 self.BackTrace = []
182 self.HeapsAddr = []
183 self.Heaps = {}
184
185
186
188 return self.Error("%d" % (0x15 * 2))
189
190
191
192
193
195 """
196 This function add a python object to the knowledge database.
197
198 @type id: STRING
199 @param id: unique name tag of the object
200
201 @type object: Python object
202 @param object: Object to be saved in the knowledge database
203 """
204
205 pickled_object=pickle.dumps(object)
206 return debugger.AddKnowledge(pickled_object,id, force_add)
207
209 """
210 Gets python object from the knowledge database.
211
212 @type id: STRING
213 @param id: unique name tag of the object
214
215 @rtype: PYTHON OBJECT
216 @return: Object retrieved from the knowledge database
217 """
218 pickled_object=debugger.GetKnowledge(id)
219
220 if not pickled_object:
221 return None
222 return pickle.loads(pickled_object)
223
225 """
226 Gets the list of saved objects in the knowledge database.
227
228 @rtype: TUPLE
229 @return: List of String ids currently saved
230 """
231 return debugger.ListKnowledge()
232
234 """
235 Find possible Packer/Cryptors/etc on a Module
236
237 @type name: STRING
238 @param name: Module name
239
240 @type OnMemory: (Optional, Def: True) BOOLEAN
241 @param OnMemory: Whether to look in memory or on a file.
242
243 @rtype: LIST of TUPLES in the form of (DWORD, LIST OF STRING)
244 @return: A list of the Packer founded (Offset, List of Packer found in that address)
245 """
246 if OnMemory:
247 mem = self.getMemoryPagebyOwner(name)
248 if not mem:
249 raise Exception, "Coudln't find a Memory Page belonging to %s" % name
250 data = ""
251 for a in mem:
252 data+= a.getMemory()
253 else:
254 mod = self.getModule( name )
255 if not mod:
256 raise Exception, "Coudln't find the correct Module belonging to %s" % name
257 data = mod.getPath()
258
259 import pefile
260 import peutils
261 if OnMemory:
262 pe = pefile.PE( data = data )
263 else:
264 pe = pefile.PE( name = data )
265
266 sig_db = peutils.SignatureDatabase('Data/UserDB.TXT')
267 return sig_db.match( pe )
268
270 """
271 Remove python object from knowledge database.
272
273 @type id: STRING
274 @param id: unique name tag of the object
275 """
276 return debugger.ForgetKnowledge(id)
277
283
284
286 """
287 Add a hook to Immunity Debugger
288 """
289
290 import pickle
291 try:
292 rtype=object.type
293 except:
294 rtype=0
295 try:
296 label=object.label
297 except:
298 label="No Label specified for this hook"
299 pickled_object=pickle.dumps(object)
300 debugger.Addhook(pickled_object,label,rtype)
301
302
304 """
305 Clean ID memory from hook objects
306 """
307 for hk in self.listHooks():
308 debugger.Removehook(hk)
309
310
311
313 """
314 Clean ID memory for every kind of object saved in it
315 """
316 self.cleanHooks()
317 self.cleanKnowledge()
318
319
321 """
322 Gets PEB.
323 @rtype: DWORD
324 @return: PEB address
325 """
326 return debugger.GetPEB()
327
328
329
330
331
333 """
334 Analyse module's code
335
336 @type Address: DWORD
337 @param Address: Address from module to be analysed
338 """
339 debugger.Analysecode(address)
340
342 """
343 Check if module is already analysed
344
345 @type Address: DWORD
346 @param Address: Address from module
347
348 @rtype: DWORD
349 @return: 1 if module already analysed
350 """
351 ret = debugger.IsAnalysed(address)
352
353 if ret == -1:
354 return 0
355 else:
356 return ret
357
359 """
360 Set Variable name to specified address.
361
362 @type Address: DWORD
363 @param Address: Address from assembly line
364
365 @type String: STRING
366 @param String: Variable name to be set
367
368 """
369 return debugger.SetVariable(address,string)
370
372 """
373 Get Variable name from specified address
374
375 @type Address: DWORD
376 @param Address: Address from assembly line
377
378 @rtype: STRING
379 @return: Variable name for given address.
380
381 """
382 return debugger.GetVariable(address)
383
384
385
387 """
388 Disasm address
389
390 @type Address: DWORD
391 @param Address: Address to disasm
392
393 @type Mode: (Optional, Def: DISASM_ALL)
394 @param Mode: Disasm mode
395
396 @rtype: opCode Object (Check libanalyze.py)
397 @return: Disassmbled Opcode
398 """
399
400 op= opCode( self, address )
401 op._getfromtuple( debugger.Disasm( address, mode) )
402 return op
403
404
405
407 return self.Disasm(address, mode)
408
409
410
412 """
413 Determine command size only
414
415 @type Address: DWORD
416 @param Address: Address to disasm
417
418 @rtype: opCode Object (Check libanalyze.py)
419 @return: Disassmbled Opcode
420 """