控制fork进程的主程序 --> fork 进程从磁盘加载二进制代码并跳转到其中。 -> 父请求任-6ren">
gpt4 book ai didi

c - Mac OS (10.13.1) fork 进程的task_for_pid "(os/kern) failure"

转载 作者:行者123 更新时间:2023-11-30 16:37:56 27 4
gpt4 key购买 nike

上次我尝试编写简单的遗传模糊器(严格使用 Mac 操作系统,只是为了好玩)。我的想法是这样的:

-> 控制fork进程的主程序

--> fork 进程从磁盘加载二进制代码并跳转到其中。

-> 父请求任务 (task_for_pid(mach_task_self(),childPID,&task))

-> 家长 try catch 陷阱(0xcc),检查我们以前是否去过那里,就像 AFL 的工作原理(当然是简化的)

--> 子进程加载一些原始二进制代码(在我的示例中必须是 System V ABI)

我收到如下错误:

16:10|domin568[15] ~/Desktop/experiments/Instrumentation $ ./run.sh
PARENT 3866
task_for_pid() failed with message (os/kern) failure !
CHILD 3867

运行.sh:

#!/bin/sh
clang -sectcreate __TEXT __info_plist Info.plist -o server server.c
codesign -s instrument ./server
./server

“乐器”出现在我的钥匙串(keychain)上,并且始终信任代码演唱,所以我认为不应该是这样。

信息.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SecTaskAccess</key>
<array>
<string>allowed</string>
<string>debug</string>
</array>
</dict>
</plist>

当然,我的代码仅对特定情况有用,它尝试模糊一个输入为字符串的函数并将其与其他字符串进行比较。

服务器.c:

#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ptrace.h>
#include <mach/mach.h>
#include <stdlib.h>
int main (int argc,char ** argv)
{
pid_t pid = fork();
pid_t parentPID, childPID; //maybe it's not really safe, nevermind
int status;
if (pid == 0)
{
printf ("CHILD %i\n",getpid());
childPID = getpid();

FILE * f = fopen(argv[1],"rb");
if (f == NULL)
{
return -2;
puts ("Cannot open file specified\n");
}
int from,to = 0;
sscanf(argv[2],"%x",&from);
sscanf(argv[3],"%x",&to);
if (from >= to)
{
puts ("R u out of your mind ? check your range of bytes within the file... \n");
return -3;
}
int fileSize = to - from;
void * mem = mmap (NULL,fileSize,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE,fileno(f),0);
if (mem == MAP_FAILED)
{
puts ("[!] Cannot allocate memory for file");
return -4;
}
printf ("[-] File mapped to virtual memory : [%p]\n",mem);

int (*pFunc)(char * str) = (int(*)(char *))(mem+from);

int ret = pFunc("AAAAA");
printf ("Returned : %d\n",ret);


}
else
{
printf ("PARENT %i\n",getpid());
parentPID = getpid();

kern_return_t kret;
mach_port_t task;
mach_port_t target_exception_port;
kret = task_for_pid (mach_task_self(),childPID,&task);
if (kret != KERN_SUCCESS)
{
printf ("task_for_pid() failed with message %s !\n",mach_error_string(kret));
sleep(100000);
}

//save the set of exception ports registered in the process

exception_mask_t saved_masks[EXC_TYPES_COUNT];
mach_port_t saved_ports[EXC_TYPES_COUNT];
exception_behavior_t saved_behaviors[EXC_TYPES_COUNT];
thread_state_flavor_t saved_flavors[EXC_TYPES_COUNT];
mach_msg_type_number_t saved_exception_types_count;

task_get_exception_ports(task,
EXC_MASK_ALL,
saved_masks,
&saved_exception_types_count,
saved_ports,
saved_behaviors,
saved_flavors);

//allocate and authorize a new port

mach_port_allocate(mach_task_self(),
MACH_PORT_RIGHT_RECEIVE,
&task);

mach_port_insert_right(mach_task_self(),
target_exception_port,
target_exception_port,
MACH_MSG_TYPE_MAKE_SEND);

//register the exception port with the target process

task_set_exception_ports(task,
EXC_MASK_ALL,
target_exception_port,
EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES,
THREAD_STATE_NONE);

ptrace (PT_ATTACHEXC, childPID,0,0);
}

return 0;
}

为什么会输出错误? fork 在 OSX 下是如何工作的?那里出了什么问题?我不是 osx 底层工作原理的专家,所以也许我错过了一些东西。感谢您的帮助:)!

多米尼克

最佳答案

您调用pid_t pid = fork();,因此父进程中子进程的PID存储在pid变量中,而不是childPID

childPID 变量仅根据您的示例代码在子进程中初始化。

task_for_pid() 调用正在使用 childPID - 基本上您正在使用未初始化的内存。

关于c - Mac OS (10.13.1) fork 进程的task_for_pid "(os/kern) failure",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47698652/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com