我正在使用代码注入扩展Windows组件的功能。我覆盖一个方法的指令,调用我自己的方法来完成原始方法的工作。假设我们有:
void Target(HDC magic123)
{ ... }
以下是该方法的前几条说明:
push rbx //
push rbp // stores registers to recover later
...
sub rsp, 0x260 // for all 7 pushes
...
mov r12, [rsp+0x28] // stores a pointer to 'magic123'
...
...a lot more instructions
在mov r12, [rsp+0x28]
之后,我立即用以下内容覆盖下一条说明:
mov rcx, r12 // 1st parameter to pass to a called function goes in RCX
add rsp, 0x260 // restore the stack
push 0 // create shadow space |EDIT: MISALIGNED STACK. WRONG.
mov rax, &DetouredFunction // function in my injected DLL
call rax // call it with the HDC as parameter
我在DLL中的功能:
void DetouredFunction(uintptr_t hdcPointer)
{
uintptr_t hdcAddress = *(uintptr_t*)(hdcPointer); // convert pointer to address
HDC hdc = (HDC)hdcAddress; // create a HDC from the address
HBITMAP hBitmapWallpaper = (HBITMAP)LoadImage(NULL, L"C:\\Users\\<user>\\Desktop\\image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
SelectObject(hdc, hBitmapWallpaper);
}
一切正常,直到我在注入的DLL函数中调用'LoadImage'。它会抛出“访问冲突异常(5)”,试图读取不存在的地址0xFFFFFFFFFFFFFFFF。
有什么问题?如果它们包含错误,请更正我上面的任何评论。谢谢!
答案 0 :(得分:0)
我的问题是未对齐堆栈。在再次调用LoadImage之前,它没有任何明显的作用。