简体   繁体   English

组装,如何使用mprotect?

[英]assembly, how to use mprotect?

I am trying to make self modifying code in Linux.我正在尝试在 Linux 中进行自我修改代码。 I thought it would works but didn't.我以为它会起作用,但没有。

section .data
section .text
global _start

_start:
    mov eax, 125 ;mprotect syscall number
    mov ebx, _start ; *addr
    mov ecx, 0x10000 ;page interval.
    mov edx, 7 ; rwx permission
    int 0x80
    jmp modify
target:
    mov eax, edx        
halt:
    mov ebx, 1
    mov eax, 1
    int 0x80
modify:
    mov ebx, [new]      
    mov [target], ebx   
    jmp target          
new:
    mov ebx, 0          

I used nasm on ubuntu 18.04.我在 ubuntu 18.04 上使用了 nasm。

INT 0x80 return value is -22 0xffffffea INT 0x80 返回值为-22 0xffffffea

I don't know what is wrong.我不知道出了什么问题。

Run your program under strace , like strace./a.out to decode system call args and return values.strace下运行您的程序,例如strace./a.out以解码系统调用参数并返回值。

Probably your base address isn't page-aligned, or the range includes some unmapped pages.可能您的基地址不是页面对齐的,或者该范围包括一些未映射的页面。 You could round down to a page boundary with and ebx, -4096 , or you could align _start by putting align 4096 before it.您可以使用and ebx, -4096向下舍入到页面边界,或者您可以通过在_start之前放置align 4096来对齐 _start。

Or instead of calling mprotect yourself, you could link your program with ld --omagic to make the text segment read+write+exec.或者,您可以将程序与ld --omagic链接起来,而不是自己调用 mprotect,以使文本段为 read+write+exec。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM