Files
rust-discovery/deploy/windows/clock-spnego-descriptor-trace/native.py
T
2026-11-14 21:09:00 +01:00

349 lines
30 KiB
Python

#!/usr/bin/env python3
"""Parent-only native SYSTEM trace bootstrap: prepare, once start, read, cleanup.
Readback is strictly read-only. Cleanup is a separate explicit phase and may
remove only this exact stopped diagnostic service. Never changes W32Time/RMM.
"""
import argparse
import base64
import datetime as dt
import hashlib
import json
import os
from pathlib import Path
import re
import shlex
import stat
import subprocess
import sys
import uuid
ROOT = Path(__file__).resolve().parents[3]
HERE = Path(__file__).resolve().parent
STATE = ROOT / 'devstate/windows-clock-spnego-descriptor-trace'
ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery')
def require(value,message):
if not value: raise RuntimeError(message)
def sha(path):
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
def pairs(items):
result = {}
for name,value in items:
require(name not in result,'duplicate JSON key'); result[name] = value
return result
def private(path,directory=False):
item = Path(path).lstat()
require(item.st_uid == os.getuid() and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600)
and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1),'private owned path')
def load(path):
private(path); require(Path(path).stat().st_size <= 2*1024*1024,'bounded private JSON')
return json.loads(Path(path).read_bytes(),object_pairs_hook=pairs)
def save(path,value):
private(path.parent,True)
fd = os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'w') as output:
json.dump(value,output,indent=2); output.flush(); os.fsync(output.fileno())
SCRIPT = r'''
$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json)
$id=[guid]::ParseExact($p.id,'D').ToString()
if($id-cne$p.id-or$p.profile-cne'clock-system-spnego-descriptor-v1'-or$p.action-notin@('prepare','start','readback','cleanup')-or$p.binary_sha256-notmatch'^[a-f0-9]{64}$'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.script_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed SYSTEM diagnostic request'}
$root='C:\ProgramData\RustDiscoveryBootstrap';$name='RmmClockSpnegoDescriptorDiagnostic';$case=Join-Path $root ('clock-spnego-descriptor-trace-'+$id);$exe=Join-Path $case 'ClockSpnegoDescriptorTrace.exe';$command='"'+$exe+'" --service '+$id
function Test-RmmTracePrivate($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private object type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private object owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private ACL'}}}
function Write-RmmTraceNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}}
function Get-RmmTraceJson($path){Test-RmmTracePrivate $path $false;if((Get-Item -LiteralPath $path).Length-gt262144){throw 'Small diagnostic receipt'};return(Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)}
function Get-RmmTraceService{return(Get-CimInstance Win32_Service -Filter "Name='RmmClockSpnegoDescriptorDiagnostic'" -OperationTimeoutSec 5)}
function Test-RmmTraceService($s){if($s-and($s.PathName-cne$command-or$s.StartName-cne'LocalSystem'-or$s.StartMode-cne'Manual')){throw 'Only own exact temporary service'}}
if($p.action-eq'readback'-and!(Test-Path -LiteralPath $case)){@{id=$id;status='remote_case_absent';read_only=$true}|ConvertTo-Json -Compress;exit 0}
Test-RmmTracePrivate $root $true
if($p.action-eq'prepare'){if((Test-Path -LiteralPath $case)-or(Get-RmmTraceService)){throw 'Existing case/service retained'};New-Item -ItemType Directory -Path $case|Out-Null;Test-RmmTracePrivate $case $true;Write-RmmTraceNew (Join-Path $case 'intent.json') $p;@{id=$id;prepared=$true}|ConvertTo-Json -Compress;exit 0}
Test-RmmTracePrivate $case $true;$old=Get-RmmTraceJson (Join-Path $case 'intent.json')
foreach($field in @('id','profile','binary_sha256','manifest_sha256','script_sha256','binary_bytes')){if($old.$field-cne$p.$field){throw 'Original exact diagnostic intent'}}
Test-RmmTracePrivate $exe $false;if((Get-Item -LiteralPath $exe).Length-ne$p.binary_bytes-or(Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.binary_sha256){throw 'Exact diagnostic executable'}
$s=Get-RmmTraceService;Test-RmmTraceService $s
if($p.action-eq'start'){
if($s-or(Test-Path -LiteralPath (Join-Path $case 'start-intent.json'))){throw 'No repeated start; original readback only'}
Write-RmmTraceNew (Join-Path $case 'start-intent.json') @{id=$id;command=$command;binary_sha256=$p.binary_sha256;create_once=$true}
New-Service -Name $name -BinaryPathName $command -StartupType Manual|Out-Null
$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s){throw 'Own service creation missing'}
Write-RmmTraceNew (Join-Path $case 'service-created.json') @{id=$id;command=$command;start_name=$s.StartName}
Write-RmmTraceNew (Join-Path $case 'service-start-intent.json') @{start_once=$true}
Start-Service -Name $name
$deadline=[DateTime]::UtcNow.AddSeconds(20)
do{$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s-or$s.State-eq'Stopped'){break};Start-Sleep -Milliseconds 100}while([DateTime]::UtcNow-lt$deadline)
}
if($p.action-eq'cleanup'){
if(!$s){if(!(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'))){throw 'Missing service without bound cleanup'};@{id=$id;cleanup_verified=$true;already_absent=$true;read_only=$true}|ConvertTo-Json -Compress;exit 0}
if($s.State-cne'Stopped'){throw 'Cleanup only own bound stopped service'}
$created=Get-RmmTraceJson (Join-Path $case 'service-created.json');if($created.id-cne$id-or$created.command-cne$command-or$created.start_name-cne'LocalSystem'){throw 'Original service creation binding'}
if(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json')){throw 'Cleanup already attempted; readback only'}
Write-RmmTraceNew (Join-Path $case 'cleanup-intent.json') @{id=$id;command=$command;delete_once=$true}
& "$env:WINDIR\System32\sc.exe" delete $name|Out-Null;if($LASTEXITCODE-ne0){throw 'Own stopped service deletion incomplete'}
if(Get-RmmTraceService){throw 'Own service still registered; readback only'}
Write-RmmTraceNew (Join-Path $case 'cleanup-completed.json') @{id=$id;service_absent=$true;service_exit_code=$s.ExitCode}
@{id=$id;cleanup_verified=$true;already_absent=$false;read_only=$false}|ConvertTo-Json -Compress;exit 0
}
$report=$null;$result=Join-Path $case 'result.json';if(Test-Path -LiteralPath $result){$report=Get-RmmTraceJson $result}
$token=$null;$tokenFile=Join-Path $case 'token.json';if(Test-Path -LiteralPath $tokenFile){$token=Get-RmmTraceJson $tokenFile}
$before=$null;$beforeFile=Join-Path $case 'privilege-before.json';if(Test-Path -LiteralPath $beforeFile){$before=Get-RmmTraceJson $beforeFile}
$adjust=$null;$adjustFile=Join-Path $case 'privilege-adjustment.json';if(Test-Path -LiteralPath $adjustFile){$adjust=Get-RmmTraceJson $adjustFile}
$cleaned=$false;$cleanedExit=$null;$cleanupReceipt=Join-Path $case 'cleanup-completed.json';if(Test-Path -LiteralPath $cleanupReceipt){$c=Get-RmmTraceJson $cleanupReceipt;if($c.id-cne$id-or$c.service_absent-ne$true-or$null-eq$c.service_exit_code-or$c.service_exit_code-lt0-or$c.service_exit_code-gt4294967295){throw 'Bound cleanup receipt'};$cleaned=$true;$cleanedExit=$c.service_exit_code}
@{id=$id;service_present=[bool]$s;service_state=$(if($s){$s.State}else{$null});service_exit_code=$(if($s){$s.ExitCode}else{$null});start_intent_present=(Test-Path -LiteralPath (Join-Path $case 'service-start-intent.json'));cleanup_intent_present=(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'));cleanup_completed=$cleaned;cleaned_service_exit_code=$cleanedExit;report=$report;token_before_rpc=$token;privilege_before=$before;privilege_adjustment=$adjust;read_only=($p.action-eq'readback');no_rmm_jobs=$true;no_w32time_change=$true;no_agent_change=$true;own_privilege_adjustment=$true;foreign_token_query_only=$true}|ConvertTo-Json -Depth 12 -Compress
'''
def encoded_command():
script = 'try {\n'+SCRIPT+'\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}'
return 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand '+base64.b64encode(script.encode('utf-16le')).decode()
def manifest(path):
path = path.absolute(); private(path.parent,True)
require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}',path.parent.name),'fixed local build manifest')
value = load(path)
require(value['profile'] == 'clock-system-spnego-descriptor-v1' and value['schema_version'] == 1
and type(value['identity_control']) is int and value['identity_control'] in (0,1,2) and value['qos_version']==5 and value['auth_principal_from_fixed_sid'] is False and value['auth_principal_from_discovery'] is True and value['client_server_descriptor'] is True
and value['native_executed'] is False and value['clock_qualified'] is False and value['product_gate_open'] is False
and value['powershell_ast_passed'] is True and value['encoded_command_characters'] == len(encoded_command()) < 32767
and value['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(),'same reviewed nonqualifying diagnostic source')
require(value['original_wrapper_sha256'] == '74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8'
and sha(path.parent / 'wrapper-source.diff') == value['wrapper_diff_sha256'],'same exact observational source delta')
require(value['token_query_only'] is False and value['own_token_privilege_adjustment'] is True and value['peer_token_query_only'] is True and value['max_enables']==value['max_restores']==1 and value['max_principal_discoveries']==value['max_status_queries']==1
and value['principal_identity_proven'] is False,'one own-token privilege adjustment/restore, peer QUERY only, no fallback/identity claim')
require(value['parent_manifest_sha256']=='d3a6222da8aed64e4e5ad5c07993177a11f56af229d4754ea04f248ee79385bc'
and value['parent_result_sha256']=='ce4004b1fd833b9590f23d31a918daefccf8870e6630be6beb28869fad1f8b5c'
and value['returned_empty_discovery_principal_allowed'] is True and value['null_discovery_principal_allowed'] is False
and sha(path.parent/'parent-wrapper-source.diff')==value['parent_wrapper_diff_sha256'],
'exact enabled-privilege parent; one explicit local impersonation-level comparison, no fallback')
for name,digest in value['unchanged_diagnostics'].items():require(sha(ROOT/name)==digest,'original SYSTEM/WINNT/EEInfo inputs retained')
for name,digest in value['own_sources'].items():require(sha(ROOT / name) == digest,'same original local driver source')
for name in ('artifact','linked'):
item=value[name];p=Path(item['path']);require(p.parent == path.parent / 'output','fixed retained PE path');private(p);require(sha(p) == item['sha256'],'retained exact diagnostic PE')
require(Path(value['artifact']['path']).stat().st_size == value['artifact']['bytes'],'exact PE size')
return value
def access(root):
require(root == ACCESS_ROOT and root.resolve() == root,'existing Main access root only')
sys.path.insert(0,str(ROOT / 'deploy'))
import vm_access_check
config = vm_access_check.load_config(root / 'credentials/server.conf')
require(config['address'] == '88.99.80.202' and config['username'] == 'root' and config.get('port','22') == '22','fixed existing cloud reverse jump')
record=load(root / 'credentials/windows-dev-access.json');expiry=dt.datetime.fromisoformat(record['expires_at'])
require(expiry.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expiry,'current existing access only')
private(root / 'credentials',True);private(root / 'credentials/windows-dev.known_hosts')
key=str(Path(config['ssh_key_path']).expanduser())
jump=['ssh','-F','/dev/null','-T','-p','22','-i',key,'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','ForwardAgent=no','-o','IdentitiesOnly=yes','-W','127.0.0.1:22240','root@88.99.80.202']
prefix=['ssh','-F','/dev/null','-T','-i',key,'-o','ProxyCommand='+shlex.join(jump),'-o','HostKeyAlias=rmm-windows-dev','-o','UserKnownHostsFile='+str(root / 'credentials/windows-dev.known_hosts'),'-o','GlobalKnownHostsFile=/dev/null','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','rmm-dev@127.0.0.1']
require(record['ssh_prefix'] == prefix,'original pinned transport only')
return prefix
def run_transport(args,data,folder):
try:
done=subprocess.run(args,input=data,capture_output=True,timeout=50)
save(folder / ('transport-'+str(uuid.uuid4())+'.json'),dict(returncode=done.returncode,stdout_b64=base64.b64encode(done.stdout).decode(),stderr_b64=base64.b64encode(done.stderr).decode()))
require(done.returncode == 0,'transport incomplete; original readback only')
return done.stdout
except subprocess.TimeoutExpired as error:
save(folder / ('transport-timeout-'+str(uuid.uuid4())+'.json'),dict(timed_out=True,stdout_b64=base64.b64encode(error.stdout or b'').decode(),stderr_b64=base64.b64encode(error.stderr or b'').decode()))
raise RuntimeError('lost reply; no repeated operation') from None
def rpc(prefix,payload,folder):
value=json.loads(run_transport(prefix+[encoded_command()],(json.dumps(payload)+'\n').encode(),folder).decode('utf-8-sig'),object_pairs_hook=pairs)
require(isinstance(value,dict) and 'bootstrap_error' not in value,'remote diagnostic incomplete')
return value
def check_rpc_report(value,ident):
require(value['id'] == ident and value['no_rmm_jobs'] is True and value['no_w32time_change'] is True and value['no_agent_change'] is True and value['own_privilege_adjustment'] is True and value['foreign_token_query_only'] is True,'exact nonproduct diagnostic case')
report=value.get('report')
if report is None:return False
require(report['schema_version'] == 1 and report['profile'] == 'clock-system-spnego-descriptor-v1' and report['case_id'] == ident
and report['token_system'] is True and report['read_only'] is False and report['diagnostic_only'] is True
and type(report['query_invocations']) is int and report['query_invocations'] in (0,1) and report['clock_qualified'] is False and report['adapter_qualified'] is False
and report['product_gate_open'] is False,'actual SYSTEM token and single nonqualifying trace')
token=report['token']
require(set(token)=={'query_status','system_user','token_type','thread_query_status','thread_present','lookup_status','privileges_status','privilege_present','privilege_attributes','privilege_enabled','privilege_default','privilege_removed'}
and all(type(v) is int and 0<=v<2**32 for v in token.values())
and token==value['token_before_rpc'] and token['query_status']==token['lookup_status']==token['privileges_status']==0
and token['system_user']==1 and token['token_type']==1 and token['thread_present']==0 and token['thread_query_status']==1008,
'durable own SYSTEM primary token/privileges observed before RPC without impersonation')
require(all(token[k] in (0,1) for k in ('privilege_present','privilege_enabled','privilege_default','privilege_removed'))
and token['privilege_enabled']==int(bool(token['privilege_attributes']&2))
and token['privilege_default']==int(bool(token['privilege_attributes']&1))
and token['privilege_removed']==int(bool(token['privilege_attributes']&4))
and (token['privilege_present'] or token['privilege_attributes']==0),'present/enabled privilege attributes remain observations only')
auth=report['auth']
require(set(auth)=={'discovery_invocations','discovery_status','discovery_return_present','principal_present','principal_units','principal_validated','status_query_invocations','authn_service','authn_level','impersonation_type','principal_free_attempted','principal_free_status','binding_free_attempted','binding_free_status','eeinfo_origin','qos_version','identity_control','server_descriptor_present'}
and all(type(v) is int and 0<=v<2**32 for v in auth.values())
and auth['qos_version']==5 and auth['identity_control'] in (0,1,2) and auth['server_descriptor_present'] in (0,1)
and auth['authn_service']==9 and auth['authn_level']==6 and auth['impersonation_type']==3
and all(auth[k] in (0,1) for k in ('discovery_invocations','discovery_return_present','principal_present','principal_validated','status_query_invocations','principal_free_attempted','binding_free_attempted'))
and auth['status_query_invocations']==report['query_invocations'] and auth['eeinfo_origin'] in (0,1,2,3)
and auth['discovery_return_present']<=auth['discovery_invocations']
and auth['principal_units']<=512,'bounded one-discovery/at-most-one-status SPNEGO Privacy Identify path')
if auth['principal_validated']:
require(auth['principal_present']==1 and 0<=auth['principal_units']<=512,'checked non-NULL bounded UTF16 principal, possibly empty')
if auth['principal_present']:
require(auth['principal_free_attempted']==1,'owned principal cleanup attempted')
if auth['status_query_invocations']:
require(auth['server_descriptor_present']==1 and auth['discovery_invocations']==auth['discovery_return_present']==auth['principal_present']==auth['principal_validated']==1
and auth['discovery_status']==0 and 0<=auth['principal_units']<=512,'no status query without successful checked principal discovery')
fields={'stage','native_rpc_return','native_rpc_return_present','rpc_exception','status_present','ul_size','c_entries','p_entries_present'}
require(set(report['trace']) == fields and type(report['rpc_code']) is int and 0 <= report['rpc_code'] < 2**32
and all(type(n) is int and 0 <= n < 2**32 for n in report['trace'].values()),'bounded numeric trace only')
ee=report['eeinfo']
require(set(ee) == {'capture_attempted','start_status','next_status','end_status','count','limit_reached','records'}
and all(type(ee[k]) is int and 0 <= ee[k] < 2**32 for k in ('capture_attempted','start_status','next_status','end_status','count','limit_reached'))
and ee['capture_attempted'] in (0,1) and 0 <= ee['count'] <= 4 and len(ee['records']) == ee['count']
and ee['limit_reached'] == int(ee['count'] == 4),'four-record numeric EEInfo budget')
require(ee['capture_attempted']==int(auth['eeinfo_origin']!=0),'numeric EEInfo origin matches capture')
record_fields={'generating_component','status','detection_location','flags','number_of_parameters'}
for record in ee['records']:
require(set(record) == record_fields and all(type(n) is int and 0 <= n < 2**32 for n in record.values())
and record['number_of_parameters'] <= 4,'numeric EEInfo fields only; no parameter contents')
if report['trace']['stage'] in (90,91):
require(ee['capture_attempted']==1 and report['rpc_code']==report['trace']['rpc_exception']
and auth['eeinfo_origin']==(2 if report['trace']['stage']==90 else 1),'original exception code preserved before immediate capture')
return (value['service_state'] == 'Stopped' and value['service_exit_code'] == 0) or (value['service_present'] is False and value['cleanup_completed'] is True and value.get('cleaned_service_exit_code')==0)
def numeric(value):
return type(value) is int and 0<=value<2**32
def validate_snapshot(value):
fields={'present','query_status','token_id','authentication_id','modified_id','expiration_time','token_type','impersonation_level','dynamic_charged','dynamic_available','group_count','privilege_count','privileges'}
require(isinstance(value,dict) and set(value)==fields,'exact numeric token snapshot')
for k in fields-{'token_id','authentication_id','modified_id','expiration_time','privileges'}:
require(numeric(value[k]),'numeric token statistic')
for k in ('token_id','authentication_id','modified_id','expiration_time'):
require(isinstance(value[k],list) and len(value[k])==2 and all(numeric(v) for v in value[k]),'numeric LUID/time pair')
require(value['present']==1 and value['query_status']==0 and value['token_type']==1,'queried primary token')
privileges=value['privileges']
require(isinstance(privileges,list) and len(privileges)==value['privilege_count']<=256
and all(isinstance(p,list) and len(p)==3 and all(numeric(n) for n in p) for p in privileges),'bounded complete privilege list')
keys=[(p[1],p[0]) for p in privileges]
require(keys==sorted(set(keys)),'sorted unique privilege LUIDs')
def same_identity(before,after):
return all(before[k]==after[k] for k in ('token_id','authentication_id','token_type'))
def check_privilege_evidence(value):
before=value.get('privilege_before');change=value.get('privilege_adjustment')
if before is None or change is None:return False
require(set(before)=={'schema_version','own_pid','own_creation','own','peers'} and before['schema_version']==1
and numeric(before['own_pid']) and before['own_pid']>0 and isinstance(before['own_creation'],list) and len(before['own_creation'])==2 and all(numeric(v) for v in before['own_creation']),'bound pre-enable numeric witness')
validate_snapshot(before['own'])
require(isinstance(before['peers'],list) and len(before['peers'])==4,'four named peer witnesses')
for i,peer in enumerate(before['peers']):
require(set(peer)=={'role','pid','creation','token'} and peer['role']==i and numeric(peer['pid']) and peer['pid']>0 and peer['pid']!=before['own_pid']
and isinstance(peer['creation'],list) and len(peer['creation'])==2 and all(numeric(v) for v in peer['creation']),'held peer PID/start binding')
validate_snapshot(peer['token'])
require(peer['token']['token_id']!=before['own']['token_id'],'different actual peer token object')
fields={'schema_version','preflight_status','enable_attempted','enable_return','enable_error','previous_count','previous_bytes','previous_luid','previous_attributes','active_status','rpc_attempted','restore_attempted','restore_return','restore_error','restore_input_from_previous','restored_status','peer_after_status','restoration_verified','flow_status','target_luid','active','restored','peers_after'}
require(set(change)==fields and all(numeric(change[k]) for k in fields-{'previous_luid','target_luid','active','restored','peers_after'}),'exact bounded adjustment witness')
for k in ('previous_luid','target_luid'):
require(isinstance(change[k],list) and len(change[k])==2 and all(numeric(n) for n in change[k]),'exact privilege LUID')
target=change['target_luid']
expected=[p.copy() for p in before['own']['privileges']]
targets=[p for p in expected if p[:2]==target]
require(len(targets)==1 and targets[0][2]==0,'exact one present originally disabled privilege')
require(change['schema_version']==1 and change['preflight_status']==0
and all(change[k] in (0,1) for k in ('enable_attempted','enable_return','rpc_attempted','restore_attempted','restore_return','restore_input_from_previous','restoration_verified')),'one bounded activation/restore flow')
# Failure reports remain available raw. Never call them preserved/complete.
if change['flow_status']!=0 or change['restoration_verified']!=1:return False
require(change['enable_attempted']==change['enable_return']==change['restore_attempted']==change['restore_return']==change['restore_input_from_previous']==1
and change['enable_error']==change['restore_error']==change['active_status']==change['restored_status']==change['peer_after_status']==0
and change['previous_count']==1 and change['previous_bytes']==16 and change['previous_luid']==target and change['previous_attributes']==0,
'one successful exact enable and PreviousState restore, not TRUE/NOT_ALL_ASSIGNED')
validate_snapshot(change['active']);validate_snapshot(change['restored'])
targets[0][2]=2
require(same_identity(before['own'],change['active']) and same_identity(before['own'],change['restored'])
and change['active']['privileges']==expected and change['restored']['privileges']==before['own']['privileges'],
'same own token only target enable bit changes and exact original attributes restored')
require(isinstance(change['peers_after'],list) and len(change['peers_after'])==4,'four post-restore peer snapshots')
for peer,after in zip(before['peers'],change['peers_after']):
validate_snapshot(after)
require(same_identity(peer['token'],after) and peer['token']['modified_id']==after['modified_id']
and peer['token']['privileges']==after['privileges'],'observed peer token and privilege state preserved')
require(change['rpc_attempted']==1 and value['report']['query_invocations']<=1,'at most one RPC function after verified enable')
return True
def check_report(value,ident):
rpc_complete=check_rpc_report(value,ident)
privilege_complete=check_privilege_evidence(value)
return rpc_complete and privilege_complete
def execute(action,path,root,ident):
os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True);private(STATE,True)
value=manifest(path);prefix=access(root)
if action == 'prepare':
require(not ident,'new diagnostic case only');ident=str(uuid.uuid4());folder=STATE / ident;folder.mkdir(mode=0o700)
payload=dict(id=ident,profile='clock-system-spnego-descriptor-v1',manifest_sha256=sha(path),binary_sha256=value['artifact']['sha256'],binary_bytes=value['artifact']['bytes'],script_sha256=value['script_sha256'])
save(folder / 'intent.json',dict(payload=payload,manifest_path=str(path.absolute()),access_root=str(root)))
print(json.dumps(dict(case_id=ident,prepare_once=True)),flush=True)
prepared=rpc(prefix,dict(payload,action='prepare'),folder);save(folder / 'prepared-remote.json',prepared);require(prepared == dict(id=ident,prepared=True),'new private remote case')
save(folder / 'upload-intent.json',dict(binary_sha256=payload['binary_sha256'],upload_once=True))
args=['sftp']+[p for p in prefix[1:-1] if p != '-T']+['-b','-',prefix[-1]]
destination='/C:/ProgramData/RustDiscoveryBootstrap/clock-spnego-descriptor-trace-'+ident+'/ClockSpnegoDescriptorTrace.exe'
run_transport(args,('put '+json.dumps(value['artifact']['path'])+' '+json.dumps(destination)+'\n').encode(),folder)
save(folder / 'upload-completed.json',dict(uploaded=True));print(json.dumps(dict(case_id=ident,prepared=True,service_started=False)));return
require(str(uuid.UUID(ident)) == ident,'canonical original case')
folder=STATE / ident;private(folder,True);original=load(folder / 'intent.json');payload=original['payload']
require(original['manifest_path'] == str(path.absolute()) and original['access_root'] == str(root)
and payload['manifest_sha256'] == sha(path) and payload['script_sha256'] == value['script_sha256'],'original same-case input tuple')
if action in ('start','cleanup'):
if action == 'start':require(load(folder / 'upload-completed.json')['uploaded'] is True,'completed original upload')
save(folder / (action+'-intent.json'),dict(action=action,once=True))
result=rpc(prefix,dict(payload,action=action),folder);save(folder / ('readback-'+str(uuid.uuid4())+'.json'),result)
if action == 'cleanup':
require(result.get('id') == ident and result.get('cleanup_verified') is True,'own stopped service cleanup only')
print(json.dumps(dict(case_id=ident,cleanup_verified=True)));return
if result.get('status'):
require(action == 'readback' and result['status'] == 'remote_case_absent','read-only original partial status')
print(json.dumps(dict(case_id=ident,complete=False,retry_allowed=False)));return
complete=check_report(result,ident)
if result.get("report") is not None:require(result["report"]["auth"]["identity_control"]==value["identity_control"],"actual compiled identity control matches original manifest")
if complete:
destination=folder / 'diagnostic-result.json'
receipt=dict(complete=True,manifest_sha256=sha(path),actual=result,clock_qualified=False,product_gate_open=False)
if destination.exists():require(load(destination)['actual']['report'] == result['report'],'preserve original numeric report')
else:save(destination,receipt)
print(json.dumps(dict(case_id=ident,diagnostic_complete=complete,rpc_code=result['report']['rpc_code'] if complete else None,
stage=result['report']['trace']['stage'] if complete else None,eeinfo_count=result['report']['eeinfo']['count'] if complete else None,clock_qualified=False,cleanup_separate=True)))
def main():
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('prepare','start','readback','cleanup'))
parser.add_argument('--manifest',type=Path,required=True);parser.add_argument('--access-root',type=Path,required=True);parser.add_argument('--id')
parser.add_argument('--execute-native',action='store_true');args=parser.parse_args()
require((args.action == 'readback' and not args.execute_native) or (args.action != 'readback' and args.execute_native),'explicit native effect phase; readback only reads')
execute(args.action,args.manifest,args.access_root,args.id)
if __name__ == '__main__':
try:main()
except Exception as error:
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_NATIVE_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
raise SystemExit(2) from None