feat: classify AD/host receipts in the phased pipeline

- add ad_evidence::classify_auto (all tool families, deduped by fact)
- persist receipts to graphs/ad-facts.json via ad_recipe::record and read
  them in facts_from_run
- classify recon/tool output, every agent transcript, and each chain
  round so the AD/privesc recipe chain advances in the phased pipeline
This commit is contained in:
CyberSecurityUP committed 2026-12-02 15:15:41 -03:00
1 parent f91fe942c7
commit 20676c3b56
3 files changed
+120

No files matched your search

@@ -177,6 +177,23 @@ pub fn classify(tool: &str, output: &str) -> Vec<AdReceipt> {
c.out
}
/// Run every tool-family classifier over the text and merge the receipts. Use
/// when the tool name is not known (e.g. a full agent transcript or the combined
/// recon/tool output). Family-gated checks run once per family; host-privesc
/// checks are family-independent and dedupe on their fact string.
pub fn classify_auto(output: &str) -> Vec<AdReceipt> {
let mut seen = std::collections::HashSet::new();
let mut out = Vec::new();
for tool in ["netexec", "impacket", "certipy", "ldap"] {
for r in classify(tool, output) {
if seen.insert(r.fact()) {
out.push(r);
}
}
}
out
}
#[cfg(test)]
mod tests {
use super::*;
@@ -185,6 +202,18 @@ mod tests {
classify(tool, out).into_iter().map(|r| r.kind).collect()
}
#[test]
fn auto_classifier_merges_tool_families() {
let out = "\
SMB 10.0.0.5 445 DC01 [+] CORP\\alice:Passw0rd (Pwn3d!)
[*] Dumping Domain Credentials (domain\\uid:rid:lmhash:nthash)
[+] DRSUAPI approach worked
";
let k: Vec<String> = classify_auto(out).into_iter().map(|r| r.kind).collect();
assert!(k.contains(&"smbadmin".to_string()));
assert!(k.contains(&"dcsync".to_string()));
}
#[test]
fn netexec_admin_and_ldap_bind() {
let out = "\
@@ -217,11 +217,50 @@ pub fn facts_from_run(workdir: Option<&str>) -> Vec<String> {
);
}
}
// Facts recorded by the phased pipeline live in their own small file (it
// does not maintain a dual graph during the run).
if let Ok(raw) = std::fs::read_to_string(graphs.join("ad-facts.json")) {
if let Ok(list) = serde_json::from_str::<Vec<String>>(&raw) {
out.extend(list);
}
}
out.sort();
out.dedup();
out
}
/// Merge classified receipts into the run's `graphs/ad-facts.json` and return
/// how many were new. Both engines and the phased pipeline read this file, so
/// the recipe chain is engine-independent.
pub fn record(workdir: Option<&str>, receipts: &[crate::ad_evidence::AdReceipt]) -> usize {
let Some(wd) = workdir else { return 0 };
if receipts.is_empty() {
return 0;
}
let dir = std::path::Path::new(wd).join("graphs");
let path = dir.join("ad-facts.json");
let mut facts: BTreeSet<String> = std::fs::read_to_string(&path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or_default();
let before = facts.len();
for r in receipts {
facts.insert(r.fact());
}
let total = facts.len();
if total == before {
return 0;
}
if std::fs::create_dir_all(&dir).is_err() {
return 0;
}
let list: Vec<String> = facts.into_iter().collect();
if let Ok(body) = serde_json::to_vec_pretty(&list) {
let _ = std::fs::write(&path, body);
}
total.saturating_sub(before)
}
/// Prompt block with the next fact-gated AD/host steps. The harness injects
/// this automatically; the operator never has to run `adchain plan`.
pub fn prompt_block(workdir: Option<&str>) -> String {
@@ -316,6 +355,20 @@ mod tests {
assert!(got.contains(&"dump-local"));
}
#[test]
fn record_merges_into_ad_facts_and_is_idempotent() {
let dir = std::env::temp_dir().join(format!("ns-adrec-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let receipts = crate::ad_evidence::classify_auto(
"SMB 10.0.0.5 445 DC01 [+] CORP\\alice:Passw0rd (Pwn3d!)",
);
assert_eq!(record(Some(dir.to_str().unwrap()), &receipts), receipts.len());
assert_eq!(record(Some(dir.to_str().unwrap()), &receipts), 0);
let facts = facts_from_run(Some(dir.to_str().unwrap()));
assert!(facts.iter().any(|f| f.starts_with("ad:smbadmin:")));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn facts_from_run_reads_the_dual_graph_too() {
let dir = std::env::temp_dir().join(format!("ns-adfacts-{}", std::process::id()));
@@ -1635,6 +1635,16 @@ async fn authz_matrix_pass(cfg: &RunConfig, recon: &str, tx: &Sender<String>) ->
.await
}
/// Classify agent transcripts for deterministic AD/host receipts and record
/// them so the fact-gated recipe chain advances in the phased pipeline too.
fn record_ad_receipts(workdir: Option<&str>, raw: &[(String, String, Vec<Finding>)]) -> usize {
let mut receipts = Vec::new();
for (_, text, _) in raw {
receipts.extend(crate::ad_evidence::classify_auto(text));
}
crate::ad_recipe::record(workdir, &receipts)
}
pub async fn run(mut cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender<String>) -> RunOutput {
pool.set_progress(tx.clone());
pool.set_token_limit(cfg.budget.token_limit);
@@ -2023,6 +2033,16 @@ pub async fn run(mut cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender
}
let probe_facts = format!("{probe_facts}{cve_block}");
// Deterministic AD/host receipts from recon + tool output, recorded so the
// recipe chain works in the phased pipeline too (shared with reactive).
let recon_receipts = crate::ad_evidence::classify_auto(&probe_facts);
let ad_new = crate::ad_recipe::record(cfg.workdir.as_deref(), &recon_receipts);
if ad_new > 0 {
let _ = tx
.send(format!("🧾 AD/host receipts from recon: {ad_new} new fact(s)"))
.await;
}
// Inject Cloudflare/WAF bypass context when a challenge was detected.
let probe_facts = if waf_challenge {
format!("{CF_BYPASS_OPS}\n{probe_facts}")
@@ -2505,6 +2525,7 @@ pub async fn run(mut cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender
crate::resilience::load_salvaged(wd)
} else { Vec::new() };
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = transcript_of(&raw);
let mut all_findings: Vec<Finding> = raw.iter().flat_map(|(_, _, f)| f.clone()).collect();
@@ -2753,6 +2774,7 @@ pub async fn run_whitebox(cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: S
.collect()
.await;
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = transcript_of(&raw);
let mut candidates: Vec<Finding> = dedup_findings(raw.iter().flat_map(|(_, _, f)| f.clone()).collect());
@@ -2968,6 +2990,7 @@ pub async fn run_greybox(cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Se
.buffer_unordered(cfg.concurrency)
.collect::<Vec<_>>().await;
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = format!("{}\n{}", code_leads, transcript_of(&raw));
let mut candidates = dedup_findings(raw.iter().flat_map(|(_, _, f)| f.clone()).collect());
@@ -3444,6 +3467,16 @@ async fn attack_chain(pool: &ModelPool, cfg: &RunConfig, recon: &str,
}
round_cands.extend(fs);
}
// Receipts from this round's loot/evidence gate the next round's steps.
{
let mut text = loot.join("\n");
for f in &round_cands {
text.push_str(&f.evidence);
text.push_str(&f.endpoint);
}
let rec = crate::ad_evidence::classify_auto(&text);
let _ = crate::ad_recipe::record(cfg.workdir.as_deref(), &rec);
}
// Keep only genuinely NEW findings (unseen key).
let fresh: Vec<Finding> = dedup_findings(round_cands)
.into_iter()
@@ -6437,6 +6470,7 @@ pub async fn run_container(cfg: RunConfig, lib: &Library, pool: &ModelPool, tx:
.buffer_unordered(cfg.concurrency)
.collect::<Vec<_>>().await;
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = transcript_of(&raw);
let candidates = dedup_findings(raw.iter().flat_map(|(_, _, f)| f.clone()).collect());
@@ -6641,6 +6675,7 @@ pub async fn run_mobile(cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sen
.buffer_unordered(cfg.concurrency)
.collect::<Vec<_>>().await;
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = transcript_of(&raw);
let candidates = dedup_findings(raw.iter().flat_map(|(_, _, f)| f.clone()).collect());
@@ -6839,6 +6874,7 @@ pub async fn run_host(cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sende
.buffer_unordered(cfg.concurrency)
.collect::<Vec<_>>().await;
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = transcript_of(&raw);
let mut candidates = dedup_findings(raw.iter().flat_map(|(_, _, f)| f.clone()).collect());
@@ -7750,6 +7786,7 @@ pub async fn run_ai(cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender<
.collect()
.await;
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = transcript_of(&raw);
let mut candidates: Vec<Finding> = dedup_findings(raw.iter().flat_map(|(_, _, f)| f.clone()).collect());
@@ -7821,6 +7858,7 @@ pub async fn run_skills_audit(cfg: RunConfig, lib: &Library, pool: &ModelPool, t
.buffer_unordered(cfg.concurrency)
.collect()
.await;
let _ = record_ad_receipts(cfg.workdir.as_deref(), &raw);
crate::traces::record_all(cfg.workdir.as_deref(), &raw);
let transcript = transcript_of(&raw);
let candidates = dedup_findings(raw.iter().flat_map(|(_, _, f)| f.clone()).collect());