【问题标题】:Still getting segmentation fault after calling mprotect in self-modifying assembly code在自修改汇编代码中调用 mprotect 后仍然出现分段错误
【发布时间】: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


【解决方案1】:

man mprotect 说:

实现可能要求addrsysconf() 返回的页面大小的倍数。

这显然是您机器上的情况。假设您有 4 KiB 页面(如在 x86 上,没有 PSE),您可以通过执行向下舍入地址

and $0xfffff000, %ebx

之后

movl %esi, %ebx

准备拨打mprotect时。

请注意,调用mprotect 会更改对整个页面的保护。

【讨论】:

  • 谢谢哥们,我以为会是问题,但我不记得为什么我至少没有尝试过!!! :-(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2011-05-09
  • 2012-05-06
  • 2015-03-31
  • 2012-05-09
  • 2022-01-04
相关资源
最近更新 更多