Skip to content

Instantly share code, notes, and snippets.

@koturn
Created April 6, 2017 23:36
Show Gist options
  • Save koturn/09a10c75da66e6530f9c39a6ad0d0886 to your computer and use it in GitHub Desktop.
Save koturn/09a10c75da66e6530f9c39a6ad0d0886 to your computer and use it in GitHub Desktop.
x64なHello World
#include <unistd.h>
#include <sys/mman.h>
static const unsigned char code[] =
// mov $0x01,%rax
"\x48\xc7\xc0\x01\x00\x00\x00"
// mov $0x0d,%edx
"\xba\x0d\x00\x00\x00"
// mov $0x01,%edi
"\xbf\x01\x00\x00\x00"
// callq
"\xe8\x0a\x00\x00\x00"
// add $0x0f,%rsi
"\x48\x81\xc6\x0f\x00\x00\x00"
// syscall
"\x0f\x05"
// ret
"\xc3"
// (%rsp),%rsi
"\x48\x8b\x34\x24"
// ret
"\xc3"
"Hello World!\n";
int
main(void)
{
unsigned long page_size = (unsigned long) (sysconf(_SC_PAGESIZE) - 1);
mprotect((void *) code, (sizeof(code) + page_size) & ~page_size, PROT_READ | PROT_EXEC);
((void (*)()) (unsigned char *) code)();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment