mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-23 15:46:13 -05:00
feat(gphip): add HIP detection for BitDefender GravityZone on Linux (#729)
* feat(gphip): add HIP detection for BitDefender GravityZone on Linux * fix(gphip): reuse trusted paths for Bitdefender detection --------- Co-authored-by: Kevin Yue <[email protected]>
This commit is contained in:
1 parent
2160bd82f8
commit
69155d04b9
4 files changed
+260
-1
No files matched your search
@@ -127,7 +127,9 @@ pub fn command_path(require_root: bool) -> io::Result<std::ffi::OsString> {
|
|||||||
std::env::join_paths(directories).map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))
|
std::env::join_paths(directories).map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn root_owned_path(path: &Path) -> bool {
|
/// Whether `path` and every ancestor are root-owned, not symlinks, and not
|
||||||
|
/// group/world-writable (unless a root-owned sticky ancestor protects them).
|
||||||
|
pub fn root_owned_path(path: &Path) -> bool {
|
||||||
path.ancestors().all(|part| {
|
path.ancestors().all(|part| {
|
||||||
fs::symlink_metadata(part).is_ok_and(|metadata| {
|
fs::symlink_metadata(part).is_ok_and(|metadata| {
|
||||||
let mode = metadata.permissions().mode();
|
let mode = metadata.permissions().mode();
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
use std::{fs, io, path::Path};
|
||||||
|
|
||||||
|
use gpapi::process::collection::{CollectorCommands, root_owned_path};
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
use super::BitdefenderInfo;
|
||||||
|
|
||||||
|
const BITDEFENDER_DIR: &str = "/opt/bitdefender-security-tools";
|
||||||
|
const MAX_VERSION_LEN: usize = 64;
|
||||||
|
|
||||||
|
pub(super) fn detect(commands: &CollectorCommands<'_>) -> io::Result<Option<BitdefenderInfo>> {
|
||||||
|
let dir = Path::new(BITDEFENDER_DIR);
|
||||||
|
if uzers::get_effective_uid() == 0 {
|
||||||
|
let cli = dir.join("bin/bduitool");
|
||||||
|
let status = commands
|
||||||
|
.run_executable(&cli, &["get", "ps"])?
|
||||||
|
.filter(|output| output.status.success())
|
||||||
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||||
|
.as_deref()
|
||||||
|
.and_then(parse_product_status);
|
||||||
|
if status.is_some() {
|
||||||
|
return Ok(status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Ok(manifest) = dir.join("etc/manifest.json").canonicalize() else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
if !manifest.is_file() || !root_owned_path(&manifest) {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
let Ok(contents) = fs::read_to_string(&manifest) else {
|
||||||
|
return Ok(None);
|
||||||
|
};
|
||||||
|
Ok(parse_manifest(&contents).map(|version| BitdefenderInfo {
|
||||||
|
version,
|
||||||
|
engine_version: None,
|
||||||
|
real_time_protection: None,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse `bduitool get ps`. All three fields must be present for a full entry.
|
||||||
|
fn parse_product_status(output: &str) -> Option<BitdefenderInfo> {
|
||||||
|
let version = product_version(value(output, "Product version:")?)?;
|
||||||
|
let engine_version = value(output, "Engines version:")
|
||||||
|
.filter(|version| valid_version(version))?
|
||||||
|
.to_string();
|
||||||
|
let real_time_protection = match value(output, "Antimalware status:")? {
|
||||||
|
"On" => true,
|
||||||
|
"Off" => false,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
Some(BitdefenderInfo {
|
||||||
|
version,
|
||||||
|
engine_version: Some(engine_version),
|
||||||
|
real_time_protection: Some(real_time_protection),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_manifest(contents: &str) -> Option<String> {
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct Manifest {
|
||||||
|
version: String,
|
||||||
|
}
|
||||||
|
let manifest: Manifest = serde_json::from_str(contents).ok()?;
|
||||||
|
product_version(&manifest.version)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The official client reports only the first three components, e.g. `7.10.0`.
|
||||||
|
fn product_version(version: &str) -> Option<String> {
|
||||||
|
if !valid_version(version) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let short = version.split('.').take(3).collect::<Vec<_>>().join(".");
|
||||||
|
Some(short)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn valid_version(version: &str) -> bool {
|
||||||
|
!version.is_empty()
|
||||||
|
&& version.len() <= MAX_VERSION_LEN
|
||||||
|
&& version.chars().all(|ch| ch.is_ascii_digit() || ch == '.')
|
||||||
|
&& version.split('.').all(|part| !part.is_empty())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value<'a>(output: &'a str, label: &str) -> Option<&'a str> {
|
||||||
|
output.lines().find_map(|line| {
|
||||||
|
let line = line.trim();
|
||||||
|
let line = line.strip_prefix("- ").unwrap_or(line);
|
||||||
|
line.strip_prefix(label).map(str::trim)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
const OBSERVED: &str = "\nProduct version: 7.10.0.200346\nLast product update: 2026-11-21 at 14:42:06 GMT\n\
|
||||||
|
New product update available: no\nEngines version: 7.101851\nLast security content update: 2026-11-21 at 13:42:12 GMT\n\
|
||||||
|
Features:\n - Antimalware status: On\n - AntiExploit status: On\n - On-Access YARA status: Off\n";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_observed_product_status() {
|
||||||
|
let info = parse_product_status(OBSERVED).expect("observed output should parse");
|
||||||
|
assert_eq!(info.version, "7.10.0");
|
||||||
|
assert_eq!(info.engine_version.as_deref(), Some("7.101851"));
|
||||||
|
assert_eq!(info.real_time_protection, Some(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn antimalware_off_reports_no_real_time_protection() {
|
||||||
|
let output = OBSERVED.replace("Antimalware status: On", "Antimalware status: Off");
|
||||||
|
assert_eq!(parse_product_status(&output).unwrap().real_time_protection, Some(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn incomplete_product_status_is_rejected() {
|
||||||
|
assert!(parse_product_status(&OBSERVED.replace("Engines version: 7.101851\n", "")).is_none());
|
||||||
|
assert!(parse_product_status(&OBSERVED.replace(" - Antimalware status: On\n", "")).is_none());
|
||||||
|
assert!(parse_product_status(&OBSERVED.replace("Antimalware status: On", "Antimalware status: Unknown")).is_none());
|
||||||
|
assert!(
|
||||||
|
parse_product_status(&OBSERVED.replace("Engines version: 7.101851", "Engines version: 7.101851\"/>")).is_none()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncates_product_version_to_three_components() {
|
||||||
|
assert_eq!(product_version("7.10.0.200346").as_deref(), Some("7.10.0"));
|
||||||
|
assert_eq!(product_version("7.10.0").as_deref(), Some("7.10.0"));
|
||||||
|
assert_eq!(product_version("7.10").as_deref(), Some("7.10"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rejects_invalid_versions() {
|
||||||
|
assert_eq!(product_version(""), None);
|
||||||
|
assert_eq!(product_version("7.10.0\"/>"), None);
|
||||||
|
assert_eq!(product_version(&"1".repeat(65)), None);
|
||||||
|
assert_eq!(product_version("7..10"), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parses_manifest_version() {
|
||||||
|
assert_eq!(
|
||||||
|
parse_manifest(r#"{"version":"7.10.0.200346","changeset":"e95aa9d9"}"#).as_deref(),
|
||||||
|
Some("7.10.0")
|
||||||
|
);
|
||||||
|
assert_eq!(parse_manifest("not json"), None);
|
||||||
|
assert_eq!(parse_manifest(r#"{"changeset":"e95aa9d9"}"#), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,8 @@ use gpapi::collection::{CollectionBudget, CollectionControl};
|
|||||||
use gpapi::process::collection::CollectorCommands;
|
use gpapi::process::collection::CollectorCommands;
|
||||||
use xmltree::Element;
|
use xmltree::Element;
|
||||||
|
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
mod bitdefender_unix;
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
mod domain_macos;
|
mod domain_macos;
|
||||||
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))]
|
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", test))]
|
||||||
@@ -355,6 +357,15 @@ struct ClamAvInfo {
|
|||||||
real_time_protection: bool,
|
real_time_protection: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Bitdefender Endpoint Security Tools (GravityZone). Engine and real-time
|
||||||
|
/// protection are only available when `bduitool` can run as root.
|
||||||
|
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
|
||||||
|
struct BitdefenderInfo {
|
||||||
|
version: String,
|
||||||
|
engine_version: Option<String>,
|
||||||
|
real_time_protection: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
struct SentinelInfo {
|
struct SentinelInfo {
|
||||||
version: String,
|
version: String,
|
||||||
real_time_protection: bool,
|
real_time_protection: bool,
|
||||||
@@ -378,6 +389,7 @@ struct HostInfo {
|
|||||||
defender: Option<DefenderInfo>,
|
defender: Option<DefenderInfo>,
|
||||||
clamav: Option<ClamAvInfo>,
|
clamav: Option<ClamAvInfo>,
|
||||||
sentinel: Option<SentinelInfo>,
|
sentinel: Option<SentinelInfo>,
|
||||||
|
bitdefender: Option<BitdefenderInfo>,
|
||||||
ufw: Option<UfwInfo>,
|
ufw: Option<UfwInfo>,
|
||||||
windows_categories: Option<String>,
|
windows_categories: Option<String>,
|
||||||
windows_updates: Option<gpapi::hip::WindowsUpdateStatus>,
|
windows_updates: Option<gpapi::hip::WindowsUpdateStatus>,
|
||||||
@@ -503,6 +515,7 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
|
|||||||
defender: None,
|
defender: None,
|
||||||
clamav: None,
|
clamav: None,
|
||||||
sentinel: None,
|
sentinel: None,
|
||||||
|
bitdefender: None,
|
||||||
ufw: None,
|
ufw: None,
|
||||||
windows_categories: None,
|
windows_categories: None,
|
||||||
windows_updates: None,
|
windows_updates: None,
|
||||||
@@ -585,6 +598,10 @@ impl<'p, 'a> HostInfoCollector<'p, 'a> {
|
|||||||
if self.profile.client_os() == ClientOs::Linux && self.profile.is_native() {
|
if self.profile.client_os() == ClientOs::Linux && self.profile.is_native() {
|
||||||
info.sentinel = sentinel_unix::detect(&commands)?;
|
info.sentinel = sentinel_unix::detect(&commands)?;
|
||||||
}
|
}
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
if self.profile.client_os() == ClientOs::Linux && self.profile.is_native() {
|
||||||
|
info.bitdefender = bitdefender_unix::detect(&commands)?;
|
||||||
|
}
|
||||||
control.check()?;
|
control.check()?;
|
||||||
Ok(info)
|
Ok(info)
|
||||||
}
|
}
|
||||||
@@ -1225,6 +1242,86 @@ mod tests {
|
|||||||
assert_eq!(report.matches("name=\"Sentinel Agent\"").count(), 1);
|
assert_eq!(report.matches("name=\"Sentinel Agent\"").count(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn render_linux_report_with_bitdefender(bitdefender: Option<BitdefenderInfo>) -> String {
|
||||||
|
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(&CollectionBudget::new(Duration::from_secs(10)))
|
||||||
|
.unwrap();
|
||||||
|
host_info.bitdefender = bitdefender;
|
||||||
|
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();
|
||||||
|
report
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bitdefender_product_info(report: &str) -> &str {
|
||||||
|
let start = report
|
||||||
|
.find("name=\"Bitdefender Endpoint Security Tools\"")
|
||||||
|
.expect("Bitdefender entry should render");
|
||||||
|
let end = start + report[start..].find("</ProductInfo>").unwrap();
|
||||||
|
&report[start..end]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn linux_report_renders_full_bitdefender_entry() {
|
||||||
|
let report = render_linux_report_with_bitdefender(Some(BitdefenderInfo {
|
||||||
|
version: "7.10.0".to_string(),
|
||||||
|
engine_version: Some("7.101851".to_string()),
|
||||||
|
real_time_protection: Some(true),
|
||||||
|
}));
|
||||||
|
assert!(report.contains(
|
||||||
|
"vendor=\"Bitdefender\" name=\"Bitdefender Endpoint Security Tools\" version=\"7.10.0\" defver=\"7.101851\" engver=\"7.101851\""
|
||||||
|
));
|
||||||
|
let entry = bitdefender_product_info(&report);
|
||||||
|
assert!(entry.contains("prodType=\"3\" osType=\"1\""));
|
||||||
|
assert!(entry.contains("<real-time-protection>yes</real-time-protection>"));
|
||||||
|
assert!(entry.contains("<last-full-scan-time>n/a</last-full-scan-time>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn linux_report_renders_bitdefender_real_time_protection_off() {
|
||||||
|
let report = render_linux_report_with_bitdefender(Some(BitdefenderInfo {
|
||||||
|
version: "7.10.0".to_string(),
|
||||||
|
engine_version: Some("7.101851".to_string()),
|
||||||
|
real_time_protection: Some(false),
|
||||||
|
}));
|
||||||
|
assert!(bitdefender_product_info(&report).contains("<real-time-protection>no</real-time-protection>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn linux_report_renders_partial_bitdefender_entry() {
|
||||||
|
let report = render_linux_report_with_bitdefender(Some(BitdefenderInfo {
|
||||||
|
version: "7.10.0".to_string(),
|
||||||
|
engine_version: None,
|
||||||
|
real_time_protection: None,
|
||||||
|
}));
|
||||||
|
let entry = bitdefender_product_info(&report);
|
||||||
|
assert!(entry.contains("version=\"7.10.0\""));
|
||||||
|
assert!(!entry.contains("defver="));
|
||||||
|
assert!(!entry.contains("engver="));
|
||||||
|
assert!(!entry.contains("real-time-protection"));
|
||||||
|
assert!(entry.contains("<last-full-scan-time>n/a</last-full-scan-time>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn linux_report_omits_absent_bitdefender() {
|
||||||
|
let report = render_linux_report_with_bitdefender(None);
|
||||||
|
assert!(!report.contains("Bitdefender"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn preview_uses_explicit_pending_digest() {
|
fn preview_uses_explicit_pending_digest() {
|
||||||
let input = ReportInput {
|
let input = ReportInput {
|
||||||
|
|||||||
@@ -174,6 +174,17 @@
|
|||||||
</ProductInfo>
|
</ProductInfo>
|
||||||
</entry>
|
</entry>
|
||||||
{% endif -%}
|
{% endif -%}
|
||||||
|
{% if let Some(bitdefender) = &host_info.bitdefender %}
|
||||||
|
<entry>
|
||||||
|
<ProductInfo>
|
||||||
|
<Prod vendor="Bitdefender" name="Bitdefender Endpoint Security Tools" version="{{ bitdefender.version }}"{% if let Some(engine_version) = &bitdefender.engine_version %} defver="{{ engine_version }}" engver="{{ engine_version }}"{% endif %} datemon="{{ month }}" dateday="{{ day }}" dateyear="{{ year }}" prodType="3" osType="1"/>
|
||||||
|
{% if let Some(real_time_protection) = bitdefender.real_time_protection -%}
|
||||||
|
<real-time-protection>{% if real_time_protection %}yes{% else %}no{% endif %}</real-time-protection>
|
||||||
|
{% endif -%}
|
||||||
|
<last-full-scan-time>n/a</last-full-scan-time>
|
||||||
|
</ProductInfo>
|
||||||
|
</entry>
|
||||||
|
{% endif -%}
|
||||||
</list>
|
</list>
|
||||||
</entry>
|
</entry>
|
||||||
<entry name="disk-backup">
|
<entry name="disk-backup">
|
||||||
|
|||||||
Reference in new issue
Block a user