デバッガーとまったく同じように、別のプログラムの実行フローを変更する C プログラムを OSX で作成しようとしています。しかし、すべての「ピース」を組み立てる前に、まずそれぞれが個別に機能することをテストする必要があります。
mach_vm_read_overwrite()
andを正常に使用mach_vm_write()
して、スタックの読み取りと書き込みを行いました。thread_get_state()
andを正常に使用しthread_set_state()
て、レジスタの読み取りと書き込みを行いました。
あとはthread_create_running()
、任意の関数を実行するためにタスク内にスレッドを作成するために使用するだけです。ただし、スレッドを作成するたびに、OSX が完全にクラッシュし、コンピューターが自動的に再起動されます (笑)。誰かが何が起こっているのかをより詳細に説明できますか?
これが私のリモート プログラム test.c です。
#include <unistd.h>
#include <stdio.h>
void function1() {
printf("lol 1\n");
}
void function2() {
printf("lol 2\n");
}
void function3() {
printf("lol 3\n");
}
int main(int argc, char **argv) {
while(1) {
function1();
sleep(1);
function2();
sleep(1);
function3();
sleep(1);
}
return 0;
}
そして、これが進行中の小さなデバッガーです。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdint.h>
#include <mach/mach_traps.h>
#include <mach/mach_init.h>
#include <mach/mach_error.h>
#include <mach/mach.h>
#include <mach/mach_types.h>
#include <mach/i386/thread_status.h>
void error(char *msg) {
printf("error: %s\n", msg);
exit(1);
}
int main(int argc, char **argv) {
pid_t pid;
mach_port_t eq_task;
kern_return_t err;
thread_act_port_array_t thread_list;
mach_msg_type_number_t thread_count;
x86_thread_state_t x86_state;
mach_msg_type_number_t sc = x86_THREAD_STATE_COUNT;
thread_act_t remoteThread;
// Make sure we have an argument
if (argc != 2)
error("requires a PID");
else
pid = (pid_t)atoi(argv[1]);
// Make sure we're root
if (getuid() && geteuid())
error("requires root");
// Get the task port
err = task_for_pid(mach_task_self(), pid, &eq_task);
if ((err != KERN_SUCCESS) || !MACH_PORT_VALID(eq_task))
error("getting eq task");
// Suspend the process
if(task_suspend(eq_task))
error("suspending the task");
// Get a list of threads from the port
if (task_threads(eq_task, &thread_list, &thread_count))
error("cannot get list of tasks");
// Get the registers
if (thread_get_state(thread_list[0], x86_THREAD_STATE, (thread_state_t)&x86_state, &sc))
error("getting state from thread");
// Create a new thread
err = thread_create_running(eq_task, x86_THREAD_STATE, (thread_state_t)&x86_state, x86_THREAD_STATE_COUNT, &remoteThread);
// BLACK SCREEN AND CRASH
// Resume the process again
if(task_resume(eq_task))
error("resuming the task");
}