【问题标题】:Why am I seg faulting when trying to use the execve sys call?为什么我在尝试使用 execve 系统调用时会出现段错误?
【发布时间】:2013-12-29 23:35:55
【问题描述】:

我正在使用 32 位 linux(ubuntu 发行版)上的 NASM 汇编器使用英特尔语法为 IA-32 架构编写一些汇编代码。

我正在尝试使用execve sys 调用来执行/bin/sh,但仍会出现段错误。我想我正在遵循 sys 调用的约定:

  • eax - 我平台上的系统电话号码是 11
  • ebx - 以null结尾的字符串“/bin/sh”的地址
  • ecx - 包含以 null 结尾的字符串和结尾的 null 的结构的地址
  • edx - 指向 null 的指针

我不确定为什么这个程序没有按预期运行。使用gdb 进行验证,我看到所有地址都按我的预期加载。我怀疑这与我定义 sh 变量的方式有关。

global _start

section .text
_start:
  mov eax, 0xb
  lea ebx, [sh]
  lea ecx, [sh]
  lea edx, [sh+8]
  int 0x80

section .data
  sh: db "/bin/sh", 0x00, 0x00,0x00,0x00,0x00

我对使用ecx 感到特别困惑。在上面的代码中,我使用指向字符串的指针加载ecx,但我真正想要的是指向数组的指针。一个数组,包含:一个指向字符串的指针和一个空值。我已经尝试重写此代码,但仍然不成功。

global _start

section .text
_start:
  mov eax, 0xb
  mov ebx, sh ; pointer to the string '/bin/sh'

  xor esi, esi
  push esi ; argv[1]
  push ebx ; argv[0]

  lea ecx, [esp] ; a pointer to the array
  lea edx, [esp+4] ; a pointer to a NULL
  int 0x80

section .data
  sh: db "/bin/sh/", 0x00
  • 为什么会出现段错误?
  • 如何使用execve执行/bin/sh

【问题讨论】:

  • 您误解了ecx 的用途。在做一些困难的事情之前,请确保你知道如何在 C 中使用这个系统调用。
  • "ecx - 包含空终止字符串和尾随空的结构的地址" [...] 错了,ecx 只需为NULL。如果您执行lea ecx, [sh+8](注意+8),您的第一个代码sn-p 可以正常工作。您可以进一步简化,只需执行mov eax,0xb; lea ebx,[sh]; mov ecx,0; mov edx,0; int 0x80;

标签: assembly nasm


【解决方案1】:

使用堆栈

global _start

section .text
_start:
xor eax, eax
push eax ; add a null onto the stack

; hs/n : 68732f6e
; ib// : 69622f2f
push 0x68732f6e
push 0x69622f2f ; add the string in reverse order

mov ebx, esp ; pointer the null terminated string

push eax ; argv[1]

mov edx, esp ; a pointer to null

push ebx ; argv[0]

mov ecx, esp ; a pointer to the array with address and null

mov al, 0xb ; the sys call number
int 0x80

或其他方式。对我来说,困难在于正确构建数组并处理空值

global _start

section .text
_start:
    xor eax, eax

    mov ebx, filename ; pointer the null terminated string

    push eax ; argv[1]

    mov edx, esp ; a pointer to null

    push filename ; argv[0]

    mov ecx, esp ; a pointer to the array with address and null

    mov al, 0xb ; the sys call number
    int 0x80

section .data
    filename: db "/bin/sh", 0x00, 0x00, 0x00, 0x00

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多