diff --git a/neurosploit-rs/crates/harness/src/goals.rs b/neurosploit-rs/crates/harness/src/goals.rs new file mode 100644 index 0000000..1ec310e --- /dev/null +++ b/neurosploit-rs/crates/harness/src/goals.rs @@ -0,0 +1,717 @@ +//! Deterministic goal decomposition and tracking for the attack chain. +//! +//! Decomposes engagement objectives into verifiable sub-goals, tracks +//! progress via loot pattern matching, and produces structured context +//! for the LLM planner so each round focuses on unmet goals. + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum GoalCategory { + Reconnaissance, + InitialAccess, + CredentialAccess, + PrivilegeEscalation, + LateralMovement, + DataAccess, + Persistence, + Impact, + Custom(String), +} + +impl std::fmt::Display for GoalCategory { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Reconnaissance => write!(f, "Reconnaissance"), + Self::InitialAccess => write!(f, "Initial Access"), + Self::CredentialAccess => write!(f, "Credential Access"), + Self::PrivilegeEscalation => write!(f, "Privilege Escalation"), + Self::LateralMovement => write!(f, "Lateral Movement"), + Self::DataAccess => write!(f, "Data Access"), + Self::Persistence => write!(f, "Persistence"), + Self::Impact => write!(f, "Impact"), + Self::Custom(s) => write!(f, "{s}"), + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum GoalStatus { + Pending, + InProgress, + Met, + Blocked, + Skipped, +} + +impl std::fmt::Display for GoalStatus { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Pending => write!(f, "PENDING"), + Self::InProgress => write!(f, "IN-PROGRESS"), + Self::Met => write!(f, "MET"), + Self::Blocked => write!(f, "BLOCKED"), + Self::Skipped => write!(f, "SKIPPED"), + } + } +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Goal { + pub id: String, + pub title: String, + pub category: GoalCategory, + pub status: GoalStatus, + pub priority: u8, + pub evidence: Vec, + pub sub_goals: Vec, + pub parent: Option, + pub round_created: usize, + pub round_completed: Option, +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize)] +pub struct GoalTracker { + goals: Vec, + next_id: usize, +} + +impl GoalTracker { + pub fn new() -> Self { + Self { goals: Vec::new(), next_id: 1 } + } + + pub fn add_goal(&mut self, title: &str, category: GoalCategory, priority: u8, round: usize) -> String { + let id = format!("G{}", self.next_id); + self.next_id += 1; + self.goals.push(Goal { + id: id.clone(), + title: title.to_string(), + category, + status: GoalStatus::Pending, + priority, + evidence: Vec::new(), + sub_goals: Vec::new(), + parent: None, + round_created: round, + round_completed: None, + }); + id + } + + pub fn add_sub_goal( + &mut self, + parent_id: &str, + title: &str, + category: GoalCategory, + priority: u8, + round: usize, + ) -> Option { + if self.goals.iter().any(|g| g.id == parent_id) { + let child_id = format!("G{}", self.next_id); + self.next_id += 1; + self.goals.push(Goal { + id: child_id.clone(), + title: title.to_string(), + category, + status: GoalStatus::Pending, + priority, + evidence: Vec::new(), + sub_goals: Vec::new(), + parent: Some(parent_id.to_string()), + round_created: round, + round_completed: None, + }); + if let Some(parent) = self.goals.iter_mut().find(|g| g.id == parent_id) { + parent.sub_goals.push(child_id.clone()); + } + Some(child_id) + } else { + None + } + } + + pub fn mark_met(&mut self, goal_id: &str, evidence: Vec, round: usize) { + if let Some(g) = self.goals.iter_mut().find(|g| g.id == goal_id) { + g.status = GoalStatus::Met; + g.evidence.extend(evidence); + g.round_completed = Some(round); + } + self.auto_check_parents(goal_id, round); + } + + pub fn mark_blocked(&mut self, goal_id: &str, reason: &str) { + if let Some(g) = self.goals.iter_mut().find(|g| g.id == goal_id) { + g.status = GoalStatus::Blocked; + g.evidence.push(format!("BLOCKED: {reason}")); + } + } + + pub fn mark_skipped(&mut self, goal_id: &str, reason: &str) { + if let Some(g) = self.goals.iter_mut().find(|g| g.id == goal_id) { + g.status = GoalStatus::Skipped; + g.evidence.push(format!("SKIPPED: {reason}")); + } + } + + pub fn mark_in_progress(&mut self, goal_id: &str) { + if let Some(g) = self.goals.iter_mut().find(|g| g.id == goal_id) { + if g.status == GoalStatus::Pending { + g.status = GoalStatus::InProgress; + } + } + } + + fn auto_check_parents(&mut self, goal_id: &str, round: usize) { + let parent_id = self.goals.iter() + .find(|g| g.id == goal_id) + .and_then(|g| g.parent.clone()); + + if let Some(pid) = parent_id { + let all_children_met = { + let parent = self.goals.iter().find(|g| g.id == pid); + if let Some(p) = parent { + !p.sub_goals.is_empty() + && p.sub_goals.iter().all(|sid| { + self.goals.iter().any(|g| g.id == *sid && g.status == GoalStatus::Met) + }) + } else { + false + } + }; + if all_children_met { + if let Some(p) = self.goals.iter_mut().find(|g| g.id == pid) { + if p.status != GoalStatus::Met { + p.status = GoalStatus::Met; + p.round_completed = Some(round); + p.evidence.push("all sub-goals met".to_string()); + } + } + self.auto_check_parents(&pid, round); + } + } + } + + pub fn get(&self, goal_id: &str) -> Option<&Goal> { + self.goals.iter().find(|g| g.id == goal_id) + } + + pub fn pending_goals(&self) -> Vec<&Goal> { + self.goals.iter().filter(|g| g.status == GoalStatus::Pending).collect() + } + + pub fn met_goals(&self) -> Vec<&Goal> { + self.goals.iter().filter(|g| g.status == GoalStatus::Met).collect() + } + + pub fn blocked_goals(&self) -> Vec<&Goal> { + self.goals.iter().filter(|g| g.status == GoalStatus::Blocked).collect() + } + + pub fn next_goals(&self) -> Vec<&Goal> { + let mut active: Vec<&Goal> = self.goals.iter() + .filter(|g| g.status == GoalStatus::Pending || g.status == GoalStatus::InProgress) + .collect(); + active.sort_by(|a, b| a.priority.cmp(&b.priority).then(a.round_created.cmp(&b.round_created))); + active + } + + pub fn all_met(&self) -> bool { + !self.goals.is_empty() + && self.goals.iter().all(|g| g.status == GoalStatus::Met || g.status == GoalStatus::Skipped) + } + + pub fn progress_pct(&self) -> f32 { + if self.goals.is_empty() { + return 0.0; + } + let met = self.goals.iter().filter(|g| g.status == GoalStatus::Met).count(); + (met as f32 / self.goals.len() as f32) * 100.0 + } + + pub fn from_profile(profile: &str) -> Self { + let mut t = Self::new(); + match profile.to_lowercase().as_str() { + "infra" | "network" | "host" => { + t.add_goal("Map all hosts and services", GoalCategory::Reconnaissance, 1, 0); + t.add_goal("Gain initial foothold on target", GoalCategory::InitialAccess, 2, 0); + t.add_goal("Obtain valid credentials", GoalCategory::CredentialAccess, 2, 0); + t.add_goal("Escalate to root/admin", GoalCategory::PrivilegeEscalation, 3, 0); + t.add_goal("Pivot to additional hosts", GoalCategory::LateralMovement, 4, 0); + t.add_goal("Prove access to sensitive data", GoalCategory::DataAccess, 4, 0); + } + "web" | "webapp" | "api" => { + t.add_goal("Map endpoints and technologies", GoalCategory::Reconnaissance, 1, 0); + t.add_goal("Bypass authentication", GoalCategory::Custom("AuthBypass".into()), 2, 0); + t.add_goal("Find injection vulnerabilities", GoalCategory::Custom("Injection".into()), 2, 0); + t.add_goal("Access unauthorized data", GoalCategory::DataAccess, 3, 0); + t.add_goal("Achieve remote code execution", GoalCategory::InitialAccess, 3, 0); + } + "mobile" | "android" | "ios" => { + t.add_goal("Static analysis of binary/source", GoalCategory::Reconnaissance, 1, 0); + t.add_goal("Find insecure data storage", GoalCategory::DataAccess, 2, 0); + t.add_goal("Find insecure communication", GoalCategory::Custom("NetworkSecurity".into()), 2, 0); + t.add_goal("Bypass authentication/authorization", GoalCategory::Custom("AuthFlaws".into()), 3, 0); + t.add_goal("Achieve code execution on device", GoalCategory::InitialAccess, 3, 0); + } + "cloud" | "aws" | "azure" | "gcp" => { + t.add_goal("Enumerate cloud resources", GoalCategory::Reconnaissance, 1, 0); + t.add_goal("Obtain cloud credentials/tokens", GoalCategory::CredentialAccess, 2, 0); + t.add_goal("Escalate IAM permissions", GoalCategory::PrivilegeEscalation, 3, 0); + t.add_goal("Access storage buckets/databases", GoalCategory::DataAccess, 3, 0); + t.add_goal("Pivot across accounts/services", GoalCategory::LateralMovement, 4, 0); + } + _ => { + t.add_goal("Reconnaissance and enumeration", GoalCategory::Reconnaissance, 1, 0); + t.add_goal("Find and exploit vulnerabilities", GoalCategory::InitialAccess, 2, 0); + t.add_goal("Prove impact", GoalCategory::Impact, 3, 0); + } + } + t + } + + pub fn from_instructions(instructions: &str, target: &str) -> Self { + let mut t = Self::new(); + let lower = instructions.to_lowercase(); + + if lower.contains("rce") || lower.contains("remote code execution") || lower.contains("command execution") { + t.add_goal(&format!("Achieve RCE on {target}"), GoalCategory::InitialAccess, 1, 0); + } + if lower.contains("escalat") || lower.contains("privesc") || lower.contains("root") || lower.contains("admin") { + t.add_goal(&format!("Escalate privileges on {target}"), GoalCategory::PrivilegeEscalation, 2, 0); + } + if lower.contains("credential") || lower.contains("password") || lower.contains("cred") { + t.add_goal(&format!("Obtain credentials on {target}"), GoalCategory::CredentialAccess, 2, 0); + } + if lower.contains("database") || lower.contains("data access") || lower.contains("sensitive data") || lower.contains("exfiltrat") { + t.add_goal(&format!("Prove data access on {target}"), GoalCategory::DataAccess, 2, 0); + } + if lower.contains("pivot") || lower.contains("lateral") { + t.add_goal(&format!("Lateral movement from {target}"), GoalCategory::LateralMovement, 3, 0); + } + if lower.contains("persist") || lower.contains("backdoor") || lower.contains("implant") { + t.add_goal(&format!("Establish persistence on {target}"), GoalCategory::Persistence, 4, 0); + } + if lower.contains("sqli") || lower.contains("sql injection") { + t.add_goal(&format!("Find SQL injection on {target}"), GoalCategory::Custom("Injection".into()), 2, 0); + } + if lower.contains("xss") || lower.contains("cross-site scripting") { + t.add_goal(&format!("Find XSS on {target}"), GoalCategory::Custom("Injection".into()), 2, 0); + } + if lower.contains("ssrf") { + t.add_goal(&format!("Find SSRF on {target}"), GoalCategory::Custom("SSRF".into()), 2, 0); + } + if lower.contains("auth") && (lower.contains("bypass") || lower.contains("break")) { + t.add_goal(&format!("Bypass authentication on {target}"), GoalCategory::Custom("AuthBypass".into()), 2, 0); + } + // Generic recon is always useful + if t.goals.is_empty() || !t.goals.iter().any(|g| g.category == GoalCategory::Reconnaissance) { + t.goals.insert(0, Goal { + id: { let id = format!("G{}", t.next_id); t.next_id += 1; id }, + title: format!("Reconnaissance of {target}"), + category: GoalCategory::Reconnaissance, + status: GoalStatus::Pending, + priority: 1, + evidence: Vec::new(), + sub_goals: Vec::new(), + parent: None, + round_created: 0, + round_completed: None, + }); + } + if t.goals.len() == 1 { + t.add_goal(&format!("Find vulnerabilities on {target}"), GoalCategory::InitialAccess, 2, 0); + t.add_goal(&format!("Prove impact on {target}"), GoalCategory::Impact, 3, 0); + } + t + } + + pub fn check_loot(&mut self, loot: &[String], round: usize) { + let lower_loot: Vec = loot.iter().map(|l| l.to_lowercase()).collect(); + + let has_verified_cred = lower_loot.iter().any(|l| l.starts_with("cred:") && l.contains("verified")); + let has_verified_vuln = lower_loot.iter().any(|l| l.starts_with("vuln:") && l.contains("verified")); + let has_privesc = lower_loot.iter().any(|l| l.starts_with("privesc:")); + let has_pivot = lower_loot.iter().any(|l| l.starts_with("pivot:")); + let has_data = lower_loot.iter().any(|l| l.starts_with("data:") || l.starts_with("file:")); + let has_rce = lower_loot.iter().any(|l| l.contains("rce:") || (l.contains("rce") && l.contains("verified"))); + let service_count = lower_loot.iter().filter(|l| l.starts_with("service:")).count(); + let endpoint_count = lower_loot.iter().filter(|l| l.starts_with("endpoint:")).count(); + let has_auth_bypass = lower_loot.iter().any(|l| l.contains("auth-bypass") || l.contains("auth_bypass")); + let has_injection = lower_loot.iter().any(|l| { + l.contains("sqli") || l.contains("xss") || l.contains("ssti") || l.contains("cmdi") + }); + let has_idor = lower_loot.iter().any(|l| l.contains("idor") || l.contains("data-leak") || l.contains("data_leak")); + let has_static = lower_loot.iter().any(|l| l.contains("sast:") || l.contains("static:") || l.contains("binary:")); + let has_insecure_storage = lower_loot.iter().any(|l| l.contains("insecure-storage") || l.contains("plaintext-cred")); + let has_insecure_comm = lower_loot.iter().any(|l| l.contains("cleartext") || l.contains("no-tls") || l.contains("insecure-comm")); + + let goal_ids: Vec<(String, GoalCategory, GoalStatus)> = self.goals.iter() + .map(|g| (g.id.clone(), g.category.clone(), g.status.clone())) + .collect(); + + for (id, cat, status) in &goal_ids { + if *status == GoalStatus::Met || *status == GoalStatus::Skipped { + continue; + } + match cat { + GoalCategory::Reconnaissance => { + if service_count >= 3 || endpoint_count >= 3 { + self.mark_met(id, vec!["auto: 3+ services/endpoints discovered".into()], round); + } else if service_count > 0 || endpoint_count > 0 { + self.mark_in_progress(id); + } + } + GoalCategory::InitialAccess => { + let title_lower = self.get(id).map(|g| g.title.to_lowercase()).unwrap_or_default(); + if title_lower.contains("rce") || title_lower.contains("remote code") { + if has_rce { + self.mark_met(id, vec!["auto: RCE verified in loot".into()], round); + } + } else if has_verified_cred || has_verified_vuln || has_rce { + self.mark_met(id, vec!["auto: initial access achieved via cred/vuln".into()], round); + } + } + GoalCategory::CredentialAccess => { + if has_verified_cred { + self.mark_met(id, vec!["auto: verified credential in loot".into()], round); + } + } + GoalCategory::PrivilegeEscalation => { + if has_privesc { + self.mark_met(id, vec!["auto: privilege escalation in loot".into()], round); + } + } + GoalCategory::LateralMovement => { + if has_pivot { + self.mark_met(id, vec!["auto: lateral movement in loot".into()], round); + } + } + GoalCategory::DataAccess => { + if has_data || has_idor { + self.mark_met(id, vec!["auto: data access proven in loot".into()], round); + } else if has_insecure_storage { + self.mark_met(id, vec!["auto: insecure data storage found".into()], round); + } + } + GoalCategory::Persistence => {} + GoalCategory::Impact => { + if has_rce || has_privesc || has_data { + self.mark_met(id, vec!["auto: impact proven via RCE/privesc/data access".into()], round); + } + } + GoalCategory::Custom(ref name) => { + let n = name.to_lowercase(); + if n.contains("authbypass") || n.contains("auth") { + if has_auth_bypass { + self.mark_met(id, vec!["auto: auth bypass in loot".into()], round); + } + } else if n.contains("injection") { + if has_injection { + self.mark_met(id, vec!["auto: injection finding in loot".into()], round); + } + } else if n.contains("network") || n.contains("comm") { + if has_insecure_comm { + self.mark_met(id, vec!["auto: insecure communication found".into()], round); + } + } else if n.contains("ssrf") { + if lower_loot.iter().any(|l| l.contains("ssrf")) { + self.mark_met(id, vec!["auto: SSRF finding in loot".into()], round); + } + } else if n.contains("static") || n.contains("analysis") { + if has_static { + self.mark_met(id, vec!["auto: static analysis findings".into()], round); + } + } + } + } + } + } + + pub fn status_summary(&self) -> String { + let mut out = String::new(); + out.push_str(&format!("GOALS: {}/{} met ({:.0}%)\n", self.met_goals().len(), self.goals.len(), self.progress_pct())); + for g in &self.goals { + let indent = if g.parent.is_some() { " " } else { "" }; + out.push_str(&format!("{indent}[{}] {} — {} (P{})\n", g.status, g.id, g.title, g.priority)); + if !g.evidence.is_empty() && g.status == GoalStatus::Met { + out.push_str(&format!("{indent} evidence: {}\n", g.evidence.last().unwrap_or(&String::new()))); + } + } + out + } + + pub fn planner_context(&self) -> String { + let mut out = String::from("GOAL STATUS:\n"); + let next = self.next_goals(); + if !next.is_empty() { + out.push_str("NEXT (focus these):\n"); + for g in &next { + out.push_str(&format!(" - [{}] {} ({}, P{})\n", g.id, g.title, g.category, g.priority)); + } + } + let met = self.met_goals(); + if !met.is_empty() { + out.push_str("MET (done):\n"); + for g in &met { + out.push_str(&format!(" - [{}] {} — {}\n", g.id, g.title, g.evidence.last().unwrap_or(&String::new()))); + } + } + let blocked = self.blocked_goals(); + if !blocked.is_empty() { + out.push_str("BLOCKED:\n"); + for g in &blocked { + out.push_str(&format!(" - [{}] {} — {}\n", g.id, g.title, g.evidence.last().unwrap_or(&String::new()))); + } + } + out.push_str(&format!("PROGRESS: {:.0}%\n", self.progress_pct())); + out + } + + pub fn to_json(&self) -> String { + serde_json::to_string_pretty(&self.goals).unwrap_or_default() + } + + pub fn goal_count(&self) -> usize { + self.goals.len() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn add_goal_returns_unique_id() { + let mut t = GoalTracker::new(); + let id1 = t.add_goal("Recon", GoalCategory::Reconnaissance, 1, 0); + let id2 = t.add_goal("Access", GoalCategory::InitialAccess, 2, 0); + assert_ne!(id1, id2); + assert!(id1.starts_with('G')); + assert!(id2.starts_with('G')); + } + + #[test] + fn add_sub_goal_links_to_parent() { + let mut t = GoalTracker::new(); + let pid = t.add_goal("Parent", GoalCategory::Reconnaissance, 1, 0); + let cid = t.add_sub_goal(&pid, "Child", GoalCategory::Reconnaissance, 1, 0).unwrap(); + assert_eq!(t.get(&cid).unwrap().parent, Some(pid.clone())); + assert!(t.get(&pid).unwrap().sub_goals.contains(&cid)); + } + + #[test] + fn add_sub_goal_invalid_parent_returns_none() { + let mut t = GoalTracker::new(); + assert!(t.add_sub_goal("FAKE", "Child", GoalCategory::Reconnaissance, 1, 0).is_none()); + } + + #[test] + fn mark_met_sets_status_and_evidence() { + let mut t = GoalTracker::new(); + let id = t.add_goal("Test", GoalCategory::Reconnaissance, 1, 0); + t.mark_met(&id, vec!["proof".into()], 3); + let g = t.get(&id).unwrap(); + assert_eq!(g.status, GoalStatus::Met); + assert_eq!(g.round_completed, Some(3)); + assert!(g.evidence.contains(&"proof".to_string())); + } + + #[test] + fn mark_met_sub_goal_auto_completes_parent() { + let mut t = GoalTracker::new(); + let pid = t.add_goal("Parent", GoalCategory::Reconnaissance, 1, 0); + let c1 = t.add_sub_goal(&pid, "C1", GoalCategory::Reconnaissance, 1, 0).unwrap(); + let c2 = t.add_sub_goal(&pid, "C2", GoalCategory::Reconnaissance, 1, 0).unwrap(); + t.mark_met(&c1, vec!["done".into()], 1); + assert_ne!(t.get(&pid).unwrap().status, GoalStatus::Met); + t.mark_met(&c2, vec!["done".into()], 2); + assert_eq!(t.get(&pid).unwrap().status, GoalStatus::Met); + } + + #[test] + fn pending_goals_excludes_met_and_skipped() { + let mut t = GoalTracker::new(); + let id1 = t.add_goal("A", GoalCategory::Reconnaissance, 1, 0); + let id2 = t.add_goal("B", GoalCategory::InitialAccess, 2, 0); + let _id3 = t.add_goal("C", GoalCategory::DataAccess, 3, 0); + t.mark_met(&id1, vec![], 1); + t.mark_skipped(&id2, "out of scope"); + assert_eq!(t.pending_goals().len(), 1); + assert_eq!(t.pending_goals()[0].title, "C"); + } + + #[test] + fn next_goals_sorted_by_priority() { + let mut t = GoalTracker::new(); + t.add_goal("Low", GoalCategory::DataAccess, 5, 0); + t.add_goal("High", GoalCategory::Reconnaissance, 1, 0); + t.add_goal("Mid", GoalCategory::InitialAccess, 3, 0); + let next = t.next_goals(); + assert_eq!(next[0].title, "High"); + assert_eq!(next[1].title, "Mid"); + assert_eq!(next[2].title, "Low"); + } + + #[test] + fn from_profile_infra_creates_six_goals() { + let t = GoalTracker::from_profile("infra"); + assert_eq!(t.goal_count(), 6); + } + + #[test] + fn from_profile_web_creates_five_goals() { + let t = GoalTracker::from_profile("web"); + assert_eq!(t.goal_count(), 5); + } + + #[test] + fn from_profile_mobile_creates_five_goals() { + let t = GoalTracker::from_profile("mobile"); + assert_eq!(t.goal_count(), 5); + } + + #[test] + fn from_profile_cloud_creates_five_goals() { + let t = GoalTracker::from_profile("cloud"); + assert_eq!(t.goal_count(), 5); + } + + #[test] + fn from_instructions_parses_rce() { + let t = GoalTracker::from_instructions("find RCE on target", "10.0.0.5"); + assert!(t.goals.iter().any(|g| g.title.contains("RCE"))); + } + + #[test] + fn from_instructions_parses_escalation() { + let t = GoalTracker::from_instructions("escalate privileges to root", "10.0.0.5"); + assert!(t.goals.iter().any(|g| g.category == GoalCategory::PrivilegeEscalation)); + } + + #[test] + fn check_loot_marks_cred_goal() { + let mut t = GoalTracker::from_profile("infra"); + t.check_loot(&["cred:ssh:root:password123@10.0.0.5:22 — VERIFIED".into()], 1); + let cred_goals: Vec<_> = t.goals.iter().filter(|g| g.category == GoalCategory::CredentialAccess).collect(); + assert!(cred_goals.iter().any(|g| g.status == GoalStatus::Met)); + } + + #[test] + fn check_loot_marks_recon_in_progress_then_met() { + let mut t = GoalTracker::from_profile("infra"); + t.check_loot(&["service:ssh".into()], 1); + let recon = t.goals.iter().find(|g| g.category == GoalCategory::Reconnaissance).unwrap(); + assert_eq!(recon.status, GoalStatus::InProgress); + + t.check_loot(&[ + "service:ssh".into(), + "service:http".into(), + "service:ftp".into(), + ], 2); + let recon = t.goals.iter().find(|g| g.category == GoalCategory::Reconnaissance).unwrap(); + assert_eq!(recon.status, GoalStatus::Met); + } + + #[test] + fn progress_pct_correct() { + let mut t = GoalTracker::new(); + let id1 = t.add_goal("A", GoalCategory::Reconnaissance, 1, 0); + let _id2 = t.add_goal("B", GoalCategory::InitialAccess, 2, 0); + t.mark_met(&id1, vec![], 1); + assert!((t.progress_pct() - 50.0).abs() < 0.01); + } + + #[test] + fn all_met_true_when_complete() { + let mut t = GoalTracker::new(); + let id1 = t.add_goal("A", GoalCategory::Reconnaissance, 1, 0); + let id2 = t.add_goal("B", GoalCategory::InitialAccess, 2, 0); + t.mark_met(&id1, vec![], 1); + t.mark_met(&id2, vec![], 2); + assert!(t.all_met()); + } + + #[test] + fn all_met_counts_skipped_as_done() { + let mut t = GoalTracker::new(); + let id1 = t.add_goal("A", GoalCategory::Reconnaissance, 1, 0); + let id2 = t.add_goal("B", GoalCategory::InitialAccess, 2, 0); + t.mark_met(&id1, vec![], 1); + t.mark_skipped(&id2, "not applicable"); + assert!(t.all_met()); + } + + #[test] + fn planner_context_includes_unmet_goals() { + let mut t = GoalTracker::from_profile("infra"); + let first_id = t.goals[0].id.clone(); + t.mark_met(&first_id, vec!["done".into()], 1); + let ctx = t.planner_context(); + assert!(ctx.contains("NEXT")); + assert!(ctx.contains("MET")); + assert!(ctx.contains("done")); + } + + #[test] + fn status_summary_readable() { + let t = GoalTracker::from_profile("web"); + let summary = t.status_summary(); + assert!(summary.contains("GOALS:")); + assert!(summary.contains("PENDING")); + assert!(summary.contains("Map endpoints")); + } + + #[test] + fn blocked_goal_not_in_next_goals() { + let mut t = GoalTracker::new(); + let id1 = t.add_goal("A", GoalCategory::Reconnaissance, 1, 0); + t.add_goal("B", GoalCategory::InitialAccess, 2, 0); + t.mark_blocked(&id1, "need auth"); + let next = t.next_goals(); + assert!(!next.iter().any(|g| g.id == id1)); + assert_eq!(next.len(), 1); + } + + #[test] + fn to_json_round_trips() { + let mut t = GoalTracker::from_profile("infra"); + let first_id = t.goals[0].id.clone(); + t.mark_met(&first_id, vec!["proof".into()], 1); + let json = t.to_json(); + let parsed: Vec = serde_json::from_str(&json).unwrap(); + assert_eq!(parsed.len(), 6); + assert_eq!(parsed[0].status, GoalStatus::Met); + } + + #[test] + fn check_loot_marks_privesc() { + let mut t = GoalTracker::from_profile("infra"); + t.check_loot(&["privesc:sudo-nopasswd-root@10.0.0.5".into()], 2); + let pe = t.goals.iter().find(|g| g.category == GoalCategory::PrivilegeEscalation).unwrap(); + assert_eq!(pe.status, GoalStatus::Met); + } + + #[test] + fn check_loot_marks_lateral_movement() { + let mut t = GoalTracker::from_profile("infra"); + t.check_loot(&["pivot:ssh-from-10.0.0.5-to-10.0.0.6".into()], 3); + let lm = t.goals.iter().find(|g| g.category == GoalCategory::LateralMovement).unwrap(); + assert_eq!(lm.status, GoalStatus::Met); + } + + #[test] + fn from_instructions_adds_recon_if_missing() { + let t = GoalTracker::from_instructions("find credentials", "host"); + assert!(t.goals.iter().any(|g| g.category == GoalCategory::Reconnaissance)); + } + + #[test] + fn mark_in_progress_only_from_pending() { + let mut t = GoalTracker::new(); + let id = t.add_goal("A", GoalCategory::Reconnaissance, 1, 0); + t.mark_met(&id, vec![], 1); + t.mark_in_progress(&id); + assert_eq!(t.get(&id).unwrap().status, GoalStatus::Met); + } +} diff --git a/neurosploit-rs/crates/harness/src/lib.rs b/neurosploit-rs/crates/harness/src/lib.rs index e508cb8..99cabbf 100644 --- a/neurosploit-rs/crates/harness/src/lib.rs +++ b/neurosploit-rs/crates/harness/src/lib.rs @@ -98,6 +98,7 @@ pub mod svc_probe; pub mod jwt_probe; pub mod nday_restore; pub mod deep_sast; +pub mod goals; pub mod keystore; pub mod threat_actors; pub mod toolbox;