some days ago I took this piece of code from opensecuritytraining.info to test a buffer overflow exploitation:
#include <stdio.h>
char *secret = "pepito";
void go_shell(){
char *shell = "/bin/sh";
char *cmd[] = { "/bin/sh", 0 };
printf("¿Quieres jugar a un juego?...\n");
setreuid(0);
execve(shell,cmd,0);
}
int authorize(){
char password[64];
printf("Escriba la contraseña: ");
gets(password);
if (!strcmp(password,secret))
return 1;
else
return 0;
}
int main(){
if (authorize()){
printf("Acceso permitido\n");
go_shell();
} else{
printf("Acceso denegado\n");
}
return 0;
}
The first test before injecting a shellcode was trying to execute the go_shell function without knowing the password, overflowing the return address of main function and pointing it to the location of go_shell.
As far as I understand the stack is divided as below:
[STACK] {Return_address}{EBP}{password_buffer(64)}...
So If I store in password_buffer 68 bytes plus the address of go_shell it should overwrite the return address and execute the desired function.
[STACK] {4bytes (Location of go_shell)}{EBP(4 Bytes of junk)}{password_buffer(64)(64 bytes of junk)}...
The problem here is that I need to fill the buffer with 76 bytes of junk plus 4 bytes of the address to actually override the return address and point %eip to go_shell. What I don't understand is where do those additional 8 bytes come from?
This is the GDB output before injecting 74 A (0x41) + the address in a breakpont at line if (!strcmp(password,secret)):
EBP:
0xbffff4a8: 0x41414141 0x0804851c
AAAA + memory_address
And continuing to go_shell execution (Breakpoint at void go_shell(){ ):
EIP now points to the last return address overwrited:
(gdb) x/2x $eip
0x804851c <go_shell>: 0x83e58955 0x45c728ec
Any help understanding this?
Regards.