diff --git a/crates/gphip/src/lib.rs b/crates/gphip/src/lib.rs index 79dae6f..35c4c3f 100644 --- a/crates/gphip/src/lib.rs +++ b/crates/gphip/src/lib.rs @@ -7,6 +7,9 @@ use std::collections::HashMap; use std::process::Command; use xmltree::Element; +#[cfg(target_os = "macos")] +mod sentinel_macos; + pub const MAX_EDITED_REPORT_BYTES: usize = 48 * 1024; /// Validate an edited report before it is stored or used as a submission body. @@ -252,6 +255,12 @@ struct ClamAvInfo { real_time_protection: bool, } +struct SentinelInfo { + version: String, + real_time_protection: bool, + firewall_enabled: Option, +} + /// Host information for HIP reporting struct HostInfo { /// Common for all OSes, e.g., "Apple", "Microsoft", "Linux" @@ -268,6 +277,7 @@ struct HostInfo { network_interfaces: Vec, defender: Option, clamav: Option, + sentinel: Option, ufw: Option, } @@ -402,6 +412,7 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> { network_interfaces: interfaces, defender: self.defender_for_profile(), clamav: self.clamav_for_profile(), + sentinel: self.sentinel_for_profile(), ufw: self.ufw_for_profile(), } } @@ -470,6 +481,14 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> { ClientOs::Mac | ClientOs::Windows => None, } } + + fn sentinel_for_profile(&self) -> Option { + #[cfg(target_os = "macos")] + if self.profile.client_os() == ClientOs::Mac && self.profile.is_native() { + return sentinel_macos::detect(); + } + None + } } fn detect_clamav() -> Option { @@ -771,6 +790,37 @@ mod tests { validate_edited_report(&report).unwrap(); } + #[test] + fn mac_report_renders_detected_sentinel_products() { + let input = make_input(make_profile(ClientOs::Mac)); + let cookie_params = HashMap::new(); + let mut host_info = HostInfoCollector::new(&input.profile, &input, &cookie_params).collect(); + host_info.sentinel = Some(SentinelInfo { + version: "25.3.4.8365".to_string(), + real_time_protection: true, + firewall_enabled: Some(false), + }); + let (generate_time, day, month, year) = get_current_time_components(); + let report = HipReportTemplate { + client_version: input.profile.client_version(), + generate_time, + day, + month, + year, + user_name: "test-user", + host_info, + md5: "test-digest", + } + .render() + .unwrap(); + let report = format_xml(&report).unwrap(); + validate_edited_report(&report).unwrap(); + assert!(report.contains("vendor=\"SentinelOne\" name=\"Sentinel Agent\" version=\"25.3.4.8365\"")); + assert!(report.contains("yes")); + assert!(report.contains("no")); + assert_eq!(report.matches("name=\"Sentinel Agent\"").count(), 2); + } + #[test] fn preview_uses_explicit_pending_digest() { let input = ReportInput { diff --git a/crates/gphip/src/sentinel_macos.rs b/crates/gphip/src/sentinel_macos.rs new file mode 100644 index 0000000..3e30cd2 --- /dev/null +++ b/crates/gphip/src/sentinel_macos.rs @@ -0,0 +1,143 @@ +use std::{ + fs, + os::unix::fs::{MetadataExt, PermissionsExt}, + path::Path, + process::Command, +}; + +use super::SentinelInfo; + +const SENTINELCTL: &str = "/Library/Sentinel/sentinel-agent.bundle/Contents/MacOS/sentinelctl"; + +pub(super) fn detect() -> Option { + if !trusted_cli(Path::new(SENTINELCTL)) { + return None; + } + + let status = run(&["status"])?; + let status = parse_status(&status)?; + let real_time_protection = if status.active { + let scan_status = run(&["scan-on-write"])?; + parse_yes_no(scan_status.trim().strip_prefix("scan-on-write:")?)? + } else { + false + }; + let firewall_enabled = run(&["firewall", "enabled"]).as_deref().and_then(parse_yes_no); + + Some(SentinelInfo { + version: status.version, + real_time_protection, + firewall_enabled, + }) +} + +fn trusted_cli(path: &Path) -> bool { + path.ancestors().take_while(|part| part.as_os_str() != "/").all(|part| { + let Ok(metadata) = fs::symlink_metadata(part) else { + return false; + }; + if metadata.uid() != 0 || metadata.permissions().mode() & 0o022 != 0 || metadata.file_type().is_symlink() { + return false; + } + if part == path { + metadata.is_file() + } else { + metadata.is_dir() + } + }) +} + +fn run(args: &[&str]) -> Option { + let output = Command::new(SENTINELCTL) + .args(args) + .env("LC_ALL", "C") + .env_remove("SENTINEL_OUTPUT_JSON") + .output() + .ok()?; + if !output.status.success() { + return None; + } + String::from_utf8(output.stdout).ok() +} + +struct AgentStatus { + version: String, + active: bool, +} + +fn parse_status(output: &str) -> Option { + let version = status_value(output, "Version:")?; + if version.is_empty() + || version.len() > 64 + || !version + .chars() + .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '.' | '-' | '_')) + { + return None; + } + + let operational = parse_status_bool(output, "Agent Operational State:", "enabled", "disabled")?; + let ready = parse_status_bool(output, "Ready:", "yes", "no")?; + let protected = parse_status_bool(output, "Protection:", "enabled", "disabled")?; + Some(AgentStatus { + version: version.to_string(), + active: operational && ready && protected, + }) +} + +fn status_value<'a>(output: &'a str, label: &str) -> Option<&'a str> { + output + .lines() + .find_map(|line| line.trim().strip_prefix(label).map(str::trim)) +} + +fn parse_status_bool(output: &str, label: &str, yes: &str, no: &str) -> Option { + match status_value(output, label)? { + value if value == yes => Some(true), + value if value == no => Some(false), + _ => None, + } +} + +fn parse_yes_no(output: &str) -> Option { + match output.trim() { + "yes" => Some(true), + "no" => Some(false), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_running_agent_status() { + let status = parse_status( + "Agent\n Version: 25.3.4.8365\n Agent Operational State: enabled\n Ready: yes\n Protection: enabled\n", + ) + .unwrap(); + assert_eq!(status.version, "25.3.4.8365"); + assert!(status.active); + } + + #[test] + fn rejects_unknown_agent_state() { + assert!(parse_status("Version: 25.3.4.8365\nProtection: maybe\n").is_none()); + assert!( + !parse_status("Version: 25.3.4.8365\nAgent Operational State: disabled\nReady: yes\nProtection: enabled\n") + .unwrap() + .active + ); + } + + #[test] + fn parses_scan_and_firewall_states() { + assert_eq!( + parse_yes_no("scan-on-write:\tyes\n".trim().strip_prefix("scan-on-write:").unwrap()), + Some(true) + ); + assert_eq!(parse_yes_no("no\n"), Some(false)); + assert_eq!(parse_yes_no("unknown\n"), None); + } +} diff --git a/crates/gphip/templates/hip_report.xml b/crates/gphip/templates/hip_report.xml index 82a0051..55c556c 100644 --- a/crates/gphip/templates/hip_report.xml +++ b/crates/gphip/templates/hip_report.xml @@ -139,6 +139,15 @@ {% elif host_info.os_vendor == "Apple" -%} + {% if let Some(sentinel) = &host_info.sentinel %} + + + + {% if sentinel.real_time_protection %}yes{% else %}no{% endif %} + n/a + + + {% endif -%} @@ -190,6 +199,16 @@ + {% if let Some(sentinel) = &host_info.sentinel %} + {% if let Some(firewall_enabled) = sentinel.firewall_enabled %} + + + + {% if firewall_enabled %}yes{% else %}no{% endif %} + + + {% endif -%} + {% endif -%}