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

AKJ*_*J88 2 linux x86 assembly 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"
Run Code Online (Sandbox Code Playgroud)

cad*_*luk 5

man mprotect 说:

实现可能要求addr是返回的页面大小的倍数sysconf().

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

and $0xfffff000, %ebx
Run Code Online (Sandbox Code Playgroud)

movl %esi, %ebx
Run Code Online (Sandbox Code Playgroud)

准备打电话的时候mprotect.

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