Skip to content

Impacket samedit.py

Back to the category index / Back to the command index

Changes a user’s password hash directly in a local SAM hive, using a SYSTEM hive or a supplied boot key.

ItemDetails
Implementationexamples/samedit.py
CategorySecrets / DPAPI / registry / file analysis
Size121 lines
DistributionInstalled as a script through scripts=examples/*.py in setup.py

Run python examples/samedit.py -h for the current option definitions.

ArgumentDescription
userUser account whose password is to be replaced
samSAM hive file to edit
-passwordNew password to set
-hashesReplace the NTLM hash directly (the LM hash is optional)
-systemSYSTEM hive containing the boot key for password encryption
-bootkeyBoot key used to encrypt and decrypt SAM passwords
-debugEnable debug output
-tsAdd a timestamp to each log message

The tool opens the SAM hive and obtains the boot key from the SYSTEM hive or a supplied value. LocalOperations resolves the boot key, and SAMHashes finds the user record and RID. It encrypts the password or LM:NT hash in the account-specific format and updates the hive cell.

Password fields in SAM user records are neither plaintext nor simple NT hashes. They are protected through layers using the boot key, SAM domain key, and user RID. This command opens the local SAM hive and either the SYSTEM hive or an explicit boot key, checks the existing record’s encryption format and version, and writes the new hash in the same format.

For password input, it calculates the LM and NT one-way functions. For hash input, it uses the supplied value directly as the account hash. It then encrypts the record fields with RID-derived and SAM keys. The handling of disabled LM hashes and history or length fields must match the record layout for the Windows version.

Because this is an in-place edit, do not update the file before parsing succeeds and the record size is known. Check that the target user is unique, the SAM and SYSTEM hives match, backups exist, and the file parses again after writing. The writer is not transactional, so minimize the update range to avoid corrupting the hive on a partial write failure.

SymbolLocationWhere to trace its responsibility
module-level entry1Parses arguments and runs at the top level
ModuleMain names used
impacketntlm, version
impacket.exampleslogger
impacket.examples.secretsdumpLocalOperations, SAMHashes

Output, side effects, and required privileges

Section titled “Output, side effects, and required privileges”
  • Some operations change the target’s state. Check the target name, SID, and path before making changes.
  • Distinguish a missing input file, a damaged file, an incomplete read, and an unsupported format.

The code depends on examples.secretsdump.LocalOperations, SAMHashes, and impacket.ntlm. This is a destructive local file update, so focus tests on record size, hash-disabling rules, and partial writes on exceptions.

  • Syntax: python -m py_compile examples/samedit.py
  • CLI: python examples/samedit.py -h
  • No test specific to this command was found. Choose shared-module tests and, if needed, a run in an isolated environment.