244 lines
16 KiB
Python
244 lines
16 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}')
|
|
|
|
# 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 = r'''$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
|
if((($p.PSObject.Properties.Name|Sort-Object)-join',')-cne'agent_sha256,helper_sha256,id'-or[guid]::ParseExact($p.id,'D').ToString()-cne$p.id-or$p.agent_sha256-cnotmatch'^[0-9a-f]{64}$'-or$p.helper_sha256-cnotmatch'^[0-9a-f]{64}$'){throw 'Fixed public sample read'}
|
|
$state='C:\ProgramData\RustDiscovery\state';$agent='RmmDiscoveryWindows';$helper='RmmDiscoverySystemHelper'
|
|
$agentSid=(New-Object Security.Principal.NTAccount('NT SERVICE',$agent)).Translate([Security.Principal.SecurityIdentifier]).Value
|
|
$allowed=@('S-1-5-18','S-1-5-32-544',$agentSid)
|
|
function Check($path,[bool]$directory){
|
|
$i=Get-Item -LiteralPath $path -Force
|
|
if($i.PSIsContainer-ne$directory-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Regular public report path'}
|
|
$ancestor=$i.Directory;if($directory){$ancestor=$i.Parent};while($null-ne$ancestor){if($ancestor.Attributes-band[IO.FileAttributes]::ReparsePoint){throw 'Public report reparse ancestry'};$ancestor=$ancestor.Parent}
|
|
$acl=Get-Acl -LiteralPath $path;$owner=$acl.GetOwner([Security.Principal.SecurityIdentifier]).Value
|
|
if($owner-cnotin$allowed-and!(!$directory-and$owner-ceq'S-1-5-19')){throw 'Public report owner'}
|
|
$rules=@($acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Public report empty DACL'}
|
|
foreach($r in $rules){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-cnotin$allowed){throw 'Public report broad DACL'}}
|
|
if($directory-and!$acl.AreAccessRulesProtected){throw 'Protected public report directory'}
|
|
return @{owner_sid=$owner;sddl=$acl.Sddl;dacl_checked=$true}
|
|
}
|
|
Add-Type -TypeDefinition @'
|
|
using System; using System.Text; using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles;
|
|
public static class RmmPassiveTimeRead {
|
|
[StructLayout(LayoutKind.Sequential)] public struct Info {public uint Attr,CL,CH,AL,AH,WL,WH,Volume,SizeH,SizeL,Links,IndexH,IndexL;}
|
|
[DllImport("kernel32.dll",SetLastError=true)] public static extern bool GetFileInformationByHandle(SafeFileHandle h,out Info i);
|
|
[DllImport("kernel32.dll",SetLastError=true)] static extern IntPtr OpenProcess(uint access,bool inherit,int pid);
|
|
[DllImport("kernel32.dll",SetLastError=true,CharSet=CharSet.Unicode)] static extern bool QueryFullProcessImageName(IntPtr h,uint flags,StringBuilder text,ref uint size);
|
|
[DllImport("kernel32.dll",SetLastError=true)] static extern bool GetProcessTimes(IntPtr h,out long creation,out long exit,out long kernel,out long user);
|
|
[DllImport("kernel32.dll",SetLastError=true)] static extern bool GetExitCodeProcess(IntPtr h,out uint code);
|
|
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h);
|
|
public static string[] Process(int pid){IntPtr h=OpenProcess(0x1000,false,pid);if(h==IntPtr.Zero)throw new InvalidOperationException("QUERY_PROCESS");try{long c,e,k,u;uint code,size=32768;StringBuilder image=new StringBuilder((int)size);if(!GetProcessTimes(h,out c,out e,out k,out u)||!QueryFullProcessImageName(h,0,image,ref size)||!GetExitCodeProcess(h,out code)||code!=259)throw new InvalidOperationException("LIVE_PROCESS");return new string[]{c.ToString(System.Globalization.CultureInfo.InvariantCulture),image.ToString()};}finally{CloseHandle(h);}}
|
|
}
|
|
'@
|
|
function Hash([byte[]]$bytes){$h=[Security.Cryptography.SHA256]::Create();try{return([BitConverter]::ToString($h.ComputeHash($bytes))).Replace('-','').ToLowerInvariant()}finally{$h.Dispose()}}
|
|
function Runtime($role){
|
|
$name=$agent;$image='C:\Program Files\RustDiscovery\rmm-agent-windows.exe';$owner='NT AUTHORITY\LocalService';$mode='Auto';$sha=$p.agent_sha256
|
|
if($role-ceq'helper'){$name=$helper;$image='C:\Program Files\RustDiscovery\rmm-helper-windows.exe';$owner='LocalSystem';$mode='Manual';$sha=$p.helper_sha256}
|
|
$s=Get-CimInstance Win32_Service -Filter ("Name='"+$name+"'") -OperationTimeoutSec 5
|
|
if(!$s-or$s.State-cne'Running'-or$s.StartName-cne$owner-or$s.StartMode-cne$mode-or$s.ProcessId-le0-or$s.ServiceType-cne'Own Process'){throw 'Current fixed service role'}
|
|
$expected='"'+$image+'" --service';if($role-ceq'agent'){$expected+=' --config "C:\ProgramData\RustDiscovery\config.json"'}
|
|
if($s.PathName-cne$expected){throw 'Current fixed service command'}
|
|
$native=[RmmPassiveTimeRead]::Process([int]$s.ProcessId)
|
|
if($native[1]-cne$image-or(Get-FileHash -LiteralPath $image -Algorithm SHA256).Hash.ToLowerInvariant()-cne$sha){throw 'Current fixed service image'}
|
|
return [ordered]@{pid=[uint32]$s.ProcessId;birth_filetime=$native[0];image=$native[1];sha256=$sha;start_name=$s.StartName;start_mode=$s.StartMode;command=$s.PathName}
|
|
}
|
|
function Boot{return (Get-CimInstance Win32_OperatingSystem -Property LastBootUpTime -OperationTimeoutSec 5).LastBootUpTime.ToUniversalTime().Ticks.ToString([Globalization.CultureInfo]::InvariantCulture)}
|
|
$null=Check 'C:\ProgramData\RustDiscovery' $true;$directory=Check $state $true
|
|
$boot=Boot;$before=[ordered]@{agent=(Runtime 'agent');helper=(Runtime 'helper')};$reports=[ordered]@{}
|
|
foreach($name in @('agent-time-sample.json','helper-time-sample.json')){
|
|
$path=Join-Path $state $name
|
|
if(!(Test-Path -LiteralPath $path)){$reports[$name]=$null;continue}
|
|
$stream=[IO.File]::Open($path,[IO.FileMode]::Open,[IO.FileAccess]::Read,[IO.FileShare]::Read)
|
|
try{
|
|
$meta=Check $path $false;$info=New-Object RmmPassiveTimeRead+Info
|
|
if(![RmmPassiveTimeRead]::GetFileInformationByHandle($stream.SafeFileHandle,[ref]$info)-or$info.Links-ne1-or($info.Attr-band0x410)-ne0-or$info.SizeH-ne0-or$info.SizeL-lt1-or$info.SizeL-gt8192-or$stream.Length-ne$info.SizeL){throw 'Single-link bounded public report'}
|
|
$raw=New-Object byte[] $info.SizeL;$offset=0;while($offset-lt$raw.Length){$count=$stream.Read($raw,$offset,$raw.Length-$offset);if($count-le0){throw 'Complete held public report'};$offset+=$count}
|
|
$after=New-Object RmmPassiveTimeRead+Info
|
|
if(![RmmPassiveTimeRead]::GetFileInformationByHandle($stream.SafeFileHandle,[ref]$after)-or$after.IndexH-ne$info.IndexH-or$after.IndexL-ne$info.IndexL-or$after.WH-ne$info.WH-or$after.WL-ne$info.WL-or$after.SizeL-ne$info.SizeL-or$after.Links-ne1){throw 'Stable held public report'}
|
|
$again=Check $path $false;if($again.sddl-cne$meta.sddl-or$again.owner_sid-cne$meta.owner_sid){throw 'Stable public report security'}
|
|
$meta['volume']=$info.Volume;$meta['index_high']=$info.IndexH;$meta['index_low']=$info.IndexL;$meta['last_write_filetime']=(([uint64]$info.WH-shl32)-bor$info.WL).ToString([Globalization.CultureInfo]::InvariantCulture)
|
|
$reports[$name]=@{raw_base64=[Convert]::ToBase64String($raw);sha256=(Hash $raw);metadata=$meta}
|
|
}finally{$stream.Dispose()}
|
|
}
|
|
$after=[ordered]@{agent=(Runtime 'agent');helper=(Runtime 'helper')}
|
|
$directoryAfter=Check $state $true
|
|
if(($before|ConvertTo-Json -Depth 8 -Compress)-cne($after|ConvertTo-Json -Depth 8 -Compress)-or(Boot)-cne$boot-or$directory.sddl-cne$directoryAfter.sddl){throw 'Stable process, directory and boot during public read'}
|
|
@{id=$p.id;read_only=$true;boot_ticks=$boot;runtime=$after;state_metadata=$directory;reports=$reports;observed_at=[DateTime]::UtcNow.ToString('o')}|ConvertTo-Json -Depth 12 -Compress
|
|
'''
|
|
|
|
|
|
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 and value.endswith('Z'), 'UTC_TIMESTAMP_REQUIRED')
|
|
result = dt.datetime.fromisoformat(value[:-1] + '+00:00')
|
|
return result
|
|
|
|
|
|
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 dt.datetime(1601, 1, 1, tzinfo=dt.timezone.utc) + dt.timedelta(microseconds=int(value) // 10)
|
|
|
|
|
|
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 - observed_now).total_seconds()) <= 10, '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 + dt.timedelta(seconds=2) and birth <= observed, 'SAMPLE_CURRENT_INSTALL_WINDOW')
|
|
need(0 <= (now - modified).total_seconds() <= 180 and 0 <= (now - observed).total_seconds() <= 180, 'SAMPLE_FILE_AGE')
|
|
need(-2 <= (modified - observed).total_seconds() <= 90, '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 <= (dt.datetime.now(dt.timezone.utc) - since).total_seconds() <= 600, '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)
|