Created
February 1, 2014 10:17
-
-
Save vdudouyt/8750392 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/perl -n | |
| chomp; | |
| next if !s/^\x20{0,3}\S+:\s*//; | |
| my ($code, $listing) = split(/\s*\t\s*/, $_, 2); | |
| $code =~ s/(^|\s)+/\\x/g; | |
| $spaces = ' ' x (30 - length($code)); | |
| print "\"$code\"$spaces// $listing\n"; |
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 <string.h> | |
| #include <malloc.h> | |
| #include <assert.h> | |
| #include <sys/mman.h> | |
| /* This code was generated by using the following commands: | |
| * $ gcc hello.c -c | |
| * $ objdump -rD hello.o -j .text | perl format_as_c_string.pl | |
| */ | |
| char program[] = | |
| "\x55" // push %ebp | |
| "\x89\xe5" // mov %esp,%ebp | |
| "\x83\xec\x18" // sub $0x18,%esp | |
| "\xc7\x04\x24\x00\x00\x00\x00" // movl $0x0,(%esp) | |
| "\xe8\xfc\xff\xff\xff" // call e <print1+0xe> | |
| "\xc9" // leave | |
| "\xc3" // ret | |
| ; | |
| int main() { | |
| char *hello = "Hello world"; | |
| assert(sizeof(void*) == 4); // IA-32 only | |
| /* Allocating memory */ | |
| char *code = mmap(NULL, sizeof(program), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); | |
| memcpy(code, program, sizeof(program)); | |
| /* R_386_PC32 relocation */ | |
| void * puts_addr_rel = (void*) ((void*) puts - (void*) &(code[18])); | |
| memcpy(&(code[14]), &puts_addr_rel, sizeof(void*)); | |
| /* R_386_32 relocation */ | |
| memcpy(&(code[9]), &hello, sizeof(void*)); | |
| /* Firing the code execution */ | |
| assert(mprotect(code, sizeof(program), PROT_READ | PROT_EXEC) == 0); | |
| (*(void (*)()) code)(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment