【问题标题】:Assembly execve failure -14装配执行失败 -14
【发布时间】:2015-05-07 17:06:53
【问题描述】:

程序将可执行文件写入磁盘的第二段,将其解密(到 /tmp/decbd),然后执行(按计划执行) 文件 decbd 出现在磁盘上,可以通过 shell 执行,最后一次 execve 调用返回 eax=-14,程序结束后,执行流向数据并获得 segfault。 http://pastebin.com/KywXTB0X

在使用 hexdump 和 dd 编译后的第二段中,我手动放置了通过 openssl 加密的 echo 二进制文件,当我在最后一个 int 0x80 命令之前停止执行时,我已经能够在 decbd 中运行我的“echo”,使用另一个终端。

【问题讨论】:

  • 学习使用调试器。
  • 我已经用过了。我得到的唯一一件事是程序运行正常,直到 execve 调用,调试器无法告诉我我的参数有什么问题。

标签: linux assembly nasm elf system-calls


【解决方案1】:
  1. 您应该将范围缩小到一个最小的示例。见MCVE
  2. 如果您希望其他人提供帮助,您应该注释您的代码。
  3. 您应该学习使用调试器和/或其他工具。

对于第 1 点,您可以继续:

section .text
    global _start   ;must be declared for linker (ld)
_start:
    mov eax,11             ; execve syscall
    mov ebx,program        ; name of program
    mov ecx,[esp+4]        ; pointer to argument array
    mov ebp,[esp]          ; number of arguments
    lea edx,[esp+4*ebp+2]  ; pointer to environ array
    int 0x80
section .data
    program db '/bin/echo',0

对于第 3 点,使用调试器您可以看到:

  • ebx 没关系
  • ebp 没关系
  • ecx错了
  • edx错了

这很容易解决。 ecx 应该加载地址,而不是值,edx 应该跳过 2 个每个 4 字节的指针,因此偏移量应该是 8 而不是 2。固定代码可能如下所示:

section .text
    global _start   ;must be declared for linker (ld)
_start:
    mov eax,11             ; execve syscall
    mov ebx,program        ; name of program
    lea ecx,[esp+4]        ; pointer to argument array
    mov ebp,[esp]          ; number of arguments
    lea edx,[esp+4*ebp+8]  ; pointer to environ array (skip argc and NULL)
    int 0x80
section .data
    program db '/bin/echo',0

【讨论】:

    【解决方案2】:

    man execve 在“错误”部分中关于返回码 -14 (-EFAULT) 说明了这一点:

            EFAULT filename points outside your accessible address space.
    

    您将错误的指针传递给execve()

    【讨论】:

    • 你怎么知道EFAULT的错误码是-14?我在“man 2 execve”中没有找到有关它的信息。如果指针错误,我仍然无法理解为什么,因为我已经在调用 openssl 的 execve 中传递了第一个参数,方式完全相同,并且可以正常工作。
    • grep -w 14 /usr/include/*/errno*.h。当没有人能看到你代码的相关部分时,没有人能解释为什么你的代码会失败。我们所能做的就是猜测......
    猜你喜欢
    • 1970-01-01
    • 2022-08-23
    • 2014-09-01
    • 2023-03-15
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2019-04-17
    • 2015-08-30
    相关资源
    最近更新 更多