-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathReg1c1de.cs
executable file
·521 lines (477 loc) · 20 KB
/
Reg1c1de.cs
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Security.Principal;
using System.Collections.Generic;
using Microsoft.Win32;
namespace Reg1c1de
{
class Regicide
{
static int tested = 0;
static bool info = true;
static string outfile = "";
static bool debug = false;
static bool performwritetest = false;
static bool filefinder_opt = true;
static List<string> fails = new List<string>();
static List<string> vulnerable = new List<string>();
static List<string> unsuccessfuls = new List<string>();
static List<string> writeablefiles = new List<string>();
static string basekey = "Software";
static string uniquestring = "sn0wflake_str1ng_";
static RegistryKey hive = Registry.LocalMachine;
static bool entirehive;
static string[] extensions = {
".dll",".exe",".wll",".inf",".ini"
};
static bool hasext(string fname)
{
foreach(string ext in extensions)
{
if (fname.Contains(ext))
{
return true;
}
}
return false;
}
static bool doihavewrite(string filename)
{
try
{
if (!System.IO.File.Exists(filename)){
return false;
}
System.IO.File.OpenWrite(filename);
return true;
} catch (System.UnauthorizedAccessException){
return false;
} catch
{
// in case
return false;
}
}
static void filefinder(string[] filenames,string keyname)
{
foreach(string fname in filenames)
{
if (hasext(fname))
{
if (doihavewrite(fname))
{
Console.WriteLine("[+]Writeable File: {0} with associated key: {1}", fname,keyname);
if (!writeablefiles.Contains(fname + "|" + keyname))
{
writeablefiles.Add(fname + "|" + keyname);
}
}
}
}
}
static void printusage()
{
Console.WriteLine("Description:\n" +
"Reg1c1de is a tool that scans specified registry hives and reports on any keys where the user has write permissions\n" +
"In addition, if any registry values are found that contain file paths with certain file extensions and they are writeable, these will be reported as well.\n" +
"These keys should be investigated further as they could potentially lead to a path to privilege escalation or other evil\n");
Console.WriteLine("Arguments: (THESE ARE ALL OPTIONAL!)\n" +
"-h \tshow this help message\n" +
"-vv\tenable debug output (more verbose)\n" +
"-e \tscan the entire specified hive, this is disabled by default\n" +
"-o \tfilename to write the vulnerable keys to csv, example -o=filename\n" +
"-k \tbase key to enumerate from under the hive, default=Software, example -k=Software\n" +
"-df\tdisables writeable file checking, in case you don't want to make thousands of access denied file open attempts\n" +
"-r \tfour letter shorthand of the root hive to enumerate from, default=HKLM, example -r=HKLM\n" +
"\tAcceptable values are: HKCU, HKLM, HKCR, HKCC, HKU\n\n"+
"-writetests\tenabling this flag will enable write tests, which will write a dummy registry key and value to every discovered " +
"instance of write access to a key.\nI DO NOT recommend using this, especially if you cannot make a registry backup, nevertheless " +
"it is here.\n");
Console.WriteLine("Example Usage:\n" +
"Reg1c1de.exe -v -o=outputfile -r=HKLM -e ");
}
static void banner()
{
Console.WriteLine("++++++++++++++Reg1c1de++++++++++++++++");
Console.WriteLine("+author: @deadjakk | http://shell.rip+");
Console.WriteLine("++++++++++++++++++++++++++++++++++++++\n\n");
}
static void Main(string[] args)
{
banner();
if (args.Length == 0)
{
Console.WriteLine("Reg1c1de -h to see list of args...");
Console.WriteLine("To continue without args hit enter, or Ctrl + C to cancel");
Console.Read();
}
else
{
int argresult = argparser(args);
if (argresult != 0)
{
return;
}
}
int result = regdive();
if (result == 9)
{
Console.WriteLine("Do not run this as admin, that's kind of pointless");
return;
}
if (result != 0)
{
return;
}
Console.WriteLine("Finished, tested {1} keys, potentially vulnerable keys found: {0}", vulnerable.Count, tested);
if (fails.Count > 0)
{
Console.WriteLine("Created keys that were created but could not be deleted: {0}", fails);
}
if (outfile != "")
{
writeoutput();
}
}
static void writeoutput()
{
List<string> output = new List<string>();
outfile = outfile.Replace(".csv", "");
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(outfile+"_"+hive.Name+".csv"))
{
writer.WriteLine("result,keyname");
foreach (string wfile in writeablefiles)
{
if (filefinder_opt) {
writer.WriteLine("WRITEABLEFILE," + wfile);
}
}
foreach (string vuln in vulnerable)
{
writer.WriteLine("VULNERABLE," + vuln);
}
foreach (string fail in fails)
{
writer.WriteLine("ERROR," + fail);
}
foreach (string item in unsuccessfuls)
{
writer.WriteLine("ACCESSDENIED," + item);
}
Console.WriteLine("Output was written to: {0}_{1}.csv", outfile, hive.Name);
}
}
static int argparser(string[] args)
{
foreach (var arg in args)
{
if (arg == "-h" || arg == "--help")
{
printusage();
return 10;
}
else if (arg == "-vv" || arg == "-v")
{
Console.WriteLine("[+]config: output set to debug");
debug = true;
}
else if (arg == "-e")
{
Console.WriteLine("[+]config: scanning entire hive");
entirehive = true;
}
else if (arg == "-df")
{
Console.WriteLine("[+]config: disabled file path checking");
filefinder_opt = false;
}
else if (arg.Contains("-o"))
{
try
{
outfile = arg.Split('=')[1];
if (outfile == "")
{
Console.WriteLine("filename is empty, please provide an actual filename, for example: -o=stuff.txt");
return 3;
}
}
catch
{
Console.WriteLine("\nError parsing outfile argument make sure there are no spaces\nRequired format: -o=filename.txt");
return 1;
}
}
else if (arg.Contains("-r"))
{
try
{
string arg_ = arg.Split('=')[1];
if (arg_ == "")
{
Console.WriteLine("key arg value is empty, please provide something, for example: -r=HKCU");
return 4;
}
int res = selecthive(arg_);
if (res != 0)
{
return 98;
}
}
catch
{
Console.WriteLine("\nError parsing -r argument make sure it follows the format: -r=VALUE");
return 1;
}
}
else if (arg.Contains("-k"))
{
try
{
basekey = arg.Split('=')[1];
if (basekey == "")
{
Console.WriteLine("key arg value is empty, please provide something, for example: -k=System");
return 3;
}
}
catch
{
Console.WriteLine("\nError parsing -k argument make sure it follows the format: -k=VALUE");
return 1;
}
}
else if (arg == "-writetests")
{
Console.WriteLine("[!!!]Write tests flag selected, there is a (high) chance that some of " +
"the keys that are created may not be removed properly or some other craziness happens\n" +
" consider backing up registry if you can, or using without -writetests flag.");
Console.Write("please confirm you would like to attempt a write and delete on all potentially" +
"vulnerable keys discovered. [y/N]: ");
string response = Console.ReadLine();
if (response.ToUpper().Contains("Y"))
{
performwritetest = true;
info = true;
Console.WriteLine("Write tests enabled, good luck");
}
else
{
return 2;
}
}
else
{
Console.WriteLine("Unknown arg: {0}, skipping it", arg);
}
}
return 0;
}
static int selecthive(string shorthand)
{
if (shorthand.ToUpper() == ("HKLM"))
{
// do nothing, this is default
}
else if (shorthand.ToUpper() == ("HKCC"))
{
hive = Registry.CurrentConfig;
}
else if (shorthand.ToUpper() == ("HKCR"))
{
hive = Registry.ClassesRoot;
}
else if (shorthand.ToUpper() == ("HKU"))
{
hive = Registry.Users;
}
else if (shorthand.ToUpper() == ("HKCU"))
{
hive = Registry.CurrentUser;
}
else
{
Console.WriteLine("invalid hive selection");
Console.WriteLine("-r value must be: HKCU, HKLM, HKCR, HKCC, HKU");
return 1;
}
return 0;
}
static void iprint(string inp)
{
if (debug || info)
{
Console.WriteLine("[I]{0}", inp);
}
}
static void dprint(string inp)
{
if (debug)
{
Console.WriteLine("[D]{0}", inp);
}
}
static void writetest(RegistryKey key)
{
//double checking
if (key.Name.Contains(uniquestring))
{
return;
}
RegistryKey nkey = key.CreateSubKey(uniquestring, true); // unique string
nkey.SetValue("dead", "jakk");
iprint("[+]successfully wrote a key to " + key.Name);
try
{
iprint("Removing key we created: " + nkey.Name);
foreach (String value in nkey.GetValueNames())
{
nkey.DeleteValue(value);
}
key.DeleteSubKey(uniquestring);
iprint("key successfully removed");
}
catch
{
Console.WriteLine("[!]Error deleting key {0}\tFOLLOW-UP", nkey.Name);
fails.Add(nkey.Name);
}
}
static bool isempty(RegistryKey testkey)
{
if (testkey.GetSubKeyNames().Length == 0)
{
return true;
}
return false;
}
static void checkrights(RegistryKey pkey, string subkey)
{
if (filefinder_opt)
{
RegistryKey tempKey = pkey.OpenSubKey(subkey, false);
List<string> tList = new List<string>();
foreach(string vname in tempKey.GetValueNames())
{
tList.Add(tempKey.GetValue(vname).ToString());
}
string[] alist = tList.ToArray();
filefinder(alist,pkey.Name + "\\" + subkey);
tempKey.Dispose();
}
if (subkey.Contains(uniquestring))
{
return;
}
if (vulnerable.Contains(pkey.Name))
{
// we already found this key
return;
}
tested++;
try
{
//Testing our privilges
RegistryKey key = pkey.OpenSubKey(subkey, true);
Console.WriteLine("[+]Writeable Key: {0}, may have weak permissions", key.Name);
vulnerable.Add(key.Name);
//dprint("Testing " + key.Name);
try
{
if (performwritetest)
{
writetest(key);
}
}
catch
{
Console.WriteLine("[?]Got an error trying to create a dummy key under {0} after obtaining write permissions", key.Name);
}
// TODO check if the values beneath have something interesting
}
catch (System.UnauthorizedAccessException) {
dprint("Access denied: couldn't write to key: " + pkey.Name + "\\" + subkey);
unsuccessfuls.Add(pkey.Name + "\\" + subkey);
} catch (System.Security.SecurityException) {
dprint("Access denied: couldn't write to key: " + pkey.Name + "\\" + subkey);
unsuccessfuls.Add(pkey.Name + "\\" + subkey);
} catch (Exception e) {
Console.WriteLine("Exception : {0}", e);
}
}
static void keycursion(RegistryKey pkey, string subkey)
{
RegistryKey rootkey = pkey.OpenSubKey(subkey);
bool empty = isempty(rootkey);
if (!empty)
{
foreach (var v in rootkey.GetSubKeyNames())
{
try
{
keycursion(rootkey, v);
}
catch (System.Security.SecurityException)
{
}
}
}
checkrights(pkey, subkey);
}
//https://stackoverflow.com/questions/3600322/check-if-the-current-user-is-administrator
public static bool IsAdministrator()
{
using (WindowsIdentity identity = WindowsIdentity.GetCurrent())
{
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
static int regdive()
{
if (IsAdministrator())
{
return 9;
}
try
{
RegistryKey bottomkey = hive;
if (!entirehive)
{
bottomkey = hive.OpenSubKey(basekey);
}
Console.WriteLine("\nSearching through keys...");
foreach (var subkey in bottomkey.GetSubKeyNames())
{
dprint(subkey);
try
{
keycursion(bottomkey, subkey);
}
catch (System.Security.SecurityException)
{
dprint("[-]security exception on " + subkey);
}
catch (System.ObjectDisposedException)
{
dprint("[-]disposed exception");
}
catch (Exception e)
{
Console.WriteLine("[-]Error occured : {0}", e);
}
}
return 0;
}
catch(System.NullReferenceException)
{
Console.WriteLine("\n[-]Base Key provided was invalid");
Console.WriteLine("Try one of the following (based on your provided -r arg):");
foreach(string name in hive.GetSubKeyNames())
{
Console.WriteLine(name);
}
return 1;
}
}
}
}