Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.c
- // testforloop
- //
- // Created by Yaniv Mashat on 3/6/19.
- // Copyright © 2019 CydiaDevsIL. All rights reserved.
- //
- #include <stdio.h>
- #include <mach/thread_act.h>
- #include <mach/task.h>
- #include <mach/mach.h>
- #include <errno.h> // errno
- #include <sched.h> // sched_yield
- #include <stdlib.h> // malloc, free
- #include <string.h> // strerror
- #include <unistd.h>
- //#include "helper.h"
- int main(int argc, const char * argv[]) {
- kern_return_t kr = KERN_FAILURE;
- pid_t pid;
- task_t child_task;
- task_t a;
- ledger_t ledger;
- ledger_array_t ledger_array;
- mach_msg_type_number_t ledger_count;
- boolean_t inhearit = TRUE;
- task_info_data_t info;
- mach_msg_type_number_t count;
- struct task_basic_info * task_basic_info;
- if (argc == 2) {
- inhearit = (atoi(argv[1])) ? TRUE : FALSE;
- }
- ledger_count =1;
- ledger = (ledger_t)0;
- ledger_array = &ledger;
- // thread_act_array_t act_list[10];
- int i =0;
- printf("our pid: %d \n", getpid());
- //MARK: -
- //MARK: a: Get first task port
- while (kr != KERN_SUCCESS) {
- if (i != getpid()) {
- kr= task_for_pid(mach_task_self(), i, &a);
- printf("pid: %d \n", i);
- }
- i++;
- }
- //MARK: -
- //MARK: a: Check task port
- if (MACH_PORT_VALID(a)) {
- printf("got a valid task: 0x%x\n", a);
- }
- //MARK: -
- //MARK: Allocate 10 mach_port array
- mach_port_t e [10];
- for (int i =0; i<10; i++) {
- kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &e[i]);
- mach_error("mach_port_allocate:", kr);
- }
- int thread_count =0;
- mach_msg_type_number_t max = 30;
- thread_act_port_array_t our_task_threads;
- thread_act_t b;
- thread_act_t c;
- //MARK: -
- //MARK: Get a's Task threads into our_task_threads
- kr= task_threads(a, &our_task_threads, &max);
- mach_error("task_threads:", kr);
- //MARK: -
- //MARK: cout a's Task threads from our_task_threads
- for (int i =0; i<1000; i++) {
- if (MACH_PORT_VALID(our_task_threads[i])) {
- printf("got valid thread in task: 0x%x\n", our_task_threads[i]);
- thread_count++;
- }
- }
- printf("we got %d threads in our task\n", thread_count);
- thread_count =0;
- //MARK: -
- //MARK: Add 2 new threads in a's Task space
- kr = thread_create(a, &b); mach_error("thread_create:", kr);
- if (MACH_PORT_VALID(b)) {
- printf("got valid thread: 0x%x\n", b);
- }
- kr = thread_create(a, &c); mach_error("thread_create:", kr);
- if (MACH_PORT_VALID(b)) {
- printf("got valid thread: 0x%x\n", c);
- }
- //MARK: -
- //MARK: Get a's Task threads into our_task_threads
- kr= task_threads(a, &our_task_threads, &max);
- mach_error("task_threads:", kr);
- //MARK: -
- //MARK: cout a's Task threads from our_task_threads
- //MARK: -
- for (int i =0; i<1000; i++) {
- if (MACH_PORT_VALID(our_task_threads[i])) {
- printf("got valid thread in task: 0x%x\n", our_task_threads[i]);
- thread_count++;
- }
- }
- printf("we got %d threads in our task\n", thread_count);
- #define ARM_THREAD_STATE64 6
- mach_port_t maybe;
- // kr= thread_get_mach_voucher(mach_thread_self(), 0, &maybe);
- kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &maybe);
- mach_error("mach_port_allocate:", kr);
- kr = mach_port_insert_right(mach_task_self(), maybe, maybe, MACH_MSG_TYPE_MAKE_SEND);
- mach_error("mach_port_insert_right:", kr);
- printf("port to test: 0x%x\n", maybe);
- for (int i =0; i<14; i++) {
- if (EXC_MASK_ALL & (1 << i)) {
- printf("entering port to %d index\n", i);
- }
- }
- int plz = 0;
- //FIXME: -
- //FIXME: this loop is not running
- while (plz & (1 << 1)) {
- printf("plz: %d\n", plz);
- plz++;
- }
- kr= thread_set_exception_ports(c, EXC_MASK_ALL, maybe, EXCEPTION_STATE,ARM_THREAD_STATE64 );
- for (int i =0; i<1000; i++) {
- if (MACH_PORT_VALID(our_task_threads[thread_count -i])) {
- kr= thread_set_exception_ports(our_task_threads[thread_count - i], EXC_MASK_ALL, maybe, EXCEPTION_STATE,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(our_task_threads[thread_count - i], EXC_MASK_SYSCALL, maybe, EXCEPTION_DEFAULT ,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(our_task_threads[thread_count - i], EXC_MASK_CRASH , maybe, 3 ,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(our_task_threads[thread_count - i], EXC_MASK_CRASH , maybe, 7 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- // thread_count++;
- }
- }
- // for (int i =0; i<1000; i++) {
- // if (MACH_PORT_VALID(our_task_threads[thread_count - i])) {
- // kr = task_generate_corpse(a, e);
- // mach_error("task_generate_corespe:", kr);
- // thread_count++;
- // }
- // }
- // mach_error("thread_set_exception_ports:", kr);
- // kr = task_generate_corpse(a, e);
- // mach_error("task_generate_corespe:", kr);
- // kr = task_generate_corpse(a, e);
- // mach_error("task_generate_corespe:", kr);
- // exception_mask_t mask = EXC_MASK_ALL;
- //exception_mask_array_t masks = NULL;
- // exception_handler_array_t handlers = NULL;//
- //exception_behavior_array_t beavhior = NULL ;
- //exception_flavor_array_t flavors = NULL;
- // mach_msg_type_number_t *max2 = 30;
- struct exc_port_info {
- mach_msg_type_number_t count;
- mach_port_t ports[EXC_TYPES_COUNT];
- exception_mask_t masks[EXC_TYPES_COUNT];
- exception_behavior_t behaviors[EXC_TYPES_COUNT];
- thread_state_flavor_t flavors[EXC_TYPES_COUNT];
- };
- struct exc_port_info aa;
- for (int i =0; i<30; i++) {
- // char mask_string[200];
- // char behavior_string[30];
- //get_exc_mask_string(masks[i], mask_string, sizeof(mask_string));
- // get_exc_behavior_string(beavhior[i], behavior_string, sizeof(behavior_string));
- }
- kr= thread_set_exception_ports(c, EXC_MASK_SYSCALL, maybe, EXCEPTION_DEFAULT ,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(c, EXC_MASK_CRASH , maybe, 3 ,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(c, EXC_MASK_CRASH , maybe, 7 ,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(c, plz, maybe, EXCEPTION_STATE,ARM_THREAD_STATE64 );
- kr = thread_get_exception_ports(c, EXC_MASK_ALL, aa.masks, &aa.count, aa.ports, aa.behaviors, aa.flavors);
- kr= thread_set_exception_ports(0xae03, EXC_MASK_SYSCALL, maybe, EXCEPTION_DEFAULT ,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(0xae03, EXC_MASK_CRASH , maybe, 3 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(0xae03, EXC_MASK_CRASH , maybe, 7 ,ARM_THREAD_STATE64 );
- kr= thread_set_exception_ports(0xae03, plz, maybe, EXCEPTION_STATE,ARM_THREAD_STATE64 );
- kr = thread_get_exception_ports(0xae03, EXC_MASK_ALL, aa.masks, &aa.count, aa.ports, aa.behaviors, aa.flavors);
- mach_error("thread_set_exception_ports:", kr);
- int count_port =0;
- for (int i =0; i<14; i++) {
- printf("0x%x\n", aa.ports[i]);
- if (maybe == aa.ports[i]) {
- printf("we got the port in the %d index\n", i);
- count_port++;
- }
- }
- if (count_port > 1) {
- printf("same port in thread!\n");
- }
- // kr = thread_get_exception_ports(c, EXC_MASK_ALL, masks, &max2, handlers, beavhior, flavors);
- mach_error("thread_get_exception_ports", kr);
- for (int i =0; i<100; i++) {
- // if (MACH_PORT_VALID(beavhior[i]) && beavhior[i]==maybe) {
- // printf("got exception portat index %d\n", i);
- //}
- }
- for (i = 0; i<10; i++) {
- printf("0x%x\n", e[i]);
- }
- for (int i =0; i < 1000; i++) {
- if (MACH_PORT_VALID(our_task_threads[i])) {
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_SYSCALL, maybe, 1 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_CRASH , maybe, 2 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_GUARD , maybe, 3 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_RESOURCE, maybe, 1,7 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_MACHINE, maybe, 2,5 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_SOFTWARE, maybe, 1,9 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_EMULATION, maybe, 2,3 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_RPC_ALERT, maybe, 3,1 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i], EXC_MASK_BAD_INSTRUCTION, maybe, 1,0 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i],EXC_MASK_ARITHMETIC, maybe, 3,0 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i],EXC_MASK_MACH_SYSCALL, maybe, 3,9 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i],EXC_MASK_BREAKPOINT, maybe, 3,8 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads[i],EXC_MASK_BAD_ACCESS, maybe, 1,2 );
- mach_error("thread_set_exception_ports:", kr);
- }
- }
- kr= thread_set_exception_ports(c, EXC_MASK_SYSCALL, maybe, 1 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_CRASH , maybe, 2 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_GUARD , maybe, 3 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_RESOURCE, maybe, 1,7 );
- mach_error("thread_set_exception_ports:", kr);
- kr = task_generate_corpse(a, e);
- mach_error("task_generate_corespe:", kr);
- task_t corspe2 = e[0];
- printf("0x%x\n", corspe2);
- // for (i = 0; i<10; i++) {
- // printf("0x%x\n", e[i]);
- // }
- mach_port_deallocate(mach_task_self(), maybe);
- kr = task_generate_corpse(a, e);
- mach_error("task_generate_corespe:", kr);
- task_t corspe1 = e[0];
- printf("0x%x\n", corspe1);
- // for (i = 0; i<10; i++) {
- // printf("0x%x\n", e[i]);
- //}
- thread_act_port_array_t our_task_corspe_threads;
- thread_act_port_array_t our_task_corspe_threads1;
- kr= task_threads(corspe1, &our_task_corspe_threads, &max);
- mach_error("task_threads:", kr);
- kr= task_threads(corspe2, &our_task_corspe_threads1, &max);
- mach_error("task_threads:", kr);
- // for (i = 0; i<1000; i++) {
- // if (MACH_PORT_VALID(our_task_threads[i]) && MACH_PORT_VALID(our_task_corspe_threads1[i])) {
- // if (our_task_corspe_threads[i] == our_task_corspe_threads1[i]) {
- // printf("same thread!\n");
- // }else{
- // printf("diffrent\n");
- // struct exc_port_info bb;
- // struct exc_port_info cc;
- // kr = thread_get_exception_ports(our_task_corspe_threads[i], EXC_MASK_ALL, bb.masks, &bb.count, bb.ports, bb.behaviors, bb.flavors);
- // kr = thread_get_exception_ports(our_task_corspe_threads1[i], EXC_MASK_ALL, cc.masks, &cc.count, cc.ports, cc.behaviors, cc.flavors);
- // for (int i =0; i<14; i++) {
- //
- // printf("0x%x\n", bb.ports[i]);
- // if (cc.ports[i] == bb.ports[i]) {
- // printf("same port\n");
- // }else{
- // printf("not smae at all\n");
- // }
- // }
- // }
- // }
- // }
- for (int i =0; i<1000; i++) {
- // if (MACH_PORT_VALID(our_task_corspe_threads[i])) {
- if (our_task_corspe_threads[i] != our_task_threads[i]) {
- printf("got diffrent thread in index %d\n", i);
- printf("thread in original task: 0x%x\n", our_task_threads[i]);
- printf("thread in corspe task: 0x%x\n", our_task_corspe_threads[i]);
- struct exc_port_info bb;
- kr = thread_get_exception_ports(our_task_corspe_threads[i], EXC_MASK_ALL, bb.masks, &bb.count, bb.ports, bb.behaviors, bb.flavors);
- count_port =0;
- for (int i =0; i<14; i++) {
- printf("0x%x\n", bb.ports[i]);
- if (maybe == bb.ports[i]) {
- printf("we got the port in the %d index\n", i);
- count_port++;
- }
- }
- }
- }
- kr= thread_set_exception_ports(c, EXC_MASK_SYSCALL, maybe, 1 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_CRASH , maybe, 2 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_GUARD , maybe, 3 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_RESOURCE, maybe, 1,7 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_MACHINE, maybe, 2,5 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_SOFTWARE, maybe, 1,9 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_EMULATION, maybe, 2,3 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_RPC_ALERT, maybe, 3,1 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c, EXC_MASK_BAD_INSTRUCTION, maybe, 1,0 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c,EXC_MASK_ARITHMETIC, maybe, 3,0 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c,EXC_MASK_MACH_SYSCALL, maybe, 3,9 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c,EXC_MASK_BREAKPOINT, maybe, 3,8 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(c,EXC_MASK_BAD_ACCESS, maybe, 1,2 );
- mach_error("thread_set_exception_ports:", kr);
- task_t corspe_task = e[0];
- mach_msg_type_number_t max1 = 30;
- thread_act_port_array_t our_task_threads_corspe;
- thread_act_t b1;
- thread_act_t c1;
- kr= task_threads(corspe_task, &our_task_threads_corspe, &max1);
- mach_error("task_threads:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_SYSCALL, maybe, 1 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_CRASH , maybe, 2 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_GUARD , maybe, 3 ,ARM_THREAD_STATE64 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_RESOURCE, maybe, 1,7 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_MACHINE, maybe, 2,5 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_SOFTWARE, maybe, 1,9 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_EMULATION, maybe, 2,3 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_RPC_ALERT, maybe, 3,1 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_BAD_INSTRUCTION, maybe, 1,0 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1],EXC_MASK_ARITHMETIC, maybe, 3,0 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1],EXC_MASK_MACH_SYSCALL, maybe, 3,9 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1],EXC_MASK_BREAKPOINT, maybe, 3,8 );
- mach_error("thread_set_exception_ports:", kr);
- kr= thread_set_exception_ports(our_task_threads_corspe[thread_count-1],EXC_MASK_BAD_ACCESS, maybe, 1,2 );
- mach_error("thread_set_exception_ports:", kr);
- kr = thread_get_exception_ports(our_task_threads_corspe[thread_count-1], EXC_MASK_ALL, aa.masks, &aa.count, aa.ports, aa.behaviors, aa.flavors);
- count_port =0;
- for (int i =0; i<14; i++) {
- printf("0x%x\n", aa.ports[i]);
- if (maybe == aa.ports[i]) {
- printf("we got the port in the %d index\n", i);
- count_port++;
- }
- }
- if (count_port > 1) {
- printf("same port in thread!\n");
- }
- kr = task_generate_corpse(corspe_task, e);
- mach_error("task_generate_corpse:", kr);
- kr = thread_get_exception_ports(c, EXC_MASK_ALL, aa.masks, &aa.count, aa.ports, aa.behaviors, aa.flavors);
- count_port =0;
- for (int i =0; i<14; i++) {
- printf("0x%x\n", aa.ports[i]);
- if (maybe == aa.ports[i]) {
- printf("we got the port in the %d index\n", i);
- count_port++;
- }
- }
- if (count_port > 1) {
- printf("same port in thread!\n");
- }
- if (MACH_PORT_VALID(maybe)) {
- printf("worked?\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement