美文网首页惊喜程序问题集
惊喜程序问题集(26) - A Hard Goodbye

惊喜程序问题集(26) - A Hard Goodbye

作者: cs_cl | 来源:发表于2018-01-09 14:04 被阅读0次

运行下面这段代码(amd64 Linux或MacOS),并分析它的工作方式。

//goodbye.c

#include <stdio.h>
#include <sys/mman.h>

char *s = "Fnncaxd";
typedef int func(int);

int main() {
    unsigned char bytes[] = {
        'U', 'H', 0x89, 0xe5, 0x89, '}', 0xfc, 0x8b,
        '}', 0xfc, 0x83, 0xc7, 0x01, 0x89, 0xf8, ']',
        0xc3, 0, 0, 0, 0, 0, 0, 0
    };
    void *page = (void *)((unsigned long)bytes & ~0xfff);
    if (mprotect(page, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC)) {
        perror("mprotect");
    }
    char c;
    while ((c = *s++) != 0) {
        putchar(((func *)((void *)bytes))(c));
    }
    putchar('\n');
    return 0;
}

相关文章

网友评论

    本文标题:惊喜程序问题集(26) - A Hard Goodbye

    本文链接:https://www.haomeiwen.com/subject/kygwnxtx.html