我寫了下面一個小的shellcode:爲什麼我的shellcode不能工作(在Linux中)?
#include <stdlib.h>
int main()
{
__asm__("jmp calloffset\n"
"poploffset: popl %%esi\n"
"movl $1,%%eax\n"
"movl $6,%%ebx\n"
"int $0x80\n"
"calloffset: call poploffset\n"
".string \"/bin/bash\"\n":::"esi");
exit(1);
}
當shellcode的工作,它將返回6. 實際上,上面的代碼工作得很好,主函數返回6確實如此。
然後我嵌入代碼到C程序:
#include <stdlib.h>
#include <unistd.h>
char shellcode[]="\xeb\x0d\x5e\xb8\x01\x00\x00\x00\xbb\x06\x00\x00\x00\xcd\x80\xe8\xee\xff\xff\xff";
void func()
{
int * ret;
ret=(int *)&ret+0x08;
*ret=(int *)shellcode;
}
int main()
{
func();
exit(0);
}
在正常情況下,代碼應該返回6.但它返回0,所有的時間。
我不認爲我的代碼是錯誤的。我會告訴你的。
首先,我從GDB得到VAL RET地址:
(gdb) print &ret
$1 = (int **) 0xbffff2f4
我也得到調用的下一條指令的地址在主:
(gdb) disass main
Dump of assembler code for function main:
0x08048ccb <+0>: push %ebp
0x08048ccc <+1>: mov %esp,%ebp
0x08048cce <+3>: and $0xfffffff0,%esp
0x08048cd1 <+6>: sub $0x10,%esp
0x08048cd4 <+9>: call 0x8048cb0 <func>
0x08048cd9 <+14>: movl $0x0,(%esp)
0x08048ce0 <+21>: call 0x80495c0 <exit>
End of assembler dump.
顯然,0x08048cd9。
然後,我得到它的地址存儲在上面的堆棧地址:
(gdb) x/16xw $esp
0xbffff2e8: 0xbffff3bc 0x00000001 0x00000000 0x08049460
0xbffff2f8: 0xbffff318 0x08048cd9 0x0804972f 0x080d6044
0xbffff308: 0x08049797 0x00000000 0x08049460 0x080493c0
0xbffff318: 0x00000000 0x08048e91 0x00000001 0xbffff3b4
顯然,地址是0xbffff2f8 + 0×04 = 0xbffff2fc。 而val ret的地址是0xbffff2f4。
因此,ret=(int *)&ret+0x08
應該得到正確的地址。並且*ret=(int *)shellcode
應該將shellcode的地址插入到堆棧中。然後程序運行到shellcode中,最後當程序返回時我得到6。
我錯了嗎?
我似乎找到了錯誤的地方:
(gdb) disass func
Dump of assembler code for function func:
0x08048cb0 <+0>: push %ebp
0x08048cb1 <+1>: mov %esp,%ebp
0x08048cb3 <+3>: sub $0x28,%esp
0x08048cb6 <+6>: lea -0xc(%ebp),%eax
0x08048cb9 <+9>: add $0x20,%eax
0x08048cbc <+12>: mov %eax,-0xc(%ebp)
0x08048cbf <+15>: mov -0xc(%ebp),%eax
0x08048cc2 <+18>: mov $0x80d6028,%edx
0x08048cc7 <+23>: mov %edx,(%eax)
0x08048cc9 <+25>: movl $0x1,(%esp)
0x08048cd0 <+32>: call 0x8053380 <sleep>
0x08048cd5 <+37>: leave
0x08048cd6 <+38>: ret
End of assembler dump.
指令add $0x20,%eax
很奇怪。這怎麼會發生?
我可以強制GCC將val ret放在固定的地方嗎? – 2012-04-28 04:21:52
不,但即使你可以:如果你的受害者必須專門構建她的軟件來啓用你的漏洞,那麼堆棧粉碎攻擊的價值是多少? :) – 2012-04-28 04:23:21