Last active
April 26, 2023 17:09
-
-
Save martinjacobd/3ee56f3c7b7ce621034ec3ecbc8e13f1 to your computer and use it in GitHub Desktop.
This file contains 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 <error.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <sys/mman.h> | |
int convert_to_hex(char c) | |
{ | |
if (c >= '0' && c <= '9') { | |
return c - '0'; | |
} else if (c >= 'A' && c <= 'F') { | |
return c - 'A' + 10; | |
} else if (c >= 'a' && c <= 'f') { | |
return c - 'a' + 10; | |
} else { | |
error_at_line(-1, 0, __FILE__, __LINE__, "illegal char\n"); | |
return -1; | |
} | |
} | |
#define BUF_SIZE 1024 | |
int main(int argc, char *argv[]) | |
{ | |
char buffer[BUF_SIZE]; | |
char *write_region = mmap(NULL, | |
getpagesize(), | |
PROT_READ | PROT_WRITE, | |
MAP_SHARED | MAP_ANONYMOUS, | |
-1, | |
0); | |
if (write_region == NULL) { | |
error_at_line(-ENOMEM, errno, __FILE__, __LINE__, "couldn't allocate\n"); | |
} | |
fgets(buffer, sizeof(buffer), stdin); | |
for (size_t i = 0; buffer[i * 2 + 1] != '\0'; i++) { | |
write_region[i] = 16 * convert_to_hex(buffer[i*2]) + convert_to_hex(buffer[i*2 + 1]); | |
} | |
mprotect(write_region, getpagesize(), PROT_READ | PROT_EXEC); | |
int (*jmp_func)(void) = (void *) write_region; | |
printf("%x\n", jmp_func()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment