Skip to content

Commit 7aa6c69

Browse files
authored
Merge pull request Pennyw0rth#696 from Pennyw0rth/neff-fix-winrm-db
Fix winrm database logic
2 parents 9f906ea + 07a0ae0 commit 7aa6c69

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
@@ -168,11 +167,13 @@ def plaintext_login(self, domain, username, password):
168167

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

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

178179
if not self.args.local_auth and self.username != "":
@@ -217,8 +218,13 @@ def hash_login(self, domain, username, ntlm_hash):
217218
self.check_if_admin()
218219
self.logger.success(f"{self.domain}\\{self.username}:{process_secret(nthash)} {self.mark_pwned()}")
219220

221+
self.db.add_credential("hash", domain, self.username, ntlm_hash)
222+
user_id = self.db.get_credential("hash", domain, self.username, ntlm_hash)
223+
host_id = self.db.get_hosts(self.host)[0].id
224+
self.db.add_loggedin_relation(user_id, host_id)
225+
220226
if self.admin_privs:
221-
self.db.add_admin_user("hash", domain, self.username, nthash, self.host)
227+
self.db.add_admin_user("hash", domain, self.username, nthash, self.host, user_id=user_id)
222228
add_user_bh(f"{self.hostname}$", domain, self.logger, self.config)
223229

224230
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)