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

时间:2015-09-22 15:35:41

标签: linux assembly x86 system-calls self-modifying

我试图学习一些堆栈溢出技术并在其中使用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 个答案:

答案 0 :(得分:5)

man mprotect说:

  

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

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

来舍入地址
and $0xfffff000, %ebx

movl %esi, %ebx

准备拨打mprotect

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