变化许可时的自我变异问题

时间:2019-04-09 20:38:47

标签: c++ visual-c++

我正在Windows下创建一个自我突变,但是当我尝试更改权限页面边界时,它可以在Dev C ++下运行,但是相同的代码在Visual Studio 2017下不起作用。一旦编译,它就可以工作,但是它总是返回我原始的函数结果1,当它应该为42时会突变1。

Dev C ++

int change_page_permissions_of_address(void *addr) {
    // Move the pointer to the page boundary
    int page_size = getpagesize();
    DWORD dwOldProtect;

    addr -= (unsigned uintptr_t)addr % page_size; // it works under dev c++

    if(VirtualProtect(addr, page_size, PAGE_EXECUTE_READWRITE,&dwOldProtect) == -1) {
        return -1;
    }

    return 0;
}

Visual Studio 2017

int change_page_permissions_of_address(void* address) {
    // Move the pointer to the page boundary

    unsigned char* addr = reinterpret_cast<unsigned char *>(address); // cast before operation

    int page_size = getpagesize();
    DWORD dwOldProtect;

    addr -= (uintptr_t)addr % page_size; // doesnt work under visual studio

    if (VirtualProtect(addr, page_size, PAGE_EXECUTE_READWRITE, &dwOldProtect) == -1) {
        return -1;
    }

    return 0;
}

主要

int main(){
    void *foo_addr = (void*)foo; 
    if(change_page_permissions_of_address(foo_addr) == -1) {
        fprintf(stderr, "Error while changing page permissions of foo(): %s\n", strerror(errno));
        return 1;
    }

    // Call the unmodified foo()
    puts("Calling foo...");
    foo();

    // Change the immediate value in the addl instruction in foo() to 42
    unsigned char *instruction = (unsigned char*)foo_addr + 18;
    *instruction = 0x2A;

    // Call the modified foo()
    puts("Calling foo..., but I am the self-modifying");
    foo();

    return 0;    
}

self-mutation_visual

0 个答案:

没有答案