-
Notifications
You must be signed in to change notification settings - Fork 65
/
CypherDog14.ps1
2045 lines (1932 loc) · 93.7 KB
/
CypherDog14.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
###########################################################
## CypherDog - PoSh BloodHound Dog Whisperer Module ##
########################################################1.4
#region ############################################## Vars
## Banner ASCII
$ASCII= @("
____________________________________________
______|_____________________________________
_____||__________________________CYPHERDOG__
_____||-________...___________________BETA__
______||-__--||||||||-._____________________
_______!||||||||||||||||||--________________
________|||||||||||||||||||||-______________
________!||||||||||||||||||||||.____________
_______.||||||!!||||||||||||||||-___________
______|||!||||___||||||||||||||||.__________
_____|||_.||!___.|||'_!||_'||||||!__________
____||___!||____|||____||___|||||.__________
_____||___||_____||_____||!__!|||'__________
__________ ||!____||!_______________________
____________________________________________
PoSh BloodHound Cmdlets - @SadProcessor 2018
")
## CypherDog Obj
$CypherDog = New-Object PSCustomObject -Property @{
Host = 'localhost'
Port = 7474
UserList = $Null
GroupList = $Null
ComputerList = $Null
DomainList = $Null
}
## EdgeType Enum
enum EdgeType{
MemberOf
AdminTo
HasSession
TrustedBy
ForceChangePassword
AddMembers
GenericAll
GenericWrite
WriteOwner
WriteDACL
AllExtendedRights
}
#endregion ################################################
###########################################################
#region ######################################### Internals
## INTERNALS ##############################################
#
# DogPost > Post Query to REST API
# CacheNode > Cache Node Lists
# DynP > Generate Dynamic Param
# ToPathObj > Unpack Path to Object
# ClipThis > Query To Clipboard
# Wald0Index > Calc Wald0's Pwn Index <----------------------------- /!\ Check Math [use Wald0s SampleDB / compare results]
#
###########################################################
###########################################################
################################################### DogPost
<#
.Synopsis
Post to API - Internal
.DESCRIPTION
Post Query (& Params) to API
.EXAMPLE
DogPost $Query [$Params] [-Raw]
# Return All Users
$query="MATCH (n:User) RETURN n"
DogPost $Query
# Specific Computer
$query = "MATCH (A:Computer {name: {ParamA}}) RETURN A"
$Params = @{ParamA="APOLLO.EXTERNAL.LOCAL"}
DogPost $Query $Params
# Path A to B
$Query = "MATCH (A:User {name: {ParamA}}), (B:Group {name: {ParamB}}), x=shortestPath((A)-[*1..]->(B)) RETURN x"
$Params= @{ParamA="[email protected]";ParamB="DOMAIN [email protected]"}
DogPost $Query $Params -Raw
#>
function DogPost{
[CmdletBinding()]
Param(
[Parameter(Mandatory=1)][string]$Query,
[Parameter(Mandatory=0)][Hashtable]$Params,
[Parameter()][Switch]$Raw,
[Parameter()][String]$Server='localhost',
[Parameter()][int]$Port=7474
)
# Uri & header
$Uri = "http://$($CypherDog.Host):$($CypherDog.Port)/db/data/cypher"
$Header=@{'Accept'='application/json; charset=UTF-8';'Content-Type'='application/json'}
# Body
if($Params){$Body = @{params=$Params; query=$Query} | Convertto-Json}
else{$Body = @{query=$Query}|Convertto-Json}
# Call
write-verbose $Body.replace(')\u003c-',')<-').replace('-\u003e(','->(')
$Reply = Try{Invoke-RestMethod -Uri $Uri -Method Post -Headers $Header -Body $Body}Catch{$Oops = $Error[0].Exception}
# Format
if($Oops){Write-Warning "$($Oops.message)" ;Return}
if($Raw){Return $Reply}
else{Return $Reply.Data.data}
}
#End
###########################################################
################################################# CacheNode
<#
.Synopsis
Cache Node Lists - Internal
.DESCRIPTION
Cache Node Lists per type
Used for dynamic Name Params
.EXAMPLE
CacheNode
.EXAMPLE
CacheNode Computer
#>
function CacheNode{
[CmdletBinding()]
Param(
# Specify Type (Default All)
[ValidateSet('All','User','Group','Computer','Domain')]
[parameter(Mandatory=0)][string]$NodeType='All'
)
# Cache User List
if($NodeType -match "All|User"){
$Query = "MATCH (n:User) RETURN n"
$Script:CypherDog.UserList = (DogPost $Query -verbose:$false).name
}
# Cache Group List
if($NodeType -match "All|Group"){
$Query = "MATCH (n:Group) RETURN n"
$Script:CypherDog.GroupList = (DogPost $Query -verbose:$false).name
}
# Cache Computer List
if($NodeType -match "All|Computer"){
$Query = "MATCH (n:Computer) RETURN n"
$Script:CypherDog.ComputerList = (DogPost $Query -verbose:$false).name
}
# Cache Domain List
if($NodeType -match "All|Domain"){
$Query = "MATCH (n:Domain) RETURN n"
$Script:CypherDog.DomainList = (DogPost $Query -verbose:$false).name
}}
#####End
###########################################################
###################################################### DynP
<#
.Synopsis
Get Dynamic Param - Internal
.DESCRIPTION
Return Single DynParam to be added to dictionnary
.EXAMPLE
DynP TestParam String -mandatory 1
#>
function DynP{
[CmdletBinding()]
Param(
[Parameter(Mandatory=1)][String]$Name,
[Parameter(Mandatory=1)][string]$Type,
[Parameter(Mandatory=0)][bool]$Mandat=0,
[Parameter(Mandatory=0)][int]$Pos=$Null,
[Parameter(Mandatory=0)][bool]$Pipe=0,
[Parameter(Mandatory=0)][bool]$PipeProp=0,
[Parameter(Mandatory=0)]$VSet=$Null
)
# Create Attribute Obj
$Attrb = New-Object Management.Automation.ParameterAttribute
$Attrb.Mandatory=$Mandat
$Attrb.ValueFromPipeline=$Pipe
$Attrb.ValueFromPipelineByPropertyName=$PipeProp
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)
# Return DynParam
Return $DynP
}
#End
###########################################################
################################################# ToPathObj
<#
.Synopsis
Parse to Path Object - Internal
.DESCRIPTION
Description
.EXAMPLE
Example-One
#>
function ToPathObj{
[CmdletBinding()]
[Alias()]
Param(
[Parameter(ValueFromPipelineByPropertyName=1)]$Data
)
Begin{$Result=@()}
Process{
$StepCount = $Data.relationships.count
# if Steps
if($StepCount -gt 0){
$PathObj = @()
0..($StepCount -1)|%{
$Props = @{
'Step' = $_
'StartNode' = (irm -Method Get -Headers $header -uri @($Data.nodes)[$_]).data.name
'Edge' = (irm -Method Get -Headers $header -uri @($Data.relationships)[$_]).type
'EndNode' = (irm -Method Get -Headers $header -uri @($Data.nodes)[$_+1]).data.name
'Direction' = @($Data.directions)[$_]
}
$PathObj += New-Object PSCustomObject -Property $props
}
$Result += $PathObj | select 'Step','StartNode','Edge','Direction','EndNode'
}}
# Return Result
End{Return $Result}
}
#End
###########################################################
################################################## ClipThis
<#
.Synopsis
Query to ClibOard - Internal
.DESCRIPTION
Displays resulting query and sets clipboard
.EXAMPLE
ClipThis $Query [-with $Params]
#>
Function ClipThis{
[CmdletBinding()]
Param(
# Query
[Parameter(Mandatory=1)][String]$Query,
# Params
[Parameter(Mandatory=0)][Alias('With')][HashTable]$Params
)
# If Params
if($Params.count){$Params.keys|%{$Query=$Query.replace("{$_}","'$($Params.$_)'")}}
# Verbose
Write-Verbose "$Query"
# Clipboard
$Query | Set-ClipBoard
# Return Query
Return $Query
}
#End
###########################################################
################################################ Wald0Index
<#
.Synopsis
Wald0 Index
.DESCRIPTION
Calculate wald0 Index for specified Group
.EXAMPLE
#>
function Wald0Index{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=1,ValueFromPipelineByPropertyName=1,Position=0)][Alias('TargetGroup')][String]$Name = "DOMAIN [email protected]"
)
Begin{
$Result = @()
}
Process{
# TargetFolder
$TargetGroup = $Name
# Split Domain
$TargetDomain = $TargetGroup.split('@')[1]
# Get User Nodes
$UserNode = Node -User | WHERE name -match $TargetDomain
$UserCount = $UserNode.Count
# Prep empty length collection
$UserPath = $UserNode.name | Path -UserToGroup -to $TargetGroup
$UserHopCount = $UserPath.count
$UserPathCount = ($UserPath | where step -eq 0).count
# Calc User avg distance
$UserAvgHop = [Math]::Round($UserHopCount/$UserPathCount,2)
# Calc User Index
$UserIndex = [Math]::Round(100*$UserPathCount/$($UserCount),2)
# Get Computer Nodes and Paths
$ComputerNode = Node -Computer | WHERE name -match $TargetDomain
$ComputerCount = $computerNode.count
# Prep empty length collection
$ComputerPath = $ComputerNode.name | Path -ComputerToGroup -to $TargetGroup
$ComputerHopCount = $ComputerPath.Count
$ComputerPathCount = ($ComputerPath | where step -eq 0).count
# Calc User avg distance
$ComputerAvgHop = [Math]::Round($ComputerHopCount/$ComputerPathCount,2)
# Calc User Index
$ComputerIndex = [Math]::Round(100*$($ComputerPathCount)/$($ComputerCount),2)
# Avg
$AvgHop = [Math]::Round($(($UserAvgHop+$ComputerAvgHop)/2),2)
$AvgIndex = [Math]::Round($(($UserIndex+$ComputerIndex)/2),2)
# Return Wald0 Index Object
$Result += New-Object PSCustomObject -Property @{
Domain = $TargetDomain
TargetG = $TargetGroup
UPath = $UserIndex
UHop = $UserAvgHop
CPath = $ComputerIndex
CHop = $ComputerAvgHop
PathAvg = $AvgIndex
HopAvg = $AvgHop
}}
# Return Result
End{Return $Result | Select Domain,TargetG,UPath,UHop,CPath,CHop,PathAvg,HopAvg}
}
#End
function Wald0Index{
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline=1,ValueFromPipelineByPropertyName=1,Position=0)][Alias('TargetGroup')][String]$Name = "DOMAIN [email protected]"
)
Begin{
$Result = @()
}
Process{
# TargetFolder
$TargetGroup = $Name
# Split Domain
$TargetDomain = $TargetGroup.split('@')[1]
# Prep Query
$Wald0Index = "
MATCH
(totalUsers:User {domain:'$TargetDomain'}),
p = shortestPath((pathToDAUsers:User {domain:'$TargetDomain'})-[r*1..]->(g:Group {name:'$TargetGroup'}))
WITH
g.name as target,
AVG(LENGTH(p)) as avgPathLength,
COUNT(DISTINCT(totalUsers)) as totalUsers,
COUNT(DISTINCT(pathToDAUsers)) as pathToDAUsers
WITH
100.0 * pathToDAUsers / totalUsers AS percentUsersToDA,
totalUsers, pathToDAUsers, avgPathLength,target
RETURN {
TargetGroup: target,
TotalUserCount: totalUsers,
UserPathCount: pathToDAUsers,
UserPathPercent: percentUsersToDA,
AvgPathLength: avgPathLength
} as result
"
# Call
$Result += try{CypherISEr $Wald0Index}catch{}
}
# Return Result
End{Return $Result}
}
#End
#endregion ################################################
###########################################################
#region ######################################### Externals
## EXTERNALS ##############################################
#
# BloodHound-Node > View Node by Name
# BloodHound-NodeSearch > Search Node by Name/Prop/Value
# BloodHound-NodeCreate > Create Node & Node Props
# BloodHound-NodeUpdate > Update Node Props
# BloodHound-NodeDelete > Delete Node by Name
# BloodHound-Edge > View Nodes per Edge <-------------------- /!\ Need BugCheck
# BloodHound-EdgeReverse > View Nodes per Reverse Edge <-------------------- /!\ Need BugFix
# BloodHound-EdgeCustom > View Nodes per Custom Edge <-------------------- /!\ Need BugFix
# BloodHound-EdgeCreate > Create Edges between Nodes
# BloodHound-EdgeDelete > Delete Edges between Nodes
# BloodHound-Path > View Shotrtest Path
# BloodHound-PathVia > View Shortest Path Via
# BloodHound-PathCypher > Cypher Query Generator
#
###########################################################
###########################################################
########################################### BloodHound-Node
<#
.Synopsis
Get BloodHound Node
.DESCRIPTION
View BloodHound Node (by type) by name
Returns all Nodes of specified type if name ommitted
Queries all Node Types if type ommitted
.EXAMPLE
Node -User [email protected]
Returns specified Nodes
.EXAMPLE
Node -User
Returns all User Nodes
.EXAMPLE
Node
Returns all Nodes (All Types)
.EXAMPLE
Node [email protected]
Query by Name without Node Type
Same as example1 but less eco-friendly
.EXAMPLE
'bob','alice' | Node -User | ft
Accepts Multiple Nodes over pipeline
#>
function BloodHound-Node{
[CmdletBinding(DefaultParameterSetName='Any')]
[Alias('Node')]
Param(
# Any Node Type (Default)
[Parameter(Mandatory=0,ParameterSetName='Any')][Switch]$Any,
# User Node Type
[Parameter(Mandatory=1,ParameterSetName='User')][Switch]$User,
# Group Node Type
[Parameter(Mandatory=1,ParameterSetName='Group')][Switch]$Group,
# Computer Node Type
[Parameter(Mandatory=1,ParameterSetName='Computer')][Switch]$Computer,
# Domain Node Type
[Parameter(Mandatory=1,ParameterSetName='Domain')][Switch]$Domain
)
DynamicParam{
# Prep Dico
$Dico = New-Object Management.Automation.RuntimeDefinedParameterDictionary
# Prep DynNamelist
Switch($PSCmdlet.ParameterSetName){
'Any' {$DynNameList += $Script:CypherDog.UserList+$Script:CypherDog.GroupList+$Script:CypherDog.ComputerList+$Script:CypherDog.DomainList}
'User' {$DynNameList += $Script:CypherDog.UserList}
'Group' {$DynNameList += $Script:CypherDog.GroupList}
'Computer'{$DynNameList += $Script:CypherDog.ComputerList}
'Domain' {$DynNameList += $Script:CypherDog.DomainList}
}
# Prep DynP
$DynName = DynP -Name 'Name' -Type 'String' -Mandat 0 -Pos 0 -Pipe 1 -PipeProp 1 -VSet $DynNameList
$DynCypher = DynP -Name 'Cypher' -Type 'Switch' -Mandat 0 -Pos 1 -Pipe 0 -PipeProp 0 -VSet $Null
# DynP to Dico
$Dico.Add("Name",$DynName)
$Dico.Add("Cypher",$DynCypher)
# Return Dico
Return $Dico
}
Begin{
# Prep Empty result
$Result = @()
# Prep NodeType
Switch($PScmdlet.ParameterSetName){
'Any' {$NodeType=$Null}
Default{$NodeType=":$($PSCmdlet.ParameterSetName)"}
}}
Process{
# If All Nodes (no INPUT) > Get All
if(-Not$DynName.IsSet){
# Prep Query
$Query = "MATCH (x$NodeType) RETURN x"
$Params = $Null
}
# Else (INPUT) > Get Specific
Else{
# Prep Query
$Query = "MATCH (x$NodeType) WHERE x.name={INPUT} RETURN x"
$Params = @{INPUT="$($DynName.Value)"}
}
# Call Dog
if(-Not$DynCypher.IsSet){$Result += DogPost $Query -Params $Params}
}
End{
# If Cypher
if($DynCypher.IsSet){ClipThis $Query -with $Params}
# Return Result
else{Return $Result | Sort name}
}}
#####End
###########################################################
##################################### BloodHound-NodeSearch
<#
.Synopsis
Search Node
.DESCRIPTION
Search Node by Partial name, Property (yes/no), Property Value
Search by Keyword is not case sensitive
Property names/values are case sensitive
.EXAMPLE
NodeSearch -User acHA
Search by partial Name (case insensitive)
.EXAMPLE
NodeSearch -User -Property myProp
Search by property (property exists)
.EXAMPLE
NodeSearch -Computer -Property myProp -NotExists
Search by property not exists
.EXAMPLE
NodeSearch -Computer -Property myProp -Value thisValue
Search by property value
#>
function BloodHound-NodeSearch{
[CmdletBinding()]
[Alias('NodeSearch')]
Param(
# Search User
[Parameter(Mandatory=1,ParameterSetName='UserByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='UserByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='UserByProperty')]
[Parameter(Mandatory=1,ParameterSetName='UserByPartialName')][Switch]$User,
# Search Group
[Parameter(Mandatory=1,ParameterSetName='GroupByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='GroupByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='GroupByProperty')]
[Parameter(Mandatory=1,ParameterSetName='GroupByPartialName')][Switch]$Group,
# Search Computer
[Parameter(Mandatory=1,ParameterSetName='ComputerByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByProperty')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByPartialName')][Switch]$Computer,
# Search Domain
[Parameter(Mandatory=1,ParameterSetName='DomainByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='DomainByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='DomainByProperty')]
[Parameter(Mandatory=1,ParameterSetName='DomainByPartialName')][Switch]$Domain,
# Search By Keyword (Partial name)
[Parameter(Mandatory=1,Position=0,ValueFromPipeline=1,ParameterSetName='UserByPartialName')]
[Parameter(Mandatory=1,Position=0,ValueFromPipeline=1,ParameterSetName='GroupByPartialName')]
[Parameter(Mandatory=1,Position=0,ValueFromPipeline=1,ParameterSetName='ComputerByPartialName')]
[Parameter(Mandatory=1,Position=0,ValueFromPipeline=1,ParameterSetName='DomainByPartialName')][String]$Key,
# Search By Property (exists)
[Parameter(Mandatory=1,ParameterSetName='UserByProperty')]
[Parameter(Mandatory=1,ParameterSetName='GroupByProperty')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByProperty')]
[Parameter(Mandatory=1,ParameterSetName='DomainByProperty')]
[Parameter(Mandatory=1,ParameterSetName='UserByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='GroupByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='DomainByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='UserByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='GroupByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='DomainByPropertyValue')][String]$Property,
# Search By Property (exists)
[Parameter(Mandatory=1,ParameterSetName='UserByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='GroupByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByPropertyNot')]
[Parameter(Mandatory=1,ParameterSetName='DomainByPropertyNot')][Switch]$NotExist,
# Search By Property Value
[Parameter(Mandatory=1,ParameterSetName='UserByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='GroupByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='ComputerByPropertyValue')]
[Parameter(Mandatory=1,ParameterSetName='DomainByPropertyValue')][String]$Value,
# Clip
[Parameter(Mandatory=0)][Switch]$Cypher
)
Begin{
# Prep Vars
$Result=@()
$PSN = $PSCmdlet.ParameterSetName
$NodeType = $PSN.replace('By','*').split('*')[0]
# Prep Query
Switch -Regex ($PSN){
"Name$" {$Query = "MATCH (X:$NodeType) WHERE X.name =~ {REGEX} RETURN X"}
"Property$"{$Query = "MATCH (X:$NodeType) WHERE exists(X.$Property) RETURN X"}
"Not$" {$Query = "MATCH (X:$NodeType) WHERE NOT exists(X.$Property) RETURN X"}
"Value$" {$Query = "MATCH (X:$NodeType) WHERE X.$Property = {VALUE} RETURN X"}
}}
Process{
# Prep Params
Switch -Regex ($PSN){
"Name$" {$Params = @{REGEX="(?i).*$Key.*"}}
"Value$" {$Params = @{VALUE="$Value"}}
Default {$Params = $Null}
}
# Call Dog
$Result += DogPost $Query -Params $Params
}
# Return Result
End{
# If Cypher
if($Cypher){ClipThis $Query -with $Params}
# Else Result
else{Return $Result | Sort name}
}}
#####End
###########################################################
##################################### BloodHound-NodeCreate
<#
.Synopsis
Create Nodes
.DESCRIPTION
Add Nodes to DB
Must specify Type and name
Can also set porperties (via HashTable)
.EXAMPLE
NodeCreate -User -Name bob
.EXAMPLE
NodeCreate -User bob -Properties @{Age=23;City='London'}
.EXAMPLE
'bob','john' | NodeCreate -User
Accepts multiple Names over pipeline
#>
function BloodHound-NodeCreate{
[CmdletBinding()]
[Alias('NodeCreate')]
Param(
# Create User Node
[Parameter(Mandatory=1,ParameterSetName='User')][Switch]$User,
# Create Group Node
[Parameter(Mandatory=1,ParameterSetName='Group')][Switch]$Group,
# Create Computer Node
[Parameter(Mandatory=1,ParameterSetName='Computer')][Switch]$Computer,
# Create Domain Node
[Parameter(Mandatory=1,ParameterSetName='Domain')][Switch]$Domain,
# Specify Name
[Parameter(Mandatory=1,Position=0,ValueFromPipeline=1)][String]$Name,
# Set Node Properties (optional)
[Parameter(Mandatory=0,Position=1)][Hashtable]$Properties,
# Cypher
[Parameter(Mandatory=0)][Switch]$Cypher
)
Begin{
# Prep Vars
$NodeType=$PSCmdlet.ParameterSetName
$Query = "MERGE (X:$NodeType {name: {NAME}})"
}
Process{
# Set Param
$Params = @{NAME="$Name"}
# if Not Cypher
if(-Not$Cypher){
# Call the Dog
DogPost $Query -Params $Params
# Refresh Cache
CacheNode $NodeType
# Update Props
if($Properties){
$Splat = @{
$NodeType = $true
Name = $Name
Properties = $Properties
}
NodeUpdate @Splat -verbose
}}
# Else Clip
Else{ClipThis $Query -with $Params}
}
# Return Nothing
End{<#NoOut#>}
}
#End
###########################################################
##################################### BloodHound-NodeUpdate
<#
.Synopsis
Update Node Props
.DESCRIPTION
Set/Update/Delete Node Properties
.EXAMPLE
NodeUpdate -User -Name bob -Properties @{Age=21;color='Blue'}
Update existing prop to new value
Create missing props
.EXAMPLE
NodeUpdate -User -Name bob -Delete -PropertyName City,Color
Delete Node specified Properties
#>
function BloodHound-NodeUpdate{
[CmdletBinding()]
[Alias('NodeUpdate')]
Param(
# Update User Node
[Parameter(Mandatory=1,ParameterSetName='DeletePropUser')]
[Parameter(Mandatory=1,ParameterSetName='User')][Switch]$User,
# Update Group Node
[Parameter(Mandatory=1,ParameterSetName='DeletePropGroup')]
[Parameter(Mandatory=1,ParameterSetName='Group')][Switch]$Group,
# Update Computer Node
[Parameter(Mandatory=1,ParameterSetName='DeletePropComputer')]
[Parameter(Mandatory=1,ParameterSetName='Computer')][Switch]$Computer,
# Update Domain Node
[Parameter(Mandatory=1,ParameterSetName='DeletePropDomain')]
[Parameter(Mandatory=1,ParameterSetName='Domain')][Switch]$Domain,
# Delete Node Properties
[Parameter(Mandatory=1,ParameterSetName='DeletePropDomain')]
[Parameter(Mandatory=1,ParameterSetName='DeletePropComputer')]
[Parameter(Mandatory=1,ParameterSetName='DeletePropGroup')]
[Parameter(Mandatory=1,ParameterSetName='DeletePropUser')][Switch]$Delete,
# Cypher
[Parameter(Mandatory=0)][Switch]$Cypher
)
DynamicParam{
# Prep Dico
$Dico = New-Object Management.Automation.RuntimeDefinedParameterDictionary
# Prep DynNamelist
Switch($PSCmdlet.ParameterSetName){
'User' {$DynNameList += $Script:CypherDog.UserList}
'Group' {$DynNameList += $Script:CypherDog.GroupList}
'Computer'{$DynNameList += $Script:CypherDog.ComputerList}
'Domain' {$DynNameList += $Script:CypherDog.DomainList}
}
# Prep DynP
$DynName = DynP -Name 'Name' -Type 'String' -Mandat 1 -Pos 0 -Pipe 1 -PipeProp 1 -VSet $DynNameList
if($PSCmdlet.ParameterSetName -match 'Delete'){
$DynProps = DynP -Name 'PropertyName' -Type 'String[]'-Mandat 1 -Pos 1 -Pipe 0 -PipeProp 0 -VSet $Null
$Dico.Add('PropertyName',$DynProps)
}
Else{$DynProps = DynP -Name 'Properties' -Type 'HashTable'-Mandat 1 -Pos $Null -Pipe 0 -PipeProp 0 -VSet $Null
$Dico.Add('Properties',$DynProps)
}
# DynP to Dico
$Dico.Add('Name',$DynName)
# Return Dico
Return $Dico
}
Begin{
# Set var
$NodeType = $PSCmdlet.ParameterSetName.replace('DeleteProp','')
}
Process{
# Prep Query
if($Delete){$Query="MATCH (X:$NodeType) WHERE X.name = {NAME} REMOVE"}
else{$Query = "MATCH (X:$NodeType) WHERE X.name = {NAME} SET"}
# Add Name to Params
$Params = @{NAME="$($DynName.Value)"}
# If Delete props
if($Delete){
$DynProps.Value|%{$Query += " X.$_,"}
$Query=$Query.TrimEnd(',')
}
# If Update Props
else{
# For each Prop
$DynProps.Value.Keys|%{
# Append to Query
$Query+=" X.$_={$_},"
# Add to Param
$Params += @{$_="$($DynProps.Value.$_)"}
}
$Query=$Query.trimEnd(',')
}
# Call Dog
if(-Not$Cypher){DogPost $Query -Params $Params}
# or clip
else{ClipThis $Query -with $Params}
}
# Return Nothing
End{<#NoOut#>}
}
#End
###########################################################
##################################### BloodHound-NodeDelete
<#
.Synopsis
Delete Nodes
.DESCRIPTION
Delete Nodes from DB
.EXAMPLE
NodeDelete -User bob
Delete specified Node
.EXAMPLE
'bob','john' | NodeDelete -User
Accepts multiple Names over pipeline
#>
function BloodHound-NodeDelete{
[CmdletBinding()]
[Alias('NodeDelete')]
Param(
# Delete User Node
[Parameter(Mandatory=1,ParameterSetName='User')][Switch]$User,
# Delete Group Node
[Parameter(Mandatory=1,ParameterSetName='Group')][Switch]$Group,
# Delete Computer Node
[Parameter(Mandatory=1,ParameterSetName='Computer')][Switch]$Computer,
# Delete Domain Node
[Parameter(Mandatory=1,ParameterSetName='Domain')][Switch]$Domain
)
DynamicParam{
# Prep Dico
$Dico = New-Object Management.Automation.RuntimeDefinedParameterDictionary
# Prep DynNamelist
Switch($PSCmdlet.ParameterSetName){
'User' {$DynNameList += $Script:CypherDog.UserList}
'Group' {$DynNameList += $Script:CypherDog.GroupList}
'Computer'{$DynNameList += $Script:CypherDog.ComputerList}
'Domain' {$DynNameList += $Script:CypherDog.DomainList}
}
# Prep DynP
$DynName = DynP -Name 'Name' -Type 'String' -Mandat 1 -Pos 0 -Pipe 1 -PipeProp 1 -VSet $DynNameList
$DynCypher = DynP -Name 'Cypher' -Type 'Switch' -Mandat 0 -Pos 1 -Pipe 0 -PipeProp 0 -VSet $Null
# DynP to Dico
$Dico.Add('Name',$DynName)
$Dico.Add('Cypher',$DynCypher)
# Return Dico
Return $Dico
}
Begin{
# Prep Query
$NodeType = $PSCmdlet.ParameterSetname
$Query = "MATCH (X:$NodeType {name: {NAME}}) DETACH DELETE X"
}
Process{
# Set Params
$Params = @{NAME="$($DynName.Value)"}
if($DynCypher.IsSet){ClipThis $Query -with $Params}
else{
# Call Dog
DogPost $Query -Params $Params
# Cache Nodes
CacheNode $NodeType
}}
# Return nothing
End{<#NoOut#>}
}
#End
###########################################################
########################################### BloodHound-Edge
<#
.Synopsis
View Nodes per Edge
.DESCRIPTION
View Nodes of specifed type with specified
relationship to specified target
.EXAMPLE
Edge -MemberOfGroup [email protected] -Return Users
.EXAMPLE
Edge -MemberOfGroup [email protected] -Return Users -Degree *
Specify Membership degree [*=all]
.EXAMPLE
## Pipeline Combos ##
Edge -AdminToComputer NYX.EXTERNAL.LOCAL -Return Groups |
Edge -MemberOfGroup -Return Users -Degree * |
Edge -SessionFromUser -Return Computers
List all Computers with session from any Users AdminTo specified target Computer
#>
function BloodHound-Edge{
[CmdletBinding()]
[Alias('Edge')]
param(
# MemberOfGroup (Input: G - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='MemberOfGroup')][Switch]$MemberOfGroup,
# AdminToComputer (Input: C - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='AdminToComputer')][Switch]$AdminToComputer,
# SessionFromUser (Input: U - Return: C)
[Parameter(Mandatory=$True,ParameterSetname='SessionFromUser')][Switch]$SessionFromUser,
# SetPasswordOfUser (Input: U - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='SetPasswordOfUser')][Switch]$SetPasswordOfUser,
# AddMemberToGroup (Input: G - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='AddMemberToGroup')][Switch]$AddMemberToGroup,
# AllExtendedOnUser (Input: U - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='AllExtendedOnUser')][Switch]$AllExtendedOnUser,
# AllExtendedOnGroup (Input: G - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='AllExtendedOnGroup')][Switch]$AllExtendedOnGroup,
# AllGenericOnUser (Input: U - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='AllGenericOnUser')][Switch]$AllGenericOnUser,
# AllGenericOnGroup (Input: G - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='AllGenericOnGroup')][Switch]$AllGenericOnGroup,
# WriteGenericOnUser (Input: U - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='WriteGenericOnUser')][Switch]$WriteGenericOnUser,
# WriteGenericOnGroup (Input: G - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='WriteGenericOnGroup')][Switch]$WriteGenericOnGroup,
# WriteOwnerOnUser (Input: U - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='WriteOwnerOnUser')][Switch]$WriteOwnerOnUser,
# WriteOwnerOnGroup (Input: G - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='WriteOwnerOnGroup')][Switch]$WriteOwnerOnGroup,
# WriteDACLOnUser (Input: U - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='WriteDACLOnUser')][Switch]$WriteDACLOnUser,
# WriteDACLOnGroup (Input: G - Return: U|G|C)
[Parameter(Mandatory=$True,ParameterSetname='WriteDACLOnGroup')][Switch]$WriteDACLOnGroup,
# TrustedByDomain (Input: D - Return: D)
[Parameter(Mandatory=$True,ParameterSetname='TrustedByDomain')][Switch]$TrustedByDomain
)
DynamicParam{
# Select ValidateSets for Dynamic Parameters
Switch($PSCmdlet.ParameterSetName){
# PSN | NAME | TARGET |
#-------------------------------------------------------------------------------------------------|
'MemberOfGroup' {$VSet = $Script:CypherDog.GroupList; $VRet = 'Users','Groups','Computers'}
'AdminToComputer' {$VSet = $Script:CypherDog.ComputerList; $VRet = 'Users','Groups','Computers'}
'SessionFromUser' {$VSet = $Script:CypherDog.UserList; $VRet = 'Computers' }
'SetPasswordOfUser' {$VSet = $Script:CypherDog.UserList; $VRet = 'Users','Groups','Computers'}
'AddMemberToGroup' {$VSet = $Script:CypherDog.GroupList; $VRet = 'Users','Groups','Computers'}
'AllExtendedOnUser' {$VSet = $Script:CypherDog.UserList; $VRet = 'Users','Groups','Computers'}
'AllExtendedOnGroup' {$VSet = $Script:CypherDog.GroupList; $VRet = 'Users','Groups','Computers'}
'AllGenericOnUser' {$VSet = $Script:CypherDog.UserList; $VRet = 'Users','Groups','Computers'}
'AllGenericOnGroup' {$VSet = $Script:CypherDog.GroupList; $VRet = 'Users','Groups','Computers'}
'WriteGenericOnUser' {$VSet = $Script:CypherDog.UserList; $VRet = 'Users','Groups','Computers'}
'WriteGenericOnGroup'{$VSet = $Script:CypherDog.GroupList; $VRet = 'Users','Groups','Computers'}
'WriteOwnerOnUser' {$VSet = $Script:CypherDog.UserList; $VRet = 'Users','Groups','Computers'}
'WriteOwnerOnGroup' {$VSet = $Script:CypherDog.GroupList; $VRet = 'Users','Groups','Computers'}
'WriteDACLOnUser' {$VSet = $Script:CypherDog.UserList; $VRet = 'Users','Groups','Computers'}
'WriteDACLOnGroup' {$VSet = $Script:CypherDog.GroupList; $VRet = 'Users','Groups','Computers'}
'TrustedByDomain' {$VSet = $Script:CypherDog.DomainList; $VRet = 'Domains' }
}
# Dico
$Dico = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# DynName
$DynName = DynP -Name 'Name' -Type string -Mandat 1 -Pos 0 -Pipe 1 -PipeProp 1 -VSet $VSet
$Dico.Add('Name',$DynName)
# DynReturn (ex DynParam2)
$DynReturn = DynP -Name 'Return' -Type String -Mandat 1 -Pos 2 -Pipe 0 -PipeProp 0 -VSet $VRet
$Dico.add('Return',$DynReturn)
# if MemberOf
If($PSCmdlet.ParameterSetName -match "MemberOf"){
## Max Degree (ex DynParam3)
# Attribute
$Attrib = New-Object System.Management.Automation.ParameterAttribute
$Attrib.Mandatory = $false
# AttributeCollection
$Collection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
# Add our custom attribute
$Collection.Add($Attrib)
# Validate Pattern
$VPat=new-object System.Management.Automation.ValidatePatternAttribute('^\d$|\*')
$Collection.Add($VPat)
# Create Runtime Parameter with matching attribute collection
$DynMax = New-Object System.Management.Automation.RuntimeDefinedParameter('Degree', [String], $Collection)
# Add to dico
$Dico.Add('Degree',$dynMax)
}
# DynCypher
$DynCypher = DynP -Name 'Cypher' -Type Switch -Mandat 0 -Pos 3 -Pipe 0 -PipeProp 0 -VSet $null
$Dico.add('Cypher',$DynCypher)
#return Dico
return $Dico
}
Begin{
# Fix DynMax
if($PScmdlet.ParameterSetName -match 'MemberOf' -AND -Not$DynMax.IsSet){$DynMax.Value=1}
if($PScmdlet.ParameterSetName -match 'MemberOf' -AND $DynMax.Value -eq '*'){$DynMax.Value=$Null}
# Populate Query vars
Switch($PSCmdlet.ParameterSetName){