-
Notifications
You must be signed in to change notification settings - Fork 8
/
bloodhound-import.py
539 lines (437 loc) · 21.1 KB
/
bloodhound-import.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
import asyncio
import argparse
import codecs
import dataclasses
import json
import logging
import os
import platform
from os.path import basename
import warnings
from tempfile import NamedTemporaryFile
from zipfile import ZipFile
import ijson
import neo4j
from neo4j import AsyncGraphDatabase, AsyncDriver
from neo4j.exceptions import ClientError
# 禁用 ExperimentalWarning
warnings.simplefilter("ignore", category=DeprecationWarning, append=True)
# 禁用 RuntimeWarning
warnings.simplefilter("ignore", category=RuntimeWarning, append=True)
@dataclass
class Query:
query: str
properties: dict
SYNC_COUNT = 100
def build_add_edge_query(source_label: str, target_label: str, edge_type: str, edge_props: str) -> str:
"""Build a standard edge insert query based on the given params"""
insert_query = 'UNWIND $props AS prop MERGE (n:Base {{objectid: prop.source}}) SET n:{0} MERGE (m:Base {{objectid: prop.target}}) SET m:{1} MERGE (n)-[r:{2} {3}]->(m)'
return insert_query.format(source_label, target_label, edge_type, edge_props)
async def process_ace_list(ace_list: list, objectid: str, objecttype: str, tx: neo4j.Transaction) -> None:
for entry in ace_list:
principal = entry['PrincipalSID']
principaltype = entry['PrincipalType']
right = entry['RightName']
if objectid == principal:
continue
query = build_add_edge_query(principaltype, objecttype, right, '{isacl: true, isinherited: prop.isinherited}')
props = dict(
source=principal,
target=objectid,
isinherited=entry['IsInherited'],
)
await tx.run(query, props=props)
async def process_spntarget_list(spntarget_list: list, objectid: str, tx: neo4j.Transaction) -> None:
for entry in spntarget_list:
query = build_add_edge_query('User', 'Computer', 'WriteSPN', '{isacl: false, port: prop.port}')
props = dict(
source=objectid,
target=entry['ComputerSID'],
port=entry['Port'],
)
await tx.run(query, props=props)
async def add_constraints(tx: neo4j.Transaction):
"""Adds bloodhound contraints to neo4j
Arguments:
tx {neo4j.Transaction} -- Neo4j transaction.
"""
await tx.run('CREATE CONSTRAINT base_objectid_unique ON (b:Base) ASSERT b.objectid IS UNIQUE')
await tx.run('CREATE CONSTRAINT computer_objectid_unique ON (c:Computer) ASSERT c.objectid IS UNIQUE')
await tx.run('CREATE CONSTRAINT domain_objectid_unique ON (d:Domain) ASSERT d.objectid IS UNIQUE')
await tx.run('CREATE CONSTRAINT group_objectid_unique ON (g:Group) ASSERT g.objectid IS UNIQUE')
await tx.run('CREATE CONSTRAINT user_objectid_unique ON (u:User) ASSERT u.objectid IS UNIQUE')
await tx.run("CREATE CONSTRAINT ON (c:User) ASSERT c.name IS UNIQUE")
await tx.run("CREATE CONSTRAINT ON (c:Computer) ASSERT c.name IS UNIQUE")
await tx.run("CREATE CONSTRAINT ON (c:Group) ASSERT c.name IS UNIQUE")
await tx.run("CREATE CONSTRAINT ON (c:Domain) ASSERT c.name IS UNIQUE")
await tx.run("CREATE CONSTRAINT ON (c:OU) ASSERT c.guid IS UNIQUE")
await tx.run("CREATE CONSTRAINT ON (c:GPO) ASSERT c.name IS UNIQUE")
async def parse_ou(tx: neo4j.Transaction, ou: dict):
"""Parses a single ou.
Arguments:
tx {neo4j.Transaction} -- Neo4j session
ou {dict} -- Single ou object.
"""
identifier = ou['ObjectIdentifier'].upper()
property_query = 'UNWIND $props AS prop MERGE (n:Base {objectid: prop.source}) SET n:OU SET n += prop.map'
props = {'map': ou['Properties'], 'source': identifier}
await tx.run(property_query, props=props)
if 'Aces' in ou and ou['Aces'] is not None:
await process_ace_list(ou['Aces'], identifier, "OU", tx)
if 'ChildObjects' in ou and ou['ChildObjects']:
targets = ou['ChildObjects']
for target in targets:
query = build_add_edge_query('OU', target['ObjectType'], 'Contains', '{isacl: false}')
await tx.run(query, props=dict(source=identifier, target=target['ObjectIdentifier']))
if 'Links' in ou and ou['Links']:
query = build_add_edge_query('GPO', 'OU', 'GpLink', '{isacl: false, enforced: prop.enforced}')
for gpo in ou['Links']:
await tx.run(query, props=dict(source=identifier, target=gpo['GUID'].upper(), enforced=gpo['IsEnforced']))
options = [
('LocalAdmins', 'AdminTo'),
('PSRemoteUsers', 'CanPSRemote'),
('DcomUsers', 'ExecuteDCOM'),
('RemoteDesktopUsers', 'CanRDP'),
]
if 'GPOChanges' in ou and ou['GPOChanges']:
gpo_changes = ou['GPOChanges']
affected_computers = gpo_changes['AffectedComputers']
for option, edge_name in options:
if option in gpo_changes and gpo_changes[option]:
targets = gpo_changes[option]
for target in targets:
query = build_add_edge_query(target['ObjectType'], 'Computer', edge_name, '{isacl: false, fromgpo: true}')
for computer in affected_computers:
await tx.run(query, props=dict(source=computer['ObjectIdentifier'], target=target['ObjectIdentifier']))
async def parse_gpo(tx: neo4j.Transaction, gpo: dict):
"""Parses a single GPO.
Arguments:
tx {neo4j.Transaction} -- Neo4j transaction
gpo {dict} -- Single gpo object.
"""
identifier = gpo['ObjectIdentifier']
query = 'UNWIND $props AS prop MERGE (n:Base {objectid: prop.source}) SET n:GPO SET n += prop.map'
props = {'map': gpo['Properties'], 'source': identifier}
await tx.run(query, props=props)
if "Aces" in gpo and gpo["Aces"] is not None:
await process_ace_list(gpo['Aces'], identifier, "GPO", tx)
async def parse_computer(tx: neo4j.Transaction, computer: dict):
"""Parse a computer object.
Arguments:
session {neo4j.Transaction} -- Neo4j transaction
computer {dict} -- Single computer object.
"""
identifier = computer['ObjectIdentifier']
property_query = 'UNWIND $props AS prop MERGE (n:Base {objectid: prop.source}) SET n:Computer SET n += prop.map'
props = {'map': computer['Properties'], 'source': identifier}
await tx.run(property_query, props=props)
if 'PrimaryGroupSid' in computer and computer['PrimaryGroupSid']:
query = build_add_edge_query('Computer', 'Group', 'MemberOf', '{isacl:false}')
await tx.run(query, props=dict(source=identifier, target=computer['PrimaryGroupSid']))
if 'AllowedToDelegate' in computer and computer['AllowedToDelegate']:
query = build_add_edge_query('Computer', 'Group', 'MemberOf', '{isacl:false}')
for entry in computer['AllowedToDelegate']:
await tx.run(query, props=dict(source=identifier, target=entry['ObjectIdentifier']))
# (Property name, Edge name, Use "Results" format)
options = [
('LocalAdmins', 'AdminTo', True),
('RemoteDesktopUsers', 'CanRDP', True),
('DcomUsers', 'ExecuteDCOM', True),
('PSRemoteUsers', 'CanPSRemote', True),
('AllowedToAct', 'AllowedToAct', False),
('AllowedToDelegate', 'AllowedToDelegate', False),
]
for option, edge_name, use_results in options:
if option in computer:
targets = computer[option]['Results'] if use_results else computer[option]
for target in targets:
query = build_add_edge_query(target['ObjectType'], 'Computer', edge_name, '{isacl:false, fromgpo: false}')
await tx.run(query, props=dict(source=target['ObjectIdentifier'], target=identifier))
# (Session type, source)
session_types = [
('Sessions', 'netsessionenum'),
('PrivilegedSessions', 'netwkstauserenum'),
('RegistrySessions', 'registry'),
]
for session_type, source in session_types:
if session_type in computer and computer[session_type]['Results']:
query = build_add_edge_query('Computer', 'User', 'HasSession', '{isacl:false, source:"%s"}' % source)
for entry in computer[session_type]['Results']:
await tx.run(query, props=dict(target=entry['UserSID'], source=identifier))
if 'Aces' in computer and computer['Aces'] is not None:
await process_ace_list(computer['Aces'], identifier, "Computer", tx)
async def parse_user(tx: neo4j.Transaction, user: dict):
"""Parse a user object.
Arguments:
tx {neo4j.Transaction} -- Neo4j session
user {dict} -- Single user object from the bloodhound json.
"""
identifier = user['ObjectIdentifier']
property_query = 'UNWIND $props AS prop MERGE (n:Base {objectid: prop.source}) SET n:User SET n += prop.map'
props = {'map': user['Properties'], 'source': identifier}
await tx.run(property_query, props=props)
if 'PrimaryGroupSid' in user and user['PrimaryGroupSid']:
query = build_add_edge_query('User', 'Group', 'MemberOf', '{isacl: false}')
await tx.run(query, props=dict(source=identifier, target=user['PrimaryGroupSid']))
if 'AllowedToDelegate' in user and user['AllowedToDelegate']:
query = build_add_edge_query('User', 'Computer', 'AllowedToDelegate', '{isacl: false}')
for entry in user['AllowedToDelegate']:
await tx.run(query, props=dict(source=identifier, target=entry['ObjectIdentifier']))
# TODO add HasSIDHistory objects
if 'Aces' in user and user['Aces'] is not None:
await process_ace_list(user['Aces'], identifier, "User", tx)
if 'SPNTargets' in user and user['SPNTargets'] is not None:
await process_spntarget_list(user['SPNTargets'], identifier, tx)
async def parse_group(tx: neo4j.Transaction, group: dict):
"""Parse a group object.
Arguments:
tx {neo4j.Transaction} -- Neo4j Transaction
group {dict} -- Single group object from the bloodhound json.
"""
properties = group['Properties']
identifier = group['ObjectIdentifier']
members = group['Members']
property_query = 'UNWIND $props AS prop MERGE (n:Base {objectid: prop.source}) SET n:Group SET n += prop.map'
props = {'map': properties, 'source': identifier}
await tx.run(property_query, props=props)
if 'Aces' in group and group['Aces'] is not None:
await process_ace_list(group['Aces'], identifier, "Group", tx)
for member in members:
query = build_add_edge_query(member['ObjectType'], 'Group', 'MemberOf', '{isacl: false}')
await tx.run(query, props=dict(source=member['ObjectIdentifier'], target=identifier))
async def parse_domain(tx: neo4j.Transaction, domain: dict):
"""Parse a domain object.
Arguments:
tx {neo4j.Transaction} -- Neo4j Transaction
domain {dict} -- Single domain object from the bloodhound json.
"""
identifier = domain['ObjectIdentifier']
property_query = 'UNWIND $props AS prop MERGE (n:Base {objectid: prop.source}) SET n:Domain SET n += prop.map'
props = {'map': domain['Properties'], 'source': identifier}
await tx.run(property_query, props=props)
if 'Aces' in domain and domain['Aces'] is not None:
await process_ace_list(domain['Aces'], identifier, 'Domain', tx)
trust_map = {0: 'ParentChild', 1: 'CrossLink', 2: 'Forest', 3: 'External', 4: 'Unknown'}
if 'Trusts' in domain and domain['Trusts'] is not None:
query = build_add_edge_query('Domain', 'Domain', 'TrustedBy', '{sidfiltering: prop.sidfiltering, trusttype: prop.trusttype, transitive: prop.transitive, isacl: false}')
for trust in domain['Trusts']:
trust_type = trust['TrustType']
direction = trust['TrustDirection']
props = {}
if direction in [1, 3]:
props = dict(
source=identifier,
target=trust['TargetDomainSid'],
trusttype=trust_map[trust_type],
transitive=trust['IsTransitive'],
sidfiltering=trust['SidFilteringEnabled'],
)
elif direction in [2, 4]:
props = dict(
target=identifier,
source=trust['TargetDomainSid'],
trusttype=trust_map[trust_type],
transitive=trust['IsTransitive'],
sidfiltering=trust['SidFilteringEnabled'],
)
else:
logging.error("Could not determine direction of trust... direction: %s", direction)
continue
await tx.run(query, props=props)
if 'ChildObjects' in domain and domain['ChildObjects']:
targets = domain['ChildObjects']
for target in targets:
query = build_add_edge_query('Domain', target['ObjectType'], 'Contains', '{isacl: false}')
await tx.run(query, props=dict(source=identifier, target=target['ObjectIdentifier']))
if 'Links' in domain and domain['Links']:
query = build_add_edge_query('GPO', 'OU', 'GpLink', '{isacl: false, enforced: prop.enforced}')
for gpo in domain['Links']:
await tx.run(
query,
props=dict(source=identifier, target=gpo['GUID'].upper(), enforced=gpo['IsEnforced'])
)
options = [
('LocalAdmins', 'AdminTo'),
('PSRemoteUsers', 'CanPSRemote'),
('DcomUsers', 'ExecuteDCOM'),
('RemoteDesktopUsers', 'CanRDP'),
]
if 'GPOChanges' in domain and domain['GPOChanges']:
gpo_changes = domain['GPOChanges']
affected_computers = gpo_changes['AffectedComputers']
for option, edge_name in options:
if option in gpo_changes and gpo_changes[option]:
targets = gpo_changes[option]
for target in targets:
query = build_add_edge_query(target['ObjectType'], 'Computer', edge_name, '{isacl: false, fromgpo: true}')
for computer in affected_computers:
await tx.run(query, props=dict(source=computer['ObjectIdentifier'], target=target['ObjectIdentifier']))
async def parse_container(tx: neo4j.Transaction, container: dict):
"""Parse a Container object.
Arguments:
tx {neo4j.Transaction} -- Neo4j session
container {dict} -- Single container object from the bloodhound json.
"""
identifier = container['ObjectIdentifier']
property_query = 'UNWIND $props AS prop MERGE (n:Base {objectid: prop.source}) SET n:Container SET n += prop.map'
props = {'map': container['Properties'], 'source': identifier}
await tx.run(property_query, props=props)
if 'Aces' in container and container['Aces'] is not None:
await process_ace_list(container['Aces'], identifier, "Container", tx)
if 'ChildObjects' in container and container['ChildObjects']:
targets = container['ChildObjects']
for target in targets:
query = build_add_edge_query('Container', target['ObjectType'], 'Contains', '{isacl: false}')
await tx.run(query, props=dict(source=identifier, target=target['ObjectIdentifier']))
async def parse_zipfile(filename: str, driver: neo4j.Driver):
"""Parse a bloodhound zip file.
Arguments:
filename {str} -- ZIP filename to parse.
driver {neo4j.GraphDatabase} -- driver to connect to neo4j.
"""
with ZipFile(filename) as zip_file:
for file in zip_file.namelist():
if not file.endswith('.json'):
logging.info("File does not appear to be JSON, skipping: %s", file)
continue
with NamedTemporaryFile(suffix=basename(file)) as temp:
temp.write(zip_file.read(file))
temp.flush()
await parse_file(temp.name, driver)
async def parse_file(filename: str, driver: neo4j.AsyncDriver):
"""Parse a bloodhound file.
Arguments:
filename {str} -- JSON filename to parse.
driver {neo4j.GraphDatabase} -- driver to connect to neo4j.
"""
logging.info("Parsing bloodhound file: %s", filename)
if filename.endswith('.zip'):
logging.info("File appears to be a zip file, importing all containing JSON files..")
await parse_zipfile(filename, driver)
return
with codecs.open(filename, 'r', encoding='utf-8-sig') as f:
meta = ijson.items(f, 'meta')
for o in meta:
obj_type = o['type']
total = o['count']
parsing_map = {
'computers': parse_computer,
'containers': parse_container,
'users': parse_user,
'groups': parse_group,
'domains': parse_domain,
'gpos': parse_gpo,
'ous': parse_ou
}
parse_function = None
try:
parse_function = parsing_map[obj_type]
except KeyError:
logging.error("Parsing function for object type: %s was not found.", obj_type)
return
ten_percent = total // 10 if total > 10 else 1
count = 0
f = codecs.open(filename, 'r', encoding='utf-8-sig')
objs = ijson.items(f, 'data.item')
async with driver.session() as session:
for entry in objs:
try:
await session.write_transaction(parse_function, entry)
count = count + 1
except neo4j.exceptions.ConstraintError as e:
print(e)
if count % ten_percent == 0:
logging.info("Parsed %d out of %d records in %s.", count, total, filename)
f.close()
logging.info("Completed file: %s", filename)
def init_driver(ip, port, scheme, user, password) -> AsyncDriver:
uri = "{}://{}:{}".format(scheme, ip, port)
driver = AsyncGraphDatabase.driver(uri, auth=(user, password))
return driver
def detect_db_config():
system = platform.system()
if system == 'Windows':
try:
directory = os.environ['APPDATA']
except KeyError:
return (None, None)
config = os.path.join(directory, 'BloodHound', 'config.json')
try:
with open(config, 'r') as configfile:
configdata = json.load(configfile)
except IOError:
return (None, None)
if system == 'Linux':
try:
directory = os.environ['XDG_CONFIG_HOME']
except KeyError:
try:
directory = os.path.join(os.environ['HOME'], '.config')
except KeyError:
return (None, None)
config = os.path.join(directory, 'bloodhound', 'config.json')
try:
with open(config, 'r') as configfile:
configdata = json.load(configfile)
except IOError:
return (None, None)
if system == 'Darwin':
try:
directory = os.path.join(os.environ['HOME'], 'Library', 'Application Support')
except KeyError:
return (None, None)
config = os.path.join(directory, 'bloodhound', 'config.json')
try:
with open(config, 'r') as configfile:
configdata = json.load(configfile)
except IOError:
return (None, None)
# If we are still here, we apparently found the config :)
try:
username = configdata['databaseInfo']['user']
except KeyError:
username = 'neo4j'
try:
password = configdata['databaseInfo']['password']
except KeyError:
password = None
return username, password
async def main():
"""
Main function
"""
argparser = argparse.ArgumentParser("bloodhound-import.py")
argparser.add_argument("files", help="Files to parse.", nargs="+")
argparser.add_argument("-du", "--database-user", help="Username to connect to neo4j, if not specified will try to auto detect a config file.")
argparser.add_argument("-dp", "--database-password", help="Password to connect to neo4j, if not specified will try to auto detect a config file.")
argparser.add_argument("--database", help="The host neo4j is running on.", default="localhost")
argparser.add_argument("-p", "--port", help="Port of neo4j", default=7687)
argparser.add_argument("-s", "--scheme", help="URI Scheme used to communicate with neo4j", default="bolt")
argparser.add_argument("-v", "--verbose", help="Verbose output", action='store_true')
arguments = argparser.parse_args()
if arguments.database_password is None:
arguments.database_user, arguments.database_password = detect_db_config()
if arguments.database_password is None:
logging.error('Error: Could not autodetect the Neo4j database credentials from your BloodHound config. Please specify them manually')
return
logging.basicConfig(format="[%(levelname)s] %(asctime)s - %(message)s")
if arguments.verbose:
logging.getLogger().setLevel(logging.DEBUG)
else:
logging.getLogger().setLevel(logging.INFO)
driver = init_driver(arguments.database, arguments.port, arguments.scheme, arguments.database_user, arguments.database_password)
try:
try:
async with driver.session() as session:
logging.debug("Adding constraints to the neo4j database")
await session.write_transaction(add_constraints)
except ClientError:
pass
logging.info("Parsing %s files", len(arguments.files))
for filename in arguments.files:
await parse_file(filename, driver)
logging.info("Done")
finally:
await driver.close()
if __name__ == "__main__":
asyncio.run(main())