import zipfile, re, io, sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
APK = r'E:\ios\vf_apk\com.vinfast.companion.app.apk'
z = zipfile.ZipFile(APK)
names = z.namelist()

# 1) X-HASH-2 ở đâu?
print('== X-HASH-2 / x-hash-2 theo dex ==')
for name in names:
    if name.endswith('.dex') or name == 'resources.arsc':
        data = z.read(name)
        for pat, tag in [(b'X-HASH-2', 'X-HASH-2'), (b'x-hash-2', 'x-hash-2'), (b'X-Hash-2', 'X-Hash-2'),
                         (b'CAPP', 'CAPP'), (b'ccarusermgnt', 'ccarusermgnt')]:
            if pat in data:
                i = data.find(pat)
                seg = data[max(0, i-110):i+150]
                seg = re.sub(rb'[^\x20-\x7e]', b'.', seg).decode()
                print(f'  [{tag}] {name} @{i}: ...{seg}...')

# 2) Danh sách lib kiểu native
print('\n== LIB .so (nơi có thể giấu secret) ==')
for n in names:
    if n.endswith('.so'):
        print(f'   {n:60s} {z.getinfo(n).file_size}')

# 3) Scan .so tìm chuỗi giống secret / vinfast
print('\n== Chuỗi đáng chú ý trong .so (poly 8-64 ký tự, có "@" hoặc "vinfast"/"ccar") ==')
pat = re.compile(rb'[ -~]{8,64}')
for name in names:
    if name.endswith('.so'):
        data = z.read(name)
        strs = set(m.group(0).decode() for m in pat.finditer(data))
        interesting = [s for s in strs if re.search(r'vinfast|ccar|CAPP|@(20\d\d|[A-Za-z]{4,10})', s, re.I) and 'unity' not in s.lower()]
        for s in sorted(interesting)[:25]:
            print(f'   {name}: {s!r}')
