-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdmpStruct.py
379 lines (316 loc) · 14.2 KB
/
dmpStruct.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
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
from WindowsMinidump import *
from datetime import *
import pandas as pd
import argparse
import sys
import yara
import os
from pathlib import Path
from colorama import init as colorama_init
from colorama import Fore
from colorama import Style
import logging
_memRanges = []
_replacements = [b"\x00",b"\x0f",b"\x1e",b"\x7f",b"\x10"]
_saveStrings = 'memoryStrings.txt'
#_saveStringHex = 'memoryStringHex.txt'
fo = open(_saveStrings,'w+')
def sysInfo(processStreams):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.system_info":
"""
Useful col: 'ofs_service_pack', 'os_build', 'os_platform', 'os_suite_mask', 'os_type', 'os_ver_major', 'os_ver_minor', 'reserved2', 'service_pack'
"""
print(f"{Fore.GREEN}[+] System Information ...{Style.RESET_ALL}")
osBuild = x.data.os_build
osPlatform = x.data.os_platform
osVerMajor = x.data.os_ver_major
osVerMinor = x.data.os_ver_minor
osType = x.data.os_type
osReserved = x.data.reserved2
osServicePack = x.data.service_pack
print(f"Build: {osBuild}")
print(f"Platform: {osPlatform}")
print(f"Windows Version: {osVerMajor}")
print(f"Type: {osType}")
print(f"Reserved: {osReserved}")
print(f"Service Pack: {osServicePack}")
print("=======\n")
#fo.write("[+] System Information ...\n")
#fo.write(f"Build: {osBuild}\n")
#fo.write(f"Platform: {osPlatform}\n")
#fo.write(f"Windows Version: {osVerMajor}\n")
#fo.write(f"Type: {osType}\n")
#fo.write(f"Reserved: {osReserved}\n")
#fo.write(f"Service Pack: {osServicePack}\n")
#fo.write("==============")
def pmemoryList(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.memory_list":
pMemoryRanges = x.data.mem_ranges
#print("[+] Process Information ...")
fo.write("[+] Process Information ...")
for pMemoryRange in pMemoryRanges:
processMemoryRange = pMemoryRange.addr_memory_range
if processMemoryRange not in _memRanges:
_memRanges.append(processMemoryRange)
processMemoryData = pMemoryRange.memory.data
processMemoryDataDecoded = processMemoryData.decode("utf-8",errors="ignore")
df = pd.Series(processMemoryDataDecoded)
for i in _replacements:
rep = i.decode("utf-8",errors="ignore")
df[0] = df[0].replace(rep,'').replace('\n','')
processMemoryDataLength = pMemoryRange.memory.len_data
#print(f"Process memory range: {processMemoryRange}")
#print(f"> Data length (Bytes): {processMemoryDataLength}")
#print(f"> Process Data (Raw): {processMemoryData}")
#print(f"> Process Data (Decoded): {df[0].strip()}")
#print("=======")
fo.write(f"Process memory range: {processMemoryRange}\n")
fo.write(f"> Data length (Bytes): {processMemoryDataLength}\n")
if hex:
fo.write(f"> Process Data (Raw): {processMemoryData}\n")
fo.write(f"> Process Data (Decoded): {df[0].strip()}\n")
fo.write("================\n")
def pmoduleList(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.module_list":
#print("[+] Process Module list ...")
fo.write("[+] Process Module list ...")
pModuleList = x.data
pModuleListDecoded = x.data.decode("utf-8",errors="ignore")
df = pd.Series(pModuleListDecoded)
for i in _replacements:
rep = i.decode("utf-8",errors="ignore")
df[0] = df[0].replace(rep,'').replace('\n','')
#print(f"Process Module list: {pModuleList}")
#print(f"Process Module list (Decoded): {df[0].strip()}")
#print("=======")
if hex:
fo.write(f"Process Module list (Raw): {pModuleList}\n")
fo.write(f"Process Module list (Decoded): {df[0].strip()}\n")
fo.write("================\n")
def pfunctionTable(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.function_table":
#print(f"{Fore.GREEN}[+] Function table ...{Style.RESET_ALL}")
fo.write("[+] Function table ...")
pFunctionTable = x.data
pFunctionTableDecoded = pFunctionTable.decode("utf-8",errors="ignore")
df = pd.Series(pFunctionTableDecoded)
for i in _replacements:
rep = i.decode("utf-8",errors="ignore")
df[0] = df[0].replace(rep,'').replace('\n','')
#print(f"Process Function table: {pFunctionTable}")
#print(f"Process Function table (Decoded): {df[0].strip()}")
#print("=======")
if hex:
fo.write(f"Process Function table (Raw): {pFunctionTable}\n")
fo.write(f"Process Function table (Decoded): {df[0].strip()}\n")
fo.write("================\n")
#print("\n")
def pThreadList(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.thread_list":
pThreads = x.data.threads
print(f"{Fore.GREEN}[+] Threads Information ...{Style.RESET_ALL}")
fo.write("[+] Threads Information ...")
numThreads = x.data.num_threads
print(f"Total Threads: {numThreads}")
for pThread in pThreads:
"""
Useful col: 'stack', 'suspend_count', 'teb', 'thread_context', 'thread_id'
"""
tID = pThread.thread_id
teb = pThread.teb
suspend = pThread.suspend_count
memoryRange = pThread.stack.addr_memory_range
dataLength = pThread.stack.memory.len_data
threadMemoryData = pThread.stack.memory.data
threadMemoryDataDecoded = threadMemoryData.decode("utf-8",errors="ignore")
df = pd.Series(threadMemoryDataDecoded)
for i in _replacements:
rep = i.decode("utf-8",errors="ignore")
df[0] = df[0].replace(rep,'').replace('\n','')
print(f"> Thread Environment Block(TEB): {teb}")
print(f"> Thread ID: {tID}")
print(f"> Suspended: {suspend}")
print(f"> Thread memory range: {memoryRange}")
print(f"> Data length (Bytes): {dataLength}")
print("\n")
#print(f"Data: {threadMemoryData}")
#print(f"Data (Decoded): {df[0].strip()}")
fo.write(f"Thread Environment Block(TEB): {teb}\n")
fo.write(f"Thread ID: {tID}\n")
fo.write(f"Suspended: {suspend}\n")
fo.write(f"Thread memory range: {memoryRange}\n")
fo.write(f"Data length (Bytes): {dataLength}\n")
if hex:
fo.write(f"Data (Raw): {threadMemoryData}\n")
fo.write(f"Data (Decoded): {df[0].strip()}\n")
fo.write("================\n")
def handleData(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.handle_data":
#print(f"{Fore.GREEN}[+] Handle Data ...{Style.RESET_ALL}")
fo.write("[+] Handle Data ...\n")
if hex:
fo.write(f"Process Handle Data (Raw): {x.data}\n")
handleDataDecoded = x.data.decode("utf-8",errors="ignore")
fo.write(f"Process Handle Data (Decoded): {handleDataDecoded}\n")
fo.write("=======\n")
def systemMemoryInfo(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.system_memory_info":
#print(f"{Fore.GREEN}[+] System Memory Info ...{Style.RESET_ALL}")
fo.write("[+] System Memory Info ...\n")
if hex:
fo.write(f"System Memory Info (Raw): {x.data}\n")
systemMemoryInfoDecoded = x.data.decode("utf-8",errors="ignore")
fo.write(f"System Memory Info (Decoded): {systemMemoryInfoDecoded}\n")
fo.write("=======\n")
def threadNames(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.thread_names":
#print(f"{Fore.GREEN}[+] Thread Names ...{Style.RESET_ALL}")
fo.write("[+] Thread Names ...\n")
threadNamesDecoded = x.data.decode("utf-8",errors="ignore")
if hex:
fo.write(f"Thread Names (Raw): {x.data}\n")
fo.write(f"Thread Names (Decoded): {threadNamesDecoded}\n")
fo.write("=======\n")
def unused(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.unused":
unused = x.data.decode("utf-8",errors="ignore")
#print(f"Unused (Decoded): {unused}")
def token(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.token":
#print(f"{Fore.GREEN}[+] Token Information ...{Style.RESET_ALL}")
fo.write("[+] Token Information ...\n")
token = x.data.decode("utf-8",errors="ignore")
if hex:
fo.write(f"Token (Raw): {x.data}\n")
fo.write(f"Token (Decoded): {token}\n")
fo.write("=======\n")
def mem64(processStreams,hex=False):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.memory_64_list":
#print(f"{Fore.GREEN}[+] Memory x64 list ...{Style.RESET_ALL}")
fo.write("[+] Memory x64 list ...\n")
if hex:
fo.write(f"Memory x64 list (Raw): {x.data}\n")
mem64 = x.data.decode("utf-8",errors="ignore")
fo.write(f"Memory x64 list (Decoded): {mem64}\n")
fo.write("=======\n")
def miscInfo(processStreams):
for x in processStreams:
streamTypeStr = str(x.stream_type)
if streamTypeStr == "StreamTypes.misc_info":
"""
Useful col: 'process_create_time', 'process_id', 'process_kernel_time', 'process_user_time'
"""
pID = x.data.process_id
pCreationTime = datetime.fromtimestamp(x.data.process_create_time)
print("\n")
print(f"{Fore.GREEN}[+] Process Information ....{Style.RESET_ALL}")
print(f"> Process ID: {pID}")
print(f"> Process Creation Time: {pCreationTime}")
print("=======\n")
def fileManager(filePath):
"""
https://www.pythoncheatsheet.org/cheatsheet/file-directory-path
"""
dir = ''
file = ''
if filePath:
# Check if is file
if os.path.isfile(filePath):
file = filePath
# Check if is dir
elif not os.path.isfile(filePath):
dir = filePath
return dir,file
def yaraScanner(file,dir,dmpFile):
rules = ''
if file and not dir:
rules = yara.compile(filepaths={'namespace1':file})
elif dir and not file:
_rules = {}
c = 1
# Listing directory
for f in Path(dir).iterdir():
_rules.update({'namespace'+str(c):f.as_posix()})
c +=1
try:
rules = yara.compile(filepaths=_rules)
except Exception as e: pass
if rules:
matches = rules.match(dmpFile,callback=console)
return matches
def console(message):
"""
https://yara.readthedocs.io/en/stable/yarapython.html
"""
if message['matches'] == True:
print(f"Matches: {message['matches']}")
print(f"Rule Name: {message['rule']}")
print(f"Strings: {message['strings']}")
print("=====")
def main():
parser = argparse.ArgumentParser(description="[*] Usage: dmpStruct.py -f <*.dmp>")
parser.add_argument("-f","--file",action="store",help="Provide a DMP file (minidump)")
parser.add_argument("-X","--hex",action="store_true",help="Writing data in hex into memoryStrings.txt")
parser.add_argument("-y","--yara",action="store",help="Yara rule(s) directory or file.")
args = parser.parse_args()
if len(sys.argv) == 1:
print(parser.print_help())
else:
if args.file:
try:
data = WindowsMinidump.from_file(args.file)
except Exception as e:
raise(f"[-] Error::{e}")
dir = ''
file = ''
if args.file and args.yara:
try:
dir,file = fileManager(args.yara)
except Exception as e:
raise(f"[-] Error::{e}")
magic1 = data.magic1
magic2 = data.magic2
flags = data.flags
nstreams = data.num_streams
memory_dumped_date = datetime.fromtimestamp(data.timestamp)
processStreams = data.streams
miscInfo(processStreams)
sysInfo(processStreams)
if (dir or file) and args.file:
print(f"{Fore.GREEN}[+] Yara scanner ... {Style.RESET_ALL}")
yaraScanner(file,dir,args.file)
print("\n")
pmemoryList(processStreams,args.hex)
pmoduleList(processStreams,args.hex)
pfunctionTable(processStreams,args.hex)
pThreadList(processStreams,args.hex)
handleData(processStreams,args.hex)
systemMemoryInfo(processStreams,args.hex)
threadNames(processStreams,args.hex)
token(processStreams,args.hex)
#unused(processStreams,args.hex)
if __name__ == "__main__":
main()
colorama_init()