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
# ntds-parse

[![Crates.io](https://img.shields.io/crates/v/ntds-parse.svg)](https://crates.io/crates/ntds-parse)
[![Docs.rs](https://docs.rs/ntds-parse/badge.svg)](https://docs.rs/ntds-parse)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

NTDS.dit-domain decode + crypto primitives for Rust. The layer between an ESE row walker and `secretsdump.py`-shaped output.

Part of the [icedracon](https://github.com/icedracon) offensive-AD / DFIR ecosystem.

## v0.2 — crypto inlined

The full offline-decryption chain runs in-tree:

```text
SYSTEM hive → bootkey (16 bytes, from windows-secrets)
                  │
                  ▼
   unwrap_peklist(bootkey, PEKLIST_bytes) ──▶ PekList { keys: [PekKey; N] }
              (SHA-256 KDF + AES-256-CBC for revision 3;
               MD5 KDF + RC4 for legacy revision 2)
                  │
                  ▼
   decrypt_nt_hash(unicodePwd_bytes, &PekList, rid) ──▶ [u8; 16]
              (AES-128-CBC unwrap with PEK[header.pek_index], IV = KeyMaterial
               → DES-ECB × 2 with keys derived from the account's RID)
```

Round-trip tests cover both PEK revisions and the full NT-hash unwrap chain end-to-end,
verified against captured wire samples.

## What's in v0.2

- **`NtdsFile::open`** — ESE file-header validation (magic `0x89ABCDEF`, format version, page size, file-type).
- **`pek::unwrap_peklist`** — SHA-256/MD5-KDF + AES-256-CBC/RC4 decrypt of the encrypted PEKLIST blob.
- **`pek::PekList::parse_plaintext`** — decode the decrypted plaintext into indexed `PekKey`s; accepts AES-block zero-padding, rejects torn buffers.
- **`hash::decrypt_nt_hash`** — full 2-layer unwrap: AES-128-CBC + DES-ECB using RID-derived keys.
- **`hash::derive_des_keys_from_rid` + `hash::expand_des_key`** — the SAM/NTDS RID→DES key derivation, corrected for the `out[0]` parity-slot bug.
- **`uac::Uac`** — 22-field typed decode of `userAccountControl` + `not_smartcard_only()` + `asreproast_eligible()` convenience.
- **`sam::SamAccountType`** — enum decode with `has_password()`.

## What v0.2 does *not* do (roadmap)

- **Full ESE row walk.** Iterating `datatable` and `link_table` needs a B-tree traversal + long-value / multi-value column reassembly. Wire the [`EseBackend`] trait to `libesedb` FFI, the `esed` crate, or wait for the standalone `ese-parser` crate that's on the icedracon roadmap.
- **LM hash + supplementalCredentials (Kerberos AES / PBKDF2 keys, cleartext, etc.)** — same crypto stack as NT hash; scope for v0.3.

## Deps

`aes` · `cbc` · `des` · `sha2` · `md-5` · `rc4` · `cipher` — all RustCrypto. Zero non-crypto deps. No optional features to configure; a call to `unwrap_peklist` + `decrypt_nt_hash` pulls the full stack.

## Usage

```rust,no_run
use ntds_parse::{NtdsFile, pek, hash};

let f = NtdsFile::open("C:\\Windows\\NTDS\\ntds.dit")?;
println!("format v{}, {}-byte pages", f.format_version(), f.page_size());

// 1. Unwrap the PEK using the SYSTEM hive bootkey (from `windows-secrets`)
let bootkey: [u8; 16] = *b"boot_key_16bytes";
let peklist_bytes: Vec<u8> = /* from datatable[Pek-List].pekList */ vec![];
let pek_list = pek::unwrap_peklist(&bootkey, &peklist_bytes)?;

// 2. Decrypt one user's NT hash
let encrypted_nt: Vec<u8> = /* from datatable[user].unicodePwd */ vec![];
let rid: u32 = 1105;
let nt_hash = hash::decrypt_nt_hash(&encrypted_nt, &pek_list, rid)?;
println!("NT: {}", hash::hex(&nt_hash));
# Ok::<_, ntds_parse::Error>(())
```

## Related icedracon crates

- [`lsass-parse`](https://crates.io/crates/lsass-parse) — live-memory sibling (offline LSASS-minidump)
- [`windows-secrets`](https://crates.io/crates/windows-secrets) — SYSTEM/SECURITY hive → bootkey, feeds `unwrap_peklist`
- [`samr`](https://crates.io/crates/samr) — live-RPC alternative
- [ADhammer](https://github.com/icedracon/adhammer) — full driver

## License

MIT © 2026 [zevs](https://github.com/icedracon)