Skip to content

Instantly share code, notes, and snippets.

@xmpf
Created February 18, 2021 09:00
Show Gist options
  • Save xmpf/ef8ddb3d73ca9d2734190840edec47da to your computer and use it in GitHub Desktop.
Save xmpf/ef8ddb3d73ca9d2734190840edec47da to your computer and use it in GitHub Desktop.
test and verify shellcode
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
const char shellcode[] =
"\x48\x31\xf6\x56\x48\xbf\x2f\x62\x69\x6e\x2f\x2f\x73\x68\x57\x54\x5f\x6a\x3b\x58\x99\x0f\x05";
int main(void)
{
long sclen = sizeof(shellcode) / sizeof(shellcode[0]);
printf("Length: %ld bytes\n", sclen - 1);
// query page size
long pagesz = sysconf(_SC_PAGESIZE);
printf("Page size = %ld\n", pagesz);
// allocate a page in memory
errno = 0;
void * buff = pvalloc(pagesz);
if (NULL == buff) {
perror("pvalloc");
exit(1);
}
// int mprotect(void *addr, size_t len, int prot);
errno = 0;
if ( mprotect(buff, pagesz, PROT_READ | PROT_WRITE | PROT_EXEC) ) {
perror("mprotect");
exit(1);
}
// copy the shellcode in executable area
memcpy(buff, shellcode, sclen);
// execute shellcode
(*(void(*)()) buff)();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment