Skip to content

Instantly share code, notes, and snippets.

@GabrielL
Created August 2, 2011 15:19
Show Gist options
  • Save GabrielL/1120401 to your computer and use it in GitHub Desktop.
Save GabrielL/1120401 to your computer and use it in GitHub Desktop.
Call me Later !
#include <err.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#define OFFSET_PARAM 0x2
#define OFFSET_ADDR (OFFSET_PARAM + 8 + 2)
void *jit_this_call(void (*f)(), void *arg)
{
char buf[] =
{
0x48, 0xbf, 0xef, 0xbe, 0xad, 0xde, 0xef, 0xbe, 0xad, 0xde, // mov $0xdeadbeefdeadbeef,%rdi
0x49, 0xbb, 0xfe, 0xca, 0xfe, 0xca, 0xfe, 0xca, 0xfe, 0xca, // mov $0xcafecafecafecafe,%r11
0x41, 0xff, 0xe3 // jmpq *%r11
};
char *addr = mmap(NULL, sizeof(buf), PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (addr == MAP_FAILED) {
err(EXIT_FAILURE, "lulz, ca a merde");
}
memcpy(addr, buf, sizeof(buf));
*(void**)(addr + OFFSET_PARAM) = arg;
*(void**)(addr + OFFSET_ADDR) = (void*)f;
mprotect(addr, sizeof(buf), PROT_EXEC);
return addr;
}
static void byebye(int *lol)
{
printf("bye : %p \n", lol);
}
static void on_int(int i)
{
exit(EXIT_SUCCESS);
}
int main()
{
printf("%d\n", getpid());
void *wut_func = jit_this_call(byebye, (void *)0x42);
atexit(wut_func);
signal(SIGINT, on_int);
for (;;)
pause();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment