Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <errno.h>
int main(int argc, char **argv)
{
pid_t pid;
unsigned long addr1;
int buf;
pid = 11316;
addr1 = 0x00CEF5D0;
ptrace(PTRACE_ATTACH, pid, NULL, NULL);
wait(NULL);
buf = ptrace(PTRACE_PEEKDATA, pid, (void *)addr1, NULL);
printf("pid: %d, addr: %lx, value: %d \n", pid, addr1, buf);
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return(0);
}
just a prove of concept code, it should work.
As I wrote earlier, I switched to python so I don't have more C code. I might consider sharing some python code if someone find it interesting.
ptrace gives possibility to read/write program data, as well as code, read and manipulation of registers. One of possible scenarios is to stop program execution, store processor context (registers), inject some code into stack (reading esp we have stack pointer), redirecting program execution (eip - register) restore processor context and resume program execution