Files
palera1n/src/main.c
T

516 lines
15 KiB
C
Raw Normal View History

2023-01-16 16:17:41 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <pthread.h>
#include <inttypes.h>
2023-01-17 13:46:42 +08:00
#include <getopt.h>
#include <errno.h>
2023-01-22 20:47:52 +08:00
#include <spawn.h>
2023-01-18 17:58:20 +08:00
#include <sys/mman.h>
2023-01-24 03:32:29 +08:00
#include <time.h>
2023-01-16 16:17:41 +08:00
2023-01-17 19:26:37 +08:00
#include <libimobiledevice/libimobiledevice.h>
2023-01-25 20:16:19 +08:00
#if defined(__APPLE__)
#include <mach-o/loader.h>
#include <mach-o/ldsyms.h>
#else
#define MH_MAGIC_64 0xfeedfacf
#define MH_CIGAM_64 0xcffaedfe
#define MH_KEXT_BUNDLE 0xb
typedef int cpu_type_t;
typedef int cpu_subtype_t;
#define CPU_ARCH_ABI64 0x01000000
#define CPU_TYPE_ARM ((cpu_type_t) 12)
#define CPU_TYPE_ARM64 (CPU_TYPE_ARM | CPU_ARCH_ABI64)
struct mach_header_64
{
uint32_t magic; /* mach magic number identifier */
cpu_type_t cputype; /* cpu specifier */
cpu_subtype_t cpusubtype; /* machine specifier */
uint32_t filetype; /* type of file */
uint32_t ncmds; /* number of load commands */
uint32_t sizeofcmds; /* the size of all the load commands */
uint32_t flags; /* flags */
uint32_t reserved; /* reserved */
};
#endif
2023-01-16 16:17:41 +08:00
#include "ANSI-color-codes.h"
#include "common.h"
2023-01-17 13:46:42 +08:00
#include "checkra1n.h"
2023-01-18 16:02:43 +08:00
#include "kerninfo.h"
2023-01-16 16:17:41 +08:00
2023-01-25 20:16:19 +08:00
#define CMD_LEN_MAX 512
#define OVERRIDE_MAGIC 0xd803b376
2023-01-25 20:16:31 +08:00
unsigned int verbose = 0;
2023-01-22 17:38:47 +08:00
int enable_rootful = 0;
2023-01-17 19:26:37 +08:00
int do_pongo_sleep = 0;
int demote = 0;
2023-01-22 20:47:52 +08:00
bool ohio = true;
2023-01-22 23:42:22 +08:00
char xargs_cmd[0x270] = "xargs wdt=-1";
2023-01-18 16:02:43 +08:00
char checkrain_flags_cmd[0x20] = "checkra1n_flags 0x0";
2023-01-22 23:20:46 +08:00
char palerain_flags_cmd[0x20] = "palera1n_flags 0x0";
char kpf_flags_cmd[0x20] = "kpf_flags 0x0";
2023-01-22 17:38:47 +08:00
char dtpatch_cmd[0x20] = "dtpatch md0";
char rootfs_cmd[512];
2023-01-22 20:47:52 +08:00
extern char** environ;
2023-01-17 13:46:42 +08:00
2023-01-25 20:16:19 +08:00
typedef unsigned char niarelap_file_t[];
2023-01-23 01:24:22 +08:00
niarelap_file_t* kpf_to_upload_1 = &checkra1n_kpf_pongo;
niarelap_file_t* ramdisk_to_upload_1 = &ramdisk_dmg;
2023-01-22 14:02:20 -05:00
niarelap_file_t* overlay_to_upload_1 = &binpack_dmg;
2023-01-23 01:24:22 +08:00
niarelap_file_t** kpf_to_upload = &kpf_to_upload_1;
niarelap_file_t** ramdisk_to_upload = &ramdisk_to_upload_1;
niarelap_file_t** overlay_to_upload = &overlay_to_upload_1;
2023-01-18 17:58:20 +08:00
override_file_t override_ramdisk;
override_file_t override_kpf;
override_file_t override_overlay;
2023-01-18 16:02:43 +08:00
uint32_t checkrain_flags = 0;
2023-01-22 17:38:47 +08:00
uint32_t palerain_flags = 0;
2023-01-22 23:20:46 +08:00
uint32_t kpf_flags = 0;
2023-01-18 16:02:43 +08:00
2023-01-25 20:16:31 +08:00
int p1_log(log_level_t loglevel, const char *fname, int lineno, const char *fxname, char *__restrict format, ...)
{
int ret = 0;
char type[0x10];
char colour[0x10];
char colour_bold[0x10];
va_list args;
va_start(args, format);
if (verbose < (loglevel - 3)) return 0;
switch (loglevel) {
case LOG_FATAL:
snprintf(type, 0x10, "%s", "!");
snprintf(colour, 0x10, "%s", RED);
snprintf(colour_bold, 0x10, "%s", BRED);
break;
case LOG_ERROR:
snprintf(type, 0x10, "%s", "Error");
snprintf(colour, 0x10, "%s", RED);
snprintf(colour_bold, 0x10, "%s", BRED);
break;
case LOG_WARNING:
snprintf(type, 0x10, "%s", "Warning");
snprintf(colour, 0x10, "%s", YEL);
snprintf(colour_bold, 0x10, "%s", BYEL);
break;
case LOG_INFO:
snprintf(type, 0x10, "%s", "Info");
snprintf(colour, 0x10, "%s", CYN);
snprintf(colour_bold, 0x10, "%s", BCYN);
break;
default:
assert(loglevel >= 0);
snprintf(type, 0x10, "%s", "Verbose");
snprintf(colour, 0x10, "%s", WHT);
snprintf(colour_bold, 0x10, "%s", BWHT);
break;
}
char timestring[0x80];
time_t curtime;
time(&curtime);
struct tm* timeinfo = localtime(&curtime);
snprintf(timestring, 0x80, "%s[%s%02d/%02d/%d %02d:%02d:%02d%s]", CRESET, HBLK, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_year - 100, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, CRESET);
if (verbose >= 2) {
printf("%s| - %s%s <%s> " CRESET "%s" HBLU "%s" CRESET ":" BLU "%d" CRESET ":" BMAG "%s()" CRESET ": \n%s| ----> ", colour_bold, timestring, colour_bold, type, WHT, fname, lineno, fxname, colour_bold);
} else {
printf(" - %s %s<%s>%s: ", timestring, colour_bold, type, CRESET);
}
printf("%s", colour);
ret = vprintf(format, args);
va_end(args);
if (verbose < 2)
printf(CRESET "\n");
else {
printf("\n%s-%s\n", colour, CRESET);
}
fflush(stdout);
return ret;
}
2023-01-16 16:17:41 +08:00
2023-01-25 20:16:19 +08:00
int found_pongo = 0;
2023-01-25 20:16:31 +08:00
void *pongo_usb_callback(void *arg)
{
2023-01-25 20:16:19 +08:00
if (found_pongo)
return NULL;
found_pongo = 1;
LOG(LOG_INFO, "Found PongoOS USB Device");
usb_device_handle_t handle = *(usb_device_handle_t *)arg;
issue_pongo_command(handle, NULL);
issue_pongo_command(handle, "fuse lock");
issue_pongo_command(handle, "sep auto");
upload_pongo_file(handle, **kpf_to_upload, checkra1n_kpf_pongo_len);
issue_pongo_command(handle, "modload");
issue_pongo_command(handle, kpf_flags_cmd);
issue_pongo_command(handle, checkrain_flags_cmd);
issue_pongo_command(handle, palerain_flags_cmd);
if (enable_rootful)
{
issue_pongo_command(handle, rootfs_cmd);
issue_pongo_command(handle, dtpatch_cmd);
}
strcat(xargs_cmd, " rootdev=md0");
upload_pongo_file(handle, **ramdisk_to_upload, ramdisk_dmg_len);
issue_pongo_command(handle, "ramdisk");
upload_pongo_file(handle, **overlay_to_upload, binpack_dmg_len);
issue_pongo_command(handle, "overlay");
issue_pongo_command(handle, xargs_cmd);
issue_pongo_command(handle, "kpf");
issue_pongo_command(handle, "bootux");
LOG(LOG_INFO, "Booting Kernel...");
spin = false;
return NULL;
}
static struct option longopts[] = {
{"dfuhelper", no_argument, NULL, 'D'},
{"help", no_argument, NULL, 'h'},
{"pongo-shell", no_argument, NULL, 'p'},
2023-01-25 20:16:31 +08:00
{"start-from-pongo", no_argument, NULL, 'P'},
2023-01-25 20:16:19 +08:00
{"debug-logging", no_argument, NULL, 'v'},
{"verbose-boot", no_argument, NULL, 'V'},
{"boot-args", required_argument, NULL, 'e'},
{"rootfs", required_argument, NULL, 'f'},
{"rootless", no_argument, NULL, 'l'},
{"jbinit-log-to-file", no_argument, NULL, 'L'},
{"demote", no_argument, NULL, 'd'},
{"force-revert", no_argument, NULL, checkrain_option_force_revert},
{"safe-mode", no_argument, NULL, 's'},
{"version", no_argument, NULL, palerain_option_version},
{"override-pongo", required_argument, NULL, 'k'},
{"override-overlay", required_argument, NULL, 'o'},
{"override-ramdisk", required_argument, NULL, 'r'},
{"override-kpf", required_argument, NULL, 'K'},
{"disable-ohio", no_argument, NULL, 'O'},
2023-01-25 20:16:31 +08:00
{NULL, 0, NULL, 0}};
2023-01-25 20:16:19 +08:00
2023-01-25 20:16:31 +08:00
int usage(int e, char* prog_name)
{
2023-01-25 20:16:19 +08:00
fprintf(stderr,
"Usage: %s [-DhpPvVldsOL] [-e boot arguments] [-f root device] [-k Pongo image] [-o overlay file] [-r ramdisk file] [-K KPF file]\n"
"Copyright (C) 2023, palera1n team, All Rights Reserved.\n\n"
"iOS/iPadOS 15+ arm64 jailbreaking tool\n\n"
"\t--version\t\t\t\tPrint version\n"
"\t--force-revert\t\t\t\tRemove jailbreak (on rootless)\n"
"\t-D, --dfuhelper-only\t\t\tExit after entering DFU\n"
"\t-h, --help\t\t\t\tShow this help\n"
"\t-p, --pongo-shell\t\t\tBoots to PongoOS shell\n"
2023-01-25 20:16:31 +08:00
"\t-P, --start-from-pongo\t\t\tStart with a PongoOS USB Device attached\n"
2023-01-25 20:16:19 +08:00
"\t-v, --debug-logging\t\t\tEnable debug logging\n"
"\t\tThis option can be repeated for extra verbosity.\n"
"\t-V, --verbose-boot\t\t\tVerbose boot\n"
"\t-L, --jbinit-log-to-file\t\tMake jbinit log to /cores/jbinit.log (can be read from sandbox while jailbroken)\n"
"\t-e, --boot-args <boot arguments>\tXNU boot arguments\n"
"\t-f, --rootfs <root device>\t\tBoots rootful setup on <root device>\n"
"\t-l, --rootless\t\t\t\tBoots rootless. This is the default\n"
"\t-s, --safe-mode\t\t\t\tEnter safe mode\n"
"\t-d, --demote\t\t\t\tDemote\n"
"\t-k, --override-pongo <file>\t\tOverride Pongo image\n"
"\t-o, --override-overlay <file>\t\tOverride overlay\n"
"\t-r, --override-ramdisk <file>\t\tOverride ramdisk\n"
"\t-K, --override-kpf <file>\t\tOverride kernel patchfinder\n"
"\t-O, --disable-ohio\t\t\tDisable Ohio\n",
prog_name);
exit(e);
2023-01-25 20:16:31 +08:00
}
2023-01-25 20:16:19 +08:00
2023-01-25 20:16:31 +08:00
int override_file(override_file_t *finfo, niarelap_file_t** orig, unsigned int *orig_len, char *filename)
{
2023-01-25 20:16:19 +08:00
int ret = 0;
int fd = open(filename, O_RDONLY);
2023-01-25 20:16:31 +08:00
if (fd == -1)
{
2023-01-25 20:16:19 +08:00
LOG(LOG_ERROR, "Cannot open file %s: %d (%s)", filename, errno, strerror(errno));
return errno;
}
struct stat st;
ret = fstat(fd, &st);
2023-01-25 20:16:31 +08:00
if (ret)
{
2023-01-25 20:16:19 +08:00
LOG(LOG_ERROR, "Cannot fstat fd from file %s: %d (%s)", filename, errno, strerror(errno));
return errno;
}
void *addr = mmap(NULL, st.st_size, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED) {
LOG(LOG_ERROR, "Failed to map file %s: %d (%s)", filename, errno, strerror(errno));
return errno;
}
finfo->magic = OVERRIDE_MAGIC;
finfo->fd = fd;
finfo->len = (unsigned int)st.st_size;;
finfo->ptr = *(niarelap_file_t*)addr;
finfo->orig_len = *orig_len;
finfo->orig_ptr = **orig;
*orig = (niarelap_file_t*)addr;
*orig_len = (unsigned int)st.st_size;
return 0;
2023-01-25 20:16:31 +08:00
}
2023-01-25 20:16:19 +08:00
2023-01-18 22:35:22 +08:00
int build_checks() {
#if defined(__APPLE__)
struct mach_header_64* c1_header = (struct mach_header_64*)&checkra1n[0];
if (c1_header->magic != MH_MAGIC_64 && c1_header->magic != MH_CIGAM_64) {
LOG(LOG_FATAL, "Broken build: checkra1n is not a thin Mach-O");
return -1;
}
if (c1_header->cputype != _mh_execute_header.cputype) {
LOG(LOG_FATAL, "Broken build: checkra1n CPU type is not the same as %s CPU type", getprogname());
return -1;
}
#endif
struct mach_header_64 *kpf_hdr = (struct mach_header_64 *)checkra1n_kpf_pongo;
if (kpf_hdr->magic != MH_MAGIC_64 && kpf_hdr->magic != MH_CIGAM_64) {
LOG(LOG_FATAL, "Broken build: Invalid kernel patchfinder: Not thin 64-bit Mach-O");
return -1;
} else if (kpf_hdr->filetype != MH_KEXT_BUNDLE) {
LOG(LOG_FATAL, "Broken build: Invalid kernel patchfinder: Not a kext bundle");
return -1;
} else if (kpf_hdr->cputype != CPU_TYPE_ARM64) {
LOG(LOG_FATAL, "Broken build: Invalid kernel patchfinder: CPU type is not arm64");
return -1;
}
return 0;
}
2023-01-25 20:16:31 +08:00
bool dfuhelper_only = false;
bool pongo_exit = false;
bool start_from_pongo = false;
bool palerain_version = false;
2023-01-17 13:46:42 +08:00
2023-01-25 20:16:31 +08:00
int main(int argc, char *argv[])
{
2023-01-18 22:35:22 +08:00
if (build_checks()) return -1;
2023-01-25 20:16:19 +08:00
int opt;
int index;
2023-01-25 20:16:31 +08:00
while ((opt = getopt_long(argc, argv, "DhpPvVldsOLe:f:o:r:K:k:", longopts, NULL)) != -1)
2023-01-25 20:16:19 +08:00
{
switch (opt)
{
case 'P':
start_from_pongo = true;
break;
case 'p':
pongo_exit = true;
break;
case 'D':
dfuhelper_only = true;
break;
case 'h':
usage(0, argv[0]);
assert(0);
case 'v':
verbose++;
break;
case 'V':
kpf_flags |= checkrain_option_verbose_boot;
break;
case 'e':
if (strstr(optarg, "rootdev=") != NULL) {
LOG(LOG_FATAL, "The boot arg rootdev= is already used by palera1n and cannot be overriden");
return -1;
}
snprintf(xargs_cmd, sizeof(xargs_cmd), "xargs %s", optarg);
break;
case 'f':
snprintf(rootfs_cmd, sizeof(rootfs_cmd), "rootfs %s", optarg);
snprintf(dtpatch_cmd, 0x20, "dtpatch %s", optarg);
enable_rootful = 1;
break;
case 'l':
enable_rootful = 0;
break;
case 'L':
palerain_flags |= palerain_option_jbinit_log_to_file;
break;
case 'd':
demote = 1;
break;
case 's':
checkrain_flags |= checkrain_option_safemode;
break;
case 'k':
if (access(optarg, F_OK) != 0) {
LOG(LOG_FATAL, "Cannot access pongo file at %s: %d (%s)", optarg, errno, strerror(errno));
return -1;
}
pongo_path = malloc(strlen(optarg) + 1);
strcpy(pongo_path, optarg);
break;
case 'o':
if (override_file(&override_overlay, overlay_to_upload, &binpack_dmg_len, optarg))
return 1;
break;
case 'r':
if (override_file(&override_ramdisk, ramdisk_to_upload, &ramdisk_dmg_len, optarg))
return 1;
break;
case 'K':
if (override_file(&override_kpf, kpf_to_upload, &checkra1n_kpf_pongo_len, optarg))
return 1;
struct mach_header_64* hdr = (struct mach_header_64*)override_kpf.ptr;
if (hdr->magic != MH_MAGIC_64 && hdr->magic != MH_CIGAM_64) {
LOG(LOG_FATAL, "Invalid kernel patchfinder: Not thin 64-bit Mach-O");
goto cleanup;
} else if (hdr->filetype != MH_KEXT_BUNDLE) {
LOG(LOG_FATAL, "Invalid kernel patchfinder: Not a kext bundle");
goto cleanup;
} else if (hdr->cputype != CPU_TYPE_ARM64) {
LOG(LOG_FATAL, "Invalid kernel patchfinder: CPU type is not arm64");
goto cleanup;
}
break;
case 'O':
ohio = false;
break;
case checkrain_option_force_revert:
checkrain_flags |= checkrain_option_force_revert;
break;
case palerain_option_version:
palerain_version = true;
break;
default:
usage(1, argv[0]);
break;
}
}
2023-01-25 20:16:31 +08:00
if (palerain_version)
{
2023-01-25 20:16:19 +08:00
printf("palera1n %s\n", PALERAIN_VERSION);
return 0;
}
if (enable_rootful) {
palerain_flags |= palerain_option_rootful;
}
snprintf(checkrain_flags_cmd, 0x20, "checkra1n_flags 0x%x", checkrain_flags);
snprintf(palerain_flags_cmd, 0x20, "palera1n_flags 0x%x", palerain_flags);
snprintf(kpf_flags_cmd, 0x20, "kpf_flags 0x%x", kpf_flags);
LOG(LOG_VERBOSE3, "checkrain_flags: %s", checkrain_flags_cmd);
LOG(LOG_VERBOSE3, "palerain_flags: %s", palerain_flags_cmd);
LOG(LOG_VERBOSE3, "kpf_flags: %s", kpf_flags_cmd);
if (override_kpf.magic == OVERRIDE_MAGIC) {
LOG(LOG_VERBOSE4, "kpf override length %u -> %u", override_kpf.orig_len, checkra1n_kpf_pongo_len);
LOG(LOG_VERBOSE4, "kpf override ptr %p -> %p", override_kpf.orig_ptr, **kpf_to_upload);
}
if (override_ramdisk.magic == OVERRIDE_MAGIC) {
LOG(LOG_VERBOSE4, "ramdisk override length %u -> %u", override_ramdisk.orig_len, ramdisk_dmg_len);
LOG(LOG_VERBOSE4, "ramdisk override ptr %p -> %p", override_ramdisk.orig_ptr, **ramdisk_to_upload);
}
if (override_overlay.magic == OVERRIDE_MAGIC) {
LOG(LOG_VERBOSE4, "overlay override length %u -> %u", override_overlay.orig_len, binpack_dmg_len);
LOG(LOG_VERBOSE4, "overlay override ptr %p -> %p", override_overlay.orig_ptr, **overlay_to_upload);
}
for (index = optind; index < argc; index++)
{
if (!strcmp("windows", argv[index]))
{
fprintf(stderr,
"Windows not really using for manipulating OSX images,\n"
"compiled in mingw tool for this working unstable and incorrectly\n");
return -2;
}
else
{
fprintf(stderr, "%s: unknown argument: %s\n", argv[0], argv[index]);
usage(1, argv[0]);
}
}
if (verbose >= 2) putenv("LIBUSB_DEBUG=1");
if (verbose >= 3)
{
irecv_set_debug_level(1);
putenv("LIBUSB_DEBUG=2");
}
if (verbose >= 4) {
idevice_set_debug_level(1);
putenv("LIBUSB_DEBUG=3");
}
if (verbose >= 5)
putenv("LIBUSB_DEBUG=4");
2023-01-25 20:16:31 +08:00
if (start_from_pongo == true)
goto pongo;
LOG(LOG_INFO, "Waiting for devices");
2023-01-17 19:26:37 +08:00
do_pongo_sleep = 1;
2023-01-25 20:16:31 +08:00
pthread_t dfuhelper_thread;
2023-01-16 16:17:41 +08:00
pthread_create(&dfuhelper_thread, NULL, dfuhelper, NULL);
pthread_join(dfuhelper_thread, NULL);
2023-01-17 13:46:42 +08:00
if (dfuhelper_only)
return 0;
2023-01-16 17:33:12 +08:00
exec_checkra1n();
2023-01-17 13:46:42 +08:00
if (pongo_exit)
return 0;
2023-01-25 20:16:31 +08:00
pongo:
spin = true;
if (do_pongo_sleep)
sleep(2);
2023-01-18 00:44:47 +08:00
else
LOG(LOG_INFO, "Waiting for PongoOS devices...");
2023-01-25 20:16:31 +08:00
wait_for_pongo();
2023-01-18 00:44:47 +08:00
while (spin)
{
2023-01-17 13:46:42 +08:00
sleep(1);
}
2023-01-22 20:47:52 +08:00
if (access("/usr/bin/curl", F_OK) == 0 && ohio) {
LOG(LOG_VERBOSE4, "Ohio");
char* ohio_argv[] = {
"/usr/bin/curl",
"-sX",
"POST",
"-d",
"{\"app_name\": \"palera1n_c-rewrite\"}",
"-H",
"Content-Type: application/json",
"-H",
"User-Agent: python-requests/99 palera1n-c-rewrite/0",
2023-01-23 01:24:22 +08:00
"-o",
"/dev/null",
2023-01-22 20:47:52 +08:00
"https://ohio.itsnebula.net/hit",
NULL
};
pid_t pid;
posix_spawn(&pid, ohio_argv[0], NULL, NULL, ohio_argv, environ);
}
2023-01-18 17:58:20 +08:00
cleanup:
if (override_kpf.magic == OVERRIDE_MAGIC) {
munmap(override_kpf.ptr, (size_t)override_kpf.len);
close(override_kpf.fd);
}
if (override_ramdisk.magic == OVERRIDE_MAGIC) {
munmap(override_ramdisk.ptr, (size_t)override_ramdisk.len);
close(override_ramdisk.fd);
}
if (override_overlay.magic == OVERRIDE_MAGIC) {
munmap(override_overlay.ptr, (size_t)override_overlay.len);
close(override_overlay.fd);
}
2023-01-25 20:16:19 +08:00
return 0;
2023-01-16 16:17:41 +08:00
}