#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "patchfinder64.h" #include #include "CSCommon.h" static mach_port_t tfp0=MACH_PORT_NULL; size_t kread(uint64_t where, void *p, size_t size); size_t kwrite(uint64_t where, const void *p, size_t size); void set_tfp0(mach_port_t port) { tfp0 = port; } void wk32(uint64_t kaddr, uint32_t val) { kwrite(kaddr, &val, sizeof(uint32_t)); } void wk64(uint64_t kaddr, uint64_t val) { kwrite(kaddr, &val, sizeof(uint64_t)); } uint32_t rk32(uint64_t kaddr) { uint32_t val = 0; if (kread(kaddr, &val, sizeof(val)) != sizeof(val)) { return 0; } return val; } uint64_t rk64(uint64_t kaddr) { uint64_t val = 0; if (kread(kaddr, &val, sizeof(val)) != sizeof(val)) { return 0; } return val; } uint64_t kmem_alloc(uint64_t size) { if (tfp0 == MACH_PORT_NULL) { printf("attempt to allocate kernel memory before any kernel memory write primitives available\n"); sleep(3); return 0; } kern_return_t err; mach_vm_address_t addr = 0; mach_vm_size_t ksize = round_page_kernel(size); err = mach_vm_allocate(tfp0, &addr, ksize, VM_FLAGS_ANYWHERE); if (err != KERN_SUCCESS) { printf("unable to allocate kernel memory via tfp0: %s %x\n", mach_error_string(err), err); sleep(3); return 0; } return addr; } // https://github.com/JonathanSeals/kernelversionhacker/blob/3dcbf59f316047a34737f393ff946175164bf03f/kernelversionhacker.c#L92 #define IMAGE_OFFSET 0x2000 #define MACHO_HEADER_MAGIC 0xfeedfacf #define MAX_KASLR_SLIDE 0x21000000 #define KERNEL_SEARCH_ADDRESS 0xfffffff007004000 #define ptrSize sizeof(uintptr_t) vm_address_t get_kernel_base(mach_port_t tfp0) { uint64_t addr = 0; addr = KERNEL_SEARCH_ADDRESS+MAX_KASLR_SLIDE; while (1) { char *buf; mach_msg_type_number_t sz = 0; kern_return_t ret = vm_read(tfp0, addr, 0x200, (vm_offset_t*)&buf, &sz); if (ret) { goto next; } if (*((uint32_t *)buf) == MACHO_HEADER_MAGIC) { int ret = vm_read(tfp0, addr, 0x1000, (vm_offset_t*)&buf, &sz); if (ret != KERN_SUCCESS) { printf("Failed vm_read %i\n", ret); goto next; } for (uintptr_t i=addr; i < (addr+0x2000); i+=(ptrSize)) { mach_msg_type_number_t sz; int ret = vm_read(tfp0, i, 0x120, (vm_offset_t*)&buf, &sz); if (ret != KERN_SUCCESS) { printf("Failed vm_read %i\n", ret); exit(-1); } if (!strcmp(buf, "__text") && !strcmp(buf+0x10, "__PRELINK_TEXT")) { return addr; } } } next: addr -= 0x200000; } } size_t kread(uint64_t where, void *p, size_t size) { int rv; size_t offset = 0; while (offset < size) { mach_vm_size_t sz, chunk = 2048; if (chunk > size - offset) { chunk = size - offset; } rv = mach_vm_read_overwrite(tfp0, where + offset, chunk, (mach_vm_address_t)p + offset, &sz); if (rv || sz == 0) { fprintf(stderr, "[e] error reading kernel @%p\n", (void *)(offset + where)); break; } offset += sz; } return offset; } size_t kwrite(uint64_t where, const void *p, size_t size) { int rv; size_t offset = 0; if (tfp0 == MACH_PORT_NULL) { printf("attempt to write to kernel memory before any kernel memory write primitives available\n"); sleep(3); return offset; } while (offset < size) { size_t chunk = 2048; if (chunk > size - offset) { chunk = size - offset; } rv = mach_vm_write(tfp0, where + offset, (mach_vm_offset_t)p + offset, (mach_msg_type_number_t)chunk); if (rv) { fprintf(stderr, "[e] error writing kernel @%p\n", (void *)(offset + where)); break; } offset += chunk; } return offset; }