113 lines
6.9 KiB
Python
113 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Read the two historical Windows sync decisions and verify their original signatures."""
|
|
import hashlib
|
|
import json
|
|
import subprocess
|
|
|
|
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
|
|
|
|
import cloud_mvp_acceptance as cloud
|
|
|
|
BOOT = 'a9ad0895-5a96-8f2c-8fb6-cfdc2d5225ea'
|
|
BINDING = '9239ee9f4142735867e77451478bf309fbf449d730a0ede6c958bc8018830dde'
|
|
HISTORY = (
|
|
(6755, '1418b4a3-8370-404d-b29a-920ba9f0f4d7', 'b6a3101f-68ad-41c8-be4e-4c7c2bc963d6', True, 'CONSISTENT'),
|
|
(6756, '9eba037c-825f-4a96-be14-cca130c1e83d', '0633a4de-7e52-4e5d-a2a1-59fb134817fe', False, 'HELPER_CHECKPOINT_REQUIRED'),
|
|
)
|
|
|
|
|
|
def read_server(_payload):
|
|
# This function is transported by the existing pinned SSH bootstrap adapter.
|
|
import subprocess
|
|
|
|
query = """
|
|
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY;
|
|
SET LOCAL statement_timeout='5s';
|
|
WITH device AS (
|
|
SELECT * FROM agent_installations WHERE tenant_id='11111111-1111-4111-8111-111111111111'
|
|
AND agent_id='63ea15b2-0643-403d-b5b4-cba1288dbea3'
|
|
), history AS (
|
|
SELECT c.* FROM sync_checkpoints c JOIN device USING (tenant_id,agent_id)
|
|
), target_jobs AS (
|
|
SELECT j.* FROM jobs j JOIN device USING (tenant_id,agent_id)
|
|
)
|
|
SELECT json_build_object(
|
|
'observed_at', now(),
|
|
'device', (SELECT json_build_object('asset_id',asset_id,'agent_id',agent_id,'tenant_id',tenant_id,
|
|
'enrollment_generation',enrollment_generation,'assignment_version',assignment_version,
|
|
'connection_generation',connection_generation,'boot_id',connection_boot_id,
|
|
'certificate_sha256',certificate_sha256,'capabilities',capabilities,'sync_required',sync_required,
|
|
'restore_blocked',restore_blocked,'sequence_regressed',sequence_regressed,
|
|
'sync_recovery_required',sync_recovery_required,'revoked',revoked_at IS NOT NULL)
|
|
FROM device),
|
|
'head', (SELECT row_to_json(s) FROM agent_sync_state s JOIN device d
|
|
ON s.tenant_id=d.tenant_id AND s.agent_id=d.agent_id),
|
|
'history_count', (SELECT count(*) FROM history),
|
|
'history', (SELECT json_agg(row_to_json(r) ORDER BY r.checkpoint) FROM
|
|
(SELECT request_id, decision_id, request_hash, checkpoint, journal_id, connection_generation,
|
|
encode(payload,'hex') AS payload, encode(signature,'hex') AS signature
|
|
FROM history WHERE checkpoint IN (6755,6756) AND octet_length(payload) <= 32768
|
|
AND octet_length(signature)=64 ORDER BY checkpoint LIMIT 3) r),
|
|
'jobs', (SELECT count(*) FROM target_jobs),
|
|
'outbox', (SELECT count(*) FROM outbox JOIN target_jobs USING (tenant_id,job_id,attempt_id)),
|
|
'permits', (SELECT count(*) FROM execution_permits JOIN target_jobs USING (tenant_id,job_id,attempt_id))
|
|
); COMMIT;
|
|
"""
|
|
result = subprocess.run(['docker', 'exec', '-i', 'rust-discovery-server-postgres-1', 'psql',
|
|
'-X', '-qAt', '-v', 'ON_ERROR_STOP=1', '-U', 'postgres', '-d', 'rmm'],
|
|
input=query.encode(), capture_output=True, check=True, timeout=10)
|
|
return json.loads(result.stdout)
|
|
|
|
|
|
def verify(report, binding):
|
|
require = cloud.require
|
|
binding_digest = hashlib.sha256(json.dumps(binding, sort_keys=True, separators=(',', ':')).encode()).hexdigest()
|
|
require(binding_digest == BINDING, 'existing Windows verification binding changed')
|
|
key = bytes.fromhex(binding['public_key_hex'])
|
|
public = Ed25519PublicKey.from_public_bytes(key)
|
|
key_id = hashlib.sha256(key).hexdigest()
|
|
device = report['device']
|
|
identity = {k: binding[k] for k in ('tenant_id', 'agent_id', 'asset_id', 'enrollment_generation', 'assignment_version')}
|
|
require(all(device[k] == v for k, v in identity.items()) and device['boot_id'] == BOOT
|
|
and device['connection_generation'] > 150 and device['revoked'] is False
|
|
and device['restore_blocked'] is True and device['sequence_regressed'] is False
|
|
and device['sync_recovery_required'] is False and device['sync_required'] is False
|
|
and sorted(device['capabilities']) == ['agent.update/1', 'heartbeat/1', 'windows.base/1'],
|
|
'expected passive device and active restore block')
|
|
require(device['certificate_sha256'] == 'c55aa8214cb8d60382fe453bddd56c6e46fa79b61ccefda502e001be56bb5075',
|
|
'device certificate changed')
|
|
require(report['history_count'] == 2 and len(report['history']) == 2 and
|
|
all(report[k] == 0 for k in ('jobs', 'outbox', 'permits')), 'history or execution artifacts changed')
|
|
head = report['head']
|
|
require(head['tenant_id'] == identity['tenant_id'] and head['agent_id'] == identity['agent_id']
|
|
and head['decision_id'] == HISTORY[0][2] and head['checkpoint'] == HISTORY[0][0]
|
|
and head['helper_checkpoint'] == 1 and head['helper_journal_id'], 'server head changed')
|
|
evidence = []
|
|
for row, (checkpoint, request_id, decision_id, ready, reason) in zip(report['history'], HISTORY, strict=True):
|
|
raw, signature = bytes.fromhex(row['payload']), bytes.fromhex(row['signature'])
|
|
public.verify(signature, raw)
|
|
d = json.loads(raw)
|
|
require(d['domain'] == 'rmm.reconciliation-decision' and d['schema_version'] == 1
|
|
and all(d[k] == v for k, v in identity.items()) and d['key_id'] == key_id
|
|
and d['boot_id'] == BOOT and d['connection_generation'] == row['connection_generation'] == 150
|
|
and d['checkpoint'] == row['checkpoint'] == checkpoint and d['helper_checkpoint'] == 1
|
|
and d['journal_id'] == row['journal_id'] == head['journal_id']
|
|
and d['helper_journal_id'] == head['helper_journal_id']
|
|
and d['request_id'] == row['request_id'] == request_id and d['request_hash'] == row['request_hash']
|
|
and d['decision_id'] == row['decision_id'] == decision_id and d['reason'] == reason
|
|
and d['ready'] is ready and d['helper_ready'] is ready and d['attempts'] == [],
|
|
'signed decision differs from stored Windows case')
|
|
evidence.append(dict(checkpoint=checkpoint, request_id=request_id, decision_id=decision_id,
|
|
request_sha256=d['request_hash'], payload_sha256=hashlib.sha256(raw).hexdigest(),
|
|
signature_sha256=hashlib.sha256(signature).hexdigest(), issued_at=d['issued_at'],
|
|
reason=reason, original_signature_verified=True))
|
|
return {**{k: report[k] for k in ('observed_at', 'device', 'head', 'history_count', 'jobs', 'outbox', 'permits')},
|
|
'verification_key_id': key_id, 'decisions': evidence, 'read_only': True}
|
|
|
|
|
|
if __name__ == '__main__':
|
|
binding_path = cloud.ROOT / 'devstate/windows-client/system-helper-public-binding.json'
|
|
binding = json.loads(binding_path.read_bytes())
|
|
result = verify(cloud.bootstrap.rpc('server.conf', read_server, {}), binding)
|
|
print(json.dumps(result, sort_keys=True, indent=2))
|