NetExec/nxc/helpers/bloodhound.py

56 lines
2.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2023-05-02 15:17:59 +00:00
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):
2023-05-02 15:17:59 +00:00
users_owned.append({"username": user.upper(), "domain": domain.upper()})
2021-11-20 21:37:14 +00:00
else:
users_owned = user
2023-05-02 15:17:59 +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
2023-05-02 15:17:59 +00:00
uri = f"bolt://{config.get('BloodHound', 'bh_uri')}:{config.get('BloodHound', 'bh_port')}"
2021-11-19 21:16:37 +00:00
2023-05-02 15:17:59 +00:00
driver = GraphDatabase.driver(
uri,
auth=(
config.get("BloodHound", "bh_user"),
config.get("BloodHound", "bh_pass"),
),
encrypted=False,
)
2021-11-19 21:16:37 +00:00
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:
2023-05-02 15:17:59 +00:00
if info["username"][-1] == "$":
user_owned = info["username"][:-1] + "." + info["domain"]
account_type = "Computer"
else:
2023-05-02 15:17:59 +00:00
user_owned = info["username"] + "@" + info["domain"]
account_type = "User"
2023-05-08 18:39:36 +00:00
result = tx.run(f'MATCH (c:{account_type} {{name:"{user_owned}"}}) RETURN c')
2023-05-02 15:17:59 +00:00
if result.data()[0]["c"].get("owned") in (False, None):
2023-05-08 18:39:36 +00:00
logger.debug(f'MATCH (c:{account_type} {{name:"{user_owned}"}}) SET c.owned=True RETURN c.name AS name')
result = tx.run(f'MATCH (c:{account_type} {{name:"{user_owned}"}}) SET c.owned=True RETURN c.name AS name')
logger.highlight(f"Node {user_owned} successfully set as owned in BloodHound")
2021-11-19 21:16:37 +00:00
except AuthError as e:
2023-05-10 20:18:07 +00:00
logger.fail(f"Provided Neo4J credentials ({config.get('BloodHound', 'bh_user')}:{config.get('BloodHound', 'bh_pass')}) are not valid.")
2021-11-19 21:16:37 +00:00
return
except ServiceUnavailable as e:
2023-05-10 20:18:07 +00:00
logger.fail(f"Neo4J does not seem to be available on {uri}.")
2021-11-19 21:16:37 +00:00
return
except Exception as e:
2023-05-10 20:18:07 +00:00
logger.fail("Unexpected error with Neo4J")
logger.fail("Account not found on the domain")
2021-11-19 21:16:37 +00:00
return
2023-05-02 15:17:59 +00:00
driver.close()