Skip to content

Instantly share code, notes, and snippets.

@LukeZGD
Forked from pwnerblu/main.c
Last active November 14, 2026 13:57
Show Gist options
  • Select an option

  • Save LukeZGD/d7c529aa5ef6e665cfbba5053ea7dc5b to your computer and use it in GitHub Desktop.

Select an option

Save LukeZGD/d7c529aa5ef6e665cfbba5053ea7dc5b to your computer and use it in GitHub Desktop.
fork of mineek's restored patcher: this fork is for seprmvr64v2 and unsigned cryptex install patch
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
// the following few functions are from patchfinder64
#define GET_OFFSET(len, x) (x - (uintptr_t) restored_external)
typedef unsigned long long addr_t;
static uint32_t arm64_branch_instruction(uintptr_t from, uintptr_t to) {
return from > to ? 0x18000000 - (from - to) / 4 : 0x14000000 + (to - from) / 4;
}
static addr_t
xref64(const uint8_t *buf, addr_t start, addr_t end, addr_t what)
{
addr_t i;
uint64_t value[32];
memset(value, 0, sizeof(value));
end &= ~3;
for (i = start & ~3; i < end; i += 4) {
uint32_t op = *(uint32_t *)(buf + i);
unsigned reg = op & 0x1F;
if ((op & 0x9F000000) == 0x90000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADRP X%d, 0x%llx\n", i, reg, ((long long)adr << 1) + (i & ~0xFFF));
value[reg] = ((long long)adr << 1) + (i & ~0xFFF);
continue; // XXX should not XREF on its own?
/*} else if ((op & 0xFFE0FFE0) == 0xAA0003E0) {
unsigned rd = op & 0x1F;
unsigned rm = (op >> 16) & 0x1F;
//printf("%llx: MOV X%d, X%d\n", i, rd, rm);
value[rd] = value[rm];*/
} else if ((op & 0xFF000000) == 0x91000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned shift = (op >> 22) & 3;
unsigned imm = (op >> 10) & 0xFFF;
if (shift == 1) {
imm <<= 12;
} else {
//assert(shift == 0);
if (shift > 1) continue;
}
//printf("%llx: ADD X%d, X%d, 0x%x\n", i, reg, rn, imm);
value[reg] = value[rn] + imm;
} else if ((op & 0xF9C00000) == 0xF9400000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: LDR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[reg] = value[rn] + imm; // XXX address, not actual value
/*} else if ((op & 0xF9C00000) == 0xF9000000) {
unsigned rn = (op >> 5) & 0x1F;
unsigned imm = ((op >> 10) & 0xFFF) << 3;
//printf("%llx: STR X%d, [X%d, 0x%x]\n", i, reg, rn, imm);
if (!imm) continue; // XXX not counted as true xref
value[rn] = value[rn] + imm; // XXX address, not actual value*/
} else if ((op & 0x9F000000) == 0x10000000) {
signed adr = ((op & 0x60000000) >> 18) | ((op & 0xFFFFE0) << 8);
//printf("%llx: ADR X%d, 0x%llx\n", i, reg, ((long long)adr >> 11) + i);
value[reg] = ((long long)adr >> 11) + i;
} else if ((op & 0xFF000000) == 0x58000000) {
unsigned adr = (op & 0xFFFFE0) >> 3;
//printf("%llx: LDR X%d, =0x%llx\n", i, reg, adr + i);
value[reg] = adr + i; // XXX address, not actual value
}
if (value[reg] == what) {
return i;
}
}
return 0;
}
static addr_t
bof64(const uint8_t *buf, addr_t start, addr_t where)
{
for (; where >= start; where -= 4) {
uint32_t op = *(uint32_t *)(buf + where);
if ((op & 0xFFC003FF) == 0x910003FD) {
unsigned delta = (op >> 10) & 0xFFF;
//printf("%x: ADD X29, SP, #0x%x\n", where, delta);
if ((delta & 0xF) == 0) {
addr_t prev = where - ((delta >> 4) + 1) * 4;
uint32_t au = *(uint32_t *)(buf + prev);
if ((au & 0xFFC003E0) == 0xA98003E0) {
//printf("%x: STP x, y, [SP,#-imm]!\n", prev);
return prev;
}
// try something else
while (where > start) {
where -= 4;
au = *(uint32_t *)(buf + where);
// SUB SP, SP, #imm
if ((au & 0xFFC003FF) == 0xD10003FF && ((au >> 10) & 0xFFF) == delta + 0x10) {
return where;
}
// STP x, y, [SP,#imm]
if ((au & 0xFFC003E0) != 0xA90003E0) {
where += 4;
break;
}
}
}
}
}
return 0;
}
uint32_t* find_next_insn(uint32_t* from, uint32_t num, uint32_t insn, uint32_t mask)
{
while(num)
{
// printf("from: %p, num: %d, insn: %x, mask: %x\n", from, num, insn, mask);
if((*from & mask) == (insn & mask))
{
return from;
}
from++;
num--;
}
return NULL;
}
uint32_t* find_prev_insn(uint32_t* from, uint32_t num, uint32_t insn, uint32_t mask)
{
while(num)
{
if((*from & mask) == (insn & mask))
{
return from;
}
from--;
num--;
}
return NULL;
}
// modified for seprmvr64 v2
int get_aks_kb_patch_restored(void *restored_external, size_t len) {
printf("getting %s()\n", __FUNCTION__);
// First patch: create_system_key_bag
void *str_ref = memmem(restored_external, len, "create_system_key_bag", strlen("create_system_key_bag"));
if (!str_ref) {
printf("[-] Failed to find string create_system_key_bag\n");
return -1;
}
addr_t xref = xref64(restored_external, 0, len, GET_OFFSET(len, (uintptr_t)str_ref));
if (!xref) {
printf("[-] Failed to find xref to create_system_key_bag\n");
return -1;
}
// printf("[+] Found xref to refFrame at 0x%llx\n", xref);
// go to the next ret
uint32_t *insn = find_next_insn((uint32_t *)(restored_external + xref), 0x300, 0xD65F03C0, 0xFFFFFFFF);
if (!insn) {
printf("[-] Failed to find next ret\n");
return -1;
}
// printf("[+] Found next ret at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
insn = find_prev_insn(insn, 0x100, 0xAA000000, 0xFF000000);
if (!insn) {
printf("[-] Failed to find prev mov\n");
return -1;
}
// printf("[+] Found prev mov at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
printf("[+] Patching create_system_key_bag at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
*insn = 0xD2800000; // mov x0, #0
printf("[+] Patched create_system_key_bag\n");
// Second patch: content-protect property not found
str_ref = memmem(restored_external, len, "content-protect property not found\n", strlen("content-protect property not found\n"));
if (!str_ref) {
printf("[-] Failed to find string content-protect property not found\n");
return -1;
}
xref = xref64(restored_external, 0, len, GET_OFFSET(len, (uintptr_t)str_ref));
if (!xref) {
printf("[-] Failed to find xref to content-protect property not found\n");
return -1;
}
// Find function start
addr_t func = bof64(restored_external, 0, xref);
if (!func) {
printf("[-] Failed to find function start for content-protect\n");
return -1;
}
insn = (uint32_t *)(restored_external + func);
uint32_t *end = (uint32_t *)(restored_external + xref + 0x100);
// Find and patch CBZ to unconditional branch
for (; insn < end; insn++) {
uint32_t op = *insn;
// CBZ / CBNZ
if ((op & 0x7F000000) == 0x34000000) {
uintptr_t pc = (uintptr_t)insn;
// Extract imm19 from CBZ
int32_t imm19 = (op >> 5) & 0x7FFFF;
if (imm19 & 0x40000) { // sign-extend
imm19 |= ~0x7FFFF;
}
// Original CBZ branch target
uintptr_t target = pc + ((uintptr_t)imm19 << 2);
// Convert to B (imm26)
int64_t imm26 = ((int64_t)target - (int64_t)pc) >> 2;
if (imm26 < -(1 << 25) || imm26 >= (1 << 25)) {
printf("[-] Branch target out of range\n");
return -1;
}
uint32_t b_insn = 0x14000000 | (imm26 & 0x03FFFFFF);
printf("[+] Found CBZ at 0x%lx\n",
pc - (uintptr_t)restored_external);
printf("[+] Patching content-protect property CBZ to unconditional B -> 0x%lx\n",
target - (uintptr_t)restored_external);
*insn = b_insn;
printf("[+] Patched content-protect property not found\n");
return 0;
}
}
printf("[-] Failed to find CBZ in content-protect function\n");
return -1;
}
// modified for seprmvr64 v2
int get_fdr_patch(void *restored_external, size_t len) {
printf("getting %s()\n", __FUNCTION__);
void *str_ref = memmem(restored_external, len, "RestoredFDRRecover", strlen("RestoredFDRRecover"));
if (!str_ref) {
printf("[-] Failed to find string RestoredFDRRecover\n");
return -1;
}
addr_t xref = xref64(restored_external, 0, len, GET_OFFSET(len, (uintptr_t)str_ref));
if (!xref) {
printf("[-] Failed to find xref to RestoredFDRRecover\n");
return -1;
}
// printf("[+] Found xref to refFrame at 0x%llx\n", xref);
// go to the next ret
uint32_t *insn = find_next_insn((uint32_t *)(restored_external + xref), 0x300, 0xD65F03C0, 0xFFFFFFFF);
if (!insn) {
printf("[-] Failed to find next ret\n");
return -1;
}
// printf("[+] Found next ret at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
insn = find_prev_insn(insn, 0x100, 0xAA000000, 0xFF000000);
if (!insn) {
printf("[-] Failed to find prev mov\n");
return -1;
}
// printf("[+] Found prev mov at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
printf("[+] Patching RestoredFDRRecover at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
*insn = 0xD2800000; // mov x0, #0
printf("[+] Patched RestoredFDRRecover\n");
return 0;
}
// modified for seprmvr64 v2
int get_sep_patch(void *restored_external, size_t len) {
printf("getting %s()\n", __FUNCTION__);
void *str_ref = memmem(restored_external, len, "AppleSEPManager", strlen("AppleSEPManager"));
if (!str_ref) {
printf("[-] Failed to find string AppleSEPManager\n");
return -1;
}
addr_t xref = xref64(restored_external, 0, len, GET_OFFSET(len, (uintptr_t)str_ref));
if (!xref) {
printf("[-] Failed to find xref to AppleSEPManager\n");
return -1;
}
// printf("[+] Found xref to refFrame at 0x%llx\n", xref);
// go to the next ret
uint32_t *insn = find_next_insn((uint32_t *)(restored_external + xref), 0x300, 0xD65F03C0, 0xFFFFFFFF);
if (!insn) {
printf("[-] Failed to find next ret\n");
return -1;
}
// printf("[+] Found next ret at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
insn = find_prev_insn(insn, 0x100, 0xAA000000, 0xFF000000);
if (!insn) {
printf("[-] Failed to find prev mov\n");
return -1;
}
// printf("[+] Found prev mov at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
printf("[+] Patching _ramrod_device_has_sep at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
*insn = 0xD2800000; // mov x0, #0
printf("[+] Patched _ramrod_device_has_sep\n");
return 0;
}
// cryptex1 install validation patch
int get_cryptex_install_patch(void *restored_external, size_t len) {
printf("getting %s()\n", __FUNCTION__);
void *str_ref = memmem(restored_external, len, "_get_and_save_large_file_internal", strlen("_get_and_save_large_file_internal"));
if (!str_ref) {
printf("[-] Failed to find string _get_and_save_large_file_internal\n");
return -1;
}
addr_t xref = xref64(restored_external, 0, len, GET_OFFSET(len, (uintptr_t)str_ref));
if (!xref) {
printf("[-] Failed to find xref to _get_and_save_large_file_internal\n");
return -1;
}
// printf("[+] Found xref to refFrame at 0x%llx\n", xref);
// go to the next ret
uint32_t *insn = find_next_insn((uint32_t *)(restored_external + xref), 0x300, 0xD65F03C0, 0xFFFFFFFF);
if (!insn) {
printf("[-] Failed to find next ret\n");
return -1;
}
// printf("[+] Found next ret at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
insn = find_prev_insn(insn, 0x100, 0xAA000000, 0xFF000000);
if (!insn) {
printf("[-] Failed to find prev mov\n");
return -1;
}
// printf("[+] Found prev mov at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
printf("[+] Patching _get_and_save_large_file_internal patch point at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
*insn = 0xD2800000; // mov x0, #0
printf("[+] Patched _get_and_save_large_file_internal\n");
return 0;
}
// -[MCMFileManager createDirectoryAtURL:withIntermediateDirectories:mode:class:error:]
int get_containermanagerd_patch(void *restored_external, size_t len) {
printf("getting %s()\n", __FUNCTION__);
void *str_ref = memmem(restored_external, len, "-[MCMFileManager createDirectoryAtURL:withIntermediateDirectories:mode:class:error:]", strlen("-[MCMFileManager createDirectoryAtURL:withIntermediateDirectories:mode:class:error:]"));
if (!str_ref) {
printf("[-] Failed to find string -[MCMFileManager createDirectoryAtURL:withIntermediateDirectories:mode:class:error:]\n");
return -1;
}
addr_t xref = xref64(restored_external, 0, len, GET_OFFSET(len, (uintptr_t)str_ref));
if (!xref) {
printf("[-] Failed to find xref to -[MCMFileManager createDirectoryAtURL:withIntermediateDirectories:mode:class:error:]\n");
return -1;
}
// printf("[+] Found xref to refFrame at 0x%llx\n", xref);
// go to the next ret
uint32_t *insn = find_next_insn((uint32_t *)(restored_external + xref), 0x300, 0xD65F03C0, 0xFFFFFFFF);
if (!insn) {
printf("[-] Failed to find next ret\n");
return -1;
}
// printf("[+] Found next ret at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
insn = find_prev_insn(insn, 0x100, 0xAA000000, 0xFF000000);
if (!insn) {
printf("[-] Failed to find prev mov\n");
return -1;
}
// printf("[+] Found prev mov at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
printf("[+] Patching -[MCMFileManager createDirectoryAtURL:withIntermediateDirectories:mode:class:error:] patch point at 0x%lx\n", (uintptr_t)insn - (uintptr_t)restored_external);
*insn = 0xD2800000; // mov x0, #0
printf("[+] Patched -[MCMFileManager createDirectoryAtURL:withIntermediateDirectories:mode:class:error:]\n");
return 0;
}
/* ================= BASEBAND PATCH ================= */
int get_baseband_patch(void *restored_external,
size_t len)
{
printf("getting %s()\n", __FUNCTION__);
const char *bb_str = "%s: querying baseband info";
void *str_ref = memmem(restored_external, len,
bb_str, strlen(bb_str));
if (!str_ref) {
printf("[-] Failed to find string querying baseband info\n");
return -1;
}
addr_t xref = xref64(restored_external, 0, len,
GET_OFFSET(len, (uintptr_t)str_ref));
if (!xref) {
printf("[-] Failed to find xref\n");
return -1;
}
addr_t func = bof64(restored_external, 0, xref);
if (!func) {
printf("[-] Failed to find function start\n");
return -1;
}
uint32_t *insn = (uint32_t *)(restored_external + func);
uint32_t *end = (uint32_t *)(restored_external + xref + 0x100);
for (; insn < end; insn++) {
uint32_t op = *insn;
// CBZ / CBNZ
if ((op & 0x7F000000) == 0x34000000) {
uintptr_t pc = (uintptr_t)insn;
// Extract imm19 from CBZ
int32_t imm19 = (op >> 5) & 0x7FFFF;
if (imm19 & 0x40000) { // sign-extend
imm19 |= ~0x7FFFF;
}
// Original CBZ branch target
uintptr_t target = pc + ((uintptr_t)imm19 << 2);
// Convert to B (imm26)
int64_t imm26 = ((int64_t)target - (int64_t)pc) >> 2;
if (imm26 < -(1 << 25) || imm26 >= (1 << 25)) {
printf("[-] Branch target out of range\n");
return -1;
}
uint32_t b_insn = 0x14000000 | (imm26 & 0x03FFFFFF);
printf("[+] Found CBZ at 0x%lx\n",
pc - (uintptr_t)restored_external);
printf("[+] Patching cbz in _ramrod_device_has_baseband to unconditional B -> 0x%lx\n",
target - (uintptr_t)restored_external);
*insn = b_insn;
printf("[+] Patched _ramrod_device_has_baseband\n");
return 0;
}
}
printf("[-] Failed to find CBZ\n");
return -1;
}
int main(int argc, char* argv[]) {
printf("restored patcher by @mineekdev\n");
printf("modified by @pwnerblu for seprmvr64v2\n");
int only_fdr = 0;
int only_cryptex = 0;
int only_systemKB = 0;
int only_container = 0;
if (argc < 3 || argc > 4) {
printf("usage: %s <restored_external> <restored_external.patched> [-b|-c|-kb|-ct]\n", argv[0]);
return -1;
}
if (argc == 4) {
if (strcmp(argv[3], "-b") == 0) {
only_fdr = 1;
} else if (strcmp(argv[3], "-c") == 0) {
only_cryptex = 1;
} else if (strcmp(argv[3], "-kb") == 0) {
only_systemKB = 1;
} else if (strcmp(argv[3], "-ct") == 0) {
only_container = 1;
} else {
printf("[-] Unknown argument: %s\n", argv[3]);
return -1;
}
}
char *in = argv[1];
char *out = argv[2];
void *restored_external;
size_t len;
FILE* fp = fopen(in, "rb");
if (!fp) {
printf("failed to open %s, does it exist?\n", in);
return -1;
}
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
restored_external = malloc(len);
if (!restored_external) {
printf("malloc failed\n");
fclose(fp);
return -1;
}
fread(restored_external, 1, len, fp);
fclose(fp);
// add option for patching RestoredFDRRecover and baseband only
// add option for splat patch
if (only_cryptex) {
get_cryptex_install_patch(restored_external, len);
} else if (only_fdr) {
get_fdr_patch(restored_external, len);
get_baseband_patch(restored_external, len);
} else if (only_systemKB) {
get_aks_kb_patch_restored(restored_external, len);
} else if (only_container) {
get_containermanagerd_patch(restored_external, len);
} else {
get_sep_patch(restored_external, len);
get_fdr_patch(restored_external, len);
get_baseband_patch(restored_external, len);
}
printf("Patched file to %s\n", out);
fp = fopen(out, "wb+");
fwrite(restored_external, 1, len, fp);
fflush(fp);
fclose(fp);
free(restored_external);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment