Created
January 5, 2013 14:42
-
-
Save akoskovacs/4461856 to your computer and use it in GitHub Desktop.
Self modifing code in C.
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 <string.h> | |
#include <stdlib.h> | |
#include <sys/mman.h> | |
typedef int (*func_t)(void); | |
int main(int argc, char *argv[]) | |
{ | |
char *fstart = "\x55\x48\x89\xe5\x48\x31\xc0"; | |
char *incrax = "\x48\xff\xc0"; | |
char *fend = "\x5d\xc3"; | |
int icnt = 5; | |
int i; | |
char *text = mmap(NULL, 4096, PROT_READ|PROT_WRITE|PROT_EXEC | |
, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); | |
if (argv[1]) { | |
icnt = atoi(argv[1]); | |
} | |
strcpy(text, fstart); | |
for (i = 0; i < icnt; i++) { | |
strcat(text, incrax); | |
} | |
strcat(text, fend); | |
func_t fn = (func_t)text; | |
printf("%d\n", fn()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment