Can a C program modify its executable file?

后端 未结 9 2028
情歌与酒
情歌与酒 2020-12-29 04:35

I had a little too much time on my hands and started wondering if I could write a self-modifying program. To that end, I wrote a \"Hello World\" in C, then used a hex editor

9条回答
  •  醉话见心
    2020-12-29 05:18

    If you are using Windows, you can do the following:

    Step-by-Step Example:

    1. Call VirtualProtect() on the code pages you want to modify, with the PAGE_WRITECOPY protection.
    2. Modify the code pages.
    3. Call VirtualProtect() on the modified code pages, with the PAGE_EXECUTE protection.
    4. Call FlushInstructionCache().

    For more information, see How to Modify Executable Code in Memory (Archived: Aug. 2010)

提交回复
热议问题