#!/usr/bin/env python3 """Pre-flight check for an Android APK before installing or pushing it as an update. Usage: verify_update_apk.py [--ip 91.99.165.207] [--port 443] [--admin 61f04dd637a74ee3] [--cert-sha256 b8ad4ced...] [--sdk ] Checks: * size <= 40MB (server-side SendApkUpdate limit) * APK/ZIP, and lib/*/libyama.so stored (not deflated) so the server can patch it * each ABI's CONNECT_ADDRESS: IP / port / superAdmin match expectations -> 「生成客户端」 checks "encrypt IP" by default, so szServerIP/szPort are ciphertext; this script decrypts with StreamCipher(0x12345678), the same algorithm the client uses in ServerIP()/ServerPort(). * dex contains UpdateInstaller (the build can accept COMMAND_UPDATE later) * with --sdk: prints package / versionCode / native-code Locating the struct: first by the szFlag plaintext ("Hello, World!" in the pristine template), then by the master id string (BuildDlg rewrites szFlag to it), then by the superAdmin needle (base = hit - 228), then by the plaintext IP string. Exit code 0 = all checks passed, 1 = something failed. """ import glob import os import re import struct import subprocess import sys import zipfile import zlib MARKERS = [b"Hello, World!\x00"] OFF_IP, LEN_IP = 32, 100 OFF_PORT, LEN_PORT = 132, 8 OFF_TYPE = 140 OFF_ENCRYPT = 144 OFF_ADMIN = 228 CLIENT_TYPE_ANDROID = 8 MAX_SIZE = 40 * 1024 * 1024 def arg(name, default=None): if name in sys.argv: return sys.argv[sys.argv.index(name) + 1] return default def stream_cipher(data, key=0x12345678): """Same as common/ip_enc.h StreamCipher::process (xorshift32 + xor).""" state = key out = bytearray() for b in data: state ^= (state << 13) & 0xFFFFFFFF state ^= state >> 17 state ^= (state << 5) & 0xFFFFFFFF state &= 0xFFFFFFFF out.append(b ^ ((state & 0xFF) ^ ((state >> 8) & 0xFF))) return bytes(out) def main(): if len(sys.argv) < 2: print(__doc__) return 1 apk = sys.argv[1] want_ip = arg("--ip") want_port = arg("--port") want_admin = arg("--admin") want_cert = (arg("--cert-sha256") or "").replace(":", "").lower() sdk = arg("--sdk") fails = [] def check(ok, msg): print((" OK " if ok else " FAIL ") + msg) if not ok: fails.append(msg) size = os.path.getsize(apk) print("apk: %s (%d bytes)" % (apk, size)) check(size <= MAX_SIZE, "size %d <= 40MB" % size) with open(apk, "rb") as f: head = f.read(4) check(head == b"PK\x03\x04", "ZIP magic") z = zipfile.ZipFile(apk) infos = z.infolist() def read_entry(name): """z.read() that tolerates a stale CRC-32. BuildDlg patches libyama.so in place without recomputing the ZIP CRC, so every generated APK fails zipfile's CRC check even though the payload is fine. Read the raw stored bytes instead. """ try: return z.read(name), True except zipfile.BadZipFile: info = z.getinfo(name) with open(apk, "rb") as f: f.seek(info.header_offset) local = f.read(30) if len(local) < 30 or local[:4] != b"PK\x03\x04": raise nlen, elen = struct.unpack_from(" len(data): return False itype = struct.unpack_from("= 0 and valid_struct(base): return base start = p + 1 return -1 for i in sos: check(i.compress_type == 0, "%s stored (not deflated)" % i.filename) data, crc_ok = read_entry(i.filename) if not crc_ok: print(" WARN %s: ZIP CRC is stale (patched in place, as BuildDlg does) - " "harmless for install, but the APK must be re-signed" % i.filename) pos = find_struct(data) abi = i.filename.split("/")[1] if pos < 0: check(False, "%s: CONNECT_ADDRESS not found" % i.filename) continue ip, port = decrypted(pos) ip_s, port_s = ip.decode("latin1"), port.decode("latin1") enc = data[pos + OFF_ENCRYPT] admin = struct.unpack_from("