ohchase/ptrace-do
GitHub: ohchase/ptrace-do
一个用Rust编写的跨平台ptrace库,提供远程函数调用和共享库注入能力。
Stars: 53 | Forks: 13
# ptrace-do
[](#)




提供使用 ptrace 在远程进程中执行函数的能力。
主要用于运行时共享库注入。
## 平台支持
Ptrace-do 支持此库适用的主要预期平台目标
- 
- 
- 
- 
- 
- 
- 
- 
- 
## 相关项目
[yaui](https://github.com/ohchase/yaui)
一个完全使用 Rust 编写的命令行应用程序,提供了一个将共享对象注入到运行中的 Unix 进程的命令行界面示例。它作为一个很好的示例,展示了如何充分利用此 crate 的功能。
[plt-rs](https://github.com/ohchase/plt-rs)
一个完全使用 Rust 编写的库,提供在运行时挂钩 Unix 应用程序过程链接表(PLT)的能力。如果您希望在将共享对象注入到运行中的 Unix 进程之后,还想对 libc::recv 或 libc::send 等函数进行 detour 以进行网络数据包检查/增强,那么这个库可能对您有益。
[ptrace_do](https://github.com/emptymonkey/ptrace_do)
这个 Rust 库被命名为 `ptrace-do`,我想明确承认这可能是我做出的一个不恰当且糟糕的决定。我使用了一个历史上流行的 C 项目相同的名称,因为这两个项目的目标相似,都致力于通过 ptrace 为 syscall 注入提供符合人体工程学的接口。需要澄清的是,此 crate 中的工作与 emptymonkey 的 ptrace_do 实现完全没有关系。此 crate 完全由 Rust 设计,而不是一个为 emptymonkey 的 ptrace_do C FFI 实现提供类型安全 Rust API 的 crate。
## 示例
### 在远程进程中调用 Libc Getpid
在这个示例中,我们 fork 当前应用程序,并生成一个子进程进行跟踪。
这有助于我们避免许多在实际使用此库时必须考虑的安全相关限制。请务必查看 yaui 项目,以了解在跟踪外部进程时必须考虑的实际安全性和访问权限问题。
```
fn main() -> Result<()> {
println!("parent: process with pid: {}", process::id());
unsafe {
let pid = libc::fork();
if pid < 0 {
anyhow::bail!("fork failed");
} else if pid == 0 {
// Child process - just sleep to allow tracing
println!("child : process started with pid: {}", process::id());
loop {
thread::sleep(Duration::from_secs(1));
}
} else {
// Parent process - spawn a thread to trace the child
println!("parent: forked child process spawned with pid: {}", pid);
// Give the child a moment to start
thread::sleep(Duration::from_millis(100));
println!("parent: attaching to child process {}", pid);
let traced_process = TracedProcess::attach(pid)?;
println!("parent: successfully attached to the process");
let frame = traced_process.next_frame()?;
println!("parent: successfully waited for a frame");
// Execute remote getpid in the child process
let (regs, _frame) = frame.invoke_remote(libc::getpid as usize, 0, &[])?;
println!("parent: successfully executed remote getpid");
let traced_pid = regs.return_value() as pid_t;
println!("parent: the return value (Traced Pid) was {}", traced_pid);
// Clean up: kill the child process
println!("parent: killing child process");
libc::kill(pid, libc::SIGKILL);
libc::waitpid(pid, std::ptr::null_mut(), 0);
println!("parent: child process terminated");
}
}
Ok(())
}
```
示例输出
```
parent: process with pid: 26042
parent: forked child process spawned with pid: 26068
child : process started with pid: 26068
parent: attaching to child process 26068
parent: successfully attached to the process
parent: successfully waited for a frame
parent: successfully executed remote getpid
parent: the return value (Traced Pid) was 26068
parent: killing child process
parent: child process terminated
```
标签:AArch64, Android, DSL, Hakrawler, Ptrace, Rust, Shared Library Injection, SSH蜜罐, x86_64, 云资产清单, 内存安全, 内存操作, 动态库注入, 可视化界面, 系统编程, 网络流量审计, 调试接口, 进程注入, 远程函数调用, 逆向工程, 通知系统