我试图写一个跳转到内存,但我似乎找不到任何地方可以解释它是如何工作的。
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位。
发布于 2016-11-26 12:49:03
这是在32位程序中通常的做法(不确定在64位上有多大不同),但这应该会让你知道该怎么做。由于VirtualProtect,这是特定于windows的,但如果您是在linux上,则可以使用mprotect。
#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;
}发布于 2016-11-26 12:22:30
我建议使用汇编程序来生成正确的字节。只需创建以下文件并通过NASM运行它:
BITS 64
JUMP 0x7fe1234结果是:
2f e9 fe 12 00 07 (由于LE字节顺序)。这是一个相对跳转,因此很难从高级代码生成。您可能希望使用操作码EA,它执行绝对跳转。然后,您可以简单地使用要跳转到的位置的绝对地址。
https://stackoverflow.com/questions/40814633
复制相似问题