在自修改汇编代码中调用 mprotect 后仍然出现段错误

Still getting segmentation fault after calling mprotect in self-modifying assembly code

我正在尝试学习一些堆栈溢出技术并在其中使用 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 [=10=]x7d, %eax
  movl %esi, %ebx
  movl [=10=]x20, %ecx
  movl , %edx
  int [=10=]x80

  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 , %ebx
  movl %ebx, 0xf(%esi)
  movl %eax, 0x13(%esi)

  movl %esi, %ebx
  leal 0xb(%esi), %ecx
  leal 0x13(%esi), %edx

  movb , %al
  int [=10=]x80

StartPoint:
  call execvecall
SomeVarHere:
  .ascii "/bin/ls0-l0111122223333"

man mprotect 说:

The implementation may require that addr be a multiple of the page size as returned by sysconf().

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

向下舍入地址
and [=10=]xfffff000, %ebx

之后

movl %esi, %ebx

准备调用时 mprotect

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