Skip to content

Commit e6ca7f4

Browse files
committed
Added try except for understanding errors
1 parent 7c97ed1 commit e6ca7f4

1 file changed

Lines changed: 26 additions & 17 deletions

File tree

nxc/modules/change-password.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +128,31 @@ def on_login(self, context, connection):
128128

129129
def _smb_samr_change(self, context, connection, target_username, target_domain, oldHash, newPassword, newHash):
130130
# Reset the password for a different user
131-
if target_username != connection.username:
132-
user_handle = self._hSamrOpenUser(connection, target_username)
133-
if not user_handle:
134-
return False
135-
samr.hSamrSetNTInternal1(self.dce, user_handle, newPassword, newHash)
136-
context.log.success(f"Successfully changed password for {target_username}")
137-
else:
138-
# Change password for the current user
139-
if newPassword:
140-
# Change the password with new password
141-
samr.hSamrUnicodeChangePasswordUser2(self.dce, "\x00", target_username, self.oldpass, newPassword, "", oldHash)
142-
else:
143-
# Change the password with new hash
131+
try:
132+
if target_username != connection.username:
144133
user_handle = self._hSamrOpenUser(connection, target_username)
145134
if not user_handle:
146135
return False
147-
samr.hSamrChangePasswordUser(self.dce, user_handle, self.oldpass, "", oldHash, "aad3b435b51404eeaad3b435b51404ee", newHash)
148-
context.log.highlight("Note: Target user must change password at next logon.")
149-
context.log.success(f"Successfully changed password for {target_username}")
136+
samr.hSamrSetNTInternal1(self.dce, user_handle, newPassword, newHash)
137+
context.log.success(f"Successfully changed password for {target_username}")
138+
else:
139+
# Change password for the current user
140+
if newPassword:
141+
# Change the password with new password
142+
samr.hSamrUnicodeChangePasswordUser2(self.dce, "\x00", target_username, self.oldpass, newPassword, "", oldHash)
143+
else:
144+
# Change the password with new hash
145+
user_handle = self._hSamrOpenUser(connection, target_username)
146+
if not user_handle:
147+
return False
148+
samr.hSamrChangePasswordUser(self.dce, user_handle, self.oldpass, "", oldHash, "aad3b435b51404eeaad3b435b51404ee", newHash)
149+
context.log.highlight("Note: Target user must change password at next logon.")
150+
context.log.success(f"Successfully changed password for {target_username}")
151+
except Exception as e:
152+
if "STATUS_ACCESS_DENIED" in str(e):
153+
self.context.log.fail(f"Access denied while changing password for '{target_username}'")
154+
else:
155+
self.context.log.fail(f"Failed to change user password: {e}")
150156

151157
def _hSamrOpenUser(self, connection, username):
152158
"""Get handle to the user object"""
@@ -158,4 +164,7 @@ def _hSamrOpenUser(self, connection, username):
158164
user_rid = samr.hSamrLookupNamesInDomain(self.dce, domain_handle, (username,))["RelativeIds"]["Element"][0]
159165
return samr.hSamrOpenUser(self.dce, domain_handle, userId=user_rid)["UserHandle"]
160166
except Exception as e:
161-
self.context.log.fail(f"Failed to open user: {e}")
167+
if "STATUS_NONE_MAPPED" in str(e):
168+
self.context.log.fail(f"User '{username}' not found or not resolvable")
169+
else:
170+
self.context.log.fail(f"Failed to open user: {e}")

0 commit comments

Comments
 (0)