mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
125 lines
4.1 KiB
Python
125 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Recover bounded selected original LSF signature descriptors, CPU-only.
|
|
Payloads are artifact evidence, not authenticated/ready firmware objects.
|
|
"""
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import zlib
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT / 'tools') not in sys.path:
|
|
sys.path.insert(0, str(ROOT / 'tools'))
|
|
|
|
from _elf64_reader import (
|
|
EXPECTED_RM_OBJECT_SHA256,
|
|
Elf64Relocatable,
|
|
find_rm_object,
|
|
verify_rm_object_sha256,
|
|
)
|
|
|
|
DESCRIPTORS = [0x01148CD8, 0x01148E10, 0x01149E90, 0x01149410, 0x0114A568]
|
|
|
|
|
|
def main() -> None:
|
|
obj_path = find_rm_object()
|
|
sha = verify_rm_object_sha256(obj_path, EXPECTED_RM_OBJECT_SHA256)
|
|
out = ROOT / 'analysis/lsf-580'
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
|
|
elf = Elf64Relocatable.from_path(obj_path)
|
|
syms = elf.symbols()
|
|
bases = {
|
|
elf.section_index('.rodata'): 0x0111D660,
|
|
elf.section_index('.data'): 0x00F7E890,
|
|
elf.section_index('.bss'): 0x05FD2180,
|
|
elf.section_index('.text'): 0x0616F810,
|
|
}
|
|
raw = {
|
|
idx: elf.section_data(idx)
|
|
for idx in bases
|
|
if elf.section_name(idx) != '.bss'
|
|
}
|
|
rel = {}
|
|
for idx in raw:
|
|
rela_name = '.rela' + elf.section_name(idx)
|
|
if elf.has_section(rela_name):
|
|
for r_offset, r_sym, _r_type, r_addend in elf.relocations(rela_name):
|
|
rel[(idx, r_offset)] = (r_sym, r_addend)
|
|
|
|
def where(address: int) -> tuple[int, int]:
|
|
for idx, base in bases.items():
|
|
if idx in raw and base <= address < base + len(raw[idx]):
|
|
return idx, address - base
|
|
raise ValueError(hex(address))
|
|
|
|
def data(address: int, length: int) -> bytes:
|
|
idx, off = where(address)
|
|
if length < 0 or off + length > len(raw[idx]):
|
|
raise SystemExit(f'slice 0x{address:x}+{length} exceeds section bounds')
|
|
return raw[idx][off:off + length]
|
|
|
|
def ptr(address: int) -> int:
|
|
idx, off = where(address)
|
|
entry = rel.get((idx, off))
|
|
if entry is None:
|
|
return int.from_bytes(raw[idx][off:off + 8], 'little')
|
|
r_sym, r_addend = entry
|
|
sym = syms[r_sym]
|
|
return bases[sym['st_shndx']] + sym['st_value'] + r_addend
|
|
|
|
records = []
|
|
for address in DESCRIPTORS:
|
|
header = data(address, 24)
|
|
size, stored, _, flags, last = struct.unpack('<IIQII', header)
|
|
storage = ptr(address + 8)
|
|
compressed = data(storage, stored if flags & 1 else size)
|
|
if flags & 1:
|
|
decoder = zlib.decompressobj(-15)
|
|
decoded = decoder.decompress(compressed) + decoder.flush()
|
|
if not decoder.eof:
|
|
raise SystemExit(f'incomplete DEFLATE stream at 0x{address:x}')
|
|
else:
|
|
decoded = compressed[:size]
|
|
if len(decoded) != size:
|
|
raise SystemExit(f'decoded size mismatch at 0x{address:x}: {len(decoded)} != {size}')
|
|
name = f'{address:x}'
|
|
(out / (name + '.packed')).write_bytes(compressed)
|
|
(out / (name + '.decoded')).write_bytes(decoded)
|
|
records.append({
|
|
'descriptor': hex(address),
|
|
'decoded_size': size,
|
|
'stored_bytes': stored,
|
|
'flags': flags,
|
|
'word20': last,
|
|
'storage': hex(storage),
|
|
'decoded_sha256': hashlib.sha256(decoded).hexdigest(),
|
|
'packed_file': str(out / (name + '.packed')),
|
|
'decoded_file': str(out / (name + '.decoded')),
|
|
'head': decoded[:96].hex(),
|
|
})
|
|
|
|
report_path = ROOT / 'reports/gpu-lsf-signature-descriptors.json'
|
|
report_path.parent.mkdir(parents=True, exist_ok=True)
|
|
report_path.write_text(
|
|
json.dumps(
|
|
{
|
|
'artifact_sha256': sha,
|
|
'records': records,
|
|
'runtime_id_assignment_proven': False,
|
|
'physical_gpu_access': False,
|
|
},
|
|
indent=2,
|
|
)
|
|
+ '\n',
|
|
encoding='utf-8',
|
|
)
|
|
for row in records:
|
|
print(row)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|