Skip to content

Instantly share code, notes, and snippets.

@deanrock
Created November 3, 2025 22:36
Show Gist options
  • Select an option

  • Save deanrock/27084d2bda88a22051b07f49620f0efb to your computer and use it in GitHub Desktop.

Select an option

Save deanrock/27084d2bda88a22051b07f49620f0efb to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/syscall.h>
int main()
{ pid_t child;
long orig_eax, eax;
long params[3];
int status;
int insyscall = 0;
struct user_regs_struct regs;
child = fork();
if(child == 0) {
ptrace(PTRACE_TRACEME, 0, NULL, NULL);
execl("/bin/ls", "ls", NULL);
}
else {
while(1) {
wait(&status);
if(WIFEXITED(status))
break;
orig_eax = ptrace(PTRACE_PEEKUSER,
child, 8 * 15,
NULL);
printf("after wai %ld\n", orig_eax);
if(orig_eax == SYS_write) {
if(insyscall == 0) {
/* Syscall entry */
insyscall = 1;
ptrace(PTRACE_GETREGSET, child,
NULL, &regs);
printf("Write called with \n");
}
else { /* Syscall exit */
eax = ptrace(PTRACE_PEEKUSER,
child, 8 * 15,
NULL);
printf("Write returned "
"with %ld\n", eax);
insyscall = 0;
}
}
ptrace(PTRACE_SYSCALL, child,
NULL, NULL);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment