-
Notifications
You must be signed in to change notification settings - Fork 17
/
regrip.py
350 lines (304 loc) · 11.1 KB
/
regrip.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
#!/usr/bin/env python3
import argparse
import logging
import os
import sys
import importlib
from Registry import Registry
logging.basicConfig()
l = logging.getLogger("regrippy")
l.setLevel("ERROR")
def first(*args):
for arg in args:
if arg:
return arg
return None
def find_file_nocase(root, file_name):
for file in os.listdir(root):
if file.lower() == file_name.lower():
return file
return None
def find_path_nocase(root_path, rest_list):
full_path_parts = []
for part in rest_list:
actual_name = find_file_nocase(os.path.join(root_path, *full_path_parts), part)
if not actual_name:
return None
full_path_parts.append(actual_name)
return os.path.join(root_path, *full_path_parts)
def get_hive_paths(args, hive_name):
# use REG_ROOT to set root path of hive
args.root = args.root or os.getenv("REG_ROOT")
if (
hive_name.lower() in ["all", "ntuser.dat", "usrclass.dat"]
and args.all_user_hives
and not args.root
):
print("Error: --all-user-hives requires --root", file=sys.stderr)
sys.exit(3)
# Basic cases
if hive_name.lower() == "system":
path = first(
args.system,
find_path_nocase(args.root, ["windows", "system32", "config", "system"])
if args.root
else None,
os.getenv("REG_SYSTEM"),
)
result = [path] if path else None
if args.backups and path:
parent = os.path.dirname(path)
regback = find_path_nocase(parent, ["regback", "system"])
if regback:
result.append(regback)
return result
elif hive_name.lower() == "software":
path = first(
args.software,
find_path_nocase(args.root, ["windows", "system32", "config", "software"])
if args.root
else None,
os.getenv("REG_SOFTWARE"),
)
result = [path] if path else None
if args.backups and path:
parent = os.path.dirname(path)
regback = find_path_nocase(parent, ["regback", "software"])
if regback:
result.append(regback)
return result
elif hive_name.lower() == "sam":
path = first(
args.sam,
find_path_nocase(args.root, ["windows", "system32", "config", "sam"])
if args.root
else None,
os.getenv("REG_SAM"),
)
result = [path] if path else None
if args.backups and path:
parent = os.path.dirname(path)
regback = find_path_nocase(parent, ["regback", "sam"])
if regback:
result.append(regback)
return result
elif hive_name.lower() == "security":
path = first(
args.security,
find_path_nocase(args.root, ["windows", "system32", "config", "security"])
if args.root
else None,
os.getenv("REG_SECURITY"),
)
result = [path] if path else None
if args.backups and path:
parent = os.path.dirname(path)
regback = find_path_nocase(parent, ["regback", "security"])
if regback:
result.append(regback)
return result
elif hive_name.lower() == "ntuser.dat" and not args.all_user_hives:
path = first(args.ntuser, os.getenv("REG_NTUSER"))
result = [path] if path else None
if args.backups and path:
parent = os.path.dirname(path)
regback = find_path_nocase(parent, ["ntuser.dat.old"])
if regback:
result.append(regback)
return result
elif hive_name.lower() == "usrclass.dat" and not args.all_user_hives:
path = first(args.usrclass, os.getenv("REG_USRCLASS"))
result = [path] if path else None
if args.backups and path:
parent = os.path.dirname(path)
regback = find_path_nocase(parent, ["usrclass.dat.old"])
if regback:
result.append(regback)
return result
# All user hives
elif hive_name.lower() in ["ntuser.dat", "usrclass.dat"] and args.all_user_hives:
hive_paths = []
users_folder = find_path_nocase(args.root, ["users"])
if users_folder is None:
# Not found, let's try "Documents and Settings"
users_folder = find_path_nocase(args.root, ["Documents And Settings"])
if users_folder is None:
# Still nothing, we didn't find the Users folder, crash&burn
raise RuntimeError("Could not find the Users folder")
for user_dir in os.listdir(users_folder):
current_user_dir = os.path.join(users_folder, user_dir)
if not os.path.isdir(current_user_dir):
continue
if hive_name.lower() == "ntuser.dat":
path = find_path_nocase(current_user_dir, ["ntuser.dat"])
if path is not None:
hive_paths.append(path)
if args.backups:
backup = find_path_nocase(current_user_dir, ["ntuser.dat.old"])
if backup:
hive_paths.append(backup)
else:
path = find_path_nocase(
current_user_dir,
["appdata", "local", "microsoft", "windows", "usrclass.dat"],
)
if path is not None:
hive_paths.append(path)
if args.backups:
backup = find_path_nocase(
current_user_dir,
[
"appdata",
"local",
"microsoft",
"windows",
"usrclass.dat.old",
],
)
if backup:
hive_paths.append(backup)
return hive_paths
elif hive_name.lower() == "all":
hive_paths = []
for hive in ["system", "software", "sam", "security", "ntuser.dat", "usrclass.dat"]:
paths = get_hive_paths(args, hive)
if paths:
hive_paths.extend(paths)
return hive_paths
return None
def list_plugins():
for f in importlib.resources.files("regrippy.plugins").iterdir():
filename = os.path.basename(f)
mod_name = filename[:-len(".py")]
if mod_name.startswith("__"):
continue
mod = importlib.import_module(f"regrippy.plugins.{mod_name}")
p = mod.Plugin
print(f"- {mod_name}({p.__REGHIVE__}): {p.__doc__}")
def load_plugin(plugin_name):
for f in importlib.resources.files("regrippy.plugins").iterdir():
filename = os.path.basename(f)
mod_name = filename[:-len(".py")]
if mod_name.startswith("__"):
continue
if mod_name == plugin_name:
mod = importlib.import_module(f"regrippy.plugins.{mod_name}")
p = mod.Plugin
return p
raise ValueError(f"No such plugin: {plugin_name}")
def main():
if os.path.basename(sys.argv[0]).lower() != "regrip.py":
# Issue #5: allow selecting plugins based on argv[0]
plugin_name = os.path.basename(sys.argv[0])
# Allow the symlink to be called reg_pluginname to reduce risk of collision
if plugin_name.startswith("reg_"):
plugin_name = plugin_name[len("reg_") :]
else:
plugin_name = None
parser = argparse.ArgumentParser(
description="Extract information from Windows Registry hives"
)
parser.add_argument(
"--security",
"-e",
help="Path to the SECURITY hive. Overrides --root and the REG_SECURITY environment variable",
type=str,
default="",
)
parser.add_argument(
"--system",
"-y",
help="Path to the SYSTEM hive. Overrides --root and the REG_SYSTEM environment variable",
type=str,
default="",
)
parser.add_argument(
"--software",
"-o",
help="Path to the SOFTWARE hive. Overrides --root and the REG_SOFTWARE environment variable",
type=str,
default="",
)
parser.add_argument(
"--sam",
"-a",
help="Path to the SAM hive. Overrides --root and the REG_SAM environment variable",
type=str,
default="",
)
parser.add_argument(
"--ntuser",
"-n",
help="Path to the NTUSER.DAT hive. Overrides the REG_NTUSER environment variable",
type=str,
default="",
)
parser.add_argument(
"--usrclass",
"-u",
help="Path to the UsrClass.DAT hive. Overrides the REG_USRCLASS environment variable",
type=str,
default="",
)
parser.add_argument(
"--root",
"-r",
help="Path to the C: folder. Overrides the REG_ROOT environment variable",
type=str,
default="",
)
parser.add_argument(
"--all-user-hives",
help="Work on all NTUSER.DAT and USRCLASS.DAT hives if required. Requires --root. Overrides --ntuser and --usrclass.",
action="store_true",
)
parser.add_argument(
"--backups",
action="store_true",
help="Run the plugin on backup registry hives as well (does not work for hives loaded from stdin)",
)
parser.add_argument("--verbose", "-v", help="Be more verbose", action="store_true")
parser.add_argument(
"--bodyfile", "-b", help="Force output in Bodyfile format", action="store_true"
)
parser.add_argument(
"--list", "-l", help="List available plugins", action="store_true"
)
if not plugin_name:
parser.add_argument("plugin_name", help="Name of the plugin to run", type=str)
if "--list" in sys.argv or "-l" in sys.argv:
list_plugins()
return
args = parser.parse_args()
if not plugin_name:
plugin_name = args.plugin_name
if args.verbose:
l.setLevel("DEBUG")
plugin = load_plugin(plugin_name)
if type(plugin.__REGHIVE__) == str:
hive_names = [plugin.__REGHIVE__]
else:
hive_names = plugin.__REGHIVE__
for hive_name in hive_names:
hive_paths = get_hive_paths(args, hive_name)
if not hive_paths:
print("[!] Hive not found:", hive_name, file=sys.stderr)
continue
for hive_path in hive_paths:
if hive_path == "-":
# Special case: read hive from stdin
reg = Registry.Registry(sys.stdin.buffer)
else:
reg = Registry.Registry(hive_path)
p = plugin(reg, l, hive_name, hive_path)
results = list(p.run())
if results:
for result in results:
if args.bodyfile:
p.display_machine(result)
else:
if hive_name == "NTUSER.DAT":
print(f"[.] User: {p.guess_username()}")
p.display_human(result)
if __name__ == "__main__":
main()