Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的外壳代码不工作(在Linux中)?_Linux_Inline Assembly_Disassembly_Shellcode - Fatal编程技术网

为什么我的外壳代码不工作(在Linux中)?

为什么我的外壳代码不工作(在Linux中)?,linux,inline-assembly,disassembly,shellcode,Linux,Inline Assembly,Disassembly,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" ".strin

我在下面写了一个小外壳代码:

#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);
}
我得到了调用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.
显然,地址是0xbffff2f8+0x04=0xbffff2fc。 val ret的地址是0xbffff2f4

因此,ret=int*&ret+0x08应该得到正确的地址。和*ret=int*外壳代码应将外壳代码的地址插入堆栈中。然后程序运行到外壳代码中,当程序返回时,我得到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字节的帧。

编译器可以自由地将ret变量放在它想要的func堆栈帧中的任何位置。大概我太懒了,没法从反汇编中算出你的偏移量8是错误的。请注意,正在设置一个40字节的帧

指令add$0x20,%eax很奇怪。这怎么会发生

这就是C指针数学的工作原理-此加法将ret更改0x08*sizeofint字节。这就是0x20的来源。但是Andy Ross的观察是正确的,编译器可以自由地按照它想要的方式排列堆栈框架,因此任何重新编译,特别是使用不同的编译器设置,都可以修改框架布局

指令add$0x20,%eax很奇怪。这怎么会发生


这就是C指针数学的工作原理-此加法将ret更改0x08*sizeofint字节。这就是0x20的来源。但是Andy Ross的观察是正确的,编译器可以自由地按照它想要的方式排列堆栈框架,因此任何重新编译,特别是不同的编译器设置,可以修改帧布局。

我可以强制GCC将val ret放在固定位置吗?不可以。但即使可以:如果受害者必须专门构建软件以启用攻击,堆栈粉碎攻击的价值是什么我可以强制GCC将val ret放在固定位置吗?不可以。但即使可以:如果受害者必须专门构建软件来启用您的漏洞攻击,堆栈粉碎攻击的价值是什么
(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.
(gdb) x/16xw $esp
0xbffff2e8: 0xbffff3bc  0x00000001  0x00000000  0x08049460
0xbffff2f8: 0xbffff318  0x08048cd9  0x0804972f  0x080d6044
0xbffff308: 0x08049797  0x00000000  0x08049460  0x080493c0
0xbffff318: 0x00000000  0x08048e91  0x00000001  0xbffff3b4
(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.
int * ret;
ret=(int *)&ret+0x08;