127 lines
6.0 KiB
Rust
127 lines
6.0 KiB
Rust
//! Read-only, fixed MVP acceptance witness. No execution or journal mutation.
|
|
#[cfg(windows)]
|
|
fn run() -> anyhow::Result<()> {
|
|
use anyhow::{Result, ensure};
|
|
use rmm_agent_core::{
|
|
auth::{Binding, Outcome, Request},
|
|
crypto::DalekVerifier,
|
|
};
|
|
use rmm_domain::jobs::{Action, hash};
|
|
use rusqlite::{Connection, OpenFlags, params};
|
|
use serde_json::{Value, json};
|
|
use std::{path::Path, time::Duration};
|
|
use windows_sys::Win32::{
|
|
Foundation::CloseHandle,
|
|
Security::{GetTokenInformation, TOKEN_ELEVATION, TOKEN_QUERY, TokenElevation},
|
|
System::Threading::{GetCurrentProcess, OpenProcessToken},
|
|
};
|
|
ensure!(std::env::args().count() == 1, "FIXED_READ_ONLY_ARGUMENTS");
|
|
let mut token = std::ptr::null_mut();
|
|
ensure!(
|
|
unsafe { OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) } != 0,
|
|
"ADMIN_TOKEN"
|
|
);
|
|
let mut elevation = TOKEN_ELEVATION { TokenIsElevated: 0 };
|
|
let mut size = 0;
|
|
let okay = unsafe {
|
|
GetTokenInformation(
|
|
token,
|
|
TokenElevation,
|
|
&mut elevation as *mut _ as *mut _,
|
|
std::mem::size_of::<TOKEN_ELEVATION>() as u32,
|
|
&mut size,
|
|
)
|
|
};
|
|
unsafe { CloseHandle(token) };
|
|
ensure!(
|
|
okay != 0 && elevation.TokenIsElevated != 0,
|
|
"ADMIN_REQUIRED"
|
|
);
|
|
fn open(path: &str) -> Result<Connection> {
|
|
ensure!(Path::new(path).is_file(), "ORIGINAL_JOURNAL_REQUIRED");
|
|
let db = Connection::open_with_flags(
|
|
path,
|
|
OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
|
|
)?;
|
|
db.busy_timeout(Duration::from_millis(500))?;
|
|
Ok(db)
|
|
}
|
|
let job_id = "5f1ffef4-2bb6-4c7e-a3a0-fb619144b4f8";
|
|
let attempt_id = "ebb387cf-ebb1-4609-a318-32f44d774d11";
|
|
let grant_id = "cc09285a-2fa2-4d05-9836-50e74eb70c82";
|
|
let plan_hash = "d40190dee2cd0b76a86022bd0fa4270a5b49dad59be2c5b25fe5368883ff3f16";
|
|
let script_hash = "dae890f8e78143e381cddbcd6dd74177045fcdf890845cc7f30ec7a9ac4857a3";
|
|
let agent = open(r"C:\ProgramData\RustDiscovery\state\telemetry.sqlite")?;
|
|
let consumption = open(r"C:\ProgramData\RustDiscovery\state\consumption.sqlite")?;
|
|
let count: u64 = consumption.query_row(
|
|
"SELECT count(*) FROM consumed WHERE job=?1 OR attempt=?2 OR grant_id=?3",
|
|
params![job_id, attempt_id, grant_id],
|
|
|r| r.get(0),
|
|
)?;
|
|
ensure!(count == 1, "EXACT_ORIGINAL_SINGLE_CONSUMPTION");
|
|
let (start,outcome,finished):(Vec<u8>,Vec<u8>,Vec<u8>)=consumption.query_row("SELECT e.start,e.outcome,c.result FROM consumed c JOIN execution_evidence e USING(grant_id) WHERE c.grant_id=?1 AND c.job=?2 AND c.attempt=?3 AND c.agent='63ea15b2-0643-403d-b5b4-cba1288dbea3' AND c.generation=1",params![grant_id,job_id,attempt_id],|r|Ok((r.get(0)?,r.get(1)?,r.get(2)?)))?;
|
|
ensure!(
|
|
start.len() <= 131072 && outcome.len() <= 131072 && outcome == finished,
|
|
"EXACT_ORIGINAL_START_OUTCOME"
|
|
);
|
|
let saved: Value = serde_json::from_slice(&start)?;
|
|
let request: Request = serde_json::from_value(saved["request"].clone())?;
|
|
let reserved = serde_json::from_value(saved["reserved_at"].clone())?;
|
|
let binding_raw = std::fs::read(r"C:\ProgramData\RustDiscovery\binding.json")?;
|
|
ensure!(binding_raw.len() <= 8192, "PUBLIC_BINDING_SIZE");
|
|
let binding: Binding = serde_json::from_slice(&binding_raw)?;
|
|
// Historical evidence only. This program has no execution/start operation.
|
|
let (job, permit) = binding.verify_with(&request, reserved, &DalekVerifier)?;
|
|
ensure!(
|
|
job.job_id.to_string() == job_id
|
|
&& job.attempt_id.to_string() == attempt_id
|
|
&& permit.grant_id.to_string() == grant_id
|
|
&& request.plan_hash == plan_hash
|
|
&& job.execution_identity == "agent_service",
|
|
"ORIGINAL_SIGNED_PLAN_BINDING"
|
|
);
|
|
let Action::RunScript { script_utf8, .. } = &job.action else {
|
|
anyhow::bail!("ORIGINAL_SCRIPT_REQUIRED")
|
|
};
|
|
ensure!(
|
|
hash(script_utf8.as_bytes()) == script_hash && script_utf8.starts_with('\u{feff}'),
|
|
"EXACT_APPROVED_BOM_SCRIPT"
|
|
);
|
|
let result: Outcome = serde_json::from_slice(&outcome)?;
|
|
ensure!(
|
|
result.state == "succeeded"
|
|
&& result.exit_code == Some(0)
|
|
&& result.stdout
|
|
== b"Windows-MVP: allgemeine Skriptwartung\r\nUnicode:252\r\nS-1-5-19\r\n"
|
|
&& result.stderr.is_empty()
|
|
&& !result.output_truncated
|
|
&& !result.reboot_required
|
|
&& result.error_code.is_empty()
|
|
&& result.evidence["script_sha256"] == script_hash
|
|
&& result.evidence["execution_sid"] == "S-1-5-19"
|
|
&& result.evidence["execution_policy_scope"] == "process"
|
|
&& result.evidence["execution_policy_requested"] == "RemoteSigned"
|
|
&& result.evidence["postconditions_passed"] == true,
|
|
"EXACT_NATIVE_RESULT"
|
|
);
|
|
let local:u64=agent.query_row("SELECT count(*) FROM job_attempts WHERE job=?1 AND attempt=?2 AND state='succeeded' AND grant_id=?3",params![job_id,attempt_id,grant_id],|r|r.get(0))?;
|
|
ensure!(local == 1, "ORIGINAL_AGENT_TERMINAL_JOB");
|
|
println!(
|
|
"{}",
|
|
json!({"schema_version":1,"read_only":true,"job_id":job_id,"attempt_id":attempt_id,"grant_id":grant_id,"plan_hash":plan_hash,"script_sha256":script_hash,"consumption_count":count,"start_count":1,"outcome_count":1,"original_permit_signature_verified":true,"historical_reserved_at":reserved,"start_sha256":hash(&start),"outcome_sha256":hash(&outcome),"consumed_result_equals_outcome":true,"local_job_state":"succeeded","exit_code":0,"exact_stdout_verified":true,"execution_sid":"S-1-5-19","unicode_codepoint":252,"rmm_job_started":false})
|
|
);
|
|
Ok(())
|
|
}
|
|
#[cfg(windows)]
|
|
fn main() {
|
|
if let Err(error) = run() {
|
|
eprintln!("SCRIPT_WITNESS_FAILED: {error}");
|
|
std::process::exit(1)
|
|
}
|
|
}
|
|
#[cfg(not(windows))]
|
|
fn main() {
|
|
eprintln!("Windows-only read-only witness");
|
|
std::process::exit(1)
|
|
}
|