用C/C++编写运行时自修改代码

问题描述 投票:0回答:1

我有一些问题,

可以在windows中制作一个可以自我修改的程序吗?

我编写了使用 _asm{} 函数的程序:


#include <iostream>

using namespace std;

void print() //FUNCTION TO MODIFY
{
    cout << "Hello, World!\n";
}

void Crypt(int adr)
{
    _asm {

        mov eax, adr

        xor_loop:
        xor byte ptr [eax], 0x24 
            inc eax
            cmp eax, 0xC3 //compare ret opcode
            jne xor_loop
    }

}

int main()
{
    print();

    void *ptr = print;  //address example : 003812E4

    cout << "Address : " << ptr << "\n";

    int mem;

    _asm {
        lea eax, print
        mov mem, eax
        call eax
    }

    cout << "EAX : " << mem << "\n";

    Crypt(mem);
    
    return 0;
}

但无法正常工作,我不知道为什么,有什么帮助吗?

伪代码:

print function:
   mov eax, 0x80
   add eax, 0x02
   call eax
   ret

modify:
   lea eax, print
   xor byte ptr [eax], 0x24 //xor for modify opcode
   inc eax       //increase eax 1 byte
   cmp eax, 0xC3 //compare ret opcode
   jne modify

---------------------------------------------
modify print function example:
   mul eax
   mov eax, edx
   jnz ebx
   ret
c++ reverse-engineering self-modifying
1个回答
0
投票

也许您应该将代码保存在

.data
部分,因为您无法更改
.code
部分中的代码,因为它受到保护(只读)。

有一个想法:

  1. 在堆区分配一块内存来保存你想要改变的函数
  2. 将功能代码字节复制到该内存中
  3. 更改代码并使用您分配的地址调用新函数。
© www.soinside.com 2019 - 2024. All rights reserved.