Skip to content

Instantly share code, notes, and snippets.

@fikovnik
Created February 5, 2024 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fikovnik/d18fcd9d64d48c210889846028dd49a8 to your computer and use it in GitHub Desktop.
Save fikovnik/d18fcd9d64d48c210889846028dd49a8 to your computer and use it in GitHub Desktop.
Intercept syscalls of a child process using ptrace
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/reg.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
int status;
pid_t child_pid = fork();
if (child_pid == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("/bin/ls", "ls", NULL);
} else {
for (;;) {
waitpid(child_pid, &status, WCONTINUED);
if (WIFEXITED(status))
break;
long syscall_id = ptrace(PTRACE_PEEKUSER, child_pid, 8 * ORIG_RAX, NULL);
printf("Syscall %ld\n", syscall_id);
ptrace(PTRACE_SYSCALL, child_pid, NULL, NULL);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment