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!("not a LAPS attribute value ({e}); retry with --raw if this is a bare CMS blob");
47                return ExitCode::FAILURE;
48            }
49        }
50    };
51
52    let blob = match ProtectedBlob::parse(&blob_bytes) {
53        Ok(b) => b,
54        Err(e) => {
55            eprintln!("CMS parse failed: {e}");
56            return ExitCode::FAILURE;
57        }
58    };
59
60    let id = &blob.key_identifier;
61    println!("key identifier");
62    println!("  version            {}", id.version);
63    println!("  flags              {:#010x}{}", id.flags, if id.is_public_key() { " (public key)" } else { "" });
64    println!("  root key           {}", id.root_key_id);
65    println!("  tree node          L0 {} · L1 {} · L2 {}", id.l0, id.l1, id.l2);
66    println!("  kdf                {} ", id.kdf_algorithm);
67    match id.hash() {
68        Ok(h) => println!("  kdf hash           {}", h.name()),
69        Err(e) => println!("  kdf hash           UNPARSED: {e}"),
70    }
71    println!("  secret algorithm   {}", id.secret_algorithm);
72    println!("  private/public len {} / {}", id.private_key_length, id.public_key_length);
73    println!("  domain / forest    {} / {}", id.domain_name, id.forest_name);
74    println!("content");
75    println!("  key wrap           {}", blob.key_wrap_oid);
76    println!("  content cipher     {}", blob.content_cipher_oid);
77    println!("  nonce              {} ({} bytes)", hex(&blob.nonce), blob.nonce.len());
78    println!("  tag length         {}", blob.tag_len);
79    println!("  wrapped key        {} bytes", blob.encrypted_key.len());
80    println!("  ciphertext         {} bytes", blob.encrypted_content.len());
81    println!(
82        "\nTo decrypt: ask a DC for root key {} at L0 {} (MS-GKDI GetKey), then\n\
83         GroupKeyEnvelope::parse(response) and dpapi_ng::decrypt(&blob, &envelope).",
84        id.root_key_id, id.l0
85    );
86
87    ExitCode::SUCCESS
88}
89
90fn hex(b: &[u8]) -> String {
91    b.iter().map(|x| format!("{x:02x}")).collect()
92}
93
94/// FILETIME → a readable UTC stamp, without pulling in a date crate.
95fn filetime_to_iso(ft: u64) -> String {
96    let unix = (ft / 10_000_000) as i64 - 11_644_473_600;
97    if unix <= 0 {
98        return "never".into();
99    }
100    let (mut days, secs) = (unix / 86_400, unix % 86_400);
101    let (mut y, mut m) = (1970i64, 1i64);
102    loop {
103        let leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
104        let len = if leap { 366 } else { 365 };
105        if days < len {
106            break;
107        }
108        days -= len;
109        y += 1;
110    }
111    let leap = (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
112    let ml = [31, if leap { 29 } else { 28 }, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
113    while days >= ml[(m - 1) as usize] {
114        days -= ml[(m - 1) as usize];
115        m += 1;
116    }
117    format!(
118        "{y:04}-{m:02}-{:02}T{:02}:{:02}:{:02}Z",
119        days + 1,
120        secs / 3600,
121        (secs % 3600) / 60,
122        secs % 60
123    )
124}