Preserve Windows runtime state and qualify native guarded adapters
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
//! Passive native machine inventory. Never queries Win32_Product or executes
|
||||
//! uninstall commands; registry entries are evidence, not verified MSI state.
|
||||
use anyhow::{Result, ensure};
|
||||
use chrono::Utc;
|
||||
use serde_json::{Value, json};
|
||||
use std::{
|
||||
mem::{size_of, zeroed},
|
||||
ptr,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use windows_sys::Win32::{
|
||||
Foundation::{ERROR_FILE_NOT_FOUND, ERROR_NO_MORE_ITEMS, ERROR_SUCCESS},
|
||||
System::{Registry::*, SystemInformation::*},
|
||||
};
|
||||
|
||||
struct Registry(HKEY);
|
||||
impl Drop for Registry {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
RegCloseKey(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
fn open(parent: HKEY, name: &str, flags: u32) -> Result<Registry> {
|
||||
let mut key = ptr::null_mut();
|
||||
let name = crate::native::wide(name);
|
||||
ensure!(
|
||||
unsafe { RegOpenKeyExW(parent, name.as_ptr(), 0, KEY_READ | flags, &mut key) }
|
||||
== ERROR_SUCCESS,
|
||||
"REGISTRY_INVENTORY_ACCESS"
|
||||
);
|
||||
Ok(Registry(key))
|
||||
}
|
||||
fn text(key: HKEY, name: &str) -> Result<Option<String>> {
|
||||
let name = crate::native::wide(name);
|
||||
let mut data = [0u16; 512];
|
||||
let mut bytes = size_of::<[u16; 512]>() as u32;
|
||||
let result = unsafe {
|
||||
RegGetValueW(
|
||||
key,
|
||||
ptr::null(),
|
||||
name.as_ptr(),
|
||||
RRF_RT_REG_SZ,
|
||||
ptr::null_mut(),
|
||||
data.as_mut_ptr().cast(),
|
||||
&mut bytes,
|
||||
)
|
||||
};
|
||||
if result == ERROR_FILE_NOT_FOUND {
|
||||
return Ok(None);
|
||||
}
|
||||
ensure!(
|
||||
result == ERROR_SUCCESS
|
||||
&& bytes >= 2
|
||||
&& bytes as usize <= size_of::<[u16; 512]>()
|
||||
&& bytes.is_multiple_of(2),
|
||||
"REGISTRY_INVENTORY_STRING"
|
||||
);
|
||||
let count = bytes as usize / 2;
|
||||
ensure!(
|
||||
data[count - 1] == 0 && !data[..count - 1].contains(&0),
|
||||
"REGISTRY_STRING_TERMINATION"
|
||||
);
|
||||
Ok(Some(String::from_utf16(&data[..count - 1])?))
|
||||
}
|
||||
|
||||
fn hardware() -> Result<Value> {
|
||||
let mut system: SYSTEM_INFO = unsafe { zeroed() };
|
||||
unsafe {
|
||||
GetNativeSystemInfo(&mut system);
|
||||
}
|
||||
let mut memory: MEMORYSTATUSEX = unsafe { zeroed() };
|
||||
memory.dwLength = size_of::<MEMORYSTATUSEX>() as u32;
|
||||
ensure!(
|
||||
unsafe { GlobalMemoryStatusEx(&mut memory) } != 0
|
||||
&& system.dwNumberOfProcessors > 0
|
||||
&& memory.ullTotalPhys > 0
|
||||
&& memory.ullAvailPhys <= memory.ullTotalPhys
|
||||
&& memory.dwMemoryLoad <= 100,
|
||||
"NATIVE_HARDWARE_QUERY"
|
||||
);
|
||||
Ok(
|
||||
json!({"logical_processors_visible":system.dwNumberOfProcessors,
|
||||
"memory_total_bytes":memory.ullTotalPhys,"memory_available_bytes":memory.ullAvailPhys,
|
||||
"memory_load_percent":memory.dwMemoryLoad}),
|
||||
)
|
||||
}
|
||||
|
||||
fn software() -> Result<Value> {
|
||||
let began = Instant::now();
|
||||
let mut entries = Vec::new();
|
||||
let mut errors = 0u32;
|
||||
let mut scanned = 0u32;
|
||||
let mut truncated = false;
|
||||
let mut views = Vec::new();
|
||||
for (view, label) in [
|
||||
(KEY_WOW64_64KEY, "machine_registry_64"),
|
||||
(KEY_WOW64_32KEY, "machine_registry_32"),
|
||||
] {
|
||||
views.push(label);
|
||||
let key = match open(
|
||||
HKEY_LOCAL_MACHINE,
|
||||
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
|
||||
view,
|
||||
) {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
errors += 1;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
for index in 0..2048 {
|
||||
if began.elapsed() > Duration::from_millis(250) || entries.len() >= 64 {
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
let mut name = [0u16; 256];
|
||||
let mut length = name.len() as u32;
|
||||
let result = unsafe {
|
||||
RegEnumKeyExW(
|
||||
key.0,
|
||||
index,
|
||||
name.as_mut_ptr(),
|
||||
&mut length,
|
||||
ptr::null(),
|
||||
ptr::null_mut(),
|
||||
ptr::null_mut(),
|
||||
ptr::null_mut(),
|
||||
)
|
||||
};
|
||||
if result == ERROR_NO_MORE_ITEMS {
|
||||
break;
|
||||
}
|
||||
scanned += 1;
|
||||
if result != ERROR_SUCCESS || length as usize >= name.len() {
|
||||
errors += 1;
|
||||
continue;
|
||||
}
|
||||
let entry = (|| -> Result<Option<Value>> {
|
||||
let name = String::from_utf16(&name[..length as usize])?;
|
||||
let child = open(key.0, &name, view)?;
|
||||
let Some(display) = text(child.0, "DisplayName")?.filter(|v| !v.is_empty()) else {
|
||||
return Ok(None);
|
||||
};
|
||||
// No arbitrary path, username, uninstall string or installer
|
||||
// execution is inventoried. Missing version stays null.
|
||||
Ok(Some(
|
||||
json!({"display_name":display,"version":text(child.0,"DisplayVersion")?,
|
||||
"publisher":text(child.0,"Publisher")?,"source":label,
|
||||
"installation_context":"machine","installed_state":"registry_entry_only"}),
|
||||
))
|
||||
})();
|
||||
match entry {
|
||||
Ok(Some(entry)) => {
|
||||
entries.push(entry);
|
||||
if serde_json::to_vec(&entries)?.len() > 12288 {
|
||||
entries.pop();
|
||||
truncated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(_) => errors += 1,
|
||||
}
|
||||
if index == 2047 {
|
||||
truncated = true;
|
||||
}
|
||||
}
|
||||
if truncated {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ensure!(
|
||||
errors < 2 || !entries.is_empty(),
|
||||
"SOFTWARE_REGISTRY_UNAVAILABLE"
|
||||
);
|
||||
Ok(
|
||||
json!({"entries":entries,"scanned_keys":scanned,"read_errors":errors,"truncated":truncated,"views_queried":views,
|
||||
"scope":"machine_registry_only","per_user_inventory":"unsupported",
|
||||
"msi_state_verification":"not_collected","appx_inventory":"not_collected"}),
|
||||
)
|
||||
}
|
||||
|
||||
/// Each provider is isolated. Missing permissions never fabricate an empty,
|
||||
/// complete software inventory or hide the last valid stored observation.
|
||||
pub fn observations() -> Vec<Value> {
|
||||
type Provider = fn() -> Result<Value>;
|
||||
[
|
||||
(
|
||||
"windows.hardware",
|
||||
"windows.win32:GetNativeSystemInfo+GlobalMemoryStatusEx",
|
||||
hardware as Provider,
|
||||
),
|
||||
(
|
||||
"windows.software",
|
||||
"windows.registry:HKLM.Uninstall:64+32",
|
||||
software as Provider,
|
||||
),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|(scope, source, collect)| {
|
||||
let data = collect();
|
||||
let observed = Utc::now();
|
||||
match data {
|
||||
Ok(data) => {
|
||||
let truncated = data
|
||||
.get("truncated")
|
||||
.and_then(Value::as_bool)
|
||||
.unwrap_or(false);
|
||||
json!({"scope":scope,"source_id":source,"observed_at":observed,"quality":"valid",
|
||||
"completeness":"partial","truncated":truncated,"error_code":null,"data":data})
|
||||
}
|
||||
Err(_) => json!({"scope":scope,"source_id":source,"observed_at":observed,
|
||||
"quality":"error","completeness":"unknown","truncated":false,
|
||||
"error_code":"NATIVE_PROVIDER_UNAVAILABLE","data":null}),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in New Issue
Block a user