module noy sécurité des systèmes · 1/19/18 2 module noy 3 sécurité dans les systèmes : les...
Post on 08-Aug-2020
0 Views
Preview:
TRANSCRIPT
1/19/18
1
Module NOY
Sécurité des systèmes
• Les bases• Droits SUID et SGID et sticky bits• Buffer overflow et contre-mesures
Module NOY 2
Sécurité dans les systèmes : les bases
¢ Modes d’exécution du processeurl Mode superviseur vs mode utilisateurl Protection des données du noyau + drivers
¢ Protection physique entre les processus utilisateurl Réalisée grâce à la paginationl Empêche les corruptions intentionnelles ou
accidentelles de la mémoire des autres processus
1/19/18
2
Module NOY 3
Sécurité dans les systèmes : les bases
¢ Droits d’accès aux fichier (et autres) - Unixl Par objet, droits pour Utilisateur, Groupe, Autresl Type : r (read), w (write), x (execute)l Exemple
• ls -l-rwxr-x--- puaut teacher [taille date] nom-fichier
l Un programme s’exécute avec les droits du processus qui exécute le fichier
l NB: [taille date] non mentionnés dans la suite
Module NOY 4
Sécurité dans les systèmes : les bases
¢ Listes de contrôle d’accès (ACL) superposées (positif ou négatif) aux droits de basel Droits d’accès plus fins l Un fichier disposant d'une liste d'ACLs sera présenté
avec un "+" en bout de ligne-rwxr--r--+
l Commandes spécifiques pour modifier les ACLsetfacl -m u:puaut:rw /var/www/index.php(-m : modification, rw donnes les droits rw et implicitement ôte le droit x)
1/19/18
3
Module NOY 5
Droits SUID
¢ Pour les fichiers exécutables¢ Exécution avec les droits du propriétaire du fichier
(et pas de celui qui exécute)l Exécution en root si le fichier est root⇒ Deux user id : real user id et effective user id (propriétaire du fichier quand on exécute un binaire suid)
¢ Identificationl s (ou S) à la place du x (droit d’exécution)
• -r-s--x--x (s à la place du x de l’utilisateur)¢ Positionnement : sudo chmod u+s fichier¢ Recherche : find / -perm -u+s
5
Module NOY 6
Droits SUID (suite)
¢ Exemple 1 : gestion des mots de passel -rw-r--r-- 1 root sys /etc/passwd
(fichier de passwd, appartient à root, il faut être root pour le modifier)
l -rws--x—x root sys /usr/bin/passwd (commande)l Pas de droit de modification par l’utilisateur, droit
accordé uniquement quand on exécute la commande
6
1/19/18
4
Module NOY 7
Droits SUID (suite)
¢ Exemple 2 : programmation d’activités l Fichier de programmation /etc/crontab, protégé en
écriture pour l’utilisateur lambdal Commande SUID /usr/bin/crontab, modifiant
/etc/crontab¢ Exemple 3 : mount
-rwsr-xr-x 1 root bin 68804 /bin/mountMontage de systèmes de gestion de fichiers
7
Module NOY 8
Droits SGID
¢ Fonctionne comme le droit SUID, mais appliqué aux groupes
¢ Pour exécutables et répertoires¢ Exécutables
l Exécution avec le droit du groupe de l’exécutablel Exemple :
-rwxr-sr-x. 1 root tty /usr/bin/writeexécution avec le droit du groupe tty
8
1/19/18
5
Module NOY 9
Droits SGID
¢ Répertoiresl Un fichier créé dans le répertoire SGID aura pour
groupe celui du répertoirel Exemple : partage de fichiers
9
Module NOY 10
Droits SUID et SGID
¢ Problèmes de sécuritél Faille dans un programme SUID (ex : injection de
code par débordement de buffer)• L’usager non-root a les droits root
l Attaque sushi (su shell)• Création par un intrus d’un shell script SUID avec droit
d’écriture à tous les utilisateurs• Par modification du contenu du fichier, tous les
utilisateurs exécutent leurs commandes avec l’accès root
10
1/19/18
6
Module NOY 11
Droits SUID et SGID
¢ Solutionsl Attaques sushi
• Linux : exécution des shell scripts sans tenir compte du bit SUID
l Limiter le nombre des programmes SUID(pour les lister : find / -perm -u+s)
l Supprimer le mode SUID selon l’utilisation : option suid/nosuid de certaines commandes
• mount –nosuid sur les clients NFS : empêche la modification ou l’utilisation des fichiers SUID sur la partition montée
11
Module NOY 12
Sticky bit
¢ Pour exécutables et répertoires¢ Rôle : protection contre la destruction¢ Identification
l t à la place du x du groupe otherl drwxrwxrwt 11 root root tmp/
¢ Répertoirel Seul le propriétaire d’un fichier peut supprimer le fichier
¢ Exécutablel Ne doit pas être déchargé de RAM/swap, pour
réexécution ultérieure plus rapide¢ Utilisations
l /tmp : accessible à tous, sans possibilité de supprimer les fichiers des autres 12
1/19/18
7
Credits: VU Amsterdam (author)Modified presentation of memory layout (stack going up)
Added explanation to feel more comfortable with the slides
Module NOY 14
Outline
¢ Buffer overflowl The basicsl How to make it really work (shellcode encryption)
¢ Countermeasuresl Avoid buffer overflowsl Compiler-directed techniques: canariesl Write xor Executel Address Space Randomization (ASLR)
1/19/18
8
Module NOY 15
Memory Layout of a Process
.text.data.bssheap
argcENV / ARG pointersENV / ARG strings
0x08048000
0xbfffffff
Stack
Module NOY 16
¢ Source codeIE (){
getURL ();}
¢ Assembly code0x08048428 <+0>: push %ebp
0x08048429 <+1>: mov %esp,%ebp0x0804842b <+3>: call 0x8048404 <getURL>
0x08048430 <+8>: pop %ebp0x08048431 <+9>: ret
Zoom on a function
Start of function
Call to getURL
Return
1/19/18
9
Module NOY 17
¢ Source codeIE (){
getURL ();}
¢ Assembly code0x08048428 <+0>: push %ebp
0x08048429 <+1>: mov %esp,%ebp0x0804842b <+3>: call 0x8048404 <getURL>
0x08048430 <+8>: pop %ebp0x08048431 <+9>: ret
Zoom on a function
Store the stack pointer in frame pointer
Push the frame pointer(ebp) on the stack
Restore frame pointer
Returns to addresson top of stack
Module NOY 18
Zoom on another function
¢ The assembly code for getURL():0x08048404 <+0>: push %ebp0x08048405 <+1>: mov %esp,%ebp0x08048407 <+3>: sub $0x18,%esp0x0804840a <+6>: mov 0x804a014,%eax0x0804840f <+11>: movl $0x40,0x8(%esp)0x08048417 <+19>: lea -0xc(%ebp),%edx0x0804841a <+22>: mov %edx,0x4(%esp)0x0804841e <+26>: mov %eax,(%esp)0x08048421 <+29>: call 0x8048320
<read@plt>0x08048426 <+34>: leave0x08048427 <+35>: ret
Same entry codethan IE
Call to read
Allocate space for buf + push parameters
1/19/18
10
Module NOY 19
push %ebpmov %esp,%ebp
call getURLpop %ebp
ret
IE
(code for read)read
0x08048428
0x08048431
getU
RL
0x08048404
0x08048427
getURL (){
char buf[40]; read(stdin,buf,64);get_webpage (buf);
}IE (){
getURL ();}
So we have
push %ebpmov %esp,%ebp
sub $0x18,%espmov 0x804a014,%eax
movl $0x40,0x8(%esp)lea -0xc(%ebp),%edxmov %edx,0x4(%esp)
mov %eax,(%esp)call read
leaveret
Module NOY 20
push %ebpmov %esp,%ebp
call getURLpop %ebp
ret
IE
(code for read)read
0x08048428
0x08048431
getU
RL
0x08048404
0x08048427
getURL (){
char buf[40]; read(stdin,buf,64);get_webpage (buf);
}IE (){
getURL ();}
What aboutthe stack?
push %ebpmov %esp,%ebp
sub $0x18,%espmov 0x804a014,%eax
movl $0x40,0x8(%esp)lea -0xc(%ebp),%edxmov %edx,0x4(%esp)
mov %eax,(%esp)call read
leaveret
1/19/18
11
Module NOY 21
push %ebpmov %esp,%ebp
call getURLpop %ebp
ret
IE
(code for read)read
0x08048428
0x08048431
getU
RL
0x08048404
0x08048427
getURL (){
char buf[40]; read(stdin,buf,64);get_webpage (buf);
}IE (){
getURL ();}
What aboutthe stack?
push %ebpmov %esp,%ebp
sub $0x18,%espmov 0x804a014,%eax
movl $0x40,0x8(%esp)lea -0xc(%ebp),%edxmov %edx,0x4(%esp)
mov %eax,(%esp)call read
leaveret
0xbfffeedc 0x08048430
old FP
64(@buf)
fd
0xbfffeed0
0xbfffeecc
0xbfffeec8
0xbfffeec4
0xbfffeec0
0xbfffeebc
0xbfffeeb8
0xbfffeeb4
0xbfffeeb0
0xbfffeeac
0xbfffeea8
0xbfffeea4
0xbfffeed8
0xbfffeed4
4 bytes
Return address in IE
buf
Paramsto read
Module NOY 22
push %ebpmov %esp,%ebp
call getURLpop %ebp
ret
IE
(code for read)read
0x08048428
0x08048431
getU
RL
0x08048404
0x08048427
getURL (){
char buf[40]; read(stdin,buf,64);get_webpage (buf);
}IE (){
getURL ();}
And nowThe exploit!
push %ebpmov %esp,%ebp
sub $0x18,%espmov 0x804a014,%eax
movl $0x40,0x8(%esp)lea -0xc(%ebp),%edxmov %edx,0x4(%esp)
mov %eax,(%esp)call read
leaveret
0xbfffeedc 0x08048430
old FP
64(@buf)
fd
0xbfffeed0
0xbfffeecc
0xbfffeec8
0xbfffeec4
0xbfffeec0
0xbfffeebc
0xbfffeeb8
0xbfffeeb4
0xbfffeeb0
0xbfffeeac
0xbfffeea8
0xbfffeea4
0xbfffeed8
0xbfffeed4
4 bytes
Return address in IE
buf
Paramsto read
0xbfffeeb0
1/19/18
12
Module NOY 23
That is it, really
¢ All we need to do is stick our program in the buffer¢ Easy to do: attacker controls what goes in the
buffer! l And that program simply consists of a few
instructions
Module NOY 24
That is it, really
¢ We don’t even need to change the return address¢ Or execute any of our code
l Let’s have a look at an example, where the bufferoverflow changes only data…
1/19/18
13
Module NOY 25
Exploit against non control data
get_medical_info(){
boolean authorized = false;char name[10];authorized = check();read_from_network (name);
if (authorized)show_medical_info (name);
elseprintf (“sorry, not allowed”);
}
Module NOY 26
nam
e
Exploit against non control data
authorized=T
get_medical_info(){
boolean authorized = false;char name[10];authorized = check();read_from_network (name);
if (authorized)show_medical_info (name);
elseprintf (“sorry, not allowed”);
}
1/19/18
14
Module NOY 27
So all the attacker needs to do…
¢ ... is stick a program in the buffer or environment!l Easy: attacker controls what goes in the buffer! l What does such code look like?
Module NOY 28
Typical injection vector
¢ Shellcode address: l The address of the memory region that contains the shellcode
¢ Shellcode: l A sequence of machine instructions to be executed (e.g.
execve("/bin/sh"))¢ NOP sled:
l A sequence of do-nothing instructions (nop). It is used to ease the exploitation: attacker can jump anywhere inside, and will eventually reach the shellcode (optional)
NOP sled shellcodeaddress
of shellcode
1/19/18
15
Module NOY 29
How do you create the vector?
1. Create the shellcode2. Prepend the NOP sled:
perl -e 'print "\x90"' | ndisasm -b 32 –00000000 90 nop
3. Add the address0xbfffeeb0
setreuid
execve
why this?
00000000 31 C0 B0 46 31 DB 31 C9 1..F1.1.00000008 CD 80 EB 16 5B 31 C0 88 ....[1..00000010 43 07 89 5B 08 89 43 0C C..[..C.00000018 B0 0B 8D 4B 08 8D 53 0C ...K..S.00000020 CD 80 E8 E5 FF FF FF 2F ......./00000028 62 69 6E 2F 73 68 4E 41 bin/shNA00000030 41 41 41 42 42 42 42 00 AAABBBB.
NOPsled
shellcode address of shellcode
Module NOY 30
In reality, things are more complicated
Q: Why encoding is so frequently used?
Unpacker Encodedshellcode
NOPsled
shellcode address of shellcode
A: if strcpy() is used to overflow the buffer, it will stop when it encounters the nullbyte. So if the shellcode contains a null byte, the attacker has a problem. So the attacker may have to encode the shellcode to remove null bytes and then generatethem dynamically
1/19/18
16
Module NOY 31
That is, fundamentally, it.
¢ Let us see whether we understood this.
Module NOY 32
Can you exploit this?
1/19/18
17
Module NOY 33
Outline
¢ Buffer overflowl The basicsl How to make it really work (shellcode encryption)
¢ Countermeasuresl Avoid buffer overflowsl Compiler-directed techniques: canariesl Write xor Executel Address Space Randomization (ASLR)
Module NOY 34
¢ The best defense is proper bounds checkingl Bad practice to avoid (left) / good practice to recommend (right)
How do we stop the attacks?
char str[100];gets(str);
char str[100];fread(str,1,100,stdin);
char str2[SIZE];strcpy(str2,str1);
char tab1[100], tab2[50];memcpy(tab1,tab2,150);
#define SIZE 100char str[SIZE];fgets(str,SIZE,stdin);
#define SIZE 100char str[SIZE];fread(str,1,SIZE,stdin);
#define SIZE 100char tab1[SIZE],tab2[SIZE];memcpy(tab1,tab2,SIZE);
char str2[50];strncpy(str2,str1,50);
1/19/18
18
Module NOY 35
¢ String functions to use and avoid
How do we stop the attacks?
Don’t use these functions Use these instead
strcat strlcat
strcpy strlcpy
strncat strlcat
strncpy strlcpy
sprintf snprintf (see note) or asprintf
vsprintf vsnprintf (see note) or vasprintf
gets fgets (see note) or use Core Foundation or Foundation APIs
Module NOY 36
¢ Avoid hard-coded buffer sizes
How do we stop the attacks?
Instead of this Do this
char buf[1024];...if (size <= 1023) {...}
#define BUF_SIZE 1024......if (size < BUF_SIZE) {...}
char buf[1024];...if (size < 1024) {...}
char buf[1024];...if (size < sizeof(buf)) {...}
1/19/18
19
Module NOY 37
¢ Avoid unsafe concatenation
How do we stop the attacks?
Instead of this Do this
{char file[MAX_PATH];...addsfx(file);...}static *suffix = ".ext";char *addsfx(char *buf){return strcat(buf, suffix);}
{char file[MAX_PATH];...addsfx(file, sizeof(file));...}static *suffix = ".ext";size_t addsfx(char *buf, uint size){size_t ret = strlcat(buf, suffix, size);if (ret >= size) {fprintf(stderr, "Buffer too small....\n");}return ret;}
Module NOY 38
¢ Other helpsl Compiler warnings when using some
unsafe functions: activate all warningsl Activate all warningsl Use memory checking tools like Valgrind
¢ This said, many C/C++ programmers and some are bound to forget
è Are there any system defenses that can help?
How do we stop the attacks?
1/19/18
20
Module NOY 39
How do we stop the attacks?
¢ A variety of tricks in combination
Canaries NX bit ASLR
Module NOY 40
¢ Goal: make sure we detect overflow of return addressl The functions' prologues insert a canary on the stackl The canary is a 32-bit value inserted between the
return address and local variables¢ Types of canaries
1. Terminator2. Random3. Random XOR
¢ The epilogue checks if the canary has been altered¢ Drawback: requires recompilation
A. Compiler-level techniques : canaries
1/19/18
21
Module NOY 41
Canaries
return address
frame pointer
canary
local variables
0xbfffffff
stac
k gr
ow u
pwar
ds
Top of the stack
Module NOY 42
¢ Types of canariesl Terminator
• Built on null terminators, CR, LF and -1• Drawback: canary value is known
l Random• Generated randomly at program initialization and stored in a
global variable, padded by unmapped pages• Can still be read if the attacker knows where it is
l Random XOR• Random xored with all or part of control data (e.g. return
address) ¢ gcc:
l -fstack-protector (default) –fno-stack-protector, -fstack-protector-all
l -fstack-protector : protects functions calling alloca and functionwwith buffers larger than 8 bytes
A. Compiler-level techniques : canaries
1/19/18
22
Module NOY 43
B. DEP / NX bit / W⊕X
¢ Idea: separate executable memory locations from writable onesl A memory page cannot be both writable and
executable at the same timel Ensured using protections in page tables
¢ “Data Execution Prevention (DEP)”
Module NOY 44
Bypassing W⊕X Return-oriented programming
¢ Principlel Do not put code to execute in the stackl Instead, smash return address to branch to existing code
¢ Three assumptions:l We can manipulate a code pointerl The stack is writablel We know the address of a “suitable" library function (e.g.,
system())
1/19/18
23
Module NOY 45
0xbfffeedc return@old FP
0xbfffeed0
0xbfffeecc
0xbfffeec8
0xbfffeec4
0xbfffeec0
0xbfffeebc
0xbfffeeb8
0xbfffeeb4
0xbfffeeb0
0xbfffeee4
0xbfffeed8
0xbfffeed4
buf
0xbfffeee8
0xbfffeee0
Calling a lib function
system()
Code ofsystem() libcfunction
• system() is called
• What happens at end ofsystem?
• Return address needede.g. call to exit()
• Parameters to system missing
return@
argarg
Module NOY 46
Return Oriented Programming
¢ ROP chainsl Small snippets of code ending with a RETl Can be chained together
¢ Libc is a good candidate (return-into-libc)l Many functionsl Always linked to programs
&gadget3
&gadget2
&gadget1
--……ret
…ret
Stack
1/19/18
24
Module NOY 47
C. Address Space Layout Randomisation
¢ Ideal Re-arrange the position of key data areas randomly
(stack, .data, .text, shared libraries, . . . )l Buffer overflow: the attacker does not know the
address of the shellcodel Return-into-libc: the attacker can't predict the
address of the library functionl Implementations: Linux kernel > 2.6.11, Windows
Vista, . . .
Module NOY 48
C. Address Space Layout Randomisation
¢ Problemsl 32-bit implementations use few randomization bitsl An attacker can still exploit non-randomized areasl A single randomized offset per region
1/19/18
25
Module NOY 49
That is all folks!
¢ Buffer overflows will be with us for quite some time¢ Best avoid them in your code!
top related