dpapi-offline 0.1.0

Pure-Rust offline Windows DPAPI decryption (DonPAPI/dploot-class): parse and decrypt user masterkey files (password/SID-derived) and DPAPI data blobs. No FFI, no live host.
Documentation
//! Offline Windows **DPAPI** decryption — a DonPAPI/dploot-class core in pure Rust. No FFI, no
//! live host: point it at files copied off disk.
//!
//! - [`masterkey`] — parse a user masterkey file and decrypt it from the user's password + SID.
//! - [`blob`] — parse a `DPAPI_BLOB` (`CryptProtectData` output) and decrypt it with a masterkey.
//! - [`crypto`] — the underlying primitives.
//!
//! # VALIDATION STATUS (read this)
//!
//! This is the one crate in the ADhammer set that is a **from-scratch crypto build**, not an
//! extraction of already-live code. Its honesty bar is therefore explicit:
//!
//! * **Primitives — VALIDATED.** PBKDF2-HMAC-SHA1 (RFC 6070), HMAC-SHA1 (RFC 2202) and
//!   HMAC-SHA512 (RFC 4231) are checked against published known-answer vectors in
//!   `crypto`'s tests — a genuine external oracle, not a self-round-trip.
//! * **Parsers — VALIDATED (structure).** Deterministic layout tests for the masterkey file,
//!   masterkey subfield, and `DPAPI_BLOB`, plus truncation-safety.
//! * **The DPAPI chain — NOT YET VALIDATED end-to-end.** The masterkey/blob *derivation* follows
//!   MS-DPAPI and impacket's `dpapi.py`, but has **not** been run against a real masterkey. Do
//!   **not** treat masterkey/blob decryption as proven until [`test_real_masterkey`] passes.
//!   Validate the same way ADhammer validated WMI/DCSync: on Kali, run impacket
//!   `dpapi.py masterkey`/`credential` against the *same* files as the byte-oracle.

pub mod blob;
pub mod crypto;
pub mod masterkey;

pub use blob::DpapiBlob;
pub use masterkey::{MasterKey, MasterKeyFile};

/// Errors from parsing or decrypting DPAPI structures.
#[derive(Debug, thiserror::Error)]
pub enum DpapiError {
    #[error("parse: {0}")]
    Parse(String),
    #[error("unsupported algorithm: {0}")]
    Unsupported(String),
    #[error("CBC decryption failed")]
    Decrypt,
    #[error("masterkey HMAC verification failed (wrong password/SID?)")]
    HmacMismatch,
}

/// Convenience: decrypt a masterkey file's masterkey from a password + SID.
pub fn unlock_masterkey(
    masterkey_file: &[u8],
    password: &str,
    sid: &str,
) -> Result<[u8; 64], DpapiError> {
    let mkf = MasterKeyFile::parse(masterkey_file)?;
    let mk = mkf
        .master_key
        .ok_or_else(|| DpapiError::Parse("file has no masterkey subfield".into()))?;
    mk.decrypt_with_password(password, sid)
}

#[cfg(test)]
mod integration {
    use super::*;

    /// The one test that actually proves the crypto chain. It stays `#[ignore]` until you point it
    /// at a REAL masterkey file (copied from `%APPDATA%\Microsoft\Protect\<SID>\<guid>`) whose
    /// password/SID you know. Run it, then cross-check with impacket on Kali:
    ///   `dpapi.py masterkey -file <guid> -sid <SID> -password <pw>`
    /// and confirm the 64-byte key matches.
    ///
    /// Env: DPAPI_MK_FILE (path), DPAPI_MK_PASSWORD, DPAPI_MK_SID, optional DPAPI_MK_EXPECTED (hex).
    #[test]
    #[ignore = "needs a real masterkey file (see VALIDATION STATUS)"]
    fn test_real_masterkey() {
        let Ok(path) = std::env::var("DPAPI_MK_FILE") else {
            return;
        };
        let pw = std::env::var("DPAPI_MK_PASSWORD").expect("DPAPI_MK_PASSWORD");
        let sid = std::env::var("DPAPI_MK_SID").expect("DPAPI_MK_SID");
        let bytes = std::fs::read(&path).expect("read masterkey file");
        let key = unlock_masterkey(&bytes, &pw, &sid).expect("decrypt masterkey");
        let hexkey: String = key.iter().map(|b| format!("{b:02x}")).collect();
        eprintln!("[dpapi] recovered masterkey: {hexkey}");
        if let Ok(expected) = std::env::var("DPAPI_MK_EXPECTED") {
            assert_eq!(
                hexkey,
                expected.to_lowercase(),
                "masterkey mismatch vs oracle"
            );
        }
    }
}