ntds-parse 0.2.0

NTDS.dit-domain decode + crypto primitives — ESE file-header validation + PEK-list AES-CBC unwrap (SYSTEM bootkey → AES key) + RID→DES-key derivation for NT-hash decrypt + typed userAccountControl / sAMAccountType decoders. Byte-verified against impacket's secretsdump
Documentation
//! Decode of `userAccountControl` — the bitfield every AD account carries.
//!
//! The raw value is a `u32`; every bit is a boolean about the account state (disabled,
//! locked, no-preauth, trusted-for-delegation, DES-only, …). Rather than sprinkle
//! `uac & 0x0002 != 0` calls through downstream code, decode once into a typed struct.
//!
//! Bit values verified against MS-ADTS §2.2.16 "User-Account-Control Attribute".

/// Symbolic decode of `userAccountControl`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Uac {
    /// `SCRIPT` (0x1) — logon script executed.
    pub script: bool,
    /// `ACCOUNTDISABLE` (0x2) — the account is disabled.
    pub account_disable: bool,
    /// `HOMEDIR_REQUIRED` (0x8).
    pub homedir_required: bool,
    /// `LOCKOUT` (0x10) — the account is locked out.
    pub lockout: bool,
    /// `PASSWD_NOTREQD` (0x20) — no password required (blank-password accounts).
    pub passwd_notreqd: bool,
    /// `PASSWD_CANT_CHANGE` (0x40) — the user cannot change their own password.
    pub passwd_cant_change: bool,
    /// `ENCRYPTED_TEXT_PWD_ALLOWED` (0x80) — reversible plaintext allowed.
    pub encrypted_text_pwd_allowed: bool,
    /// `TEMP_DUPLICATE_ACCOUNT` (0x100) — a duplicate of a primary account.
    pub temp_duplicate_account: bool,
    /// `NORMAL_ACCOUNT` (0x200) — a regular user account.
    pub normal_account: bool,
    /// `INTERDOMAIN_TRUST_ACCOUNT` (0x800) — a trust account (system).
    pub interdomain_trust_account: bool,
    /// `WORKSTATION_TRUST_ACCOUNT` (0x1000) — a workstation joined to the domain.
    pub workstation_trust_account: bool,
    /// `SERVER_TRUST_ACCOUNT` (0x2000) — a domain controller.
    pub server_trust_account: bool,
    /// `DONT_EXPIRE_PASSWORD` (0x10000).
    pub dont_expire_password: bool,
    /// `MNS_LOGON_ACCOUNT` (0x20000).
    pub mns_logon_account: bool,
    /// `SMARTCARD_REQUIRED` (0x40000).
    pub smartcard_required: bool,
    /// `TRUSTED_FOR_DELEGATION` (0x80000) — Kerberos-unconstrained-delegation candidate.
    pub trusted_for_delegation: bool,
    /// `NOT_DELEGATED` (0x100000).
    pub not_delegated: bool,
    /// `USE_DES_KEY_ONLY` (0x200000).
    pub use_des_key_only: bool,
    /// `DONT_REQ_PREAUTH` (0x400000) — AS-REP-roasting candidate.
    pub dont_req_preauth: bool,
    /// `PASSWORD_EXPIRED` (0x800000).
    pub password_expired: bool,
    /// `TRUSTED_TO_AUTH_FOR_DELEGATION` (0x1000000) — constrained delegation w/ protocol
    /// transition (S4U2Self capable).
    pub trusted_to_auth_for_delegation: bool,
    /// `PARTIAL_SECRETS_ACCOUNT` (0x4000000) — read-only DC computer account.
    pub partial_secrets_account: bool,
}

impl Uac {
    /// Decode a raw `userAccountControl` value.
    pub fn from_bits(v: u32) -> Self {
        Uac {
            script: v & 0x0000_0001 != 0,
            account_disable: v & 0x0000_0002 != 0,
            homedir_required: v & 0x0000_0008 != 0,
            lockout: v & 0x0000_0010 != 0,
            passwd_notreqd: v & 0x0000_0020 != 0,
            passwd_cant_change: v & 0x0000_0040 != 0,
            encrypted_text_pwd_allowed: v & 0x0000_0080 != 0,
            temp_duplicate_account: v & 0x0000_0100 != 0,
            normal_account: v & 0x0000_0200 != 0,
            interdomain_trust_account: v & 0x0000_0800 != 0,
            workstation_trust_account: v & 0x0000_1000 != 0,
            server_trust_account: v & 0x0000_2000 != 0,
            dont_expire_password: v & 0x0001_0000 != 0,
            mns_logon_account: v & 0x0002_0000 != 0,
            smartcard_required: v & 0x0004_0000 != 0,
            trusted_for_delegation: v & 0x0008_0000 != 0,
            not_delegated: v & 0x0010_0000 != 0,
            use_des_key_only: v & 0x0020_0000 != 0,
            dont_req_preauth: v & 0x0040_0000 != 0,
            password_expired: v & 0x0080_0000 != 0,
            trusted_to_auth_for_delegation: v & 0x0100_0000 != 0,
            partial_secrets_account: v & 0x0400_0000 != 0,
        }
    }

    /// True if this account is *not* smartcard-only — a necessary but not sufficient
    /// precondition for Kerberoasting (the actual check needs a servicePrincipalName, a
    /// separate LDAP attribute this crate doesn't see).
    pub fn not_smartcard_only(&self) -> bool {
        !self.smartcard_required
    }

    /// Convenience — is this an AS-REP-roasting candidate? [`Self::dont_req_preauth`] set.
    pub fn asreproast_eligible(&self) -> bool {
        self.dont_req_preauth
    }
}

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

    #[test]
    fn typical_normal_user_bits() {
        // 0x200 (NORMAL_ACCOUNT) | 0x10000 (DONT_EXPIRE_PASSWORD) = 0x10200
        let u = Uac::from_bits(0x10200);
        assert!(u.normal_account);
        assert!(u.dont_expire_password);
        assert!(!u.account_disable);
        assert!(!u.dont_req_preauth);
    }

    #[test]
    fn disabled_and_asreproast_ready() {
        // ACCOUNTDISABLE (0x2) + DONT_REQ_PREAUTH (0x400000)
        let u = Uac::from_bits(0x400002);
        assert!(u.account_disable);
        assert!(u.dont_req_preauth);
        assert!(u.asreproast_eligible());
    }

    #[test]
    fn dc_computer_account() {
        // SERVER_TRUST_ACCOUNT | TRUSTED_FOR_DELEGATION = 0x2000 | 0x80000
        let u = Uac::from_bits(0x82000);
        assert!(u.server_trust_account);
        assert!(u.trusted_for_delegation);
    }

    #[test]
    fn not_smartcard_only_gate() {
        let normal = Uac::from_bits(0x200);
        assert!(normal.not_smartcard_only());
        let sc_only = Uac::from_bits(0x200 | 0x40000);
        assert!(!sc_only.not_smartcard_only());
    }
}