【发布时间】:2017-12-20 00:08:44
【问题描述】:
我是 asm 的新手,我正在尝试对 /bin/bash 执行系统调用。但是我目前遇到以下问题:
我的代码适用于第一个参数长度小于 8 个字节的任何 execve 调用,即“/bin/sh”或“/bin/ls”:
.section .data
name: .string "/bin/sh"
.section .text
.globl _start
_start:
#third argument of execve, set to NULL
xor %rdx, %rdx
#push nullbyte to the stack
pushq %rdx
#push /bin/sh to the stack
pushq name
#copy stack to rdi, 1st arg of execve
mov %rsp, %rdi
#copy 59 to rax, defining syscall number for execve
movq $59, %rax
#3rd arg of execve set to NULL
movq $0, %rsi
syscall
令我困惑的是我无法使用它
name: .string "/bin/bash"
我试图将字符串分成几部分,将“/bash”然后“/bin”推送到堆栈,似乎没有什么能让我让它工作,而且我每次都会收到“非法指令”错误。我究竟做错了什么?
非工作代码:
.section .data
name: .string "/bin/bash"
.section .text
.globl _start
_start:
#third argument of execve, set to NULL
xor %rdx, %rdx
#push nullbyte to the stack
pushq %rdx
#push /bin/sh to the stack
pushq name
#copy stack to rdi, 1st arg of execve
mov %rsp, %rdi
#copy 59 to rax, defining syscall number for execve
movq $59, %rax
#3rd arg of execve set to NULL
movq $0, %rsi
syscall
其他非工作代码:
.section .data
.section .text
.globl _start
_start:
#third argument of execve, set to NULL
xor %rdx, %rdx
#push nullbyte to the stack
pushq %rdx
#push /bin/bash to the stack
pushq $0x68
pushq $0x7361622f
pushq $0x6e69622f
#copy stack to rdi, 1st arg of execve
mov %rsp, %rdi
#copy 59 to rax, defining syscall number for execve
movq $59, %rax
#3rd arg of execve set to NULL
movq $0, %rsi
syscall
【问题讨论】:
-
您忘记显示 non-working 代码。你也忘了使用调试器。您可能忘记了堆栈是反向工作的。您可能忘记了
pushalways 写入 8 个字节。所以你应该把你的字符串分成 8 个字节的部分,除了最后一部分(你先推送)。 -
显然,如果你在
.data中有一个字符串,则无需将其复制到堆栈中。您可以直接使用它的地址并完成它。 -
您查看过 X86_64 ABI 函数调用约定吗?这可能会帮助您找出问题所在。
-
@Jester 谢谢你的帮助。实际上,我确实考虑到堆栈向后工作并相应地对推送进行排序这一事实,错误保持不变。
-
@MichaelPetch 感谢您的建议。正如我的问题中提到的,这适用于 /bin/sh。即使我拆分了字符串(cf 编辑),我也无法调用 /bin/bash。 "/bin/bash" 将是 0x687361622f6e69622f,不能放入 64 位寄存器。
标签: assembly 64-bit system-calls execve