diff --git a/neurosploit-rs/crates/harness/src/ad_evidence.rs b/neurosploit-rs/crates/harness/src/ad_evidence.rs index 4cfdcde..6c7c68b 100644 --- a/neurosploit-rs/crates/harness/src/ad_evidence.rs +++ b/neurosploit-rs/crates/harness/src/ad_evidence.rs @@ -177,6 +177,23 @@ pub fn classify(tool: &str, output: &str) -> Vec { 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 { + 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 = 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 = "\ diff --git a/neurosploit-rs/crates/harness/src/ad_recipe.rs b/neurosploit-rs/crates/harness/src/ad_recipe.rs index 0b5d4d8..ebe328b 100644 --- a/neurosploit-rs/crates/harness/src/ad_recipe.rs +++ b/neurosploit-rs/crates/harness/src/ad_recipe.rs @@ -217,11 +217,50 @@ pub fn facts_from_run(workdir: Option<&str>) -> Vec { ); } } + // 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::>(&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 = 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 = 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())); diff --git a/neurosploit-rs/crates/harness/src/pipeline.rs b/neurosploit-rs/crates/harness/src/pipeline.rs index 1d5d841..1c06d1f 100644 --- a/neurosploit-rs/crates/harness/src/pipeline.rs +++ b/neurosploit-rs/crates/harness/src/pipeline.rs @@ -1635,6 +1635,16 @@ async fn authz_matrix_pass(cfg: &RunConfig, recon: &str, tx: &Sender) -> .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)]) -> 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) -> 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 = 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 = 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::>().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 = 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::>().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::>().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::>().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 = 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());