Help with mprotect and writing on code section
I am trying to write on the code section of an executing C program. I tried using mprotect to get PROT_WRITE access to a section in memory and tried to write bytecode on it. It doesnt seem to write on that section. The code is:
The first print returns: ccccc3c9 8a14097.
#define PAGESIZE 4096
unsigned char buffer;
unsigned long *p;
unsigned char c;
unsigned long *d;
p = ␣
p = (unsigned long *)(((int) p + PAGESIZE-1) & ~(PAGESIZE-1));
perror("Could not mprotect");
p = 0xccccc3c9; // basically I was trying to make it return without executing
p = 0x08a14097;
printf(" written values %x %x",p,p);
printf(" written values %x %x",d,d);
unsigned char *p;
p = &main;
buffer[index++] = p[i];
The second print returns: 83e58955 45c718ec // these are the values in blank() on objdump of the binary.
Can someone help me on this, or is there any easier way to make a running program write byte code onto itself?