feat: collect native HIP facts and log redacted XML responses

This commit is contained in:
Kevin Yue committed 2026-11-18 13:56:12 +08:00
1 parent f2ca674b88
commit bde11186f8
26 files changed
+1567 -206

No files matched your search

+311 -40
View File
@@ -9,11 +9,20 @@ use gpapi::collection::{CollectionBudget, CollectionControl};
use gpapi::process::collection::CollectorCommands;
use xmltree::Element;
#[cfg(target_os = "macos")]
mod domain_macos;
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))]
mod domain_unix;
#[cfg(target_os = "macos")]
mod sentinel_macos;
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
mod sentinel_unix;
#[cfg(windows)]
mod windows;
#[cfg(any(windows, test))]
mod windows_report;
pub const MAX_EDITED_REPORT_BYTES: usize = 48 * 1024;
/// Validate an edited report before it is stored or used as a submission body.
@@ -98,9 +107,9 @@ pub fn refresh_edited_report_with_control(
control.check()?;
let mut edited = validate_edited_report(edited_xml)?;
let cookie_params = cookie_params(input);
let identity = HostInfoCollector::new(&input.profile, input, &cookie_params).collect_identity();
let identity = HostInfoCollector::new(&input.profile, input, &cookie_params).collect_identity(control)?;
control.check()?;
let domain = format!("{}.internal", identity.domain);
let domain = &identity.domain;
let generate_time = get_current_time_components().0;
for (name, value) in [
("md5-sum", input.context.md5()),
@@ -201,6 +210,27 @@ impl ReportContext {
}
}
/// Return a computer domain only for native profiles with a platform-specific source.
fn native_domain(profile: &OsProfile, control: &dyn CollectionControl) -> io::Result<Option<String>> {
control.check()?;
if !profile.is_native() {
return Ok(None);
}
#[cfg(windows)]
if profile.client_os() == ClientOs::Windows {
return windows::domain().map(Some);
}
#[cfg(target_os = "macos")]
if profile.client_os() == ClientOs::Mac {
return domain_macos::collect(&CollectorCommands::new(control)).map(Some);
}
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
if profile.client_os() == ClientOs::Linux {
return domain_unix::collect(&CollectorCommands::new(control)).map(Some);
}
Ok(None)
}
/// Inputs collected by the caller for one HIP report.
pub struct ReportInput {
pub profile: OsProfile,
@@ -214,14 +244,18 @@ pub fn generate_report(input: &ReportInput) -> anyhow::Result<String> {
}
/// Identity-only maintenance report. This deliberately does not enumerate the
/// machine or execute posture collectors; addresses come from the session owner.
/// interfaces or execute posture collectors; addresses come from the session owner.
/// Native profiles read the computer domain, consistent with generated and refreshed reports.
pub fn generate_identity_report(input: &ReportInput) -> anyhow::Result<String> {
let params = cookie_params(input);
let budget = CollectionBudget::new(Duration::from_secs(60));
let domain =
native_domain(&input.profile, &budget)?.unwrap_or_else(|| params.get("domain").cloned().unwrap_or_default());
let mut report = Element::new("hip-report");
for (name, value) in [
("md5-sum", input.context.md5()),
("user-name", report_user_name(input, &params)),
("domain", params.get("domain").map(String::as_str).unwrap_or_default()),
("domain", domain.as_str()),
("host-name", input.profile.computer()),
("host-id", input.profile.host_id()),
("ip-address", input.context.client_ip().unwrap_or_default()),
@@ -338,6 +372,7 @@ struct HostInfo {
clamav: Option<ClamAvInfo>,
sentinel: Option<SentinelInfo>,
ufw: Option<UfwInfo>,
windows_categories: Option<String>,
}
impl HostInfo {
@@ -425,16 +460,6 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
}
}
/// Domain belongs to the runtime user/session, not the simulated OS, so
/// it is sourced from the auth cookie when available.
fn get_domain(&self) -> String {
self
.cookie_params
.get("domain")
.map(|s| s.to_string())
.unwrap_or_default()
}
/// Collect network interface information with fallback
fn collect_network_interface(&self) -> NetworkInterface {
match netdev::get_default_interface() {
@@ -452,28 +477,27 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
}
}
/// Single entry point for collecting host info — dispatches per-OS
/// behavior via `OsProfile` methods rather than `#[cfg(target_os)]`.
fn collect_identity(&self) -> HostInfo {
fn collect_identity(&self, control: &dyn CollectionControl) -> io::Result<HostInfo> {
let runtime_iface = self.collect_network_interface();
let primary = self.adapt_primary_interface(&runtime_iface);
let mut interfaces = vec![primary.clone()];
interfaces.extend(self.extra_interfaces(&primary));
HostInfo {
Ok(HostInfo {
os_vendor: self.profile.os_vendor().to_string(),
os_version: self.profile.os_version().to_string(),
host_id: self.profile.host_id().to_string(),
host_name: self.profile.computer().to_string(),
software_version: self.profile.software_version().to_string(),
domain: self.domain_for_profile(),
domain: self.domain_for_profile(control)?,
network_interfaces: interfaces,
defender: None,
clamav: None,
sentinel: None,
ufw: None,
}
windows_categories: None,
})
}
/// Build the primary network interface with OS-appropriate naming and
@@ -496,9 +520,12 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
}
}
/// Additional interfaces appended after the primary. Only Windows
/// emits the software loopback interface in HIP reports.
/// Native Windows enumerates loopback adapters; Windows emulation retains its synthetic interface.
fn extra_interfaces(&self, primary: &NetworkInterface) -> Vec<NetworkInterface> {
#[cfg(windows)]
if self.profile.client_os() == ClientOs::Windows && self.profile.is_native() {
return windows::loopback_interfaces();
}
match self.profile.client_os() {
ClientOs::Windows => vec![NetworkInterface {
name: derive_windows_network_name(self.profile.host_id(), primary),
@@ -511,20 +538,29 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
}
}
/// Linux HIP reports omit the domain field; macOS/Windows include it
/// from the auth cookie.
fn domain_for_profile(&self) -> String {
match self.profile.client_os() {
ClientOs::Linux => String::new(),
ClientOs::Mac | ClientOs::Windows | ClientOs::Ios => self.get_domain(),
fn domain_for_profile(&self, control: &dyn CollectionControl) -> io::Result<String> {
if let Some(domain) = native_domain(self.profile, control)? {
return Ok(domain);
}
let domain = match self.profile.client_os() {
ClientOs::Linux => "",
ClientOs::Mac | ClientOs::Windows | ClientOs::Ios => {
self.cookie_params.get("domain").map(String::as_str).unwrap_or_default()
}
};
Ok(format!("{domain}.internal"))
}
fn collect(&self, control: &dyn CollectionControl) -> io::Result<HostInfo> {
control.check()?;
let mut info = self.collect_identity();
let mut info = self.collect_identity(control)?;
control.check()?;
let commands = CollectorCommands::new(control);
#[cfg(windows)]
if self.profile.client_os() == ClientOs::Windows && self.profile.is_native() {
info.windows_categories = Some(windows::collect(&commands, control)?.render()?);
}
if self.profile.client_os() == ClientOs::Linux && self.profile.is_native() {
info.defender = detect_microsoft_defender(&commands)?;
info.clamav = detect_clamav(&commands)?;
@@ -671,16 +707,20 @@ fn placeholder_interface_for(profile: &OsProfile, runtime: &NetworkInterface) ->
}
}
/// MAC address formatting per target OS. Windows uses hyphen separators;
/// Linux and macOS preserve the colon-separated form returned by the
/// platform.
/// Native Windows uses uppercase hex and hyphens. Windows emulation retains its
/// existing casing; other profiles preserve the platform's MAC representation.
fn format_mac_for(profile: &OsProfile, mac: Option<String>) -> Option<String> {
match profile.client_os() {
ClientOs::Windows => mac.map(|m| m.replace(':', "-")),
ClientOs::Windows if profile.is_native() => mac.map(format_native_windows_mac),
ClientOs::Windows => mac.map(|value| value.replace(':', "-")),
ClientOs::Linux | ClientOs::Mac | ClientOs::Ios => mac,
}
}
fn format_native_windows_mac(mac: String) -> String {
mac.replace(':', "-").to_ascii_uppercase()
}
// ============================================================================
// Utility Functions
// ============================================================================
@@ -796,13 +836,39 @@ mod tests {
}
}
#[test]
fn native_windows_mac_casing_does_not_change_emulation_or_other_platforms() {
for (runtime_os, client_os, expected) in [
(ClientOs::Windows, ClientOs::Windows, "AA-BB-CC-DD-EE-FF"),
(ClientOs::Mac, ClientOs::Windows, "aa-bb-cc-dd-ee-ff"),
(ClientOs::Linux, ClientOs::Windows, "aa-bb-cc-dd-ee-ff"),
(ClientOs::Mac, ClientOs::Mac, "aa:bb:cc:dd:ee:ff"),
(ClientOs::Linux, ClientOs::Linux, "aa:bb:cc:dd:ee:ff"),
] {
let profile = OsProfileBuilder::new(client_os).build(gpapi::os_profile::HostSnapshot {
runtime_os,
identity: gpapi::os_profile::HostIdentity::application("host".into(), "id".into()),
os_version: "OS".into(),
software_version: "version".into(),
});
assert_eq!(
format_mac_for(&profile, Some("aa:bb:cc:dd:ee:ff".into())).as_deref(),
Some(expected)
);
assert_eq!(format_mac_for(&profile, Some(String::new())).as_deref(), Some(""));
assert_eq!(format_mac_for(&profile, None), None);
}
}
#[test]
fn host_info_os_vendor_is_linux_for_linux_profile() {
let input = make_input(make_profile(ClientOs::Linux));
let profile = make_profile(ClientOs::Linux);
let cookie_params: HashMap<String, String> = HashMap::new();
let info = HostInfoCollector::new(&profile, &input, &cookie_params).collect_identity();
let info = HostInfoCollector::new(&profile, &input, &cookie_params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
assert_eq!(info.os_vendor, "Linux");
}
@@ -813,7 +879,9 @@ mod tests {
let profile = make_profile(ClientOs::Mac);
let cookie_params: HashMap<String, String> = HashMap::new();
let info = HostInfoCollector::new(&profile, &input, &cookie_params).collect_identity();
let info = HostInfoCollector::new(&profile, &input, &cookie_params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
assert_eq!(info.os_vendor, "Apple");
}
@@ -824,7 +892,9 @@ mod tests {
let profile = make_profile(ClientOs::Windows);
let cookie_params: HashMap<String, String> = HashMap::new();
let info = HostInfoCollector::new(&profile, &input, &cookie_params).collect_identity();
let info = HostInfoCollector::new(&profile, &input, &cookie_params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
assert_eq!(info.os_vendor, "Microsoft");
}
@@ -835,7 +905,9 @@ mod tests {
let profile = make_profile(ClientOs::Linux);
let cookie_params: HashMap<String, String> = HashMap::new();
let info = HostInfoCollector::new(&profile, &input, &cookie_params).collect_identity();
let info = HostInfoCollector::new(&profile, &input, &cookie_params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
assert_eq!(info.host_id, profile.host_id());
}
@@ -849,7 +921,9 @@ mod tests {
let profile = make_profile(client_os);
let cookie_params: HashMap<String, String> = HashMap::new();
let info = HostInfoCollector::new(&profile, &input, &cookie_params).collect_identity();
let info = HostInfoCollector::new(&profile, &input, &cookie_params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
assert_eq!(info.host_id, profile.host_id());
}
@@ -870,11 +944,206 @@ mod tests {
validate_edited_report(&report).unwrap();
}
#[test]
fn emulated_windows_uses_v4_categories_and_preserves_template_posture() {
use gpapi::os_profile::HostSnapshot;
let identity = gpapi::os_profile::desktop::identity_from_parts(
"emulated-device".into(),
"emulated-id".into(),
"serial".into(),
"00:11:22:33:44:55".into(),
);
let profile = OsProfileBuilder::new(ClientOs::Windows).build(HostSnapshot {
runtime_os: ClientOs::Mac,
identity,
os_version: "macOS".into(),
software_version: "15.0".into(),
});
assert!(!profile.is_native());
let mut input = make_input(profile);
input.context = ReportContext::Connected {
cookie: "domain=emulated-domain".into(),
client_ip: None,
client_ipv6: None,
md5: "test-digest".into(),
};
let report = generate_report(&input).unwrap();
let root = validate_edited_report(&report).unwrap();
assert_eq!(
unique_child(&root, "domain").unwrap().get_text().unwrap(),
"emulated-domain.internal"
);
assert!(report.contains("<domain>emulated-domain.internal</domain>"));
let identity = Element::parse(generate_identity_report(&input).unwrap().as_bytes()).unwrap();
assert_eq!(
unique_child(&identity, "domain").unwrap().get_text().unwrap(),
"emulated-domain"
);
let categories = unique_child(&root, "categories").unwrap();
let names = categories
.children
.iter()
.filter_map(xmltree::XMLNode::as_element)
.filter_map(|entry| entry.attributes.get("name").map(String::as_str))
.collect::<Vec<_>>();
assert_eq!(
names,
[
"host-info",
"anti-malware",
"disk-backup",
"disk-encryption",
"firewall",
"patch-management",
"data-loss-prevention"
]
);
assert!(report.contains("McAfee VirusScan Enterprise"));
assert!(report.contains("McAfee ePolicy Orchestrator Agent"));
assert_eq!(report.matches("prodType=\"3\"").count(), 2);
assert!(report.contains("<drive-name>C:\\</drive-name>"));
assert!(report.contains("<enc-state>encrypted</enc-state>"));
assert!(report.contains("<missing-patches />"));
}
#[test]
fn emulated_unix_domains_keep_existing_cookie_and_suffix_behavior() {
for client_os in [ClientOs::Mac, ClientOs::Linux] {
let expected_domain = if client_os == ClientOs::Linux {
".internal"
} else {
"emulated-domain.internal"
};
let profile = OsProfileBuilder::new(client_os).build(gpapi::os_profile::HostSnapshot {
runtime_os: ClientOs::Windows,
identity: gpapi::os_profile::HostIdentity::from_parts(
"host".into(),
"id".into(),
"serial".into(),
"00:11:22:33:44:55".into(),
),
os_version: "Windows".into(),
software_version: "11".into(),
});
assert!(!profile.is_native());
let mut input = make_input(profile);
input.context = ReportContext::Connected {
cookie: "domain=emulated-domain".into(),
client_ip: None,
client_ipv6: None,
md5: "test-digest".into(),
};
let xml = generate_report(&input).unwrap();
let generated = validate_edited_report(&xml).unwrap();
let refreshed = validate_edited_report(&refresh_edited_report(&xml, &input).unwrap()).unwrap();
for report in [generated, refreshed] {
assert_eq!(
unique_child(&report, "domain").unwrap().get_text().unwrap(),
expected_domain
);
assert_eq!(
unique_child(host_info(&report).unwrap(), "domain")
.unwrap()
.get_text()
.unwrap(),
expected_domain
);
}
let identity = Element::parse(generate_identity_report(&input).unwrap().as_bytes()).unwrap();
assert_eq!(
unique_child(&identity, "domain").unwrap().get_text().unwrap(),
"emulated-domain"
);
}
}
#[test]
fn native_domain_is_shared_by_generated_edited_and_identity_reports() {
let client_os = if cfg!(windows) {
ClientOs::Windows
} else if cfg!(target_os = "macos") {
ClientOs::Mac
} else {
ClientOs::Linux
};
let profile = OsProfileBuilder::new(client_os).build(gpapi::os_profile::HostSnapshot {
runtime_os: client_os,
identity: gpapi::os_profile::HostIdentity::from_parts(
"host".into(),
"id".into(),
"serial".into(),
"00:11:22:33:44:55".into(),
),
os_version: "Windows".into(),
software_version: "11".into(),
});
let mut input = make_input(profile);
input.context = ReportContext::Connected {
cookie: "domain=auth-cookie-must-not-be-used.invalid".into(),
client_ip: None,
client_ipv6: None,
md5: "test-digest".into(),
};
let expected = native_domain(&input.profile, &CollectionBudget::new(Duration::from_secs(10)))
.unwrap()
.unwrap();
let params = cookie_params(&input);
let mut host_info = HostInfoCollector::new(&input.profile, &input, &params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
// Render the identity path without executing the product collectors.
if client_os == ClientOs::Windows {
host_info.windows_categories = Some(windows_report::WindowsReport::default().render().unwrap());
}
let (generate_time, day, month, year) = get_current_time_components();
let xml = HipReportTemplate {
client_version: input.profile.client_version(),
generate_time,
day,
month,
year,
user_name: "test-user",
host_info,
md5: "test-digest",
}
.render()
.unwrap();
let generated = validate_edited_report(&xml).unwrap();
let mut stale = generated.clone();
set_field(&mut stale, "domain", "stale.internal").unwrap();
set_field(host_info_mut(&mut stale).unwrap(), "domain", "stale.internal").unwrap();
let refreshed = refresh_edited_report(&write_xml(&stale).unwrap(), &input).unwrap();
let refreshed = validate_edited_report(&refreshed).unwrap();
let identity = Element::parse(generate_identity_report(&input).unwrap().as_bytes()).unwrap();
for report in [&generated, &refreshed, &identity] {
assert_eq!(
unique_child(report, "domain")
.unwrap()
.get_text()
.unwrap_or_default()
.as_ref(),
expected
);
}
for mut report in [generated, refreshed] {
assert_eq!(
unique_child(host_info_mut(&mut report).unwrap(), "domain")
.unwrap()
.get_text()
.unwrap_or_default()
.as_ref(),
expected
);
}
}
#[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_identity();
let mut host_info = HostInfoCollector::new(&input.profile, &input, &cookie_params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
host_info.sentinel = Some(SentinelInfo {
version: "25.3.4.8365".to_string(),
real_time_protection: true,
@@ -905,7 +1174,9 @@ mod tests {
fn linux_report_renders_detected_sentinel_antimalware() {
let input = make_input(make_profile(ClientOs::Linux));
let cookie_params = HashMap::new();
let mut host_info = HostInfoCollector::new(&input.profile, &input, &cookie_params).collect_identity();
let mut host_info = HostInfoCollector::new(&input.profile, &input, &cookie_params)
.collect_identity(&CollectionBudget::new(Duration::from_secs(10)))
.unwrap();
host_info.sentinel = Some(SentinelInfo {
version: "25.2.2.14".to_string(),
real_time_protection: true,