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 `sAMAccountType` — the numeric object-class field.
//!
//! Values verified against MS-ADTS §2.2.20 "sAMAccountType Attribute".

/// Symbolic decode of `sAMAccountType`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SamAccountType {
    /// `SAM_DOMAIN_OBJECT` (0x0000_0000).
    DomainObject,
    /// `SAM_GROUP_OBJECT` (0x1000_0000) — a global security group.
    GroupObject,
    /// `SAM_NON_SECURITY_GROUP_OBJECT` (0x1000_0001) — a distribution group.
    NonSecurityGroupObject,
    /// `SAM_ALIAS_OBJECT` (0x2000_0000) — a domain-local security group.
    AliasObject,
    /// `SAM_NON_SECURITY_ALIAS_OBJECT` (0x2000_0001).
    NonSecurityAliasObject,
    /// `SAM_USER_OBJECT` / `SAM_NORMAL_USER_ACCOUNT` (0x3000_0000) — a regular user.
    NormalUserAccount,
    /// `SAM_MACHINE_ACCOUNT` (0x3000_0001) — a computer account.
    MachineAccount,
    /// `SAM_TRUST_ACCOUNT` (0x3000_0002) — an inter-domain trust account.
    TrustAccount,
    /// `SAM_APP_BASIC_GROUP` (0x4000_0000).
    AppBasicGroup,
    /// `SAM_APP_QUERY_GROUP` (0x4000_0001).
    AppQueryGroup,
    /// An unknown value (with the original bits preserved).
    Unknown(u32),
}

impl SamAccountType {
    /// Decode a raw `sAMAccountType` value.
    pub fn from_bits(v: u32) -> Self {
        match v {
            0x0000_0000 => SamAccountType::DomainObject,
            0x1000_0000 => SamAccountType::GroupObject,
            0x1000_0001 => SamAccountType::NonSecurityGroupObject,
            0x2000_0000 => SamAccountType::AliasObject,
            0x2000_0001 => SamAccountType::NonSecurityAliasObject,
            0x3000_0000 => SamAccountType::NormalUserAccount,
            0x3000_0001 => SamAccountType::MachineAccount,
            0x3000_0002 => SamAccountType::TrustAccount,
            0x4000_0000 => SamAccountType::AppBasicGroup,
            0x4000_0001 => SamAccountType::AppQueryGroup,
            other => SamAccountType::Unknown(other),
        }
    }

    /// True if this object type carries a password (users, computers, trusts).
    pub fn has_password(self) -> bool {
        matches!(
            self,
            SamAccountType::NormalUserAccount
                | SamAccountType::MachineAccount
                | SamAccountType::TrustAccount
        )
    }
}

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

    #[test]
    fn decode_common_values() {
        assert_eq!(
            SamAccountType::from_bits(0x3000_0000),
            SamAccountType::NormalUserAccount
        );
        assert_eq!(
            SamAccountType::from_bits(0x3000_0001),
            SamAccountType::MachineAccount
        );
        assert_eq!(
            SamAccountType::from_bits(0x1000_0000),
            SamAccountType::GroupObject
        );
    }

    #[test]
    fn unknown_preserved() {
        let v = 0xdead_beef;
        assert_eq!(SamAccountType::from_bits(v), SamAccountType::Unknown(v));
    }

    #[test]
    fn only_user_computer_trust_have_password() {
        assert!(SamAccountType::NormalUserAccount.has_password());
        assert!(SamAccountType::MachineAccount.has_password());
        assert!(SamAccountType::TrustAccount.has_password());
        assert!(!SamAccountType::GroupObject.has_password());
        assert!(!SamAccountType::AliasObject.has_password());
    }
}