Skip to content

Commit 07a0ae0

Browse files
committed
Add database logic for hash login and ensure admin relation is added via queried userid
1 parent 07b4eba commit 07a0ae0

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

nxc/protocols/winrm.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def __init__(self, args, db, host):
3131
self.server_os = None
3232
self.output_filename = None
3333
self.endpoint = None
34-
self.hash = None
3534
self.lmhash = ""
3635
self.nthash = ""
3736
self.ssl = False
@@ -165,11 +164,13 @@ def plaintext_login(self, domain, username, password):
165164

166165
self.logger.debug(f"Adding credential: {domain}/{self.username}:{self.password}")
167166
self.db.add_credential("plaintext", domain, self.username, self.password)
168-
# TODO: when we can easily get the host_id via RETURNING statements, readd this in
167+
user_id = self.db.get_credential("plaintext", domain, self.username, self.password)
168+
host_id = self.db.get_hosts(self.host)[0].id
169+
self.db.add_loggedin_relation(user_id, host_id)
169170

170171
if self.admin_privs:
171172
self.logger.debug("Inside admin privs")
172-
self.db.add_admin_user("plaintext", domain, self.username, self.password, self.host) # , user_id=user_id)
173+
self.db.add_admin_user("plaintext", domain, self.username, self.password, self.host, user_id=user_id) # , user_id=user_id)
173174
add_user_bh(f"{self.hostname}$", domain, self.logger, self.config)
174175

175176
if not self.args.local_auth and self.username != "":
@@ -211,8 +212,13 @@ def hash_login(self, domain, username, ntlm_hash):
211212
self.check_if_admin()
212213
self.logger.success(f"{self.domain}\\{self.username}:{process_secret(nthash)} {self.mark_pwned()}")
213214

215+
self.db.add_credential("hash", domain, self.username, ntlm_hash)
216+
user_id = self.db.get_credential("hash", domain, self.username, ntlm_hash)
217+
host_id = self.db.get_hosts(self.host)[0].id
218+
self.db.add_loggedin_relation(user_id, host_id)
219+
214220
if self.admin_privs:
215-
self.db.add_admin_user("hash", domain, self.username, nthash, self.host)
221+
self.db.add_admin_user("hash", domain, self.username, nthash, self.host, user_id=user_id)
216222
add_user_bh(f"{self.hostname}$", domain, self.logger, self.config)
217223

218224
if not self.args.local_auth and self.username != "":

nxc/protocols/winrm/database.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def add_host(self, ip, port, hostname, domain, os=None):
128128

129129
def add_credential(self, credtype, domain, username, password, pillaged_from=None):
130130
"""Check if this credential has already been added to the database, if not add it in."""
131-
domain = domain.split(".")[0].upper()
132131
credentials = []
133132

134133
credential_data = {}
@@ -275,6 +274,16 @@ def get_credentials(self, filter_term=None, cred_type=None):
275274

276275
return self.db_execute(q).all()
277276

277+
def get_credential(self, cred_type, domain, username, password):
278+
q = select(self.UsersTable).filter(
279+
self.UsersTable.c.domain == domain,
280+
self.UsersTable.c.username == username,
281+
self.UsersTable.c.password == password,
282+
self.UsersTable.c.credtype == cred_type,
283+
)
284+
results = self.db_execute(q).first()
285+
return results.id
286+
278287
def is_credential_local(self, credential_id):
279288
q = select(self.UsersTable.c.domain).filter(self.UsersTable.c.id == credential_id)
280289
user_domain = self.db_execute(q).all()

0 commit comments

Comments
 (0)