Files
rust-discovery/deploy/windows/passive_time_closeout.py
T
2026-11-15 17:01:33 +01:00

189 lines
9.5 KiB
Python

#!/usr/bin/env python3
"""Read two public service observations; never request time or alter the guest."""
import argparse
import base64
import datetime as dt
import hashlib
import json
import os
from pathlib import Path
import re
import sys
import time
import uuid
import system_pipe_upgrade as access_bridge
import time_route_context as context_reader
ROOT = Path(__file__).resolve().parents[2]
ACCESS_ROOT = ROOT.parent / 'rust-discovery'
STATE = ROOT / 'devstate/windows-passive-time-closeout'
FIELDS = {'schema_version', 'agent_pid', 'observed_at', 'role', 'windows_boot_id',
'connection_generation', 'verifier_incarnation', 'request_sha256', 'response_sha256'}
FILES = {'agent': 'agent-time-sample.json', 'system_helper': 'helper-time-sample.json'}
HEX = re.compile(r'[0-9a-f]{64}')
SECOND_NS = 1_000_000_000
# No report is requested from a service. The only content reads on the guest
# are these two public JSONs and public executable hashes. CIM/process queries
# obtain current boot/SCM/image/birth metadata without starting another binary.
SCRIPT = Path(__file__).with_name('Read-PassiveTimeSamples.ps1').read_text(encoding='utf-8')
def need(value, tag):
if not value:
raise ValueError(tag)
def strict(pairs):
value = {}
for key, item in pairs:
need(key not in value, 'DUPLICATE_PUBLIC_FIELD')
value[key] = item
return value
def stamp(value):
need(type(value) is str, 'UTC_TIMESTAMP_REQUIRED')
match = re.fullmatch(r'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?Z', value)
need(match is not None, 'UTC_TIMESTAMP_REQUIRED')
base = dt.datetime.strptime(match[1], '%Y-%m-%dT%H:%M:%S').replace(tzinfo=dt.timezone.utc)
elapsed = base - dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
return (elapsed.days * 86400 + elapsed.seconds) * SECOND_NS + int((match[2] or '').ljust(9, '0'))
def utc_ns(value):
return stamp(value.astimezone(dt.timezone.utc).isoformat().replace('+00:00', 'Z'))
def canonical_uuid(value):
need(type(value) is str and str(uuid.UUID(value)) == value and uuid.UUID(value).int > 0, 'UUID_REQUIRED')
return value
def sha(path):
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
def save(folder, name, value):
fd = os.open(folder / name, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600)
with os.fdopen(fd, 'w') as stream:
json.dump(value, stream, indent=2)
stream.flush()
os.fsync(stream.fileno())
def windows_boot(ticks):
need(type(ticks) is str and ticks.isascii() and ticks.isdigit() and 17 <= len(ticks) <= 19, 'BOOT_TICKS')
raw = bytearray(hashlib.sha256(('rmm.windows.boot.cim.ticks.v1:' + ticks).encode()).digest()[:16])
raw[6] = (raw[6] & 15) | 0x80
raw[8] = (raw[8] & 63) | 0x80
return str(uuid.UUID(bytes=bytes(raw)))
def filetime(value):
need(type(value) is str and value.isascii() and value.isdigit(), 'FILETIME_REQUIRED')
return (int(value) - 116444736000000000) * 100
def validate(native, server, since, observed_now):
need(native['read_only'] is True and server['sql_read_only'] is True and server['secrets_read'] is False, 'READ_ONLY_WITNESSES')
current = server['installation']
caps = current['capabilities']
need(type(caps) is list and len(caps) == 3 and set(caps) == {'windows.base/1', 'heartbeat/1', 'agent.update/1'}, 'PASSIVE_SERVER_CAPABILITIES')
boot = windows_boot(native['boot_ticks'])
need(current['windows_boot_id'] == boot, 'CURRENT_SERVER_WINDOWS_BOOT')
generation = current['connection_generation']
need(type(generation) is int and generation > 0, 'CURRENT_POSITIVE_GENERATION')
now = stamp(native['observed_at'])
need(abs(now - utc_ns(observed_now)) <= 10 * SECOND_NS, 'CURRENT_WINDOWS_UTC')
records = []
for role, filename in FILES.items():
wrapped = native['reports'][filename]
need(wrapped is not None, 'PUBLIC_SERVICE_SAMPLE_MISSING')
raw = base64.b64decode(wrapped['raw_base64'], validate=True)
need(len(raw) <= 8192 and hashlib.sha256(raw).hexdigest() == wrapped['sha256'], 'PUBLIC_SAMPLE_RAW_HASH')
record = json.loads(raw, object_pairs_hook=strict)
need(set(record) == FIELDS and type(record['schema_version']) is int and record['schema_version'] == 1, 'PUBLIC_SAMPLE_SCHEMA')
need(record['role'] == role and type(record['agent_pid']) is int and record['agent_pid'] == native['runtime']['agent']['pid'], 'SAMPLE_PROCESS_ROLE')
need(record['windows_boot_id'] == boot and type(record['connection_generation']) is int and record['connection_generation'] == generation, 'SAMPLE_BOOT_GENERATION')
canonical_uuid(record['verifier_incarnation'])
for field in ('request_sha256', 'response_sha256'):
need(type(record[field]) is str and HEX.fullmatch(record[field]) is not None, 'SAMPLE_HASH_FORMAT')
observed = stamp(record['observed_at'])
modified = filetime(wrapped['metadata']['last_write_filetime'])
birth = filetime(native['runtime']['agent']['birth_filetime'])
need(since <= observed <= now + 2 * SECOND_NS and birth <= observed, 'SAMPLE_CURRENT_INSTALL_WINDOW')
need(0 <= now - modified <= 180 * SECOND_NS and 0 <= now - observed <= 180 * SECOND_NS, 'SAMPLE_FILE_AGE')
need(-2 * SECOND_NS <= modified - observed <= 90 * SECOND_NS, 'SAMPLE_PUBLICATION_DELAY')
need(wrapped['metadata']['dacl_checked'] is True, 'SAMPLE_PROTECTED_METADATA')
records.append(record)
for field in ('verifier_incarnation', 'request_sha256', 'response_sha256'):
need(records[0][field] != records[1][field], 'INDEPENDENT_ROLE_' + field.upper())
return records
def collect(args):
need(HEX.fullmatch(args.agent_sha256) and HEX.fullmatch(args.helper_sha256), 'QUALIFIED_RUNTIME_HASHES_REQUIRED')
since = stamp(args.profile_after)
need(0 <= utc_ns(dt.datetime.now(dt.timezone.utc)) - since <= 600 * SECOND_NS, 'RECENT_PROFILE_INSTALL_WINDOW')
STATE.mkdir(parents=True, mode=0o700, exist_ok=True)
ident = str(uuid.uuid4())
folder = STATE / ident
folder.mkdir(mode=0o700)
save(folder, 'intent.json', dict(id=ident, operator_sha256=sha(__file__), source_commit='cc821246ccd98ee6d3dc5c1b12c7303d3010e8a7',
read_only=True, agent_sha256=args.agent_sha256, helper_sha256=args.helper_sha256, profile_after=args.profile_after,
wait_seconds=180, in_progress_read_budget_seconds=105, sample_max_age_seconds=180, no_requests_to_services=True))
access_bridge.STATE.mkdir(parents=True, mode=0o700, exist_ok=True)
access = access_bridge.Access(ACCESS_ROOT)
deadline = time.monotonic() + 180
attempt = 0
last = 'PUBLIC_SERVICE_SAMPLE_MISSING'
while time.monotonic() < deadline:
attempt += 1
before = context_reader.rpc(access, dict(id=ident, deployment_result_sha256=context_reader.DEPLOYMENT_RESULT_SHA, image=context_reader.IMAGE), folder)
native = access.powershell(SCRIPT, dict(id=ident, agent_sha256=args.agent_sha256, helper_sha256=args.helper_sha256), timeout=45)
save(folder, f'{attempt:02d}-native.json', native) # persist before assertions
after = context_reader.rpc(access, dict(id=ident, deployment_result_sha256=context_reader.DEPLOYMENT_RESULT_SHA, image=context_reader.IMAGE), folder)
save(folder, f'{attempt:02d}-server.json', dict(before=before, after=after))
try:
need(before == after, 'SERVER_CONTEXT_CHANGED_DURING_READ')
records = validate(native, after, since, dt.datetime.now(dt.timezone.utc))
except ValueError as error:
last = str(error)
save(folder, f'{attempt:02d}-not-accepted.json', dict(reason=last, read_only=True))
if last not in ('PUBLIC_SERVICE_SAMPLE_MISSING', 'SAMPLE_BOOT_GENERATION', 'SAMPLE_PROCESS_ROLE', 'SAMPLE_CURRENT_INSTALL_WINDOW', 'SAMPLE_FILE_AGE', 'SERVER_CONTEXT_CHANGED_DURING_READ'):
raise
time.sleep(min(5, max(0, deadline-time.monotonic())))
continue
links = {name: sha(folder/name) for name in ('intent.json', f'{attempt:02d}-native.json', f'{attempt:02d}-server.json')}
save(folder, 'receipt.json', dict(id=ident, operator_sha256=sha(__file__), verified=True, read_only=True,
historical_role_samples_only=True, current_job_clock_proven=False, records=records, links=links))
print(json.dumps(dict(id=ident, verified=True, read_only=True, receipt_sha256=sha(folder/'receipt.json'))))
return
save(folder, 'incomplete.json', dict(id=ident, verified=False, read_only=True, reason=last, no_diagnostic_triggered=True))
print(json.dumps(dict(id=ident, verified=False, reason=last, read_only=True)))
raise SystemExit(1)
def guards():
forbidden = ('Start-Service', 'Stop-Service', 'Restart-Service', 'Remove-Item', 'WriteAll', 'WriteNew', 'TimeBegin', 'TimeComplete', '--native-probe', 'Snapshot', 'Execute')
need(all(item not in SCRIPT for item in forbidden), 'READ_ONLY_SOURCE_ONLY')
need(all(name in SCRIPT for name in FILES.values()), 'TWO_FIXED_REPORT_NAMES')
print('PASS local source guards; no target call')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('action', choices=('guards', 'collect'))
parser.add_argument('--agent-sha256')
parser.add_argument('--helper-sha256')
parser.add_argument('--profile-after')
args = parser.parse_args()
if args.action == 'guards':
guards()
else:
need(all((args.agent_sha256, args.helper_sha256, args.profile_after)), 'ROOT_ACTUAL_PROFILE_AND_RUNTIME_REQUIRED')
guards()
collect(args)