@@ -122,37 +122,34 @@ def on_login(self, context, connection):
122122 else :
123123 self .context .db .add_credential ("plaintext" , target_domain , target_username , self .newpass )
124124 except Exception as e :
125- context .log .fail (f"SMB-SAMR password change failed: { e } " )
125+ if "STATUS_ACCESS_DENIED" in str (e ):
126+ self .context .log .fail (f"Access denied while changing password for '{ target_username } '" )
127+ else :
128+ context .log .fail (f"SMB-SAMR password change failed: { e } " )
126129 finally :
127130 self .dce .disconnect ()
128131
129132 def _smb_samr_change (self , context , connection , target_username , target_domain , oldHash , newPassword , newHash ):
130133 # Reset the password for a different user
131- try :
132- if target_username != connection .username :
134+ if target_username != connection .username :
135+ user_handle = self ._hSamrOpenUser (connection , target_username )
136+ if not user_handle :
137+ return False
138+ samr .hSamrSetNTInternal1 (self .dce , user_handle , newPassword , newHash )
139+ context .log .success (f"Successfully changed password for { target_username } " )
140+ else :
141+ # Change password for the current user
142+ if newPassword :
143+ # Change the password with new password
144+ samr .hSamrUnicodeChangePasswordUser2 (self .dce , "\x00 " , target_username , self .oldpass , newPassword , "" , oldHash )
145+ else :
146+ # Change the password with new hash
133147 user_handle = self ._hSamrOpenUser (connection , target_username )
134148 if not user_handle :
135149 return False
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 } " )
150+ samr .hSamrChangePasswordUser (self .dce , user_handle , self .oldpass , "" , oldHash , "aad3b435b51404eeaad3b435b51404ee" , newHash )
151+ context .log .highlight ("Note: Target user must change password at next logon." )
152+ context .log .success (f"Successfully changed password for { target_username } " )
156153
157154 def _hSamrOpenUser (self , connection , username ):
158155 """Get handle to the user object"""
0 commit comments