diff --git a/crates/agent-windows/src/native.rs b/crates/agent-windows/src/native.rs index 9c4e0d0..c59e535 100644 --- a/crates/agent-windows/src/native.rs +++ b/crates/agent-windows/src/native.rs @@ -1,5 +1,4 @@ -//! Read-only Windows APIs. Boot identity comes from Win32_OperatingSystem's -//! LastBootUpTime, never from the lifetime of this agent process. +//! Read-only Windows APIs. The kernel's recorded startup time identifies the boot. use anyhow::{Context, Result, ensure}; use chrono::Utc; use serde_json::{Value, json}; @@ -295,7 +294,19 @@ pub async fn boot_id() -> Result { let executable = Path::new(&std::env::var("SystemRoot")?) .join("System32/WindowsPowerShell/v1.0/powershell.exe"); let mut command = tokio::process::Command::new(executable); - command.args(["-NoLogo","-NoProfile","-NonInteractive","-Command","$ErrorActionPreference='Stop'; (Get-CimInstance -ClassName Win32_OperatingSystem -Property LastBootUpTime).LastBootUpTime.ToUniversalTime().Ticks.ToString([Globalization.CultureInfo]::InvariantCulture)"]); + // LastBootUpTime is adjusted by W32Time while the machine is running. + // Kernel-General records StartTime once at startup. Use the newest record + // in log order and its invariant UTC payload, not its localized message. + const START_TIME: &str = r#" +$ErrorActionPreference = 'Stop' +$filter = @{LogName='System'; ProviderName='Microsoft-Windows-Kernel-General'; Id=12} +$event = Get-WinEvent -FilterHashtable $filter -MaxEvents 1 +if ($event.ProviderId -ne [Guid]'a68ca8b7-004f-d7b6-a698-07e2de0f1f5d') { throw 'BOOT_PROVIDER' } +$nodes = @(([xml]$event.ToXml()).Event.EventData.Data | Where-Object { $_.Name -ceq 'StartTime' }) +if ($nodes.Count -ne 1) { throw 'BOOT_START_TIME' } +[Xml.XmlConvert]::ToDateTimeOffset($nodes[0].InnerText).UtcTicks.ToString([Globalization.CultureInfo]::InvariantCulture) +"#; + command.args(["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", START_TIME]); command.kill_on_drop(true); let output = tokio::time::timeout(Duration::from_secs(15), command.output()).await??; ensure!( @@ -307,6 +318,7 @@ pub async fn boot_id() -> Result { (17..=19).contains(&text.len()) && text.bytes().all(|b| b.is_ascii_digit()), "BOOT_TIME_FORMAT" ); + // Preserve the wire ID when the old CIM value equals the recorded startup time. let mut bytes: [u8; 16] = Sha256::digest(format!("rmm.windows.boot.cim.ticks.v1:{text}")) .as_slice()[..16] .try_into()?;