Skip to main content

inspect_blob/
inspect_blob.rs

1//! Inspect a real DPAPI-NG blob: `msLAPS-EncryptedPassword`, or a gMSA/dMSA managed-password
2//! blob, straight off the wire.
3//!
4//! ```text
5//! cargo run --example inspect_blob -- laps.bin            # with the LAPS 16-byte header
6//! cargo run --example inspect_blob -- --raw enveloped.bin # bare CMS EnvelopedData
7//! ```
8//!
9//! Everything printed here is derived without any key: which KDS root key and tree node the
10//! content was wrapped for is public metadata. Decryption needs a `GetKey` response, which is
11//! an RPC call this crate deliberately does not make.
12
13use dpapi_ng::{cms::ProtectedBlob, LapsBlob};
14use std::process::ExitCode;
15
16fn main() -> ExitCode {
17    let args: Vec<String> = std::env::args().skip(1).collect();
18    let raw_mode = args.iter().any(|a| a == "--raw");
19    let Some(path) = args.iter().find(|a| !a.starts_with("--")) else {
20        eprintln!("usage: inspect_blob [--raw] <blob.bin>");
21        return ExitCode::FAILURE;
22    };
23
24    let bytes = match std::fs::read(path) {
25        Ok(b) => b,
26        Err(e) => {
27            eprintln!("cannot read {path}: {e}");
28            return ExitCode::FAILURE;
29        }
30    };
31
32    let blob_bytes = if raw_mode {
33        bytes
34    } else {
35        match LapsBlob::parse(&bytes) {
36            Ok(l) => {
37                println!(
38                    "LAPS header: updated {} (FILETIME {}), {} byte blob",
39                    filetime_to_iso(l.update_timestamp),
40                    l.update_timestamp,
41                    l.blob.len()
42                );
43                l.blob
44            }
45            Err(e) => {
46                eprintln!(
47                    "not a LAPS attribute value ({e}); retry with --raw if this is a bare CMS blob"
48                );
49                return ExitCode::FAILURE;
50            }
51        }
52    };
53
54    let blob = match ProtectedBlob::parse(&blob_bytes) {
55        Ok(b) => b,
56        Err(e) => {
57            eprintln!("CMS parse failed: {e}");
58            return ExitCode::FAILURE;
59        }
60    };
61
62    let id = &blob.key_identifier;
63    println!("key identifier");
64    println!("  version            {}", id.version);
65    println!(
66        "  flags              {:#010x}{}",
67        id.flags,
68        if id.is_public_key() {
69            " (public key)"
70        } else {
71            ""
72        }
73    );
74    println!("  root key           {}", id.root_key_id);
75    println!(
76        "  tree node          L0 {} · L1 {} · L2 {}",
77        id.l0, id.l1, id.l2
78    );
79    println!("  kdf                {} ", id.kdf_algorithm);
80    match id.hash() {
81        Ok(h) => println!("  kdf hash           {}", h.name()),
82        Err(e) => println!("  kdf hash           UNPARSED: {e}"),
83    }
84    println!("  secret algorithm   {}", id.secret_algorithm);
85    println!(
86        "  private/public len {} / {}",
87        id.private_key_length, id.public_key_length
88    );
89    println!(
90        "  domain / forest    {} / {}",
91        id.domain_name, id.forest_name
92    );
93    println!("content");
94    println!("  key wrap           {}", blob.key_wrap_oid);
95    println!("  content cipher     {}", blob.content_cipher_oid);
96    println!(
97        "  nonce              {} ({} bytes)",
98        hex(&blob.nonce),
99        blob.nonce.len()
100    );
101    println!("  tag length         {}", blob.tag_len);
102    println!("  wrapped key        {} bytes", blob.encrypted_key.len());
103    println!(
104        "  ciphertext         {} bytes",
105        blob.encrypted_content.len()
106    );
107    println!(
108        "\nTo decrypt: ask a DC for root key {} at L0 {} (MS-GKDI GetKey), then\n\
109         GroupKeyEnvelope::parse(response) and dpapi_ng::decrypt(&blob, &envelope).",
110        id.root_key_id, id.l0
111    );
112
113    ExitCode::SUCCESS
114}
115
116fn hex(b: &[u8]) -> String {
117    b.iter().map(|x| format!("{x:02x}")).collect()
118}
119
120/// FILETIME → a readable UTC stamp, without pulling in a date crate.
121fn filetime_to_iso(ft: u64) -> String {
122    let unix = (ft / 10_000_000) as i64 - 11_644_473_600;
123    if unix <= 0 {
124        return "never".into();
125    }
126    let (mut days, secs) = (unix / 86_400, unix % 86_400);
127    let (mut y, mut m) = (1970i64, 1i64);
128    loop {
129        let leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
130        let len = if leap { 366 } else { 365 };
131        if days < len {
132            break;
133        }
134        days -= len;
135        y += 1;
136    }
137    let leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
138    let ml = [
139        31,
140        if leap { 29 } else { 28 },
141        31,
142        30,
143        31,
144        30,
145        31,
146        31,
147        30,
148        31,
149        30,
150        31,
151    ];
152    while days >= ml[(m - 1) as usize] {
153        days -= ml[(m - 1) as usize];
154        m += 1;
155    }
156    format!(
157        "{y:04}-{m:02}-{:02}T{:02}:{:02}:{:02}Z",
158        days + 1,
159        secs / 3600,
160        (secs % 3600) / 60,
161        secs % 60
162    )
163}