Skip to content

Commit

Permalink
do some cleanup for hash_spider for #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Marshall-Hallenbeck committed Sep 22, 2023
1 parent 76bf3be commit 2a5c01d
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions nxc/modules/hash_spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from lsassy.impacketfile import ImpacketFile

credentials_data = []
admin_results = []
found_users = []
reported_da = []

Expand All @@ -37,15 +36,15 @@ def neo4j_conn(context, connection, driver):


def neo4j_local_admins(context, driver):
global admin_results
try:
session = driver.session()
admins = session.run("MATCH (c:Computer) OPTIONAL MATCH (u1:User)-[:AdminTo]->(c) OPTIONAL MATCH (u2:User)-[:MemberOf*1..]->(:Group)-[:AdminTo]->(c) WITH COLLECT(u1) + COLLECT(u2) AS TempVar,c UNWIND TempVar AS Admins RETURN c.name AS COMPUTER, COUNT(DISTINCT(Admins)) AS ADMIN_COUNT,COLLECT(DISTINCT(Admins.name)) AS USERS ORDER BY ADMIN_COUNT DESC") # This query pulls all PCs and their local admins from Bloodhound. Based on: https://github.com/xenoscr/Useful-BloodHound-Queries/blob/master/List-Queries.md and other similar posts
context.log.success("Admins and PCs obtained.")
except Exception:
context.log.fail("Could not pull admins")
exit()
admin_results = [record for record in admins.data()]
context.log.success("Admins and PCs obtained")
except Exception as e:
context.log.fail(f"Could not pull admins: {e}")
return None
results = [record for record in admins.data()]
return results


def create_db(local_admins, dbconnection, cursor):
Expand All @@ -69,7 +68,7 @@ def create_db(local_admins, dbconnection, cursor):
if user not in admin_users:
admin_users.append(user)
for user in admin_users:
cursor.execute("""INSERT OR IGNORE INTO admin_users(username) VALUES(?)""", [user])
cursor.execute("INSERT OR IGNORE INTO admin_users(username) VALUES(?)", [user])
dbconnection.commit()


Expand Down Expand Up @@ -113,7 +112,7 @@ def process_creds(context, connection, credentials_data, dbconnection, cursor, d
if path:
for key, value in path.items():
for item in value:
if type(item) == dict:
if isinstance(item, dict):
if {item["name"]} not in reported_da:
context.log.success(f"You have a valid path to DA as {item['name']}.")
reported_da.append({item["name"]})
Expand Down Expand Up @@ -147,6 +146,7 @@ def __init__(self, context=None, module_options=None):
self.reset = None
self.reset_dumped = None
self.method = None

@staticmethod
def save_credentials(context, connection, domain, username, password, lmhash, nthash):
host_id = context.db.get_computers(connection.host)[0][0]
Expand All @@ -156,6 +156,7 @@ def save_credentials(context, connection, domain, username, password, lmhash, nt
credential_type = 'hash'
password = ':'.join(h for h in [lmhash, nthash] if h is not None)
context.db.add_credential(credential_type, domain, username, password, pillaged_from=host_id)

def options(self, context, module_options):
"""
METHOD Method to use to dump lsass.exe with lsassy
Expand Down Expand Up @@ -220,17 +221,23 @@ def run_lsassy(self, context, connection, cursor): # copied and pasted from lsa
cred["lmhash"],
cred["nthash"],
] not in credentials_unique:
credentials_unique.append(
[
cred["domain"],
cred["username"],
cred["password"],
cred["lmhash"],
cred["nthash"],
]
)
credentials_unique.append([
cred["domain"],
cred["username"],
cred["password"],
cred["lmhash"],
cred["nthash"],
])
credentials_output.append(cred)
self.save_credentials(context, connection, cred["domain"], cred["username"], cred["password"], cred["lmhash"], cred["nthash"])
self.save_credentials(
context,
connection,
cred["domain"],
cred["username"],
cred["password"],
cred["lmhash"],
cred["nthash"]
)
global credentials_data
credentials_data = credentials_output

Expand Down Expand Up @@ -302,7 +309,7 @@ def on_admin_login(self, context, connection):
neo4j_db = f"bolt://{neo4j_uri}:{neo4j_port}"
driver = GraphDatabase.driver(neo4j_db, auth=basic_auth(neo4j_user, neo4j_pass), encrypted=False)
neo4j_conn(context, connection, driver)
neo4j_local_admins(context, driver)
admin_results = neo4j_local_admins(context, driver)
create_db(admin_results, dbconnection, cursor)
initial_run(connection, cursor)
context.log.display("Running lsassy")
Expand Down

0 comments on commit 2a5c01d

Please sign in to comment.