#!/usr/bin/env python3 """One fixed original-case SYSTEM fixture repair; no RMM job or role start.""" import argparse import json from pathlib import Path import subprocess import uuid import signed_clock_capture_acceptance as original import signed_clock_capture_system_repair as repair ROOT = Path(__file__).resolve().parents[2] FOLDER = ROOT / 'devstate/windows-signed-clock-capture-system-repair/cff25311-d30b-495a-8b50-e0e28e2e5b48' SCRIPT_SHA = 'ac3da3d86bd93497e8ecfe55369cb228ed0ce30c7f2301afb74463c789bb50ca' GENERATOR_SHA = '9b44aa1491be2d154fb37fd0431370657f9623a2440649c29e910826beb44d2e' NAME = 'SignedClockCaptureSystemRepair.ps1' require, save, load = original.require, original.save, original.load LAUNCH = r'''$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json) if((($p.PSObject.Properties.Name|Sort-Object)-join',')-cne'action,id,profile,script_sha256'-or$p.id-cne'efd17b43-7e04-49a7-9bd4-862d13365eec'-or$p.profile-cne'rmm.windows.raw-clock.original-system-create-repair.v1'-or$p.script_sha256-cne'ac3da3d86bd93497e8ecfe55369cb228ed0ce30c7f2301afb74463c789bb50ca'-or$p.action-notin@('repair','read')){throw 'Exact original fixture repair launch'} $script='C:\ProgramData\RustDiscoveryBootstrap\ac3da3d86bd93497e8ecfe55369cb228ed0ce30c7f2301afb74463c789bb50ca\SignedClockCaptureSystemRepair.ps1' $i=Get-Item -LiteralPath $script -Force;if($i.PSIsContainer-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)-or$i.Length-ne15551-or(Get-FileHash -LiteralPath $script -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.script_sha256){throw 'Exact original fixture repair bytes'} $parent=$i.Directory;while($null-ne$parent){if($parent.Attributes-band[IO.FileAttributes]::ReparsePoint){throw 'No repair launch reparse ancestry'};$parent=$parent.Parent} $a=Get-Acl -LiteralPath $script;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Repair controller owner'};foreach($r in $a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private repair controller DACL'}} $global:RmmCaptureSystemRepairPayload=$p & $script ''' def verify(): require(original.sha(ROOT / 'deploy/windows/signed_clock_capture_acceptance.py') == repair.RUNNER_SHA, 'unchanged original capture operator') require(original.sha(ROOT / 'deploy/windows/signed_clock_capture_system_repair.py') == GENERATOR_SHA, 'reviewed original-case repair generator') original.private(FOLDER, True) manifest = load(FOLDER / 'manifest.json') require(manifest['original_case'] == repair.CASE and manifest['generator_sha256'] == GENERATOR_SHA and manifest['repair_script_sha256'] == SCRIPT_SHA and manifest['repair_script_bytes'] == 15551, 'original fixed repair artifact') script = FOLDER / NAME original.private(script) require(script.stat().st_size == 15551 and original.sha(script) == SCRIPT_SHA, 'reviewed fixed repair controller') require(original.sha(repair.ORIGINAL / 'manifest.json') == repair.MANIFEST_SHA, 'original partial setup provenance') def stage(access): # Separate literal allowlist; the original capture transport is unchanged. prepare = original.pipe.u.transport.PREPARE_SCRIPT.replace('TransportAcceptance.ps1', NAME) observed = access.powershell(prepare, dict(sha256=SCRIPT_SHA)) if not observed['already_uploaded']: credentials = original.pipe.cb.contract.protected_json(access.root / 'credentials/windows-dev-access.json') prefix = credentials['ssh_prefix'] args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b', '-', prefix[-1]] target = '/C:/ProgramData/RustDiscoveryBootstrap/' + SCRIPT_SHA + '/' + NAME result = subprocess.run(args, input=('put ' + json.dumps(str(FOLDER / NAME)) + ' ' + json.dumps(target) + '\n').encode(), capture_output=True, timeout=60) require(result.returncode == 0, 'one exact repair transfer; lost reply read only') observed = access.powershell(prepare, dict(sha256=SCRIPT_SHA)) require(observed['already_uploaded'] is True and observed['sha256'] == SCRIPT_SHA, 'immutable staged original-case repair') def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('action', choices=('stage', 'repair', 'read')) args = parser.parse_args() verify() access = original.CaptureAccess(original.ACCESS_ROOT) baseline = load(repair.ORIGINAL / 'baseline.json') require(original.server_closed(access) == baseline['server'], 'original closed server counters and policy') original.preserved(baseline['native'], original.native_witness(access)) action = args.action if action != 'read': save(FOLDER / (action + '-attempt.json'), dict(original_case=repair.CASE, once=True, no_retry=True, script_sha256=SCRIPT_SHA, operator_sha256=original.sha(Path(__file__)))) if action == 'stage': stage(access) response = dict(staged=True, script_sha256=SCRIPT_SHA) else: if action == 'repair': require((FOLDER / 'stage-reply.json').exists(), 'completed original stage; no restaging') payload = dict(id=repair.CASE, profile=repair.PROFILE, action=action, script_sha256=SCRIPT_SHA) response = access.powershell(LAUNCH, payload, timeout=60) identifier = str(uuid.uuid4()) if action == 'read' else action save(FOLDER / (identifier + '-reply.json'), response) after_server, after_native = original.server_closed(access), original.native_witness(access) require(after_server == baseline['server'], 'closed server policy and counters unchanged') original.preserved(baseline['native'], after_native) save(FOLDER / (identifier + '-preservation.json'), dict(server=after_server, native=after_native, original_case=repair.CASE, response_sha256=original.sha(FOLDER / (identifier + '-reply.json')), no_rmm_jobs=True, no_role_start=True, setup_completed=False)) if action == 'repair': require(response.get('system_created_stopped') is True and response.get('setup_completed') is False and response.get('service_started') is False, 'only own SYSTEM fixture created stopped') print(json.dumps(dict(action=action, original_case=repair.CASE, read_only=action == 'read', preservation_sha256=original.sha(FOLDER / (identifier + '-preservation.json')), response_sha256=original.sha(FOLDER / (identifier + '-reply.json')), preserved=True, system_created_stopped=response.get('system_created_stopped'), setup_completed=False))) if __name__ == '__main__': main()