monitor each write system call of child procces via ptrace. I need to know file name and…
- User
monitor each write system call of child procces via ptrace. I need to know file name and amount of bytes being written on each write
- Assistant
monitor each write system call of child procces via ptrace. I need to know file name and amount of bytes being written on each write
To monitor each write system call of a child process via ptrace, you can follow these steps in a Linux environment:
ptrace to trace the child process.write syscall entry and exit points./proc/[pid]/fd/ directory.write syscall.Below is an example program written in C that demonstrates how to do this:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/reg.h>
#include <string.h>
#include <fcntl.h>
#ifdef __x86_64__
#define SYSCALL_REG orig_rax
#define RETURN_REG rax
#define PARAM1_REG rdi
#define PARAM2_REG rsi
#define PARAM3_REG rdx
#else
#define SYSCALL_REG orig_eax
#define RETURN_REG eax
#define PARAM1_REG ebx
#define PARAM2_REG ecx
#define PARAM3_REG edx
#endif
const int long_size = sizeof(long);
void get_filename_from_fd(pid_t child_pid, int fd, char* filename, size_t filename_len) {
char fd_path[256];
snprintf(fd_path, sizeof(fd_path), "/proc/%d/fd/%d", child_pid, fd);
ssize_t len = readlink(fd_path, filename, filename_len - 1);
if (len > 0) {
filename[len] = '\0';
} else {
strcpy(filename, "unknown");
}
}
int main() {
pid_t child;
long orig_rax;
int status;
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;
ptrace(PTRACE_GETREGS, child, NULL, ®s);
orig_rax = regs.SYSCALL_REG;
if(orig_rax == SYS_write) {
int fd = regs.PARAM1_REG;
size_t count = regs.PARAM3_REG;
char filename[1024];
if(ptrace(PTRACE_PEEKUSER, child, 8 * PARAM2_REG, NULL) == -1) {
perror("PTRACE_PEEKUSER");
return 1;
}
get_filename_from_fd(child, fd, filename, sizeof(filename));
printf("Write called with fd %d, file %s, %zu bytes\n", fd, filename, count);
}
ptrace(PTRACE_SYSCALL, child, NULL, NULL);
}
}
return 0;
}
Please note the following points about this example code:
SYS_write and register macros might differ depending on the system and should match the system calling conventions./bin/ls; you can replace this with any other command or a different executable.write system call. To actually see what is being written, you'll need to read the data from the child's memory, which can be a more complex task involving multiple ptrace calls to read memory.Compile the program with gcc -o ptrace_write_monitor ptrace_write_monitor.c and then run the program. You will need to run this with proper permissions (often as root) since ptrace can be restricted on some systems for security reasons.
Keep in mind that tracing system calls can be quite an invasive procedure, and doing so in a production environment should be carried out with caution.