236 lines
9.1 KiB
Rust
236 lines
9.1 KiB
Rust
//! Exact reviewed SYSTEM nonstart/grant and connection221 closure; no job authority.
|
|
use crate::{
|
|
checkpoint_repair::{SignedDecision, inventory_sha256},
|
|
jobs::hash,
|
|
windows_echo_probe::{AGENT, ASSET, TENANT, TEST_PRINCIPAL},
|
|
};
|
|
use chrono::{DateTime, Duration, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
pub const DOMAIN: &str = "rmm.dev.windows-system-nonstart-221.v1";
|
|
pub const FIRST: Uuid = Uuid::from_u128(0x193b951ba3aa46e29f481a57f315078e);
|
|
pub const SECOND: Uuid = Uuid::from_u128(0x0442c73282264c2596157f007e24b269);
|
|
pub const FIRST_REQUEST: Uuid = Uuid::from_u128(0xf07ea08e84f248f484766921057db9af);
|
|
pub const SECOND_REQUEST: Uuid = Uuid::from_u128(0x09952f6d45f3433082f387ebf363eb3b);
|
|
pub const BOOT: Uuid = Uuid::from_u128(0xa9ad08955a968f2c8fb6cfdc2d5225ea);
|
|
pub const JOB: Uuid = Uuid::from_u128(0x940478735088433bbc1b490d3eafe0da);
|
|
pub const ATTEMPT: Uuid = Uuid::from_u128(0xf071e6d6863542f792fa533c681edd8b);
|
|
pub const GRANT: Uuid = Uuid::from_u128(0x97e705b8fb2b415884d9d90595d1ce6e);
|
|
pub const UNKNOWN_SHA256: &str = "1f284b66578d61c4364cc0060a67ce9b467e1d661ed8885804391b51a6f18b1a";
|
|
pub const HELPER_IMAGE: &str = "0502d6c87bd74c99f1665f3cc4b9d74b8e31d0f829d2ce1a1597d72e9be1e726";
|
|
pub const SCRIPT_SOURCE: &str = "a3563e1aa8327b2e7046c7cdfb4f45ac213fec6ff7d477820ea2676a266c1695";
|
|
pub const ERROR_CODE: &str = "WINDOWS_SYSTEM_NONSTART_CONFIRMED_221";
|
|
pub const MAX_WITNESS_BYTES: usize = 768 * 1024;
|
|
pub const FILE_ROLES: [&str; 6] = [
|
|
"telemetry",
|
|
"consumption",
|
|
"agent_lock",
|
|
"helper_ledger",
|
|
"helper_lock",
|
|
"system_intents",
|
|
];
|
|
pub use crate::windows_sync_alignment::{Counts, NativeFile};
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Witness {
|
|
pub schema_version: u32,
|
|
pub tenant_id: Uuid,
|
|
pub asset_id: Uuid,
|
|
pub agent_id: Uuid,
|
|
pub enrollment_generation: u64,
|
|
pub assignment_version: u64,
|
|
pub boot_id: Uuid,
|
|
pub connection_generation: u64,
|
|
pub agent_journal_id: Uuid,
|
|
pub helper_journal_id: Uuid,
|
|
pub agent_checkpoint: u64,
|
|
pub helper_checkpoint: u64,
|
|
pub agent_last_decision: Uuid,
|
|
pub helper_last_decision: Uuid,
|
|
pub verification_key_id: String,
|
|
pub verification_public_key_hex: String,
|
|
pub binding_sha256: String,
|
|
pub helper_binding_sha256: String,
|
|
pub files: [NativeFile; 6],
|
|
pub agent_inventory_sha256: String,
|
|
pub consumption_inventory_sha256: String,
|
|
pub helper_inventory_sha256: String,
|
|
pub intent_inventory_sha256: String,
|
|
pub counts: Counts,
|
|
pub original_requests: [Vec<u8>; 2],
|
|
pub original_responses: [SignedDecision; 2],
|
|
pub target_job_offer: Vec<u8>,
|
|
pub agent_start: Vec<u8>,
|
|
pub helper_start: Vec<u8>,
|
|
pub agent_outcome: Vec<u8>,
|
|
pub helper_outcome: Vec<u8>,
|
|
pub intent: Vec<u8>,
|
|
pub nonstart: NonstartProof,
|
|
}
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct NonstartProof {
|
|
pub helper_pid: u32,
|
|
pub helper_birth_filetime: String,
|
|
pub helper_image_sha256: String,
|
|
pub system_script_source_sha256: String,
|
|
pub read_share_error: u32,
|
|
pub read_write_share_passed: bool,
|
|
pub state_directory_file_id: [u32; 3],
|
|
pub evidence_sha256: Vec<String>,
|
|
}
|
|
impl NonstartProof {
|
|
pub fn valid(&self) -> bool {
|
|
self.helper_pid == 1304
|
|
&& self.helper_birth_filetime == "134393135436289391"
|
|
&& self.helper_image_sha256 == HELPER_IMAGE
|
|
&& self.system_script_source_sha256 == SCRIPT_SOURCE
|
|
&& self.read_share_error == 32
|
|
&& self.read_write_share_passed
|
|
&& self.state_directory_file_id != [0; 3]
|
|
&& !self.evidence_sha256.is_empty()
|
|
&& self.evidence_sha256.len() <= 16
|
|
&& self
|
|
.evidence_sha256
|
|
.iter()
|
|
.all(|v| crate::agent_update::digest(v))
|
|
}
|
|
}
|
|
impl Witness {
|
|
pub fn digest(&self) -> Option<String> {
|
|
inventory_sha256(&serde_json::to_value(self).ok()?).ok()
|
|
}
|
|
pub fn valid(&self) -> bool {
|
|
let digest = crate::agent_update::digest;
|
|
self.schema_version == 1
|
|
&& (self.tenant_id, self.asset_id, self.agent_id) == (TENANT, ASSET, AGENT)
|
|
&& self.enrollment_generation == 1
|
|
&& self.assignment_version == 1
|
|
&& self.boot_id == BOOT
|
|
&& self.connection_generation == 221
|
|
&& !self.agent_journal_id.is_nil()
|
|
&& !self.helper_journal_id.is_nil()
|
|
&& self.agent_journal_id != self.helper_journal_id
|
|
&& self.agent_checkpoint == 10784
|
|
&& self.helper_checkpoint >= 413
|
|
&& self.agent_last_decision == SECOND
|
|
&& self.helper_last_decision == FIRST
|
|
&& [
|
|
&self.verification_key_id,
|
|
&self.verification_public_key_hex,
|
|
&self.binding_sha256,
|
|
&self.helper_binding_sha256,
|
|
&self.agent_inventory_sha256,
|
|
&self.consumption_inventory_sha256,
|
|
&self.helper_inventory_sha256,
|
|
&self.intent_inventory_sha256,
|
|
]
|
|
.iter()
|
|
.all(|h| digest(h))
|
|
&& self.binding_sha256 == self.helper_binding_sha256
|
|
&& hex::decode(&self.verification_public_key_hex)
|
|
.is_ok_and(|k| hash(&k) == self.verification_key_id)
|
|
&& self.files.iter().zip(FILE_ROLES).all(|(f, role)| {
|
|
f.role == role
|
|
&& match (&f.file_id, &f.sha256) {
|
|
(Some(id), Some(h)) => *id != [0; 3] && digest(h),
|
|
(None, None) => role == "system_intents",
|
|
_ => false,
|
|
}
|
|
})
|
|
&& self.counts
|
|
== (Counts {
|
|
agent_jobs: 4,
|
|
agent_consumed: 3,
|
|
agent_starts: 3,
|
|
agent_outcomes: 3,
|
|
helper_consumed: 1,
|
|
helper_starts: 1,
|
|
helper_outcomes: 1,
|
|
system_intents: 1,
|
|
})
|
|
&& self
|
|
.original_requests
|
|
.iter()
|
|
.all(|v| !v.is_empty() && v.len() <= 65536)
|
|
&& self.original_responses.iter().all(|v| {
|
|
!v.decision_bytes.is_empty()
|
|
&& v.decision_bytes.len() <= 65536
|
|
&& v.signature.len() == 64
|
|
})
|
|
&& [
|
|
&self.target_job_offer,
|
|
&self.agent_start,
|
|
&self.helper_start,
|
|
&self.agent_outcome,
|
|
&self.helper_outcome,
|
|
&self.intent,
|
|
]
|
|
.iter()
|
|
.all(|v| !v.is_empty() && v.len() <= 131072)
|
|
&& hash(&self.agent_outcome) == UNKNOWN_SHA256
|
|
&& hash(&self.helper_outcome) == UNKNOWN_SHA256
|
|
&& self.nonstart.valid()
|
|
&& serde_json::to_vec(self).is_ok_and(|v| v.len() <= MAX_WITNESS_BYTES)
|
|
}
|
|
}
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Receipt {
|
|
pub domain: String,
|
|
pub schema_version: u32,
|
|
pub receipt_id: Uuid,
|
|
pub request_id: Uuid,
|
|
pub witness: Witness,
|
|
pub witness_sha256: String,
|
|
pub server_transport_generation: u64,
|
|
pub key_id: String,
|
|
pub issued_at: DateTime<Utc>,
|
|
pub valid_until: DateTime<Utc>,
|
|
pub confirmed_by: String,
|
|
pub approval_origin: String,
|
|
pub testsimulation: bool,
|
|
pub ready: bool,
|
|
pub helper_ready: bool,
|
|
pub unlock: bool,
|
|
}
|
|
impl Receipt {
|
|
pub fn valid(&self, now: DateTime<Utc>) -> bool {
|
|
self.domain == DOMAIN
|
|
&& self.schema_version == 1
|
|
&& !self.receipt_id.is_nil()
|
|
&& !self.request_id.is_nil()
|
|
&& self.receipt_id != self.request_id
|
|
&& self.server_transport_generation >= 221
|
|
&& self.witness.valid()
|
|
&& self.witness.digest().as_deref() == Some(self.witness_sha256.as_str())
|
|
&& self.key_id == self.witness.verification_key_id
|
|
&& self.confirmed_by == TEST_PRINCIPAL
|
|
&& self.approval_origin == "dev_test_browser_simulation"
|
|
&& self.testsimulation
|
|
&& !self.ready
|
|
&& !self.helper_ready
|
|
&& !self.unlock
|
|
&& self.issued_at <= now
|
|
&& now < self.valid_until
|
|
&& self.valid_until > self.issued_at
|
|
&& self.valid_until - self.issued_at <= Duration::minutes(5)
|
|
}
|
|
}
|
|
|
|
pub fn failed_outcome(
|
|
receipt_id: Uuid,
|
|
receipt_sha256: &str,
|
|
) -> crate::reconciliation::ReconciledOutcome {
|
|
crate::reconciliation::ReconciledOutcome {
|
|
state: "failed".into(),
|
|
stdout: vec![],
|
|
stderr: vec![],
|
|
exit_code: None,
|
|
output_truncated: false,
|
|
reboot_required: false,
|
|
error_code: ERROR_CODE.into(),
|
|
evidence: serde_json::json!({"postconditions_passed":false,"not_started":true,"original_unknown_sha256":UNKNOWN_SHA256,"administrative_receipt_id":receipt_id,"administrative_receipt_sha256":receipt_sha256}),
|
|
}
|
|
}
|