"""Prepare the fixed RMM passive telemetry observation repair media.""" import argparse import hashlib import json import os from pathlib import Path from tempfile import TemporaryDirectory parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--previous-media', type=Path, required=True) parser.add_argument('--agent', type=Path, required=True) parser.add_argument('--build-receipt', type=Path, required=True) parser.add_argument('--build-receipt-sha256', required=True) parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() previous = args.previous_media base = args.output.parent case = 'c62d698d-d044-4815-acbe-3eeb383d44d5' previous_case = '1d61d88b-ab17-49b1-9a8d-2427348313b4' old_agent = '78c16dcaee53fe9a04d4d84c7a1475330c5a1af30c104f8f9d6f56b5b7408513' new_agent = '742257bfb86ff3ccb5b7c04371471afbdf3c26c5d5cf73ea7bf6d0a51586e80f' def sha(data): return hashlib.sha256(data).hexdigest() def once(data, old, new): assert data.count(old) == 1, old return data.replace(old, new) def bound(path, digest): data = path.read_bytes() assert sha(data) == digest return data old_sums = bound(previous/'SHA256SUMS','122dce40acfba6aa1bd17d01d395fee85d2779110e0f2a79cd0ce8e34e1789de') files = {name:bound(previous/name,digest) for digest,name in [row.split(' ') for row in old_sums.decode().splitlines()] if name != 'rmm-agent-windows.exe'} binary, receipt_file, expected_receipt = args.agent, args.build_receipt, args.build_receipt_sha256 files['rmm-agent-windows.exe'] = bound(Path(binary), new_agent) files['agent-build.json'] = bound(Path(receipt_file), expected_receipt) build = json.loads(files['agent-build.json']) manifest = json.loads(files['passive-build.json']) manifest.update(case_id=case, agent_sha256=new_agent, agent_build_sha256=expected_receipt) for name,field in [('cargo-inputs.json','cargo_inputs_sha256'),('cross-crate-inputs.json','cross_crate_inputs_sha256')]: proof=Path(receipt_file).with_name(name) digest=build.get(field) if not digest and field=='cross_crate_inputs_sha256': digest=manifest[field] assert isinstance(digest,str) and len(digest)==64 files[name]=bound(proof if proof.is_file() else previous/name,digest) manifest[field]=digest files['passive-build.json']=json.dumps(manifest,sort_keys=True,separators=(',',':')).encode() ps = files['Set-PassiveJobPair.ps1'].decode() ps = once(ps,"$previousCase = $work",f"$id = '{previous_case}'\n $previousCase = Join-Path $boot \"job-passive-pair-$id\"") ps = once(ps,"$previousSample = Join-Path $previousCase 'samples-after-restart-a1959583-44f4-47db-aff4-f0229468a15b.json'",'''$samples = @(Get-ChildItem -LiteralPath $previousCase -File | Where-Object { $_.Name -cin @('samples-passive-92911196-1bda-4ba0-83ad-11427e24a902.json','samples-after-restart-92911196-1bda-4ba0-83ad-11427e24a902.json') }) if ($samples.Count -ne 1) { throw 'Expected the known revision 3 sample' } $previousSample = $samples[0].FullName''') ps = once(ps,"34fe01eb8413c37f7e5269e1465d764bf730aa46b1a6cc371f113c7f9d285c9a","454ddfee4477b9c19f70dea593196c108687ac5db59e8cd04150faf0c2bc62aa") ps = once(ps," $id = '"+previous_case+"'\n if ($build.case_id"," $id = '"+case+"'\n if ($build.case_id") ps = once(ps," $oldHashes = $newHashes",''' $pub = Get-Content -LiteralPath $previousPublication -Raw | ConvertFrom-Json $rolePath = Join-Path $previousCase 'profile-role-agent.json' $installPath = Join-Path $previousCase 'installed.json' CheckControl $rolePath $false CheckControl $installPath $false if ($pub.id -cne $sample.pair_case_id -or $pub.successor_revision -ne 3 -or $pub.configuration.sha256 -cne $sample.profile_sha256 -or $pub.role_proofs.agent -cne (Hash $rolePath)) { throw 'Previous profile role binding' } $roleProof=Get-Content -LiteralPath $rolePath -Raw | ConvertFrom-Json if ($roleProof.pair_case_id -cne $pub.id -or $roleProof.pair_result_sha256 -cne (Hash $installPath)) { throw 'Previous role-to-pair binding' } $installedPair=Get-Content -LiteralPath $installPath -Raw | ConvertFrom-Json if ($installedPair.id -cne $pub.id -or $installedPair.job_mode -ne $false -or $installedPair.files.agent.sha256 -cne 'OLD_AGENT' -or $installedPair.helper.image_file.sha256 -cne $helperHash) { throw 'Unknown previous pair' } $oldHashes=[ordered]@{agent=$installedPair.files.agent.sha256;settings=$installedPair.files.settings.sha256;pin=$installedPair.files.pin.sha256}'''.replace('OLD_AGENT',old_agent)) ps=once(ps," $previousRevision = 2"," $previousRevision = 3") ps=once(ps,"7c331501e40c57597624ed889ea12f34a9b16d8d10f41a2a5a73d8b7d01280a1",sha(files['passive-build.json'])) prior_pair=sha(files['Set-PassiveJobPair.ps1']) files['Set-PassiveJobPair.ps1']=ps.encode() for name in ['Publish-JobPairTimeProfile.ps1','Read-JobPairTimeSamples.ps1']: src=once(files[name].decode(),prior_pair,sha(files['Set-PassiveJobPair.ps1'])) if name.startswith('Read'): src=once(src,previous_case,case) src=once(src,' $revision = 3',' $revision = 4') files[name]=src.encode() files['SHA256SUMS']=''.join(f'{sha(data)} {name}\n' for name,data in sorted(files.items())).encode() out=args.output with TemporaryDirectory(dir=base) as tmp: stage=Path(tmp)/'media' stage.mkdir(mode=0o700) for name,data in files.items(): (stage/name).write_bytes(data) assert not out.exists() os.rename(stage,out) print(out,sha(files['SHA256SUMS']),sha(files['passive-build.json']))