feat: add HIP support for Linux ClamAV (anti-malvare) and UFW (firewall) (#650)

Co-authored-by: J. Andres Guerrero <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2026-09-01 15:03:16 +08:00
1 parent 8eb4d48887
commit 0e1aedce82
2 files changed
+164 -4

No files matched your search

+145
View File
@@ -40,6 +40,19 @@ struct RealTimeProtection {
value: bool,
}
#[derive(Debug, Clone)]
struct UfwInfo {
version: String,
is_enabled: bool,
}
#[derive(Debug, Clone)]
struct ClamAvInfo {
version: String,
definitions_version: Option<String>,
real_time_protection: bool,
}
/// Host information for HIP reporting
struct HostInfo {
/// Common for all OSes, e.g., "Apple", "Microsoft", "Linux"
@@ -55,6 +68,8 @@ struct HostInfo {
domain: String,
network_interfaces: Vec<NetworkInterface>,
defender: Option<DefenderInfo>,
clamav: Option<ClamAvInfo>,
ufw: Option<UfwInfo>,
}
impl HostInfo {
@@ -286,6 +301,8 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
domain: self.domain_for_profile(),
network_interfaces: interfaces,
defender: self.defender_for_profile(),
clamav: self.clamav_for_profile(),
ufw: self.ufw_for_profile(),
}
}
@@ -339,6 +356,108 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
ClientOs::Mac | ClientOs::Windows => None,
}
}
fn clamav_for_profile(&self) -> Option<ClamAvInfo> {
match self.profile.client_os() {
ClientOs::Linux => detect_clamav(),
ClientOs::Mac | ClientOs::Windows => None,
}
}
fn ufw_for_profile(&self) -> Option<UfwInfo> {
match self.profile.client_os() {
ClientOs::Linux => detect_ufw(),
ClientOs::Mac | ClientOs::Windows => None,
}
}
}
fn detect_clamav() -> Option<ClamAvInfo> {
let version_out = Command::new("clamscan").arg("--version").output().ok()?;
if !version_out.status.success() {
debug!("clamscan not found or failed");
return None;
}
let version_str = String::from_utf8(version_out.stdout).ok()?;
let (version, definitions_version) = parse_clamav_version(&version_str)?;
let rtp_out = Command::new("systemctl")
.args(["is-active", "clamav-onaccess.service"])
.output()
.ok()?;
let real_time_protection = rtp_out.status.success();
Some(ClamAvInfo {
version,
definitions_version,
real_time_protection,
})
}
fn detect_ufw() -> Option<UfwInfo> {
let version_out = Command::new("ufw").arg("version").output().ok()?;
if !version_out.status.success() {
debug!("ufw not found or failed");
return None;
}
let version_str = String::from_utf8(version_out.stdout).ok()?;
let version = parse_ufw_version(&version_str)?;
let status_out = Command::new("ufw").arg("status").env("LC_ALL", "C").output().ok()?;
if !status_out.status.success() {
log::warn!("ufw status failed");
return None;
}
let status_str = String::from_utf8(status_out.stdout).ok()?;
let is_enabled = parse_ufw_status(&status_str)?;
Some(UfwInfo { version, is_enabled })
}
fn parse_clamav_version(output: &str) -> Option<(String, Option<String>)> {
let mut output_parts = output.split_whitespace();
if output_parts.next()? != "ClamAV" {
return None;
}
let mut version_parts = output_parts.next()?.split('/');
let version = version_parts.next()?;
if version.is_empty() {
return None;
}
let definitions_version = version_parts
.next()
.filter(|definitions_version| !definitions_version.is_empty())
.map(str::to_string);
Some((version.to_string(), definitions_version))
}
fn parse_ufw_version(output: &str) -> Option<String> {
let mut output_parts = output.split_whitespace();
if output_parts.next()? != "ufw" {
return None;
}
output_parts.next().map(str::to_string)
}
fn parse_ufw_status(output: &str) -> Option<bool> {
let status = output
.lines()
.find_map(|line| line.trim().strip_prefix("Status:"))?
.trim();
match status {
"active" => Some(true),
"inactive" => Some(false),
_ => None,
}
}
fn detect_microsoft_defender_blocking() -> Option<DefenderInfo> {
@@ -577,4 +696,30 @@ mod tests {
fn rejects_invalid_microsoft_defender_health_json() {
assert!(parse_defender_info("{}").is_none());
}
#[test]
fn parses_clamav_version() {
let version =
parse_clamav_version("ClamAV 1.4.3/27562/Sun Aug 31 10:23:42 2026").expect("ClamAV version should parse");
assert_eq!(version, ("1.4.3".to_string(), Some("27562".to_string())));
}
#[test]
fn parses_clamav_version_without_definitions_version() {
assert_eq!(parse_clamav_version("ClamAV 1.4.3"), Some(("1.4.3".to_string(), None)));
}
#[test]
fn parses_ufw_version() {
assert_eq!(parse_ufw_version("ufw 0.36.2\n"), Some("0.36.2".to_string()));
assert_eq!(parse_ufw_version("unexpected output\n"), None);
}
#[test]
fn parses_ufw_status() {
assert_eq!(parse_ufw_status("Status: active\n"), Some(true));
assert_eq!(parse_ufw_status("Status: inactive\n"), Some(false));
assert_eq!(parse_ufw_status("Status: unknown\n"), None);
}
}
+19 -4
View File
@@ -218,8 +218,8 @@
</entry>
{% elif host_info.os_vendor == "Linux" -%}
<entry name="anti-malware">
{% if let Some(defender) = &host_info.defender -%}
<list>
{% if let Some(defender) = &host_info.defender %}
<entry>
<ProductInfo>
<Prod name="Microsoft Defender ATP" version="{{ defender.app_version }}" defver="{{ defender.definitions_version }}" prodType="3" engver="{{ defender.engine_version }}" osType="1" vendor="Microsoft Corporation" dateday="{{ day }}" dateyear="{{ year }}" datemon="{{ month }}">
@@ -228,10 +228,17 @@
<last-full-scan-time>n/a</last-full-scan-time>
</ProductInfo>
</entry>
</list>
{% else -%}
<list/>
{% endif -%}
{% if let Some(clamav) = &host_info.clamav %}
<entry>
<ProductInfo>
<Prod vendor="Cisco Systems, Inc." name="ClamAV" version="{{ clamav.version }}"{% if let Some(definitions_version) = &clamav.definitions_version %} defver="{{ definitions_version }}"{% endif %} engver="" datemon="{{ month }}" dateday="{{ day }}" dateyear="{{ year }}" prodType="3" osType="1"/>
<real-time-protection>{% if clamav.real_time_protection %}yes{% else %}no{% endif %}</real-time-protection>
<last-full-scan-time>n/a</last-full-scan-time>
</ProductInfo>
</entry>
{% endif -%}
</list>
</entry>
<entry name="disk-backup">
<list/>
@@ -254,6 +261,14 @@
</entry>
<entry name="firewall">
<list>
{% if let Some(ufw) = &host_info.ufw %}
<entry>
<ProductInfo>
<Prod vendor="Canonical Ltd." name="UFW" version="{{ ufw.version }}"/>
<is-enabled>{% if ufw.is_enabled %}yes{% else %}no{% endif %}</is-enabled>
</ProductInfo>
</entry>
{% endif -%}
<entry>
<ProductInfo>
<Prod name="IPTables" version="1.8.4" vendor="IPTables">