首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将跳转写入内存

将跳转写入内存
EN

Stack Overflow用户
提问于 2016-11-26 11:30:07
回答 2查看 1.1K关注 0票数 1

我试图写一个跳转到内存,但我似乎找不到任何地方可以解释它是如何工作的。

代码语言:javascript
复制
typedef UINT(WINAPI* tResetWriteWatch)(LPVOID lpBaseAddress, SIZE_T dwRegionSize);

UINT WINAPI ResetWriteWatchHook(LPVOID lpBaseAddress, SIZE_T dwRegionSize){
    printf("Function called\n");
    return 0;
}

void main(){
    DWORD64 hookAddr = (DWORD64)&ResetWriteWatch;
    WriteJump(hookAddr, ResetWriteWatchHook/*Let's say it's 0x7FE12345678*/);//Writes E9 XX XX XX XX to memory
}

我的主要问题是我不明白:如何将ASMJMP0x7FE12345678转换为E9 XX,这样我就可以在hookAddr上编写它。

进程为64位。

EN

回答 2

Stack Overflow用户

发布于 2016-11-26 12:49:03

这是在32位程序中通常的做法(不确定在64位上有多大不同),但这应该会让你知道该怎么做。由于VirtualProtect,这是特定于windows的,但如果您是在linux上,则可以使用mprotect

代码语言:javascript
复制
#include <stdio.h>
#include <windows.h>

void foo() {
    printf("Foo!");
}

void my_foo() {
    printf("My foo!");
}

int setJMP(void *from, void *to) {
    DWORD protection;
    if (!VirtualProtect(from, 5, PAGE_EXECUTE_READWRITE, &protection)) { // We must be able to write to it (don't necessarily need execute and read)
        return 0;
    }

    *(char *)from = 0xE9; // jmp opcode
    *(int *)(from + 1) = (int)(to - from - 5); // relative addr

    return VirtualProtect(from, 5, protection, &protection); // Restore original protection
}

int main() {
    setJMP(foo, my_foo);
    foo(); // outputs "My foo!"

    return 0;
}
票数 2
EN

Stack Overflow用户

发布于 2016-11-26 12:22:30

我建议使用汇编程序来生成正确的字节。只需创建以下文件并通过NASM运行它:

代码语言:javascript
复制
BITS 64
JUMP 0x7fe1234

结果是:

2f e9 fe 12 00 07 (由于LE字节顺序)。这是一个相对跳转,因此很难从高级代码生成。您可能希望使用操作码EA,它执行绝对跳转。然后,您可以简单地使用要跳转到的位置的绝对地址。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40814633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档