...

La protezione dai memory error exploit

by user

on
Category: Documents
15

views

Report

Comments

Transcript

La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
La protezione dai memory error exploit
Giampaolo Fresi Roglia
Università degli Studi di Milano
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Sommario
Introduzione
1
Stack Guard
Terminator Canaries
Random Canaries
2
Pro Police (SSP)
3
Address Space Layout Randomization
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Introduzione
Buffer Overflow
Buffer Overflow
Condizione anomala.
Memorizzazione di dati su buffer oltre i limiti della sua
capienza.
Sovrascrittura dati adiacenti.
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Introduzione
Buffer Overflow [2]
Buffer Overflow [2]
In determinate condizioni è possibile:
Indurre un crash dell’applicazione.
Modificarne il comportamento.
Forzare l’esecuzione di codice arbitrario.
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Introduzione
Indurre crash
Esempio
#i n c l u d e <s t d i o . h>
#i n c l u d e <s t r i n g . h>
i n t foo ( char ∗ i n )
{
char buf [ 5 1 2 ] ;
s t r c p y ( buf , i n ) ;
return 0;
}
i n t main ( i n t a r g c , c h a r ∗∗ a r g v )
{
return foo ( argv [ 1 ] ) ;
}
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Introduzione
Layout stack
080483a0 <main>:
<...>
80483b5:
call
80483ba:
leave
80483bb:
ret
08048370 <foo>:
8048370:
8048371:
8048373:
8048379:
804837c:
8048380:
8048386:
8048389:
804838e:
804838f:
8048391:
push
mov
sub
mov
mov
lea
mov
call
leave
xor
ret
return address
8048370 <foo>
%ebp
%esp,%ebp
$0x218,%esp
0x8(%ebp),%eax
%eax,0x4(%esp)
-0x208(%ebp),%eax
%eax,(%esp)
80482b8 <strcpy@plt>
frame pointer
buffer
%eax,%eax
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Introduzione
Modificarne il comportamento
esempio [2]
#i n c l u d e <s t d i o . h>
#i n c l u d e <s t d l i b . h>
int check user ( void )
{
return 0;
}
i n t main ( i n t a r g c , c h a r ∗∗ a r g v )
{
i n t admin ;
char buf [ 5 1 2 ] ;
admin=c h e c k u s e r ( ) ;
s t r c p y ( buf , a r g v [ 1 ] ) ;
i f ( admin == 1 )
system ( ’ / b i n / sh ’ ) ;
else
return 0;
}
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Introduzione
Eseguire codice arbitrario
Tipico payload
nop sled.
shellcode.
retaddress.
<nop><nop><nop> ... <shellcode><ret><ret><ret> ..
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
Canarini
Canarini
Miniere di carbone fine ’800
Evitare stragi in miniera
“sentono” prima degli uomini le fughe di gas
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
Canarini digitali [1]
#i n c l u d e <s t d i o . h>
#i n c l u d e <s t d l i b . h>
i n t main ( i n t a r g c , c h a r ∗∗ a r g v )
{
i n t canary ;
c a n a r y = 0 x01010101 ;
char buf [ 5 1 2 ] ;
s t r c p y ( buf , a r g v [ 1 ] ) ;
i f ( c a n a r y != 0 x01010101 )
e x i t ( EXIT FAILURE ) ;
return 0;
}
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
Canarini digitali [2]
#i n c l u d e <s t d i o . h>
#i n c l u d e <s t d l i b . h>
i n t main ( i n t a r g c , c h a r ∗∗ a r g v )
{
i n t canary ;
c a n a r y = 0 x00010101 ;
char buf [ 5 1 2 ] ;
s t r c p y ( buf , a r g v [ 1 ] ) ;
i f ( c a n a r y != 0 x00010101 )
e x i t ( EXIT FAILURE ) ;
return 0;
}
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
Canarini digitali [3]
#i n c l u d e <s t d i o . h>
#i n c l u d e <s t d l i b . h>
i n t main ( i n t a r g c , c h a r ∗∗ a r g v )
{
i n t canary ;
c a n a r y = 0 x00010101 ;
char buf [ 5 1 2 ] ;
gets ( buf ) ;
i f ( c a n a r y != 0 x00010101 )
e x i t ( EXIT FAILURE ) ;
return 0;
}
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
scelta dei canarini
valore del canarino
ostacolo agli exploit
maggior numero di funzioni bloccate
strcpy, gets,...
terminator canary: “0x000aff0d”
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
prologo ed epilogo
prologo
return address
0804836b <foo>:
804836b:
push
8048370:
push
8048371:
mov
canary
$0x000aff0d
%ebp
%esp,%ebp
frame pointer
epilogo
8048389:
804838e:
804838f:
8048396:
8048398:
804839a:
call
leave
cmpl
jne
xor
ret
80482b8 <strcpy@plt>
buffer
$0x000aff0d, (%esp)
canary_changed
%eax,%eax
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
Terminator canaries: problemi
problemi
canarino noto
non blocca tutte le funzioni (read)
frame pointer non protetto
variabili locali non protette
“finestra” di codice in cui agire
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
canarini random
canarini random
variabile globale
inizializzata all’avvio del processo
xor con return address
difficile da ricavare (a meno di casi particolari)
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
Random canaries: problemi
risolvono:
canarino noto
non blocca tutte le funzioni (read)
non risolvono:
frame pointer non protetto
variabili locali non protette
“finestra” di codice in cui agire
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
esempio:
codice vulnerabile
int func(char *msg)
{
char buf[80];
strcpy(buf, msg);
...
strcpy(msg, buf);
}
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Terminator Canaries
Random Canaries
esempio:
codice vulnerabile [2]
#include <stdio.h>
#include <string.h>
void bar(void)
{
printf("Hello World!\n");
}
int foo(char *in)
{
volatile int canary;
void (*f)(void);
char buf[512];
canary = 0x000aff0d;
f = bar;
strcpy(buf, in);
f();
if(canary != 0x000aff0d)
{
printf("smash detected!\n");
exit(0);
}
return 0;
}
int main(int argc, char **argv)
{
return foo(argv[1]);
}
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Pro police (SSP)
Pro police (SSP)
Hiroaki Etoh (IBM, 2000)
canarini random
controllo piu’ granulare
riordinamento variabili
integrato in gcc 4
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
riordinamento variabili
riordinamento variabili
canarini posti dopo frame pointer
array e strutture contenenti array posti “in alto”
restanti variabili locali appena sotto gli array
controllo canarini piu’ granulare
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
SSP layout
SSP layout
overflow non influenzano var
locali
parametri
retaddr
frame pointer
frame pointer protetto
canary
ret addr protetto
arrays
parametri protetti
check dei canarini dopo
operazioni su array
Giampaolo Fresi Roglia
local vars
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
ASLR
ASLR obiettivi:
complicare lavoro attaccante
indirizzi di shellcode difficili da indovinare
overhead limitato
difetti:
non tutto e’ randomizzabile (plt e testo)
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
ASLR
cosa si randomizza
posizione dello stack
posizione dello heap
indirizzo di rilocazione librerie
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Stack Guard
Pro Police (SSP)
Address Space Layout Randomization
Problemi risolti e non:
Problemi risolti:
esecuzione shellcode su stack o heap
modifica GOT piu’ difficile (ret to libc)
Non risolti:
ret to libc ancora possibile
non control data attacks
format bugs
Giampaolo Fresi Roglia
La protezione dai memory error exploit
Fly UP