gpt4 book ai didi

linux - 为什么我的 shellcode 不起作用(在 Linux 中)?

转载 作者:太空狗 更新时间:2023-10-29 11:46:34 25 4
gpt4 key购买 nike

我在下面写了一个小的 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应该将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 指令很奇怪。这怎么会发生?

最佳答案

The instruction add $0x20,%eax is strange. How can this happen?

int * ret;
ret=(int *)&ret+0x08;

这就是 C 指针数学的工作原理 - 此添加将 ret 更改为 0x08 * sizeof(int) 字节。这就是 0x20 的来源。但 Andy Ross 的观察是正确的,编译器可以随意安排堆栈帧,因此任何重新编译,尤其是使用不同的编译器设置,都可以修改帧布局。

关于linux - 为什么我的 shellcode 不起作用(在 Linux 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10360195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com