2021-11-19 21:16:37 +00:00
|
|
|
def add_user_bh(user, domain, logger, config):
|
2021-11-20 21:37:14 +00:00
|
|
|
users_owned = []
|
|
|
|
if isinstance(user, str):
|
|
|
|
users_owned.append({'username': user.upper(), 'domain': domain.upper()})
|
|
|
|
else:
|
|
|
|
users_owned = user
|
2021-11-19 21:16:37 +00:00
|
|
|
if config.get('BloodHound', 'bh_enabled') != "False":
|
2021-11-20 21:37:14 +00:00
|
|
|
try:
|
|
|
|
from neo4j.v1 import GraphDatabase
|
|
|
|
except:
|
|
|
|
from neo4j import GraphDatabase
|
|
|
|
from neo4j.exceptions import AuthError, ServiceUnavailable
|
2021-11-19 21:16:37 +00:00
|
|
|
uri = "bolt://{}:{}".format(config.get('BloodHound', 'bh_uri'), config.get('BloodHound', 'bh_port'))
|
|
|
|
|
|
|
|
driver = GraphDatabase.driver(uri, auth=(config.get('BloodHound', 'bh_user'), config.get('BloodHound', 'bh_pass')), encrypted=False)
|
|
|
|
try:
|
|
|
|
with driver.session() as session:
|
|
|
|
with session.begin_transaction() as tx:
|
2021-11-20 21:37:14 +00:00
|
|
|
for info in users_owned:
|
2022-02-02 11:13:01 +00:00
|
|
|
if info['username'][-1] == '$':
|
|
|
|
user_owned = info['username'][:-1] + "." + info['domain']
|
|
|
|
account_type = 'Computer'
|
|
|
|
else:
|
|
|
|
user_owned = info['username'] + "@" + info['domain']
|
|
|
|
account_type = 'User'
|
|
|
|
|
2021-11-20 21:37:14 +00:00
|
|
|
result = tx.run(
|
2022-02-02 11:13:01 +00:00
|
|
|
"MATCH (c:{} {{name:\"{}\"}}) RETURN c".format(account_type, user_owned))
|
|
|
|
|
|
|
|
if result.data()[0]['c'].get('owned') in (False, None):
|
|
|
|
logger.debug("MATCH (c:{} {{name:\"{}\"}}) SET c.owned=True RETURN c.name AS name".format(account_type, user_owned))
|
2021-11-20 21:37:14 +00:00
|
|
|
result = tx.run(
|
2022-02-02 11:13:01 +00:00
|
|
|
"MATCH (c:{} {{name:\"{}\"}}) SET c.owned=True RETURN c.name AS name".format(account_type, user_owned))
|
2021-11-20 21:37:14 +00:00
|
|
|
logger.highlight("Node {} successfully set as owned in BloodHound".format(user_owned))
|
2021-11-19 21:16:37 +00:00
|
|
|
except AuthError as e:
|
|
|
|
logger.error(
|
|
|
|
"Provided Neo4J credentials ({}:{}) are not valid.".format(config.get('Bloodhound', 'bh_user'), config.get('Bloodhound', 'bh_pass')))
|
|
|
|
return
|
|
|
|
except ServiceUnavailable as e:
|
|
|
|
logger.error("Neo4J does not seem to be available on {}.".format(uri))
|
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Unexpected error with Neo4J")
|
2022-02-06 12:33:49 +00:00
|
|
|
logger.error("Account not found on the domain")
|
2021-11-19 21:16:37 +00:00
|
|
|
return
|
|
|
|
driver.close()
|