![](/img/trans.png)
[英]Why would this shellcode work if it does NOT pass 0 as the environment pointer for execve?
[英]Why my shellcode does not work (in Linux)?
我在下面写了一个小的shellcode:
#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。实际上,以上代码运行良好,main函数确实返回了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
然后我在main中获得下一条调用指令的地址:
(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 + 0x04 = 0xbffff2fc。 val ret的地址为0xbffff2f4。
因此, ret=(int *)&ret+0x08
应该获得正确的地址。 并且*ret=(int *)shellcode
应该将*ret=(int *)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
很奇怪。 怎么会这样
编译器可以自由地将“ ret”变量放在所需的func()堆栈框架中的任何位置。 大概(我太懒了,无法从反汇编中得出数学值),您的8偏移量完全是错误的。 请注意,正在设置一个40字节的帧。
指令
add $0x20,%eax
很奇怪。 怎么会这样
int * ret;
ret=(int *)&ret+0x08;
这就是C指针数学的工作方式-此加法将ret
更改为0x08 * sizeof(int)
字节。 这就是0x20
来源。 但是安迪·罗斯(Andy Ross)的观察是正确的,编译器可以自由地按需要安排堆栈框架,因此任何重新编译(尤其是使用不同的编译器设置)都可以修改框架布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.