【发布时间】:2020-11-09 23:17:16
【问题描述】:
我试图在另一个进程中注入一个共享库,但我设法让它在 x64 上运行。但是,当我尝试将它用于 32 位时,发生了一些奇怪的事情:由于输入/输出错误(errno 5),ptrace 无法正确执行。我不知道该怎么做,因为同样的代码适用于 x64。
然后,我尝试使用我称为test_ptrace 的函数创建一个较小的示例。令人惊讶的是,错误并没有发生在那里,尽管它本质上是做同样的事情(在目标进程上分配内存,注入有效负载,设置寄存器以匹配有效负载,运行有效负载)。当我看到错误没有发生时,我再次尝试使用名为load_library 的函数使用 ptrace 注入共享库。但不幸的是,错误又出现了。
//this is the function that is NOT working, 'load_library'
void* load_library(pid_t pid, std::string path, int mode)
{
int status;
struct user_regs_struct old_regs, regs;
void* dlopen_ex = (void*)0xf7c29700; //I disabled ASLR, so this address does not change
void* handle_ex = (void*)-1;
unsigned char inj_buf[] =
{
0x51, //push ecx
0x53, //push ebx
0xFF, 0xD0, //call eax
0xCC, //int3 (SIGTRAP)
};
size_t path_size = path.size();
size_t inj_size = sizeof(inj_buf) + path_size;
void* inj_addr = allocate_memory(pid, inj_size, PROT_EXEC | PROT_READ | PROT_WRITE);
void* path_addr = (void*)((uintptr_t)inj_addr + sizeof(inj_buf));
write_memory(pid, inj_addr, (void*)inj_buf, sizeof(inj_buf));
write_memory(pid, path_addr, (void*)path.c_str(), path_size);
if(ptrace(PTRACE_ATTACH, pid, NULL, NULL))
{
perror("PTRACE_ATTACH");
std::cout << "Errno: " << errno << std::endl;
return handle_ex;
}
wait(&status);
if(ptrace(PTRACE_GETREGS, pid, NULL, &old_regs) == -1)
{
perror("PTRACE_GETREGS");
std::cout << "Errno: " << errno << std::endl;
return handle_ex;
}
regs.eax = (unsigned long)dlopen_ex;
regs.ebx = (unsigned long)path_addr;
regs.ecx = (unsigned long)mode;
regs.eip = (unsigned long)inj_addr;
if(ptrace(PTRACE_SETREGS, pid, NULL, ®s) == -1)
{
perror("PTRACE_SETREGS");
std::cout << "Errno: " << errno << std::endl;
return handle_ex;
}
if(ptrace(PTRACE_CONT, pid, NULL, NULL) == -1)
{
perror("PTRACE_CONT");
std::cout << "Errno: " << errno << std::endl;
return handle_ex;
}
waitpid(pid, &status, WSTOPPED);
if(ptrace(PTRACE_GETREGS, pid, NULL, ®s) == -1)
{
perror("PTRACE_GETREGS");
std::cout << "Errno: " << errno << std::endl;
return handle_ex;
}
handle_ex = (void*)old_regs.eax;
if(ptrace(PTRACE_SETREGS, pid, NULL, &old_regs) == -1)
{
perror("PTRACE_SETREGS");
std::cout << "Errno: " << errno << std::endl;
return handle_ex;
}
if(ptrace(PTRACE_DETACH, pid, NULL, NULL) == -1)
{
perror("PTRACE_DETACH");
std::cout << "Errno: " << errno << std::endl;
return handle_ex;
}
deallocate_memory(pid, inj_addr, inj_size);
return handle_ex;
}
//this one, though, is working, but it is very similar to the function
//above (except it doesn't restore the execution, but the code of the
//other function doesn't even get there anyway.
void test_ptrace(pid_t pid)
{
int status;
struct user_regs_struct regs;
unsigned char inj_buf[] =
{
0xCD, 0x80, //int80 (syscall)
0xCC, //int3 (SIGTRAP)
};
void* inj_addr = allocate_memory(pid, sizeof(inj_buf), PROT_EXEC | PROT_READ | PROT_WRITE);
write_memory(pid, inj_addr, inj_buf, sizeof(inj_buf));
std::cout << "--ptrace test started--" << std::endl;
if(ptrace(PTRACE_ATTACH, pid, NULL, NULL) == -1)
{
perror("PTRACE_ATTACH");
std::cout << "Errno: " << errno << std::endl;
return;
}
wait(&status);
if(ptrace(PTRACE_GETREGS, pid, NULL, ®s) == -1)
{
perror("PTRACE_GETREGS");
std::cout << "Errno: " << errno << std::endl;
return;
}
regs.eax = __NR_exit;
regs.ebx = 222;
regs.eip = (unsigned long)inj_addr;
if(ptrace(PTRACE_SETREGS, pid, NULL, ®s) == -1)
{
perror("PTRACE_SETREGS");
std::cout << "Errno: " << errno << std::endl;
return;
}
if(ptrace(PTRACE_DETACH, pid, NULL, NULL) == -1)
{
perror("PTRACE_DETACH");
std::cout << "Errno: " << errno << std::endl;
return;
}
std::cout << "--ptrace test ended--" << std::endl;
}
节目入口:
int main()
{
pid_t pid = get_process_id("target");
std::cout << "PID: " << pid << std::endl;
std::string lib_path = "<my_path>/ptrace-test/libtest.so";
load_library(pid, lib_path, RTLD_LAZY);
return 0;
}
输出:
PID: 2383
PTRACE_SETREGS: Input/output error
Errno: 5
如果您需要将整个项目作为“最小”的可重现示例,请点击此处:https://github.com/rdbo/ptrace-test
PID 是正确的,我以 root 身份运行,tracer 和 tracee 都是用 G++ 在 32 位上编译的。运行最新的 Manjaro。有什么想法吗?
【问题讨论】:
-
问题出在
allocate_memory。 x64 和 x86 在保护模式下具有不同的内存布局。如果inj_addr无效,它将失败.... -
我不明白,allocate_memory 运行没有任何问题并返回一个有效地址。问题发生在 PTRACE_SETREGS 中,但它与之前分配的地址有什么关系?
-
allocate_memory可以返回任何地址(你隐藏了这个函数,我假设它可以指向目标进程的任何地址);你不能保证它指向一个安全的。如果它指向例如一个系统区域,你肯定会得到错误... -
可以在GitHub仓库查看
allocate_memory的代码。它在目标进程中注入一个 __NR_mmap(或 __NR_mmap2 用于 32 位)系统调用,然后从 EAX/RAX 检索返回值。我检查了那个地址在内存中的样子,它确实显示了“rwxp”,所以我认为它是另外一回事。此外,allocate_memory正在用于函数test_ptrace,该函数正在运行。 -
我刚刚发现了一些奇怪的东西,它似乎在 x64 上也出现了输入/输出错误,但它仍然以某种方式注入了寄存器。这只会变得更加混乱
标签: c++ linux code-injection errno ptrace