百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

ptrace_do

> 编程语言
开源

一个用于在 Linux 中轻松注入系统调用的 ptrace 库。

187 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个用于在 Linux 中轻松注入系统调用的 ptrace 库。

ptrace_do

ptrace_do is a ptrace library designed to simplify syscall injection in Linux.

What is ptrace?

ptrace is the debugging interface provided by the Linux kernel. It allows you to connect to a running process, examine and alter its memory, and change it's runtime state. Unfortunately, it's quite complex and requires a solid understanding of the underlying architecture and OS. ptrace_do was written to allow pentesters access to a simplified interface for injecting syscalls into a target process.

The best introduction to ptrace that I've seen comes in the form of two articles by Pradeep Padala dating back to 2002:

  • Playing with ptrace, Part I
  • Playing with ptrace, Part II

That's awesome! 1337 h4X0rZ rUL3!!

Sorry, no. This isn't an "exploit". This code only uses standard, though not commonly understood, interfaces for process interaction and control. In order to affect a process you don't already own, you will need to have the CAP_SYS_PTRACE capability (i.e. root).

Can I use this on any Linux host?

Currently, ptrace_do will only work on x86_64 Linux. Because it uses the Linux ptrace interface to inject assembly language syscalls into a target process, nothing here is portable. I did try to keep it as modular as possible, and I would consider porting it to another architecture if it became popular enough.

Usage

Example: Injecting "exit(42);"

struct ptrace_do *target;      

target = ptrace_do_init(TARGET_PID);
ptrace_do_syscall(target, _NR_exit, 42, 0, 0, 0, 0, 0);

Example: Injecting open / dup2 / close calls to hijack stdin / stdout / stderr.

char *buffer;
struct ptrace_do *target;
void *remote_addr;
int fd;

target = ptrace_do_init(TARGET_PID);
buffer = (char *) ptrace_do_malloc(target, BUFF_SIZE);
memset(buffer, 0, BUFF_SIZE);
snprintf(buffer, BUFF_SIZE, "/dev/pts/4");
remote_addr = ptrace_do_push_mem(target, buffer);
fd = ptrace_do_syscall(target, __NR_open, remote_addr, O_RDWR, 0, 0, 0, 0);
ptrace_do_syscall(target, __NR_dup2, fd, 0, 0, 0, 0, 0);
ptrace_do_syscall(target, __NR_dup2, fd, 1, 0, 0, 0, 0);
ptrace_do_syscall(target, __NR_dup2, fd, 2, 0, 0, 0, 0);
ptrace_do_syscall(target, __NR_close, fd, 0, 0, 0, 0, 0);
ptrace_do_cleanup(target);

For a more advanced usage, please examine my shelljack code, for which this library was written to accomidate.

Documentation

Here is the brief list of function interfaces. These functions are documented in greater detail within the source.

bash
/* ptrace_do_init() hooks the target and prepares it to run our commands. */
struct ptrace_do *ptrace_do_init(int pid);

/* ptrace_do_malloc() allocates memory in the remote process for our use, without worry of upsetting the remote memory state. */
void *ptrace_do_malloc(struct ptrace_do *target, size_t size);

/* ptrace_do_free() frees a joint memory object. "operation" refers to the FREE_* modes above. */
void ptrace_do_free(struct ptrace_do *target, void *local_address, int operation);

/* ptrace_do_push_mem() and ptrace_do_pull_mem() synchronize the memory states between local and remote buffers. */
void *ptrace_do_push_mem(struct ptrace_do *target, void *local_address);
void *ptrace_do_pull_mem(struct ptrace_do *target, void *local_address);

/* Short helper function to translate your local address to the remote one. */
void *ptrace_do_get_remote_addr(struct ptrace_do *target, void *local_addr);

/* ptrace_do_sig_ignore() sets the signal mask for the remote process. */
/* This is simple enough, we only need a macro. */
/* Note, this is for *our* handling of remote signals. This won't persist once we detatch. */
#define ptrace_do_sig_ignore(TARGET, SIGNAL)  TARGET->sig_ignore |= 1<<SIGNAL

/* ptrace_do_syscall() will execute the given syscall inside the remote process. */
unsigned long ptrace_do_syscall(struct ptrace_do *target, unsigned long rax, \
unsigned long rdi, unsigned long rsi, unsigned long rdx, unsigned long r10, unsigned long r8, unsigned long r9);

/* ptrace_do_cleanup() will detatch and do it's best to clean up the data structures. */
void ptrace_do_cleanup(struct ptrace_do *target);

/* get_proc_pid_maps() processes the maps file and returns the created object.*/
struct parse_maps *get_proc_pid_maps(pid_t target);

/* free_parse_maps_list() destroys a parse_maps object chain. */
void free_parse_maps_list(struct parse_maps *head);

/* Mostly for debugging, but in case it comes in handy, this function prints the parse_maps object members. */
void dump_parse_maps_list(struct parse_maps *head);

Installation

bash
git clone https://github.com/emptymonkey/ptrace_do.git
cd ptrace_do
make

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言