Skip to main content

Module masterkey

Module masterkey 

Source
Expand description

DPAPI master-key file parser + user master-key derivation.

This is the on-disk counterpart of the LSASS master-key source: the same 64-byte master key that crate::decrypt_dpapi_blob consumes, but recovered from a master-key file (%APPDATA%\Microsoft\Protect\<SID>\<GUID>) plus the user’s password (or pre-hashed SHA1), rather than read from LSASS memory.

Layout and crypto follow impacket’s MasterKeyFile / MasterKey.decrypt (impacket 0.13.1, impacket/dpapi.py). The master-key file is:

  • a fixed 128-byte header — Version(4), unk1(4), unk2(4), Guid(72, UTF-16LE), Unknown(4), Policy(4), Flags(4), then four <u64 length fields: MasterKeyLen, BackupKeyLen, CredHistLen, DomainKeyLen;
  • followed by the four sub-blobs in that order, each exactly its length.

The MasterKey sub-blob is itself Version(4), Salt(16), IterationCount(4), HashAlgo(4), CryptAlgo(4), then the encrypted data.

Derivation (MasterKey.decrypt): deriveKey(preKey, salt, keyLen+ivLen, rounds, prf=HMAC_H) — impacket’s iterated XOR construction, NOT standard PBKDF2 — yields cryptKey || iv; CBC-decrypt data; the trailing 64 bytes are the master key, the leading 16 are hmacSalt, and the next digestLen bytes are an HMAC verified via HMAC_H(HMAC_H(preKey, hmacSalt), masterKey).

The pre-key for the user path is HMAC-SHA1(SHA1(UTF16LE(password)), UTF16LE(sid + "\0")) (impacket deriveKeysFromUser, SHA1 variant).

All cryptography uses audited RustCrypto crates — no hand-rolled primitives.

Structs§

MasterKey
The MasterKey sub-blob: salt + rounds + alg IDs + encrypted payload.
MasterKeyFile
A parsed DPAPI master-key file, mirroring impacket’s MasterKeyFile.

Constants§

MASTER_KEY_LEN
Size of the decrypted DPAPI master key consumed by blob decryption.

Functions§

derive_master_key_from_domain_backup
Decrypt the master key via the domain RSA backup key (DomainKey sub-blob).
derive_master_key_from_password
Full user-password path: parse the file, derive the pre-key, decrypt the key.
derive_master_key_from_prekey
Derive the 64-byte master key from a master-key sub-blob and a pre-key.
parse_master_key
Parse the MasterKey sub-blob (impacket MasterKey structure).
parse_masterkey_file
Parse a DPAPI master-key file (impacket MasterKeyFile layout).
prekey_from_password
Derive the pre-key directly from a plaintext password and SID.
prekey_from_sha1
Derive the per-user pre-key from a SID and a pre-hashed SHA-1 password.