Skip to content

Instantly share code, notes, and snippets.

@nathiss
Created April 18, 2018 20:45
Show Gist options
  • Save nathiss/8f867ad5b6d8d83f902507259741c11a to your computer and use it in GitHub Desktop.
Save nathiss/8f867ad5b6d8d83f902507259741c11a to your computer and use it in GitHub Desktop.
Simple shellcode executor for Linux(-like) systems.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
void* alloc_mem(size_t size) {
void *ptr = mmap(0, size, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1 ,0);
if(ptr == MAP_FAILED)
return NULL;
return ptr;
}
FILE* open_file(char *path) {
FILE* ptr = fopen(path, "rb");
return ptr;
}
size_t get_size(FILE *file) {
fseek(file, 0L, SEEK_END);
size_t size = ftell(file);
rewind(file);
return size;
}
int main(int argc, char** argv) {
if(argc < 2) {
puts("Usage: executor [file]");
return 1;
}
FILE *file = open_file(argv[1]);
if(file == NULL) {
puts("Can not open file.");
return 2;
}
size_t fs = get_size(file);
void *mem = alloc_mem(fs);
if(mem == NULL) {
puts("Can not allocate memory.");
return 3;
}
fread(mem, fs, 1, file);
fclose(file);
((void (*)())mem)();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment