-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathEmpireStrikeX.ps1
2735 lines (2539 loc) · 102 KB
/
EmpireStrikeX.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
################################################
############## EmpireStrikeX #############Beta
#######################################Empire2.5
## KNOWN ISSUES
# /!\ AgentUploadFile <------------------------- /!\ Check/Fix
# /!\ AgentDownloadFile <------------------------- /!\ Check/Fix
# /!\ CredAdd <------------------------- /!\ Check POST format
# /!\ Reporting (All) <------------------------- /!\ Upcoming Changes (Breaking?)
## TWEAK/PR
# /?\ Stager OutFile API <------------------------- /?\ Maybe cool (w\ static)
################################################
########################################### VARS
#region VARS
# Enum ListenerType -> For Param Vset
# Enum ModuleCategory -> For Param Vset
# Glob EmpireSession -> Empire Session Objs
# Glob EmpireTarget -> Target Session + Agent
# Glob EmpireStrike -> Target Module + Option
# Glob EmpireList -> All Other Empire Objs
################################### ListenerType
enum ListenerType{
dbx
http
http_com
http_foreign
http_hop
http_mapi
meterpreter
onedrive
redirector
}
################################# ModuleCategory
enum ModuleCategory{
code_execution
collection
credentials
exfiltration
exploitation
lateral_movement
management
persistence
privesc
recon
situational_awareness
trollsploit
exploit
custom
}
################################## EmpireSession
if(!$Global:EmpireSession){
$Global:EmpireSession = New-Object Collections.ArrayList
}
################################### EmpireTarget
if(!$Global:EmpireTarget){
$Global:EmpireTarget = New-Object PSCustomObject -Prop @{
ID = '?'
Agent = '?'
}}
################################### EmpireStrike
if(!$Global:EmpireStrike){
$Global:EmpireStrike = New-Object PSCustomObject -Prop @{
Module = '?'
Option = '?'
}}
##################################### EmpireList
if(!$Global:EmpireList){
$Global:EmpireList = New-Object PSCustomObject -Prop @{
Module = $Null
ModuleName = $Null
AgentName = $Null
ListenerName = $Null
StagerType = $Null
Temp = $Null
TempType = $Null
}}
######################################### Banner
$Banner = @('
`````````
``````.--::///+
````-+sydmmmNNNNNNN
``./ymmNNNNNNNNNNNNNN
``-ymmNNNNNNNNNNNNNNNNN
```ommmmNNNNNNNNNNNNNNNNN
``.ydmNNNNNNNNNNNNNNNNNNNN
```odmmNNNNNNNNNNNNNNNNNNNN
```/hmmmNNNNNNNNNNNNNNNNMNNN
````+hmmmNNNNNNNNNNNNNNNNNMMN
````..ymmmNNNNNNNNNNNNNNNNNNNN
````:.+so+//:---.......----::-
`````.`````````....----:///++++
``````.-/osy+////:::---...-dNNNN
````:sdyyydy` ```:mNNNNM
````-hmmdhdmm:` ``.+hNNNNNNM
```.odNNmdmmNNo````.:+yNNNNNNNNNN
```-sNNNmdh/dNNhhdNNNNNNNNNNNNNNN
```-hNNNmNo::mNNNNNNNNNNNNNNNNNNN
```-hNNmdNo--/dNNNNNNNNNNNNNNNNNN
````:dNmmdmd-:+NNNNNNNNNNNNNNNNNNm
```/hNNmmddmd+mNNNNNNNNNNNNNNds++o
``/dNNNNNmmmmmmmNNNNNNNNNNNmdoosydd
`sNNNNdyydNNNNmmmmmmNNNNNmyoymNNNNN
:NNmmmdso++dNNNNmmNNNNNdhymNNNNNNNN
-NmdmmNNdsyohNNNNmmNNNNNNNNNNNNNNNN
`sdhmmNNNNdyhdNNNNNNNNNNNNNNNNNNNNN
/yhmNNmmNNNNNNNNNNNNNNNNNNNNNNmhh
`+yhmmNNNNNNNNNNNNNNNNNNNNNNmh+:
`./dmmmmNNNNNNNNNNNNNNNNmmd.
`ommmmmNNNNNNNmNmNNNNmmd:
:dmmmmNNNNNmh../oyhhhy:
`sdmmmmNNNmmh/++-.+oh.
`/dmmmmmmmmdo-:/ossd:
`/ohhdmmmmmmdddddmh/
`-/osyhdddddhyo:
``.----.`
.-------------------------------------.
| EmpireStrikeX - 0.0 - @SadProcessor |
''-------------------------------------''
')#
#endregion #####################################
################################################
###################################### INTERNALS
#region INTERNALS
# EmpireConnect -> Connect to Server
# EmpireCall -> Call Empire API
# DynDico -> Dyn Param Generator
# AutoSession -> Auto Session Update/Target
# AutoTarget -> Auto Target Agent + Warning
# UnpackOption -> Reformat Option Obj
# MFilter -> Module Name Filter
# Only -> Property Filter Pipe
# FixSession -> Re-Allocate Session IDs
# LastError -> Last Error to Warning
# DynSession -> Dyn Session Param List
################################## EmpireConnect
<#
.Synopsis
New Empire Session
.DESCRIPTION
# INTERNAL
Connect to Empire Server
Add Session Obj to $EmpireSession
.EXAMPLE
EmpireConnect 10.1.2.3 Sadprocessor -NoSSL
Connects to Empire
Prompts for Password
#>
function EmpireConnect{
[CmdletBinding()]
Param(
# Server IP
[Parameter(Mandatory=1)][Alias('Host')][String]$Server,
# UserName (Will Prompt for Password)
[Parameter(Mandatory = $true)]
[Management.Automation.CredentialAttribute()][Management.Automation.PSCredential]$User,
# Switch Disable SSL
[Parameter(Mandatory=0)][Switch]$NoSSL,
# Optional Port
[Parameter(Mandatory=0)][Int]$Port=1337
)
# If No SSL
if($NoSSL){
Try{
# Dissable Cert Check ################################# <------------------------ /!\ Check /!\
Add-Type @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback{
public static void Ignore(){
ServicePointManager.ServerCertificateValidationCallback +=
delegate(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
){return true;};}}
"@
[ServerCertificateValidationCallback]::Ignore()}Catch{<#nothing#>}
}
# Cred to Json
$Pass = ConvertTo-Json @{
username=$User.UserName
password=$User.GetNetworkCredential().Password
}
# Prep Call Options
$Call = @{
Content = 'application/json'
Method = 'POST'
Uri = "https://$($Server):$($Port)/api/admin/login"
Body = "$Pass"
}
# Make Call
$Reply = Invoke-RestMethod @Call
# If Reply Token
if($Reply.token){
# Create Session Obj
$session = New-Object PSCustomObject -Property @{
ID = $Global:EmpireSession.token.Count
Host = $Server
Port = $Port
Token= $Reply.token
}
# Add to Empire Sessions
$Null = $Global:EmpireSession.Add($Session)
# Return Session Obj
Return $Session | Select ID,Host,Port,Token
}}
#####end
##################################### EmpireCall
<#
.Synopsis
Call Empire API
.DESCRIPTION
# INTERNAL
Invoke-WebRequest to Empire API
Requires Action Parameter
Optional Spec And BodyHash
Defaults to target Session ID
.EXAMPLE
EmpireCall ModuleList
#>
function EmpireCall{
Param(
# Call Action
[ValidateSet(<#'AdminLogin',#>'AdminToken','AdminRestart','AdminShutDown','AdminConfig','AdminVersion','AdminFile','AdminMap',
'AgentList','AgentRemove','AgentView','AgentClearBuffer','AgentKill','AgentRename','AgentResult','AgentDeleteResult','AgentExec',
'AgentUploadFile','AgentDownloadFile','AgentListStale','AgentRemoveStale','CredList','CredAdd','ListenerList','ListenerView',
'ListenerKill','ListenerNew','ListenerOptions','ModuleList','ModuleView','ModuleExec',<#'ModuleSearch','ModuleSearchAuth',
'ModuleSearchComm','ModuleSearchDesc','ModuleSearchName',#>'EventList','EventView','EventMessage','EventType','StagerList',
'StagerNew','StagerView')]
[Parameter(Mandatory=1)][String]$Act,
# Spec Param
[Parameter(Mandatory=0)][String]$Spec,
# Post Options
[Parameter(Mandatory=0)][PSCustomObject]$Opt,
# Session ID
[Parameter(Mandatory=0)][Int]$ID=$Global:EmpireTarget.ID
)
# Prep Session Vars
$Session = $Global:EmpireSession | where ID -eq $ID
if(!$Session.token){Write-Warning "Session Not Found.";Break}
$Server = $Session.Host
$Port = $Session.Port
$Token = $Session.token
# Action to Call Vars
Switch($Act){
######################################################################################################
<# ACTION { METHOD ; KEY ; ROUTE } TEST #>
##Admin ##############################################################################################
AdminToken {$M='GET' ; $K=$Null ; $R="/api/admin/permanenttoken" } <# OK #>
AdminRestart {$M='GET' ; $K=$Null ; $R="/api/admin/restart" } <# OK #>
AdminShutDown {$M='GET' ; $K=$Null ; $R="/api/admin/shutdown" } <# OK #>
AdminConfig {$M='GET' ; $K='config' ; $R="/api/config" } <# OK #>
AdminVersion {$M='GET' ; $K=$Null ; $R="/api/version" } <# OK #>
AdminFile {$M='GET' ; $K=$Null ; $R="/static/$Spec" } <# OK #>
AdminMap {$M='GET' ; $K='Routes' ; $R="/api/map" } <# OK #>
<#AdminLogin {$M='POST' ; $K=$Null ; $R="/api/admin/login" } <# XX #> #<------- EmpireConnect
##Agent ##############################################################################################
AgentList {$M='GET' ; $K='agents' ; $R="/api/agents" } <# OK #>
AgentRemove {$M='DELETE'; $K=$Null ; $R="/api/agents/$Spec" } <# OK #>
AgentView {$M='GET' ; $K='agents' ; $R="/api/agents/$Spec" } <# OK #>
AgentClearBuffer {$M='GET' ; $K=$Null ; $R="/api/agents/$Spec/clear" } <# OK #>
AgentKill {$M='GET' ; $K=$Null ; $R="/api/agents/$Spec/kill" } <# OK #>
AgentRename {$M='POST' ; $K=$Null ; $R="/api/agents/$Spec/rename" } <# OK #>
AgentResult {$M='GET' ; $K='results' ; $R="/api/agents/$Spec/results" } <# OK #>
AgentDeleteResult{$M='DELETE'; $K=$Null ; $R="/api/agents/$Spec/results" } <# OK #>
AgentExec {$M='POST' ; $K=$Null ; $R="/api/agents/$Spec/shell" } <# OK #>
AgentUploadFile {$M='POST' ; $K=$Null ; $R="/api/agents/$Spec/upload" } <# ?? #> #<-------- /!\ Check/Fix
AgentDownloadFile{$M='POST' ; $K=$Null ; $R="/api/agents/$Spec/download" } <# ?? #> #<-------- /!\ Check/Fix
AgentListStale {$M='GET' ; $K='agents' ; $R="/api/agents/stale" } <# OK #>
AgentRemoveStale {$M='DELETE'; $K=$Null ; $R="/api/agents/stale" } <# OK #>
##Cred ###############################################################################################
CredList {$M='GET' ; $K='creds' ; $R="/api/creds" } <# OK #>
CredAdd {$M='POST' ; $K=$Null ; $R="/api/creds" } <# ?? #> #<-------- /!\ Check required POST format
##Listener ###########################################################################################
ListenerList {$M='GET' ; $K='listeners' ; $R="/api/listeners" } <# OK #>
ListenerView {$M='GET' ; $K='listeners' ; $R="/api/listeners/$Spec" } <# OK #>
ListenerKill {$M='DELETE'; $K=$Null ; $R="/api/listeners/$Spec" } <# OK #>
ListenerNew {$M='POST' ; $K=$Null ; $R="/api/listeners/$Spec" } <# OK #>
ListenerOptions {$M='GET' ; $K='listeneroptions'; $R="/api/listeners/options/$Spec" } <# OK #>
##Module #############################################################################################
ModuleList {$M='GET' ; $K='modules' ; $R="/api/modules" } <# OK #>
ModuleView {$M='GET' ; $K='modules' ; $R="/api/modules/$Spec" } <# OK #>
ModuleExec {$M='POST' ; $K=$Null ; $R="/api/modules/$Spec" } <# OK #>
<#ModuleSearch {$M='POST' ; $K=$Null ; $R="/api/modules/search" } <# XX #>
<#ModuleSearchAuth {$M='POST' ; $K=$Null ; $R="/api/modules/search/author" } <# XX #>
<#ModuleSearchComm {$M='POST' ; $K=$Null ; $R="/api/modules/search/comments" } <# XX #>
<#ModuleSearchDesc {$M='POST' ; $K=$Null ; $R="/api/modules/search/description"} <# XX #>
<#ModuleSearchName {$M='POST' ; $K=$Null ; $R="/api/modules/search/modulename" } <# XX #>
##Event ##############################################################################################
EventList {$M='GET' ; $K='reporting' ; $R="/api/reporting" } <# OK #>
EventView {$M='GET' ; $K='reporting' ; $R="/api/reporting/agent/$Spec" } <# OK #>
EventMessage {$M='GET' ; $K='reporting' ; $R="/api/reporting/msg/$Spec" } <# OK #>
EventType {$M='GET' ; $K='reporting' ; $R="/api/reporting/type/$Spec" } <# OK #>
##Stager #############################################################################################
StagerList {$M='GET' ; $K='stagers' ; $R="/api/stagers" } <# OK #>
StagerNew {$M='POST' ; $K=$Null ; $R="/api/stagers" } <# OK #>
StagerView {$M='GET' ; $K='stagers' ; $R="/api/stagers/$Spec" } <# OK #>
}###################################################################################################
# <# XX = not used / ?? = Needs check/Fix #>
# Prep Call Options
$Uri = "https://$($Server):$($Port)${R}?token=$Token"
$Call = @{Content='application/json'; Method=$M; Uri=$Uri}
Write-Verbose "[$ID] - $Act $Spec"
# if POST add Body (Json)
if($M -eq 'POST' -AND $Opt){
$Json = ConvertTo-Json $Opt
$Call.Add('Body',$Json)
Write-Verbose "BODY`n`t$($Json.trim().trimEnd('}').trimStart('{').trim())"
}
# Make Call
$Reply = Try{Invoke-RestMethod @Call}Catch{LastError}
# Unpack & Return Reply
if($K -ne $Null){$Reply = $Reply.$K}
Return $Reply
}
#end
######################################## DynDico
<#
.Synopsis
DynParam Dictionnary
.DESCRIPTION
# INTERNAL
Return Single DynParam Dictionnary
Used for Dynamic Param Validation
.EXAMPLE
DynParam: Switch on ParamSet
DynamicParam{
# Filter ParamSet
if($PSCmdlet.ParameterSetName -in 'SetA','SetB'){
# Populate Vars
Switch($PSCmdlet.ParameterSetName){
#SET { NAME ; TYPE ; MAND ; PIPE ; POS ; VSET
###############################################################...
SetA { $N='Name'; $T='String'; $M=1 ; $L=0 ; $P=0 ; $V=@('A1','A2')}
SetB { $N='Num' ; $T='Int' ; $M=0 ; $L=0 ; $P=0 ; $V=@(1,2,3)}
}
# Return Dico
return DynDico -Name $N -Type $T -Mandat $M -Pipe $L -Pos $P -VSet $V
}}
#>
function DynDico{
Param(
[Parameter(Mandatory=1)][String]$Name,
[Parameter(Mandatory=1)][String]$Type,
[Parameter(Mandatory=0)][bool]$Mandat=0,
[Parameter(Mandatory=0)][bool]$Pipe=0,
[Parameter(Mandatory=0)][int]$Pos=$Null,
[Parameter(Mandatory=0)][Array]$VSet=$Null
)
# Prep Empty Dico
$Dico = New-Object Management.Automation.RuntimeDefinedParameterDictionary
# Create Attribute Obj
$Attrb = New-Object Management.Automation.ParameterAttribute
$Attrb.Mandatory=$Mandat
$Attrb.ValueFromPipeline=$Pipe
$Attrb.ValueFromPipelineByPropertyName=$Pipe
if($Pos -ne $null){$Attrb.Position=$Pos}
# Create AttributeCollection
$Cllct = New-Object Collections.ObjectModel.Collection[System.Attribute]
# Add Attribute Obj to Collection
$Cllct.Add($Attrb)
if($VSet -ne $Null){
# Create ValidateSet & add to collection
$VldSt=New-Object Management.Automation.ValidateSetAttribute($VSet)
$Cllct.Add($VldSt)
}
# Create Runtine DynParam
$DynP = New-Object Management.Automation.RuntimeDefinedParameter($Name,$($Type-as[type]),$Cllct)
# Add dynParam to Dictionary
$Dico.Add($Name,$DynP)
## Return Dictionary
return $Dico
}
#end
#################################### AutoSession
<#
.Synopsis
Auto Session
.DESCRIPTION
# INTERNAL
Auto Session ID
Used when Connecting/Switching Session
- Set Target Session ID
- Sync Session Objects
- Auto Target Agent / Warning
.EXAMPLE
AutoSession 0
#>
function AutoSession{
Param([Parameter()][Int]$ID=$Global:EmpireTarget.ID)
# Set Target Session
$Global:EmpireTarget.ID = $ID
# Update Session Vars
Empire-Sync -Session
# AutoTarget Agent
AutoTarget
}
#end
##################################### AutoTarget
<#
.Synopsis
Auto Target Agent
.DESCRIPTION
# INTERNAL
Auto target Agent
Used when Connecting/Switching Session
.EXAMPLE
AutoTarget
#>
function AutoTarget{
# If target not in Session
if($Global:EmpireTarget.Agent -notin $Global:EmpireList.AgentName -OR $Global:EmpireTarget.Agent -eq $null){
$Global:EmpireTarget.Agent = ''
$L = [Array]$Global:EmpireList.AgentName
Switch($L.count){
0 {Write-Warning 'No Agents in Session'}
1 {$Global:EmpireTarget.Agent = $L[0];Write-Warning "Auto Target - $($L[0])"}
Default {Write-Warning 'Select Target Agent'}
}}}
#########end
################################### UnpackOption
<#
.Synopsis
Unpack Options
.DESCRIPTION
# INTERNAL
Make Nice Option Object
Pipe after Module/Listener/Stager obj
.EXAMPLE
$Object | Unpack
#>
function UnpackOptions{
[Alias('Unpack')]
Param(
# Input Option Object
[Parameter(Mandatory=1,ValueFromPipeline=1)]$In
)
Begin{$Out= @()}
Process{
if($In.Options){
# For Each Object Property
($In.Options|gm|? MemberType -eq NoteProperty).name|%{
# Add expanded object to Output
$Out += New-Object PSCustomObject -Property @{
Name = $_
Description = $In.Options.$_.Description
Required = $In.Options.$_.Required
Value = $In.Options.$_.Value
}}}
# If .options Obj
else{
# For Each Object Property
($In|gm|? MemberType -eq NoteProperty).name|%{
# Add expanded object to Output
$Out += New-Object PSCustomObject -Property @{
Name = $_
Description = $In.$_.Description
Required = $In.$_.Required
Value = $In.$_.Value
}}}}
End{Return $Out|Select Name,Required,Value,Description}
}
#end
#################################### WarnMissing
<#
.Synopsis
Warn Missing Option
.DESCRIPTION
# INTERNAL
Missing Option Warning
Used for Strike/Execute/Generate
.EXAMPLE
WarnMissing $obj
.EXAMPLE
if(WarnMissing $Global:EmpireList.temp){Return}
#>
function WarnMissing{
[OutputType([bool])]
Param([Parameter(Mandatory=1,ValueFromPipeline=1)][PSCustomObject]$Obj)
Begin{$result=@()}
Process{
$Missing = [Array]($Obj | ? {($_.required -eq 'True' -AND ([String]$_.Value -eq ''))}).Name
if($Missing.Count){
Write-Warning "Required Option $($Missing-join',')"
$result+=$true
}}
# Return $true/$Null
End{if($Result.Count){Return $true}}
}
#End
######################################## MFilter
<#
.Synopsis
Filter Module List
.DESCRIPTION
# INTERNAL
Filter Module List
Used for Dynamic Module Name
.EXAMPLE
MFilter -Lang PowerShell
#>
function MFilter{
Param(
[ValidateSet('PowerShell','Python')]
[Parameter()][String]$Lang,
[Parameter()][String]$Cat
)
# Lang Only
if($Lang -AND !$Cat){
$Lang=$Lang.ToLower()
$List = ($Global:EmpireList.ModuleName -match "$Lang/").replace("$Lang/",'')
}
# Cat Only
elseif($Cat -AND !$Lang){
$Cat=$Cat.ToLower()
$List = $Global:EmpireList.ModuleName -match "$Cat/"
$List = $List -replace "(.*)$Cat/",''
}
# Lang & Cat
elseif($Lang -AND $Cat){
$Lang=$Lang.ToLower()
$List = ($Global:EmpireList.ModuleName -match "$Lang/").replace("$Lang/",'')
$Cat=$cat.ToLower()
$List = ($List -match "$Cat/").replace("$Cat/",'')
}
# No Filter
elseif(!$Lang -AND !$Cat){$List = $Global:EmpireList.ModuleName}
# Return List
return $List
}
#end
########################################### Only
<#
.Synopsis
Pipe Prop Only
.DESCRIPTION
# INTERNAL
List Prop Value over Pipe
Defaults to Name Property
.EXAMPLE
$ObjectCollection | Name
.EXAMPLE
$ObjectCollection | Only ThisOption
#>
function Only{
[Alias('Name')]
Param(
# Prop Name
[Parameter()][String]$Prop='Name',
# Input Obj
[Parameter(ValuefromPipeline=$true)]$Obj
)
## Prep Empty Collector
Begin{$List=@()}
## Filter Prop
Process{if($Obj.$Prop){$List += $Obj.$Prop}}
## Return List
End{Return $List}
}
#end
##################################### DynSession
<#
.Synopsis
Dyn Session ID
.DESCRIPTION
# INTERNAL
Return Dyn Session List
for ValidateSet
.EXAMPLE
FixSession
#>
function DynSession{
Switch($Global:EmpireSession.token.count){
1{[Array]"$($Global:EmpireSession.id)"}
Default{[Array]$Global:EmpireSession.id}
}}
#####end
##################################### FixSession
<#
.Synopsis
Fix Session IDs
.DESCRIPTION
# INTERNAL
Re-Allocate Session IDs
Used after Session -Remove
.EXAMPLE
FixSession
#>
function FixSession{
Write-Warning "New Session IDs"
1..($Global:EmpireSession.token.Count) | %{
$Global:EmpireSession[$_-1].ID = $_-1
}}
#####end
###################################### LastError
<#
.Synopsis
Last Error Warning
.DESCRIPTION
# INTERNAL
Last Error Message to Warning
Used in Try/Catch for EmpireCall
.EXAMPLE
LastError
#>
function LastError{
[Cmdletbinding()]
Param()
## Make It So
if($Error[0].Exception){Write-Warning $Error[0].Exception.message.replace('. ',".`n`t`t ")}
else{Write-Warning $Error[0].PSMessageDetails.split("`n")[0]}
if($Error[0].ErrorDetails.message){Write-Warning ('Error: '+($Error[0].ErrorDetails.message|COnvertfrom-Json).error)}
}
#end
#endregion #####################################
################################################
###################################### EXTERNALS
#region EXTERNALS
# Empire-Admin -> Empire Admin Stuff
# Empire-Agent -> Interact with Empire Agents
# Empire-Cred -> Interact with Empire Cred DB
# Empire-Event -> View Empire Events <----------------- /!\ upcoming (breaking) changes /!\
# Empire-Exec -> Execute Commands on agent
# Empire-Help -> View EmpireStrike Cheat Sheet
# Empire-Listener -> Interact with Empire Listeners
# Empire-Module -> Interact with Empire Modules
# Empire-Option -> Set Module Options
# Empire-Result -> Get Agent result
# Empire-Search -> Search Module|Agent
# Empire-Session -> Interact with Empire Sessions
# Empire-Sniper -> Shoot from Scriptpane (ISE/VSCode)
# Empire-Speak -> Add Voice to Automations
# Empire-Stager -> Interact with Empire Stagers
# Empire-Strike -> Execute Empire Modules
# Empire-Sync -> Sync Empire Objects
# Empire-Target -> View/Set Target
# Empire-Tune -> Play Imperial March
# Empire-Use -> Use Stager|Listener|Module
# Empire-View -> View Empire Objects
################################### Empire-Admin
<#
.Synopsis
Empire Admin Actions
.DESCRIPTION
Admin -Login : Connect to Empire Server
Admin -Token : View session Token
Admin -Restart : Restart Empire
Admin -ShutDown : Stop Empire
Admin -Config : View Empire Config
Admin -Version : View Empire Version
Admin -File : View static file
Admin -Map : View API Routes
More Info >> Help Admin -Examples
.EXAMPLE
Empire-Admin -Login -Host 10.1.2.3 -User sadProcessor -Port 1337 -NoSSL
Login to server
Short: Admin -login 10.1.2.3 sadProcessor -NoSSL
.EXAMPLE
Admin -Token
View session token
Same for Config/Version
.EXAMPLE
Admin -Restart -ID 1
Restart empire server ID 1
Same for Shutdown
.EXAMPLE
Admin -Map
Display all rest API routes
.NOTES
Notes
#>
Function Empire-Admin{
[CmdletBinding(
DefaultParameterSetname='AdminConfig',
HelpUri='/~https://github.com/EmpireProject/Empire/wiki',
SupportsShouldProcess=1,
ConfirmImpact='High')]
[Alias('Admin','Server','srv')]
Param(
# Connect to Server
[Parameter(Mandatory=1,ParameterSetName='AdminLogin')][Alias('X','Connect ')][switch]$Login,
# Get Permanent Token
[Parameter(Mandatory=1,ParameterSetName='AdminToken')][switch]$Token,
# Restart Empire
[Parameter(Mandatory=1,ParameterSetName='AdminRestart')][switch]$Restart,
# ShutDown Empire
[Parameter(Mandatory=1,ParameterSetName='AdminShutDown')][switch]$ShutDown,
# Empire Config
[Parameter(Mandatory=0,ParameterSetName='AdminConfig')][switch]$Config,
# Empire Version
[Parameter(Mandatory=1,ParameterSetName='AdminVersion')][switch]$Version,
# Empire File
[Parameter(Mandatory=1,ParameterSetName='AdminFile')][string]$File,
# Map Empire API
[Parameter(Mandatory=1,ParameterSetName='AdminMap')][Alias('API')][switch]$Map,
# Server IP
[Parameter(Mandatory=1,ParameterSetName='AdminLogin',Position=0)][Alias('Host','Srv')][string]$Server,
# UserName
[Parameter(Mandatory=1,ParameterSetName='AdminLogin',Position=1)][Alias('Name','Usr')][string]$User,
# Port Number
[Parameter(Mandatory=0,ParameterSetName='AdminLogin',Position=2)][int]$Port,
# No SSL Check
[Parameter(Mandatory=0,ParameterSetName='AdminLogin')][switch]$NoSSL
# Empire File
#[Parameter(Mandatory=1,ParameterSetName='AdminFile')][Alias('Path')][string]$FileName
)
DynamicParam{
# Filter ParamSet
if($PSCmdlet.ParameterSetName -ne 'AdminLogin'){
# Return Dico
return DynDico -Name ID -Type int -Mandat 0 -Pipe 1 -Pos 0 -VSet (DynSession)
}}
## Make It So
Begin{$Result = @()}
Process{
# Session
if($PSBoundParameters.ID.count){$Sess = $PSBoundParameters.ID}
else{$Sess = $Global:EmpireTarget.ID}
# Switch Action
Switch($PsCmdlet.ParameterSetName){
## Login
AdminLogin{
$Null=$PSBoundParameters.Remove('Login')
$Result = EmpireConnect @PSBoundParameters
if($Result.token){AutoSession -ID ($Global:EmpireSession.count -1)}
}
## Token
AdminToken {$Result += EmpireCall AdminToken -ID $Sess}
## Config
AdminConfig {$Result += EmpireCall AdminConfig -ID $Sess}
## Version
AdminVersion{$Result += EmpireCall AdminVersion -ID $Sess}
## File
AdminFile{$Result += EmpireCall Adminfile -Spec $File -ID $Sess}
## Restart
AdminRestart{
# Confirm [Use -Confirm:$false to skip]
if($PSCmdlet.ShouldProcess("Session $Sess","Restart")){
$result += EmpireCall AdminRestart -ID $Sess
}}
## ShutDown
AdminShutDown{
# Confirm [Use -Confirm:$false to skip]
if($PSCmdlet.ShouldProcess("Session $Sess","Shutdown")){
$Result = EmpireCall AdminShutDown -ID $Sess
}}
## Map (API)
AdminMap{
# Fix Reply
$All = EmpireCall AdminMap -ID $Sess -ErrorAction Stop
$Obj = @()
foreach($Route in $All.split("`n").trim()-ne''){
# Extract
$Act = ($Route | sls "\[ \{ '(.*)': \[ \{ 'methods':").Matches.Groups[1].Value
$Met = ($Route | sls "'methods': '(.*)', 'url':" ).Matches.Groups[1].Value
$Rte = ($Route | sls "'url': '(.*)' } ] } ]" ).Matches.Groups[1].Value
# Add to Result
$Obj += New-Object PSCustomObject -Property @{
Action = $Act
Method = $Met
Route = $Rte
}}
# Format
$Result += $Obj | Select Action,Method,Route | Sort Route
}}}
## Return Result
End{Return $Result}
}
#end
################################### Empire-Agent
<#
.Synopsis
Interact with Agents
.DESCRIPTION
Agent -View : View Agent details
Agent -List : List all Agents
Agent -Remove : Remove Agent fronm DB
Agent -ClearBuffer : Clear Agent Buffer
Agent -Kill : Kill Agent
Agent -Rename : Rename Agent
Agent -Result : View Agent Result
Agent -DeleteResult : Delete Agent result
Agent -Exec : Execute command
Agent -Upload : Upload file
#Agent -Download : Download file <-------------- ToDo
Agent -ListStale : List stale Agents
Agent -RemoveStale : Remove Stale Agents
Agent -Sync : Sync local Agent list
Agent -Target : Set Target Agent
More Info >> Help Agent -Examples
.EXAMPLE
Agent -List
List all agent in current session
.EXAMPLE
Agent [-View] ABCDEFGH
Show details of specified agent
.EXAMPLE
Agent -Target ABCDEFGH
Set specified agent as target
.EXAMPLE
agent BCWGEZ3T -Exec -command Get-Date
Task Agent command
.EXAMPLE
Agent -ClearBuffer ABCDEFGH
Clear specified agent buffer
.EXAMPLE
Agent -Kill ABCDEFGH
Kill specified agent
.EXAMPLE
Agent -Remove ABCDEFGH
Remove specified agent
.EXAMPLE
Agent -ListStale
List Stale agents
Use -RemoveStale to remove
.EXAMPLE
Agent -Download <---------------------- ToDo
.EXAMPLE
Agent -Upload <---------------------- ToDo
.EXAMPLE
Agent -Sync
Sync local agent name list
Done in background for each Agent -List
.NOTES
(Most) commands default to current target
when name parameter ommited
#>
Function Empire-Agent{
[CmdletBinding(DefaultParameterSetname='AgentView',HelpUri='/~https://github.com/EmpireProject/Empire/wiki')]
[Alias('Agent','Agt')]
Param(
# List all Agents
[Parameter(Mandatory=1,ParameterSetName='AgentList')][switch]$List,
# Remove Agent
[Parameter(Mandatory=1,ParameterSetName='AgentRemove')][switch]$Remove,
# View Agent
[Parameter(Mandatory=0,ParameterSetName='AgentView')][switch]$View,
# Clear Agent buffer
[Parameter(Mandatory=1,ParameterSetName='AgentClearBuffer')][switch]$ClearBuffer,
# Kill Agent
[Parameter(Mandatory=1,ParameterSetName='AgentKill')][switch]$Kill,
# Rename Agent
[Parameter(Mandatory=0,ParameterSetName='AgentRename')][switch]$Rename,
# Agent Result
[Parameter(Mandatory=1,ParameterSetName='AgentResult')][switch]$Result,
# Delete Agent Result
[Parameter(Mandatory=1,ParameterSetName='AgentDeleteResult')][Alias('ResultDelete')][switch]$DeleteResult,
# Execute Command
[Parameter(Mandatory=0,ParameterSetName='AgentExec')][Alias('PoSh','X')][switch]$Exec,
# Upload File
[Parameter(Mandatory=1,ParameterSetName='AgentUploadFile')][switch]$Upload,
# Download File
#[Parameter(Mandatory=1,ParameterSetName='AgentUploadFile')][switch]$Download,
# List Stale Agents
[Parameter(Mandatory=1,ParameterSetName='AgentListStale')][Alias('Stale')][switch]$ListStale,
# Remove Stale Agents
[Parameter(Mandatory=1,ParameterSetName='AgentRemoveStale')][Alias('StaleRemove')][switch]$RemoveStale,
# Sync Agent List
[Parameter(Mandatory=1,ParameterSetName='AgentSync')][switch]$Sync,
# Set Target Agent
[Parameter(Mandatory=1,ParameterSetName='AgentTarget')][Alias('Tgt','Select')][switch]$Target,
# New Agent Name
[Parameter(Mandatory=1,ParameterSetName='AgentRename')][string]$NewName,
# Command to run
[Parameter(Mandatory=1,ParameterSetName='AgentExec')][Alias('Cmd')][string]$Command,
# File Path
[Parameter(Mandatory=1,ParameterSetName='AgentUploadFile')][string]$File
)
DynamicParam{
# Filter ParamSet
if($PSCmdlet.ParameterSetName -notin 'AgentList','AgentSync','AgentListStale','AgentRemoveStale'){
# Populate Vars
Switch($PSCmdlet.ParameterSetName){
#SET { NAME ; TYPE ; MAND ; PIPE ; POS ; VSET
#########################################################################################...
AgentView { $N='Name'; $T='String'; $M=0 ; $L=1 ; $P=0 ; $V=[Array]$Global:EmpireList.AgentName}
AgentRename { $N='Name'; $T='String'; $M=1 ; $L=0 ; $P=0 ; $V=[Array]$Global:EmpireList.AgentName}
Default { $N='Name'; $T='String'; $M=0 ; $L=1 ; $P=0 ; $V=[Array]$Global:EmpireList.AgentName}
}
# Return Dico
return DynDico -Name $N -Type $T -Mandat $M -Pipe $L -Pos $P -VSet $V
}
if($PSCmdlet.ParameterSetName -in 'AgentList','AgentListStale','AgentRemoveStale'){
# Return Dico
return DynDico -Name ID -Type String -Mandat 0 -Pipe 1 -Pos 0 -VSet (DynSession)
}}
## Make It So
Begin{$Reply=@()}
Process{
# Agent Name
if($PSCmdlet.ParameterSetName -notin 'AgentList','AgentSync','AgentListStale','AgentRemoveStale'){
if($PSBoundParameters.Name){$Agt = $PSBoundParameters.Name}
else{if($PSCmdlet.ParameterSetName -notin 'AgentRemove','AgentKill'){$Agt=$Global:EmpireTarget.Agent}}
if($Agt -eq $null){Write-Warning 'No Target Agent';Break}}
# Session ID
if($PSCmdlet.ParameterSetName -in 'AgentList','AgentListStale','AgentRemoveStale'){
if($Global:EmpireTarget.ID.count){$Sess = $Global:EmpireTarget.ID}
else{$Sess = $Global:EmpireTarget.ID}
}
# Switch Action
Switch($PSCmdlet.ParameterSetName){
# Sync