-
Notifications
You must be signed in to change notification settings - Fork 106
/
database.py
94 lines (85 loc) · 3.15 KB
/
database.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
from __future__ import unicode_literals, print_function
from neo4j.v1 import GraphDatabase
import platform
import requests
import json
import os
driver = None
restapi = requests.session()
def init_driver(database, user, password):
global driver
uri = "bolt://%s:7687" % database
driver = GraphDatabase.driver(uri, auth=(user, password))
restapi.auth = (user, password)
return driver
def detect_db_config():
"""
Detect bloodhound config, which is stored in appData.
OS dependent according to https://electronjs.org/docs/api/app#appgetpathname
"""
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
def close_driver():
global driver
driver.close()
preparequeries = [
"MATCH (n)-[r:MemberOf]->(m:Group) SET r.aclpwncost = 0",
"MATCH (n)-[r:AddMember|GenericAll|GenericWrite|AllExtendedRights]->(m:Group) SET r.aclpwncost = 1",
"MATCH (n)-[r:WriteOwner]->(m:Group) SET r.aclpwncost = 3",
"MATCH (n)-[r:WriteDacl|Owns]->(m:Group) SET r.aclpwncost = 2",
# These privileges on user objects are not wanted since they work only when resetting passwords
"MATCH (n)-[r:WriteDacl|Owns|WriteOwner|GenericAll|GenericWrite|ForceChangePassword|AllExtendedRights]->(m:User) SET r.aclpwncost = 200",
"MATCH (n)-[r:WriteDacl]->(m:Domain) SET r.aclpwncost = 1",
"MATCH (n)-[r:DCSync|GetChangesAll|AllExtendedRights]->(m:Domain) SET r.aclpwncost = 0",
]
def preparedb():
global driver
with driver.session() as session:
with session.begin_transaction() as tx:
for query in preparequeries:
tx.run(query)