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<u64length 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§
- Master
Key - The
MasterKeysub-blob: salt + rounds + alg IDs + encrypted payload. - Master
KeyFile - 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 (
DomainKeysub-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
MasterKeysub-blob (impacketMasterKeystructure). - parse_
masterkey_ file - Parse a DPAPI master-key file (impacket
MasterKeyFilelayout). - 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.