-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolAutoDump.py
267 lines (222 loc) · 10.9 KB
/
VolAutoDump.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
#!/usr/bin/env python3
from volatility3.framework.interfaces.configuration import path_join
from volatility3.framework.plugins import construct_plugin
from volatility3.framework import contexts, automagic, interfaces
from volatility3.cli import text_renderer
from volatility3 import plugins
import volatility3.framework
import argparse, logging
import os, sys, io, tempfile
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
volatility3.framework.require_interface_version(2, 0, 0)
# Redirect stdout to both log files and terminal
class RedirectOutput(object):
def __init__(self, output_file, verbose):
# Handling 'verbose' flag
if verbose:
self.terminal = sys.stdout
self.log = open(output_file, "w")
else:
self.terminal = open(os.devnull, "w")
self.log = open(output_file, "w")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
# this flush method is needed for python 3 compatibility.
# this handles the flush command by doing nothing.
# you might want to specify some extra behavior here.
pass
def fileHandler(output_dir):
class CLIFileHandler(interfaces.plugins.FileHandlerInterface):
"""The FileHandler from Volatility3 CLI"""
def _get_final_filename(self):
"""Gets the final filename"""
if output_dir is None:
raise TypeError("Output directory is not a string")
os.makedirs(output_dir, exist_ok = True)
pref_name_array = self.preferred_filename.split('.')
filename, extension = os.path.join(output_dir, '.'.join(pref_name_array[:-1])), pref_name_array[-1]
output_filename = f"{filename}.{extension}"
counter = 1
if os.path.exists(output_filename):
os.remove(output_filename)
return output_filename
class CLIDirectFileHandler(CLIFileHandler):
"""We want to save our files directly to disk"""
def __init__(self, filename: str):
fd, self._name = tempfile.mkstemp(suffix = '.vol3', prefix = 'tmp_', dir = output_dir)
self._file = io.open(fd, mode = 'w+b')
CLIFileHandler.__init__(self, filename)
for item in dir(self._file):
if not item.startswith('_') and not item in ['closed', 'close', 'mode', 'name']:
setattr(self, item, getattr(self._file, item))
def __getattr__(self, item):
return getattr(self._file, item)
@property
def closed(self):
return self._file.closed
@property
def mode(self):
return self._file.mode
@property
def name(self):
return self._file.name
def close(self):
"""Closes and commits the file (by moving the temporary file to the correct name"""
# Don't overcommit
if self._file.closed:
return
self._file.close()
output_filename = self._get_final_filename()
os.rename(self._name, output_filename)
return CLIDirectFileHandler
class DumpHandler:
def __init__(self, image_path, output_path, verbose, csv):
self.ctx = contexts.Context()
self.failures = volatility3.framework.import_files(volatility3.plugins, True) # Load plugins
if self.failures:
logger.warning(f"Volatility can't load these plugins: {self.failures}")
else:
logger.info("Plugins are loaded without failures")
self.plugin_list = volatility3.framework.list_plugins()
self.base_config_path = "plugins"
self.plugin_name = ["windows.dumpfiles.DumpFiles",
"windows.memmap.Memmap",
"windows.pslist.PsList"]
self.output_path = output_path
self.image_path = self.handlePath(image_path, self.output_path)
self.verbose = verbose
self.csv = csv
# Convert normal path to URL path
def handlePath(self, image_path, output_path):
if not (os.path.exists(output_path)):
os.makedirs(output_path)
if image_path.startswith("./") or image_path.startswith("../") or image_path.startswith(""):
abs_path = os.path.abspath(image_path)
file_url = "file://" + abs_path
return file_url
elif image_path.startswith("file://"):
return image_path
else:
return image_path
# Building context to run
def buildContext(self, plugin):
_config_path = path_join(self.base_config_path, plugin.__name__)
available_automagics = automagic.available(self.ctx)
plugin_config_path = interfaces.configuration.path_join(self.base_config_path, plugin.__name__)
automagics = automagic.choose_automagic(available_automagics, plugin)
self.ctx.config["automagic.LayerStacker.stackers"] = automagic.stacker.choose_os_stackers(plugin)
self.ctx.config["automagic.LayerStacker.single_location"] = self.image_path
constructed = construct_plugin(self.ctx, automagics, plugin, self.base_config_path, None, fileHandler(self.output_path))
return constructed
# Parse output to files (log/csv)
def prettyPrint(self, constructed, output_file, verbose):
sys.stdout = RedirectOutput(output_file, verbose)
result = text_renderer.PrettyTextRenderer().render(constructed.run())
sys.stdout.log.close()
sys.stdout = sys.__stdout__
def prettyCSV(self, constructed, output_file, verbose):
sys.stdout = RedirectOutput(output_file, verbose)
result = text_renderer.CSVRenderer().render(constructed.run())
sys.stdout.log.close()
sys.stdout = sys.__stdout__
class ProcDump(DumpHandler):
def __init__(self, image_path, output_path, verbose, csv, pid_list):
super().__init__(image_path, output_path, verbose, csv)
self.pid_list = pid_list
self.plugin = self.plugin_list[self.plugin_name[2]]
self.handlePID()
def handlePID(self):
ctx_pid = [int(pid) for pid in self.pid_list]
self.ctx.config["plugins.PsList.pid"] = ctx_pid
self.ctx.config["plugins.PsList.dump"] = True
constructed = super().buildContext(self.plugin)
if not constructed:
logger.info("Plugin could not extract anything")
if self.csv:
output_file = self.output_path + '/' + self.plugin_name[2].split(".")[1] + ".csv"
super().prettyCSV(constructed, output_file, self.verbose)
else:
output_file = self.output_path + '/' + self.plugin_name[2].split(".")[1] + ".log"
super().prettyPrint(constructed, output_file, self.verbose)
class FileDump(DumpHandler):
def __init__(self, image_path, output_path, verbose, csv, offset_list):
super().__init__(image_path, output_path, verbose, csv)
self.offset_list = offset_list
self.plugin = self.plugin_list[self.plugin_name[0]]
print(self.plugin_name)
self.handleOffset()
def handleOffset(self):
for ctx_offset in self.offset_list:
self.ctx.config["plugins.DumpFiles.virtaddr"] = int(ctx_offset, 16)
constructed = super().buildContext(self.plugin)
if not constructed:
logger.info("Plugin could not extract anything")
if self.csv:
output_file = self.output_path + '/' + self.plugin_name[0].split(".")[1] + ".csv"
super().prettyCSV(constructed, output_file, self.verbose)
else:
output_file = self.output_path + '/' + self.plugin_name[0].split(".")[1] + ".log"
super().prettyPrint(constructed, output_file, self.verbose)
class MemmapDump(DumpHandler):
def __init__(self, image_path, output_path, verbose, csv, pid_list):
super().__init__(image_path, output_path, verbose, csv)
self.pid_list = pid_list
self.plugin = self.plugin_list[self.plugin_name[1]]
self.handlePID()
def handlePID(self):
for ctx_pid in self.pid_list:
self.ctx.config["plugins.Memmap.pid"] = int(ctx_pid)
self.ctx.config["plugins.Memmap.dump"] = True
constructed = super().buildContext(self.plugin)
if not constructed:
logger.info("Plugin could not extract anything")
if self.csv:
output_file = self.output_path + '/' + self.plugin_name[1].split(".")[1] + ".csv"
super().prettyCSV(constructed, output_file, self.verbose)
else:
output_file = self.output_path + '/' + self.plugin_name[1].split(".")[1] + ".log"
super().prettyPrint(constructed, output_file, self.verbose)
def parseListOrFile(value):
if ',' in value:
return value.split(',')
else:
with open(value, 'r') as file:
return file.read().splitlines()
def main():
parser = argparse.ArgumentParser(description="Automatic Dumping Volatility Tool")
parser.add_argument("-p", "--path", metavar="<PATH>", help="Path to the memory image", required=True)
parser.add_argument("-o", "--output_path", metavar="<OUTPUT_PATH>", help="Out files folder", required=True)
parser.add_argument("-v", "--verbose", action="store_true", help="Print plugin's output")
parser.add_argument("-csv", action="store_true", help="Write to csv files")
subparsers = parser.add_subparsers(help='Dump Modes')
group_filedump = subparsers.add_parser("filedump")
group_filedump.set_defaults(group="filedump")
group = group_filedump.add_mutually_exclusive_group(required=True)
group.add_argument("-pid", metavar="<PROCESS' ID>", type=parseListOrFile, help="Process' ID list")
group.add_argument("-off", "--offset", metavar="<FILE'S OFFSET>", type=parseListOrFile, help="File's offset list")
group_procdump = subparsers.add_parser("procdump")
group_procdump.set_defaults(group="procdump")
group_procdump.add_argument("-pid", metavar="<PROCESS' ID>", type=parseListOrFile, help="Process' ID list", required=True)
group_memmap = subparsers.add_parser("memmap")
group_memmap.set_defaults(group="memmap")
group_memmap.add_argument("-pid", metavar="<PROCESS' ID>", type=parseListOrFile, help="Process' ID list", required=True)
args = parser.parse_args()
image_path = args.path
output_path = args.output_path
verbose = args.verbose
csv = args.csv
if args.group == 'procdump':
pid = args.pid
ProcDump(image_path, output_path, verbose, csv, pid)
elif args.group == 'memmap':
pid = args.pid
MemmapDump(image_path, output_path, verbose, csv, pid)
else:
pid = args.pid
offset = args.offset
FileDump(image_path, output_path, verbose, csv, offset)
if __name__=='__main__':
main()