Skip to content

Commit c78bb6b

Browse files
committed
Move error handling to login function, so if we crash we don't add users to db
1 parent 95f67b7 commit c78bb6b

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

nxc/modules/change-password.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def on_login(self, context, connection):
123123
self.context.db.add_credential("plaintext", target_domain, target_username, self.newpass)
124124
except Exception as e:
125125
if "STATUS_ACCESS_DENIED" in str(e):
126-
self.context.log.fail(f"Access denied while changing password for '{target_username}'")
126+
self.context.log.fail(f"STATUS_ACCESS_DENIED while changing password for user: {target_username}")
127+
elif "STATUS_NONE_MAPPED" in str(e):
128+
self.context.log.fail(f"User '{target_username}' not found or not resolvable")
127129
else:
128130
context.log.fail(f"SMB-SAMR password change failed: {e}")
129131
finally:
@@ -152,16 +154,9 @@ def _smb_samr_change(self, context, connection, target_username, target_domain,
152154
context.log.success(f"Successfully changed password for {target_username}")
153155

154156
def _hSamrOpenUser(self, connection, username):
155-
"""Get handle to the user object"""
156-
try:
157-
# Connect to the target server and retrieve handles
158-
server_handle = samr.hSamrConnect(self.dce, connection.host + "\x00")["ServerHandle"]
159-
domain_sid = samr.hSamrLookupDomainInSamServer(self.dce, server_handle, connection.domain)["DomainId"]
160-
domain_handle = samr.hSamrOpenDomain(self.dce, server_handle, domainId=domain_sid)["DomainHandle"]
161-
user_rid = samr.hSamrLookupNamesInDomain(self.dce, domain_handle, (username,))["RelativeIds"]["Element"][0]
162-
return samr.hSamrOpenUser(self.dce, domain_handle, userId=user_rid)["UserHandle"]
163-
except Exception as e:
164-
if "STATUS_NONE_MAPPED" in str(e):
165-
self.context.log.fail(f"User '{username}' not found or not resolvable")
166-
else:
167-
self.context.log.fail(f"Failed to open user: {e}")
157+
"""Connect to the target server and retrieve the user handle"""
158+
server_handle = samr.hSamrConnect(self.dce, connection.host + "\x00")["ServerHandle"]
159+
domain_sid = samr.hSamrLookupDomainInSamServer(self.dce, server_handle, connection.domain)["DomainId"]
160+
domain_handle = samr.hSamrOpenDomain(self.dce, server_handle, domainId=domain_sid)["DomainHandle"]
161+
user_rid = samr.hSamrLookupNamesInDomain(self.dce, domain_handle, (username,))["RelativeIds"]["Element"][0]
162+
return samr.hSamrOpenUser(self.dce, domain_handle, userId=user_rid)["UserHandle"]

0 commit comments

Comments
 (0)