67 lines
4.6 KiB
Python
67 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract rows actually submitted by the opt-in PE hotfix capture boundary.
|
|
|
|
This inventories transmission; it does not approve omission or equate a raw codec
|
|
registry with the bytes sent. Compare these hashes with freshly pinned DB2 rows.
|
|
"""
|
|
import argparse,hashlib,json,struct
|
|
from pathlib import Path
|
|
MAX_PACKET=1048576
|
|
STATUSES={0:'not_set',1:'valid',2:'removed',3:'invalid',4:'not_public'}
|
|
def decode(body):
|
|
if not 8<=len(body)<=MAX_PACKET:raise ValueError('Hotfix packet size')
|
|
count=struct.unpack_from('<I',body)[0];end=4+21*count
|
|
if end+4>len(body):raise ValueError('Truncated hotfix headers')
|
|
size=struct.unpack_from('<I',body,end)[0]
|
|
if end+4+size!=len(body):raise ValueError('Hotfix payload extent')
|
|
at=end+4;rows=[];seen=set()
|
|
for i in range(count):
|
|
push,unique,table,ident,length,bits=struct.unpack_from('<iIIiIB',body,4+21*i)
|
|
status=bits>>5
|
|
if bits&31 or status not in STATUSES:raise ValueError('Hotfix status bits')
|
|
if (push,unique) in seen:raise ValueError('Duplicate push cache identity')
|
|
seen.add((push,unique))
|
|
if at+length>len(body) or (status!=1 and length):raise ValueError('Hotfix row payload extent/status')
|
|
payload=body[at:at+length];at+=length
|
|
rows.append({'push_id':push,'unique_id':unique,'table_hash':f'0x{table:08x}','id':ident,'status':STATUSES[status],'bytes':length,'payload_sha256':hashlib.sha256(payload).hexdigest()})
|
|
if at!=len(body):raise ValueError('Unclaimed hotfix payload bytes')
|
|
return rows
|
|
|
|
def decode_db_reply(body):
|
|
if not 17<=len(body)<=MAX_PACKET:raise ValueError('DBReply packet size')
|
|
table,ident,timestamp,bits,size=struct.unpack_from('<IIIBI',body)
|
|
status=bits>>5
|
|
if bits&31 or status not in STATUSES:raise ValueError('DBReply status bits')
|
|
if size!=len(body)-17 or (status!=1 and size):raise ValueError('DBReply payload extent/status')
|
|
return [{'table_hash':f'0x{table:08x}','id':ident,'timestamp':timestamp,'status':STATUSES[status],'bytes':size,'payload_sha256':hashlib.sha256(body[17:]).hexdigest()}]
|
|
|
|
def inventory(capture,output):
|
|
capture=capture.resolve();output=output.resolve()
|
|
if output==capture or output in capture.parents:raise ValueError('Output must not contain the capture input')
|
|
before=capture.stat()
|
|
if not 0<before.st_size<=1073741824:raise ValueError('Bounded nonempty capture required')
|
|
digest=hashlib.sha256();rows=[];packets=0;opcode_counts={}
|
|
with capture.open('rb') as stream:
|
|
while line:=stream.readline(2*MAX_PACKET+4097):
|
|
if len(line)>2*MAX_PACKET+4096:raise ValueError('Capture line bound')
|
|
digest.update(line);event=json.loads(line)
|
|
if event.get('opcode') not in (0x4b0003,0x4b0000):continue
|
|
if event.get('direction')!='native_in' or event.get('capture_stage')!='pe-after-engine-submit' or event.get('scope')!='authenticated-world-transport':raise ValueError('Hotfix was not captured at the authenticated submit boundary')
|
|
if event.get('body_omitted') or not isinstance(event.get('body_hex'),str):raise ValueError('Hotfix body missing')
|
|
body=bytes.fromhex(event['body_hex'])
|
|
if len(body)!=event['bytes']:raise ValueError('Capture size mismatch')
|
|
packets+=1;key=f"0x{event['opcode']:06x}";opcode_counts[key]=opcode_counts.get(key,0)+1
|
|
for row in (decode_db_reply(body) if event['opcode']==0x4b0000 else decode(body)):
|
|
if len(rows)>=1000000:raise ValueError('Sent row inventory bound')
|
|
rows.append({'capture_sequence':event['sequence'],'opcode':event['opcode'],**row})
|
|
after=capture.stat()
|
|
if (before.st_size,before.st_mtime_ns)!=(after.st_size,after.st_mtime_ns):raise ValueError('Capture changed during read')
|
|
if not packets:raise ValueError('No submitted hotfix packets; no transmission proof')
|
|
output.mkdir(parents=True,exist_ok=True)
|
|
with (output/'sent-hotfix-rows.jsonl').open('w') as stream:
|
|
for row in rows:stream.write(json.dumps(row,separators=(',',':'))+'\n')
|
|
report={'capture_sha256':digest.hexdigest(),'submitted_packets':packets,'submitted_by_opcode':opcode_counts,'sent_row_occurrences':len(rows),'unique_rows':len({(r['table_hash'],r['id']) for r in rows}),'scope':'Actual engine-submit bytes; not DB2 parity, omission approval, or proof that the client accepted every row'}
|
|
(output/'sent-hotfix-summary.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report))
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser(description=__doc__);p.add_argument('capture',type=Path);p.add_argument('output',type=Path);a=p.parse_args();inventory(a.capture,a.output)
|