Files
nvdriver/tools/gpu_extract_acr_packed.py

51 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Recover bounded selected original ACR packed bundle storage, CPU-only.
Payloads are artifact evidence, not authenticated/ready firmware objects.
"""
from pathlib import Path
import struct,json,hashlib,zlib
from elftools.elf.elffile import ELFFile
R=Path(__file__).resolve().parents[1];P=Path('/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary');OUT=R/'analysis/acr-580';OUT.mkdir(parents=True,exist_ok=True)
with P.open('rb')as f:
e=ELFFile(f);ss=list(e.get_section_by_name('.symtab').iter_symbols());bases={e.get_section_index('.rodata'):0x0111d660,e.get_section_index('.data'):0x00f7e890,e.get_section_index('.bss'):0x05fd2180,e.get_section_index('.text'):0x0616f810};raw={i:e.get_section(i).data()for i in bases if e.get_section(i).name!='.bss'};rel={}
for i in raw:
section=e.get_section_by_name('.rela'+e.get_section(i).name)
if section:
for x in section.iter_relocations():rel[(i,x['r_offset'])]=x
def where(address):
for i,b in bases.items():
if i in raw and b<=address<b+len(raw[i]):return i,address-b
raise ValueError(hex(address))
def data(address,length):i,o=where(address);assert o+length<=len(raw[i]);return raw[i][o:o+length]
def ptr(address):
i,o=where(address);x=rel.get((i,o))
if x is None:return int.from_bytes(raw[i][o:o+8],'little')
s=ss[x['r_info_sym']];return bases[s['st_shndx']]+s['st_value']+x['r_addend']
objects={}
for s in ss:
if s['st_shndx']in bases and s['st_info']['type']=='STT_OBJECT' and s['st_size']>0:objects.setdefault(bases[s['st_shndx']]+s['st_value'],[]).append((s.name,s['st_size']))
records=[]
for bundle in [0x011430c8,0x01141350,0x01141058,0x01141ea0,0x01140830,0x011407a0]:
count=int.from_bytes(data(bundle,4),'little');assert count<128
for index in range(count):
tag=int.from_bytes(data(bundle+8+index*16,4),'little');address=ptr(bundle+16+index*16)
header=data(address,24);size,unknown,_,flags,_=struct.unpack('<IIQII',header);storage=ptr(address+8);candidates=objects.get(storage,[])
row={'bundle':hex(bundle),'tag':hex(tag),'descriptor':hex(address),'decoded_size':size,'word4':unknown,'flags':flags,'storage':hex(storage),'storage_symbols':candidates}
if not candidates:
candidates=[(name,base,length)for base,items in objects.items()for name,length in items if base<=storage<base+length]
row['enclosing_storage_symbols']=candidates
# Descriptor word4 is the stored byte count; section bounds checked by data().
stored_size=unknown if flags&1 else size
assert 0<stored_size<16*1024*1024,row
compressed=data(storage,stored_size)
if flags&1:
decoder=zlib.decompressobj(-15);decoded=decoder.decompress(compressed)+decoder.flush();assert decoder.eof
consumed=len(compressed)-len(decoder.unused_data)
else:decoded=compressed[:size];consumed=size
assert len(decoded)==size,(row,len(decoded))
name=f'{bundle:x}-{tag:x}';(OUT/(name+'.packed')).write_bytes(compressed);(OUT/(name+'.decoded')).write_bytes(decoded)
row.update(stored_bytes=len(compressed),consumed=consumed,decoded_sha256=hashlib.sha256(decoded).hexdigest(),storage_sha256=hashlib.sha256(compressed).hexdigest(),packed_file=str(OUT/(name+'.packed')),decoded_file=str(OUT/(name+'.decoded')),head=decoded[:32].hex())
records.append(row)
(R/'reports/gpu-acr-packed-inventory.json').write_text(json.dumps({'artifact_sha256':hashlib.sha256(P.read_bytes()).hexdigest(),'decoder_reference':'Python zlib raw DEFLATE; not original decoder execution','records':records,'physical_gpu_access':False},indent=2)+'\n')
print(json.dumps({'records':len(records),'decoded_bytes':sum(x['decoded_size']for x in records)}))