#!/usr/bin/env python3 """Apply adaptive region unlock to the agy CLI on macOS (arm64 Mach-O) and Linux (x86-64 / arm64 ELF64, PIE & non-PIE). Patches authclient.ValidateAndOnboardAccount to bypass the ineligibleTiers "free-tier" check while leaving allowedTiers untouched. Usage: agy-patch.py [--binary PATH] [--dry-run] [--in-place] agy-patch.py [--binary PATH] --locate # print symbols, change nothing Without --in-place the result is written to PATH.patched (and ad-hoc signed on macOS). """ import argparse import hashlib import os import shutil import struct import subprocess import sys PCLNTAB_MAGICS = (0xFFFFFFF1, 0xFFFFFFF0) FREE_TIE = bytes.fromhex("667265652d746965") FUNC_REGION = "authclient.ValidateAndOnboardAccount" class PatchError(Exception): pass def sxt(val, bits): mask = 1 << (bits - 1) return (val & (mask - 1)) - (val & mask) def read_cstr(data, off, limit=2048): if off < 0 or off >= len(data): return "" end = data.find(b"\x00", off, min(len(data), off + limit)) if end < 0: end = min(len(data), off + limit) return data[off:end].decode("utf-8", "replace") def parse_container(data): if len(data) < 64: raise PatchError("file too small to be a valid executable") if data[:4] == b"\xcf\xfa\xed\xfe": return _macho_container(data) if data[:4] == b"\x7fELF": return _elf_container(data) if data[:2] == b"MZ": return _pe_container(data) raise PatchError("unrecognised container (want Mach-O, ELF64 or PE)") def _macho_container(data): ncmds = struct.unpack_from(" len(data): break cmd, size = struct.unpack_from(" len(data): break if cmd == 0x19: # LC_SEGMENT_64 nsects = struct.unpack_from(" off + size: break if data[sect:sect + 16].rstrip(b"\0") == b"__text": addr, sz, offset = struct.unpack_from("= shnum: raise PatchError("ELF missing valid section headers") shstr_entry = shoff + shstrndx * shentsize if shstr_entry + 64 > len(data): raise PatchError("ELF shstrtab entry out of bounds") strtab_off = struct.unpack_from(" len(data): break nameoff, _stype, _sflags, addr, offset, size = struct.unpack_from(" 0 and sa > 0 and so <= file_off < so + ss: return sa + (file_off - so) return file_off return container["text_va"] - container["text_off"] + file_off def _resolve_elf_go_text_base(data, container, pcln_off, text_start): tva = container["text_va"] toff = container["text_off"] tsz = container["text_size"] if tva <= text_start < tva + tsz: return text_start, toff + (text_start - tva), "pclntab_header" rela_sec = container["rela_dyn"] if rela_sec and rela_sec[3] >= 24: pcln_va = file_off_to_va(container, pcln_off) _, _, roff, rsz = rela_sec rend = min(len(data), roff + rsz) needle = struct.pack(" len(data): continue nfunc = struct.unpack_from(" len(data) or fnm >= len(data): continue if container["format"] == "ELF64": go_tva, go_toff, source = _resolve_elf_go_text_base(data, container, base, text_start) else: go_tva, go_toff, source = container["text_va"], container["text_off"], "text_section_start" found = {} for k in range(nfunc): eo, fo = struct.unpack_from(" len(data): continue no = struct.unpack_from("= len(data): continue nm = read_cstr(data, fnm + no, 512) if nm == FUNC_REGION or nm.endswith("/" + FUNC_REGION) or nm.endswith("." + FUNC_REGION): neo = struct.unpack_from(" fn_s and b[p - 1] in (0x48, 0x49)): clen = 3 else: continue jo = p + clen if jo in seen or jo + 2 > fn_e: continue b0, b1 = b[jo], b[jo + 1] if b0 == 0x0F and b1 == 0x85 and jo + 6 <= fn_e: r32 = struct.unpack_from(" fn_e: continue tt, te = None, None if b[nq] == 0x0F and b[nq + 1] == 0x85 and nq + 6 <= fn_e: tt = nq + 6 + struct.unpack_from(" tail_end + 0x10 for d in exits) seen.add(jo) cands.append((jo, st, pbytes, has_body, is_brk)) inel = [c for c in cands if c[3]] alw = [c for c in cands if c[4] and not c[3]] if len(inel) != 1 or any(c[1] != "unpatched" for c in alw): return None return inel[0][0], inel[0][1], inel[0][2] def analyze_region_a64(b, fn_s, fn_e): def dec_bne(w, o): return o + sxt((w >> 5) & 0x7FFFF, 19) * 4 if (w & 0xFF00001F) == 0x54000001 else None def dec_cb(w, o): return o + sxt((w >> 5) & 0x7FFFF, 19) * 4 if (w & 0xFF000010) == 0x54000000 else None def dec_b(w, o): return o + sxt(w & 0x03FFFFFF, 26) * 4 if (w & 0xFC000000) == 0x14000000 else None cands = [] for p in range(fn_s, fn_e - 20, 4): w0, w1 = struct.unpack_from("= te: exits.append(d) if not exits: continue has_body = any(d > te for d in exits) is_brk = all(d == te for d in exits) cands.append((p + 4, st, t_off, has_body, is_brk)) inel = [c for c in cands if c[3]] alw = [c for c in cands if c[4] and not c[3]] if len(inel) != 1 or any(c[1] != "unpatched" for c in alw): return None bo, st, t_off = inel[0][0], inel[0][1], inel[0][2] off26 = (t_off - bo) // 4 pbytes = struct.pack(" va={funcs[FUNC_REGION]['fn_va']:#x} off={funcs[FUNC_REGION]['fn_start']:#x}") else: print(f"[i] {FUNC_REGION} -> MISSING") if args.locate: return ok, msg = patch_region(data, funcs, arch) print(("[*] " if ok else "[=] ") + msg) after = hashlib.sha256(bytes(data)).hexdigest() if args.dry_run: if before == after: print("[=] dry run: nothing to change") else: print(f"[*] dry run: {before[:12]} -> {after[:12]}") return dst = src_real if args.in_place else src + ".patched" if before != after: assert_not_official_antigravity(dst) tmp_dst = f"{dst}.hakimi_tmp_{os.getpid()}" try: with open(tmp_dst, "wb") as fh: fh.write(data) os.chmod(tmp_dst, 0o755) os.replace(tmp_dst, dst) resign(dst, container["format"]) finally: if os.path.exists(tmp_dst): try: os.unlink(tmp_dst) except OSError: pass print(f"[*] wrote {dst}\n sha256 {before[:12]} -> {after[:12]}") else: print("[=] binary already up to date") if __name__ == "__main__": try: main() except PatchError as exc: sys.exit(f"error: {exc}")