Skip to content

Instantly share code, notes, and snippets.

@vtols
Created October 19, 2013 14:55
Show Gist options
  • Save vtols/7056976 to your computer and use it in GitHub Desktop.
Save vtols/7056976 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
typedef int (*ifunc)();
unsigned char icode[] = { 0x90,
0xB8,
0x00, 0x00, 0x00, 0x00,
0xC3
};
int main()
{
*(int *) (icode + 2) = 42;
void *xcode = mmap(0, sizeof(icode),
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON, 0, 0);
memcpy(xcode, icode, sizeof(icode));
ifunc fcode = (ifunc) xcode;
printf("%d\n", fcode());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment