forked from BloodHoundAD/BloodHound-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloodhoundanalytics.py
1255 lines (1039 loc) · 40.9 KB
/
bloodhoundanalytics.py
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
from openpyxl import Workbook, styles
from openpyxl.utils import get_column_letter
from neo4j import GraphDatabase
import cmd
import sys
from timeit import default_timer as timer
# Authors:
# Andy Robbins - @_wald0
# Rohan Vazarkar - @CptJesus
# https://www.specterops.io
# License: GPLv3
class Messages(object):
def title(self):
print("================================================================")
print(" BloodHound Analytics Generator")
print(" connect - connect to database")
print(" dbconfig - configure database settings")
print(" changedomain - change domain for analysis")
print(" startanalysis - start analysis for specified domain")
print(" changefilename - change output filename")
print("================================================================")
def input_default(self, prompt, default):
return raw_input("%s [%s] " % (prompt, default)) or default
class FrontPage(object):
def __init__(self, driver, domain, workbook):
self.driver = driver
self.domain = domain
self.col_count = 1
self.workbook = workbook
def write_single_cell(self, sheet, row, column, text):
sheet.cell(row, column, value=text)
def do_front_page_analysis(self):
func_list = [self.create_node_statistics,
self.create_edge_statistics, self.create_qa_statistics]
sheet = self.workbook._sheets[0]
self.write_single_cell(sheet, 1, 1, "Node Statistics")
self.write_single_cell(sheet, 1, 2, "Edge Statistics")
self.write_single_cell(sheet, 1, 3, "QA Info")
font = styles.Font(bold=True)
sheet.cell(1, 1).font = font
sheet.cell(1, 2).font = font
sheet.cell(1, 3).font = font
for f in func_list:
s = timer()
f(sheet)
print("{} completed in {}s".format(f.__name__, timer() - s))
def create_node_statistics(self, sheet):
session = self.driver.session()
for result in session.run("MATCH (n:User {domain:$domain}) RETURN count(n)", domain=self.domain):
self.write_single_cell(
sheet, 2, 1, "Users: {:,}".format(result[0]))
for result in session.run("MATCH (n:Group {domain:$domain}) RETURN count(n)", domain=self.domain):
self.write_single_cell(
sheet, 3, 1, "Groups: {:,}".format(result[0]))
for result in session.run("MATCH (n:Computer {domain:$domain}) RETURN count(n)", domain=self.domain):
self.write_single_cell(
sheet, 4, 1, "Computers: {:,}".format(result[0]))
for result in session.run("MATCH (n:Domain) RETURN count(n)", domain=self.domain):
self.write_single_cell(
sheet, 5, 1, "Other Domains: {:,}".format(result[0]-1))
for result in session.run("MATCH (n:GPO) WHERE n.name =~ '.*"+ self.domain +"$' RETURN count(n)"):
self.write_single_cell(
sheet, 6, 1, "GPOs: {:,}".format(result[0]))
for result in session.run("MATCH (n:OU) WHERE n.name =~ '.*@"+ self.domain +"$' RETURN count(n)"):
self.write_single_cell(
sheet, 7, 1, "OUs: {:,}".format(result[0]))
session.close()
def create_edge_statistics(self, sheet):
session = self.driver.session()
for result in session.run("MATCH ()-[r:MemberOf]->({domain:$domain}) RETURN count(r)", domain=self.domain):
self.write_single_cell(
sheet, 2, 2, "MemberOf: {:,}".format(result[0]))
for result in session.run("MATCH ()-[r:AdminTo]->({domain:$domain}) RETURN count(r)", domain=self.domain):
self.write_single_cell(
sheet, 3, 2, "AdminTo: {:,}".format(result[0]))
for result in session.run("MATCH ()-[r:HasSession]->({domain:$domain}) RETURN count(r)", domain=self.domain):
self.write_single_cell(
sheet, 4, 2, "HasSession: {:,}".format(result[0]))
for result in session.run("MATCH ()-[r:GpLink]-(n) WHERE n.name =~ '.*"+ self.domain +"$' RETURN count(r)"):
self.write_single_cell(
sheet, 5, 2, "GpLinks: {:,}".format(result[0]))
for result in session.run("MATCH ()-[r {isacl:true}]->({domain:$domain}) RETURN count(r)", domain=self.domain):
self.write_single_cell(
sheet, 6, 2, "ACLs: {:,}".format(result[0]))
session.close()
def create_qa_statistics(self, sheet):
session = self.driver.session()
computer_local_admin_pct = 0
computer_session_pct = 0
user_session_pct = 0
query = """MATCH (n)-[:AdminTo]->(c:Computer {domain:$domain})
WITH COUNT(DISTINCT(c)) as computersWithAdminsCount
MATCH (c2:Computer {domain:$domain})
RETURN toInteger(100 * (toFloat(computersWithAdminsCount) / COUNT(c2)))
"""
for result in session.run(query, domain=self.domain):
computer_local_admin_pct = result[0]
query = """MATCH (c:Computer {domain:$domain})-[:HasSession]->()
WITH COUNT(DISTINCT(c)) as computersWithSessions
MATCH (c2:Computer {domain:$domain})
RETURN toInteger(100 * (toFloat(computersWithSessions) / COUNT(c2)))
"""
for result in session.run(query, domain=self.domain):
computer_session_pct = result[0]
query = """MATCH ()-[:HasSession]->(u:User {domain:$domain})
WITH COUNT(DISTINCT(u)) as usersWithSessions
MATCH (u2:User {domain:$domain,enabled:true})
RETURN toInteger(100 * (toFloat(usersWithSessions) / COUNT(u2)))
"""
for result in session.run(query, domain=self.domain):
user_session_pct = result[0]
session.close()
self.write_single_cell(sheet, 2, 3, "Computers With Local Admin Data: {}%".format(computer_local_admin_pct))
self.write_single_cell(sheet, 3, 3, "Computers With Session Data: {}%".format(computer_session_pct))
self.write_single_cell(sheet, 4, 3, "Users With Session Data: {}%".format(user_session_pct))
class LowHangingFruit(object):
def __init__(self, driver, domain, workbook):
self.driver = driver
self.domain = domain
self.col_count = 1
self.workbook = workbook
def write_column_data(self, sheet, title, results):
count = len(results)
offset = 6
font = styles.Font(bold=True)
c = sheet.cell(offset, self.col_count)
c.font = font
sheet.cell(offset, self.col_count, value=title.format(count))
for i in range(0, count):
sheet.cell(i+offset+1, self.col_count, value=results[i])
self.col_count += 1
def write_single_cell(self, sheet, row, column, text):
sheet.cell(row, column, value=text)
def do_low_hanging_fruit_analysis(self):
func_list = [
self.domain_user_admin, self.everyone_admin, self.authenticated_users_admin,
self.domain_users_control, self.everyone_control, self.authenticated_users_control,
self.domain_users_rdp, self.everyone_rdp, self.authenticated_users_dcom,
self.domain_users_dcom, self.everyone_dcom, self.authenticated_users_dcom,
self.shortest_acl_path_domain_users, self.shortest_derivative_path_domain_users, self.shortest_hybrid_path_domain_users,
self.shortest_acl_path_everyone, self.shortest_derivative_path_everyone, self.shortest_hybrid_path_everyone,
self.shortest_acl_path_auth_users, self.shortest_derivative_path_auth_users, self.shortest_hybrid_path_auth_users,
self.kerberoastable_path_len, self.asreproastable_path_len, self.high_admin_comps
]
sheet = self.workbook._sheets[2]
self.write_single_cell(sheet, 1, 1, "Domain Users to Domain Admins")
self.write_single_cell(sheet, 1, 2, "Everyone to Domain Admins")
self.write_single_cell(
sheet, 1, 3, "Authenticated Users to Domain Admins")
font = styles.Font(bold=True)
sheet.cell(1, 1).font = font
sheet.cell(1, 2).font = font
sheet.cell(1, 3).font = font
for f in func_list:
s = timer()
f(sheet)
print("{} completed in {}s".format(f.__name__, timer() - s))
def domain_user_admin(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-513"
OPTIONAL MATCH (g)-[:AdminTo]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:AdminTo]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Users with Local Admin: {}", results)
def everyone_admin(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = "S-1-1-0"
OPTIONAL MATCH (g)-[:AdminTo]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:AdminTo]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(sheet, "Everyone with Local Admin: {}", results)
def authenticated_users_admin(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = "S-1-5-11"
OPTIONAL MATCH (g)-[:AdminTo]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:AdminTo]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Authenticated Users with Local Admin: {}", results)
def domain_users_control(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-513"
OPTIONAL MATCH (g)-[{isacl:true}]->(n)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(m)
WITH COLLECT(n) + COLLECT(m) as tempVar
UNWIND tempVar AS objects
RETURN DISTINCT(objects)
ORDER BY objects.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Objects Controlled by Domain Users: {}", results)
def everyone_control(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = 'S-1-1-0'
OPTIONAL MATCH (g)-[{isacl:true}]->(n)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(m)
WITH COLLECT(n) + COLLECT(m) as tempVar
UNWIND tempVar AS objects
RETURN DISTINCT(objects)
ORDER BY objects.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Objects Controlled by Everyone: {}", results)
def authenticated_users_control(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = 'S-1-5-11'
OPTIONAL MATCH (g)-[{isacl:true}]->(n)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(m)
WITH COLLECT(n) + COLLECT(m) as tempVar
UNWIND tempVar AS objects
RETURN DISTINCT(objects)
ORDER BY objects.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Objects Controlled by Authenticated Users: {}", results)
def domain_users_rdp(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-513"
OPTIONAL MATCH (g)-[:CanRDP]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:CanRDP]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Users with RDP Rights: {}", results)
def everyone_rdp(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = "S-1-1-0"
OPTIONAL MATCH (g)-[:CanRDP]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:CanRDP]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(sheet, "Everyone with RDP Rights: {}", results)
def authenticated_users_rdp(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = "S-1-5-11"
OPTIONAL MATCH (g)-[:CanRDP]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:CanRDP]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Authenticated Users with RDP Rights: {}", results)
def domain_users_dcom(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-513"
OPTIONAL MATCH (g)-[:ExecuteDCOM]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:ExecuteDCOM]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Users with DCOM Rights: {}", results)
def everyone_dcom(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = "S-1-1-0"
OPTIONAL MATCH (g)-[:ExecuteDCOM]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:ExecuteDCOM]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Users with DCOM Rights: {}", results)
def authenticated_users_dcom(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid = "S-1-5-11"
OPTIONAL MATCH (g)-[:ExecuteDCOM]->(c1)
OPTIONAL MATCH (g)-[:MemberOf*1..]->(:Group)-[:ExecuteDCOM]->(c2)
WITH COLLECT(c1) + COLLECT(c2) as tempVar
UNWIND tempVar AS computers
RETURN DISTINCT(computers.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Users with DCOM Rights: {}", results)
def shortest_acl_path_domain_users(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid ENDS WITH "-513"
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[:Owns|AllExtendedRights|ForceChangePassword|GenericAll|GenericWrite|WriteDacl|WriteOwner*1..]->(g2))
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 2, 1, "Shortest ACL Path Length: {}".format(count))
def shortest_derivative_path_domain_users(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid ENDS WITH "-513"
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[:AdminTo|HasSession|MemberOf*1..]->(g2))
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 3, 1, "Shortest Derivative Path Length: {}".format(count))
def shortest_hybrid_path_domain_users(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid ENDS WITH "-513"
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[r*1..]->(g2))
WHERE NONE(rel in r WHERE type(rel)="GetChanges")
WITH *
WHERE NONE(rel in r WHERE type(rel)="GetChangesAll")
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 4, 1, "Shortest Hybrid Path Length: {}".format(count))
def shortest_acl_path_everyone(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid = 'S-1-1-0'
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[:Owns|AllExtendedRights|ForceChangePassword|GenericAll|GenericWrite|WriteDacl|WriteOwner*1..]->(g2))
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 2, 2, "Shortest ACL Path Length: {}".format(count))
def shortest_derivative_path_everyone(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid = 'S-1-1-0'
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[:AdminTo|HasSession|MemberOf*1..]->(g2))
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 3, 2, "Shortest Derivative Path Length: {}".format(count))
def shortest_hybrid_path_everyone(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid = 'S-1-1-0'
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[r*1..]->(g2))
WHERE NONE(rel in r WHERE type(rel)="GetChanges")
WITH *
WHERE NONE(rel in r WHERE type(rel)="GetChangesAll")
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 4, 2, "Shortest Hybrid Path Length: {}".format(count))
def shortest_acl_path_auth_users(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid = 'S-1-5-11'
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[:Owns|AllExtendedRights|ForceChangePassword|GenericAll|GenericWrite|WriteDacl|WriteOwner*1..]->(g2))
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 2, 3, "Shortest ACL Path Length: {}".format(count))
def shortest_derivative_path_auth_users(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid = 'S-1-5-11'
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[:AdminTo|HasSession|MemberOf*1..]->(g2))
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 3, 3, "Shortest Derivative Path Length: {}".format(count))
def shortest_hybrid_path_auth_users(self, sheet):
count_query = """MATCH (g1:Group {domain:$domain})
WHERE g1.objectsid = 'S-1-5-11'
MATCH (g2:Group {domain:$domain})
WHERE g2.objectsid ENDS WITH "-512"
MATCH p = shortestPath((g1)-[r*1..]->(g2))
WHERE NONE(rel in r WHERE type(rel)="GetChanges")
WITH *
WHERE NONE(rel in r WHERE type(rel)="GetChangesAll")
RETURN LENGTH(p)
"""
session = self.driver.session()
count = 0
for result in session.run(count_query, domain=self.domain):
count = result[0]
session.close()
self.write_single_cell(
sheet, 4, 3, "Shortest Hybrid Path Length: {}".format(count))
def kerberoastable_path_len(self, sheet):
list_query = """MATCH (u:User {domain:$domain,hasspn:true})
MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-512" AND NOT u.name STARTS WITH "KRBTGT@"
MATCH p = shortestPath((u)-[*1..]->(g))
RETURN u.name,LENGTH(p)
ORDER BY LENGTH(p) ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(
"{} - {}".format(result[0], result[1]))
session.close()
self.write_column_data(
sheet, "Kerberoastable User to DA Path Length", results)
def asreproastable_path_len(self, sheet):
list_query = """MATCH (u:User {domain:$domain,dontreqpreauth:True})
MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-512" AND NOT u.name STARTS WITH "KRBTGT@"
MATCH p = shortestPath((u)-[*1..]->(g))
RETURN u.name,LENGTH(p)
ORDER BY LENGTH(p) ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(
"{} - {}".format(result[0], result[1]))
session.close()
self.write_column_data(
sheet, "ASReproastable User to DA Path Length", results)
def high_admin_comps(self, sheet):
list_query = """MATCH (c:Computer {domain:$domain})
OPTIONAL MATCH (n)-[:AdminTo]->(c)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[:AdminTo]->(c)
WITH COLLECT(n) + COLLECT(m) as tempVar,c
UNWIND tempVar as admins
RETURN c.name,COUNT(DISTINCT(admins))
ORDER BY COUNT(DISTINCT(admins)) DESC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
count = result[1]
if (count > 1000):
results.append(
"{} - {}".format(result[0], count))
session.close()
self.write_column_data(
sheet, "Computers with > 1000 Admins: {}", results)
class CriticalAssets(object):
def __init__(self, driver, domain, workbook):
self.driver = driver
self.domain = domain
self.col_count = 1
self.workbook = workbook
def write_column_data(self, sheet, title, results):
count = len(results)
offset = 1
font = styles.Font(bold=True)
c = sheet.cell(offset, self.col_count)
c.font = font
sheet.cell(offset, self.col_count, value=title.format(count))
for i in range(0, count):
sheet.cell(i+offset+1, self.col_count, value=results[i])
self.col_count += 1
def do_critical_asset_analysis(self):
func_list = [
self.admins_on_dc, self.rdp_on_dc, self.gpo_on_dc, self.admin_on_exch,
self.rdp_on_exch, self.gpo_on_exch, self.da_controllers, self.da_sessions,
self.gpo_on_da, self.da_equiv_controllers, self.da_equiv_sessions, self.gpo_on_da_equiv]
sheet = self.workbook._sheets[1]
for f in func_list:
s = timer()
f(sheet)
print("{} completed in {}s".format(f.__name__, timer() - s))
def admins_on_dc(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-516"
MATCH (c:Computer)-[:MemberOf*1..]->(g)
OPTIONAL MATCH (n)-[:AdminTo]->(c)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[:AdminTo]->(c)
WHERE (n:User OR n:Computer) AND (m:User OR m:Computer)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 as tempVar2
WITH DISTINCT(tempVar2) as tempVar3
RETURN tempVar3.name
ORDER BY tempVar3.name ASC"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Admins on Domain Controllers: {}", results)
def rdp_on_dc(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-516"
MATCH (c:Computer)-[:MemberOf*1..]->(g)
OPTIONAL MATCH (n)-[:CanRDP]->(c)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[:CanRDP]->(c)
WHERE (n:User OR n:Computer) AND (m:User OR m:Computer)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 as tempVar2
WITH DISTINCT(tempVar2) as tempVar3
RETURN tempVar3.name
ORDER BY tempVar3.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "RDPers on Domain Controllers: {}", results)
def gpo_on_dc(self, sheet):
list_query = """MATCH (g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-516"
MATCH (c:Computer)-[:MemberOf*1..]->(g)
OPTIONAL MATCH p1 = (g1:GPO)-[r1:GpLink {enforced:true}]->(container1)-[r2:Contains*1..]->(c)
OPTIONAL MATCH p2 = (g2:GPO)-[r3:GpLink {enforced:false}]->(container2)-[r4:Contains*1..]->(c)
WHERE NONE (x in NODES(p2) WHERE x.blocksinheritance = true AND x:OU AND NOT (g2)-->(x))
WITH COLLECT(g1) + COLLECT(g2) AS tempVar1
UNWIND tempVar1 as tempVar2
WITH DISTINCT(tempVar2) as GPOs
OPTIONAL MATCH (n)-[{isacl:true}]->(GPOs)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(GPOs)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 as tempVar2
RETURN DISTINCT(tempVar2.name)
ORDER BY tempVar2.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Controller GPO Controllers: {}", results)
def admin_on_exch(self, sheet):
list_query = """MATCH (n:Computer)
UNWIND n.serviceprincipalnames AS spn
MATCH (n) WHERE TOUPPER(spn) CONTAINS "EXCHANGEMDB"
WITH n as c
MATCH (c)-[:MemberOf*1..]->(g:Group {domain:$domain})
WHERE g.name CONTAINS "EXCHANGE"
OPTIONAL MATCH (n)-[:AdminTo]->(c)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[:AdminTo]->(c)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 AS exchangeAdmins
RETURN DISTINCT(exchangeAdmins.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Admins on Exchange Servers: {}", results)
def rdp_on_exch(self, sheet):
list_query = """MATCH (n:Computer)
UNWIND n.serviceprincipalnames AS spn
MATCH (n) WHERE TOUPPER(spn) CONTAINS "EXCHANGEMDB"
WITH n as c
MATCH (c)-[:MemberOf*1..]->(g:Group {domain:$domain})
WHERE g.name CONTAINS "EXCHANGE"
OPTIONAL MATCH (n)-[:CanRDP]->(c)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[:CanRDP]->(c)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 AS exchangeAdmins
RETURN DISTINCT(exchangeAdmins.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "RDPers on Exchange Servers: {}", results)
def gpo_on_exch(self, sheet):
list_query = """MATCH (n:Computer)
UNWIND n.serviceprincipalnames AS spn
MATCH (n) WHERE TOUPPER(spn) CONTAINS "EXCHANGEMDB"
WITH n as c
MATCH (c)-[:MemberOf*1..]->(g:Group {domain:$domain})
WHERE g.name CONTAINS "EXCHANGE"
OPTIONAL MATCH p1 = (g1:GPO)-[r1:GpLink {enforced:true}]->(container1)-[r2:Contains*1..]->(c)
OPTIONAL MATCH p2 = (g2:GPO)-[r3:GpLink {enforced:false}]->(container2)-[r4:Contains*1..]->(c)
WHERE NONE (x in NODES(p2) WHERE x.blocksinheritance = true AND x:OU AND NOT (g2)-->(x))
WITH COLLECT(g1) + COLLECT(g2) AS tempVar1
UNWIND tempVar1 as tempVar2
WITH DISTINCT(tempVar2) as GPOs
OPTIONAL MATCH (n)-[{isacl:true}]->(GPOs)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(GPOs)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 as tempVar2
RETURN DISTINCT(tempVar2.name)
ORDER BY tempVar2.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Exchange Server GPO Controllers: {}", results)
def da_controllers(self, sheet):
list_query = """MATCH (DAUser)-[:MemberOf*1..]->(g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-512"
OPTIONAL MATCH (n)-[{isacl:true}]->(DAUser)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(DAUser)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 AS DAControllers
RETURN DISTINCT(DAControllers.name)
ORDER BY DAControllers.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Admin Controllers: {}", results)
def da_sessions(self, sheet):
list_query = """MATCH (c:Computer)-[:HasSession]->()-[:MemberOf*1..]->(g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-512"
RETURN DISTINCT(c.name)
ORDER BY c.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Computers with DA Sessions: {}", results)
def gpo_on_da(self, sheet):
list_query = """MATCH (DAUser)-[:MemberOf*1..]->(g:Group {domain:$domain})
WHERE g.objectsid ENDS WITH "-512"
OPTIONAL MATCH p1 = (g1:GPO)-[r1:GpLink {enforced:true}]->(container1)-[r2:Contains*1..]->(DAUser)
OPTIONAL MATCH p2 = (g2:GPO)-[r3:GpLink {enforced:false}]->(container2)-[r4:Contains*1..]->(DAUser)
WHERE NONE (x in NODES(p2) WHERE x.blocksinheritance = true AND x:OU AND NOT (g2)-->(x))
WITH COLLECT(g1) + COLLECT(g2) AS tempVar1
UNWIND tempVar1 as tempVar2
WITH DISTINCT(tempVar2) as GPOs
OPTIONAL MATCH (n)-[{isacl:true}]->(GPOs)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(GPOs)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 as tempVar2
RETURN DISTINCT(tempVar2.name)
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "Domain Admin GPO Controllers: {}", results)
def da_equiv_controllers(self, sheet):
list_query = """MATCH (u:User)-[:MemberOf*1..]->(g:Group {domain:$domain,highvalue:true})
OPTIONAL MATCH (n)-[{isacl:true}]->(u)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(u)
WITH COLLECT(n) + COLLECT(m) as tempVar
UNWIND tempVar as highValueControllers
RETURN DISTINCT(highValueControllers.name)
ORDER BY highValueControllers.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "High Value Object Controllers: {}", results)
def da_equiv_sessions(self, sheet):
list_query = """MATCH (c:Computer)-[:HasSession]->(u:User)-[:MemberOf*1..]->(g:Group {domain:$domain,highvalue:true})
RETURN DISTINCT(c.name)
ORDER BY c.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "High Value User Sessions: {}", results)
def gpo_on_da_equiv(self, sheet):
list_query = """MATCH (u:User)-[:MemberOf*1..]->(g:Group {domain:$domain,highvalue:true})
OPTIONAL MATCH p1 = (g1:GPO)-[r1:GpLink {enforced:true}]->(container1)-[r2:Contains*1..]->(u)
OPTIONAL MATCH p2 = (g2:GPO)-[r3:GpLink {enforced:false}]->(container2)-[r4:Contains*1..]->(u)
WHERE NONE (x in NODES(p2) WHERE x.blocksinheritance = true AND x:OU AND NOT (g2)-->(x))
WITH COLLECT(g1) + COLLECT(g2) AS tempVar1
UNWIND tempVar1 as tempVar2
WITH DISTINCT(tempVar2) as GPOs
OPTIONAL MATCH (n)-[{isacl:true}]->(GPOs)
OPTIONAL MATCH (m)-[:MemberOf*1..]->(:Group)-[{isacl:true}]->(GPOs)
WITH COLLECT(n) + COLLECT(m) as tempVar1
UNWIND tempVar1 as tempVar2
RETURN DISTINCT(tempVar2.name)
ORDER BY tempVar2.name ASC
"""
session = self.driver.session()
results = []
for result in session.run(list_query, domain=self.domain):
results.append(result[0])
session.close()
self.write_column_data(
sheet, "High Value User GPO Controllers: {}", results)
class CrossDomain(object):
def __init__(self, driver, domain, workbook):
self.driver = driver
self.domain = domain
self.col_count = 1
self.workbook = workbook
def write_column_data(self, sheet, title, results):
count = len(results)
offset = 1
font = styles.Font(bold=True)
c = sheet.cell(offset, self.col_count)
c.font = font
sheet.cell(offset, self.col_count, value=title.format(count))
for i in range(0, count):
sheet.cell(i+offset+1, self.col_count, value=results[i])