Created
April 18, 2018 20:45
-
-
Save nathiss/8f867ad5b6d8d83f902507259741c11a to your computer and use it in GitHub Desktop.
Simple shellcode executor for Linux(-like) systems.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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