【发布时间】:2015-09-22 15:35:40
【问题描述】:
我正在尝试学习一些堆栈溢出技术并在其中使用 shellcode。
我能够成功使用一些基本的 shellcode。然后我开始在汇编中使用exeve 并用它调用ls -l,再次成功。
现在我正在尝试使用相对寻址并摆脱代码中的空值。因此,我尝试了一个简单的自修改代码。我知道代码段是只读的,所以我尝试调用mprotect 使其可写。我的代码仍然无法工作,并且在movb %al, 0x7(%esi) 处出现分段错误。如果有人能给我一些关于我代码中的错误之处的见解,我将不胜感激。
.text
.globl _start
_start:
jmp StartPoint
execvecall:
popl %esi # the address of string
#calling mprotect to make the memory writable
movl $0x7d, %eax
movl %esi, %ebx
movl $0x20, %ecx
movl $7, %edx
int $0x80
xorl %eax, %eax
movb %al, 0x7(%esi) #putting zero for at the end of /bin/ls
movb %al, 0xa(%esi) #putting another zero at the end of -l
#this part forms an array ending with for the second parameter of execve
movl %esi, 0xb(%esi)
movl %esi, %ebx
addl $8, %ebx
movl %ebx, 0xf(%esi)
movl %eax, 0x13(%esi)
movl %esi, %ebx
leal 0xb(%esi), %ecx
leal 0x13(%esi), %edx
movb $11, %al
int $0x80
StartPoint:
call execvecall
SomeVarHere:
.ascii "/bin/ls0-l0111122223333"
【问题讨论】:
-
我建议检查返回值。
-
我查过了,不是-1。
-
我执行了你的代码,gdb 告诉我它返回了
-22(在我的机器上又名EINVAL)。 -
使用
strace看看你的代码做了什么,如果你没有在gdb下运行它。它还可以在您编写的 C 代码中保存大量调试打印,以尝试一些系统调用。 -
某些系统可能不允许同时拥有写入和执行权限,例如:PaX en.wikipedia.org/wiki/PaX#Restricted_mprotect.28.29 ,OpenBSD W^X en.wikipedia.org/wiki/W%5EX ,...另见akkadia.org/drepper/selinux-mem.html Ulrich Drepper 的推荐方式在 SELinux 下这样做。
标签: linux assembly x86 system-calls self-modifying