【问题标题】:WriteProcessMemory fails inside functionWriteProcessMemory 在函数内部失败
【发布时间】:2020-09-07 20:36:13
【问题描述】:

您好,我是函数挂钩的新手,我正在使用文章中的代码。

这是我的代码

#include <windows.h>

#include <iostream>

FARPROC messageBoxAddress = NULL;

SIZE_T bytesWritten = 0;

unsigned char messageBoxOriginalBytes[6] = { } ;

int __stdcall HookedMessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) {

    printf("la la la ");

    printf("\n");

    WriteProcessMemory(GetCurrentProcess(), (LPVOID)messageBoxAddress, messageBoxOriginalBytes, sizeof(messageBoxOriginalBytes), &bytesWritten);

    return MessageBoxA(NULL, lpText, lpCaption, MB_OK);

}


int main()
{

    SIZE_T bytesRead = 0; 

    HINSTANCE library = LoadLibraryA("user32.dll");

    FARPROC messageBoxAddress =GetProcAddress(library, "MessageBoxA");

    ReadProcessMemory(GetCurrentProcess(), messageBoxAddress, messageBoxOriginalBytes, 6, &bytesRead);

    void* hookedMessageBoxAddress = &HookedMessageBox;

    char patch[6] = { 0 };

    memcpy_s(patch, 1, "\x68", 1);

    memcpy_s(patch + 1, 4, &hookedMessageBoxAddress, 4);

    memcpy_s(patch + 5, 1, "\xC3", 1);


    WriteProcessMemory(GetCurrentProcess(), (LPVOID)messageBoxAddress, patch, sizeof(patch), &bytesWritten);


    MessageBoxA(NULL, "hello", "Welcome", MB_OK);

    return 0;
}

输出

la la la
la la la
la la la
la la la
la la la
la la la
la la la

问题是我只想要一个啦啦啦

这显示了一千个啦啦啦

int main() 中的 WriteProcessMemory 可以工作,但 'HookedMessageBox' 中的那个不工作。

谁能给我指点一下

我也想坚持基础。图书馆有弯路等。但坚持基本的帮助我理解它。

我尝试使用 GetLastError() 获取错误,它显示 998 表示访问被拒绝(在网上搜索)

【问题讨论】:

  • 我想由于最近执行了该功能,该页面被标记为不可写。您可以尝试调整此问题的答案中提到的内存权限。 stackoverflow.com/questions/39732894/…
  • 您不应该在 HookedMessageBox 内调用 MessageBox,因为 MessageBox 已被钩住,而您正在调用您的钩子 HookedMessabeBox。所以会导致无限递归。相反,您应该调用 messageBoxOriginalBytes。但是 messageBoxOriginalBytes 最后应该包含五个字节,jmp + 地址到 MessabeBox 中的下一个字节
  • @TheSteve 页面不会被标记为不可写,具体取决于它们的执行时间。与最近的时间无关
  • 如果您想要基础知识,请从错误检查开始。
  • 永远不需要使用(Read|Write)ProcessMemory()GetCurrentProcess()。进程可以访问自己的内存空间。一个简单的memcpy()CopyMemory() 就足够了。

标签: c++ winapi hook user32


【解决方案1】:

这称为钩子递归,要解决您需要使用蹦床钩子的问题。

蹦床钩子就像一个常规的绕道,它会跳转到你的代码,然后它会跳回到你实际的 jmp 指令之后的地址,这样它就不会一遍又一遍地执行你的代码。

在内部工作时,您不需要使用 WriteProcessMemory(),也不应该在您的钩子中修改您的钩子。

这是我对您的问题的解决方案,它使用了蹦床钩,并希望一些代码更有意义:

#include <iostream>
#include <Windows.h>

bool Detour32(char* src, char* dst, const intptr_t len)
{
    if (len < 5) return false;

    DWORD  curProtection;
    VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &curProtection);

    intptr_t  relativeAddress = (intptr_t)(dst - (intptr_t)src) - 5;

    *src = (char)'\xE9';
    *(intptr_t*)((intptr_t)src + 1) = relativeAddress;

    VirtualProtect(src, len, curProtection, &curProtection);
    return true;
}

char* TrampHook32(char* src, char* dst, const intptr_t len)
{
    // Make sure the length is greater than 5
    if (len < 5) return 0;

    // Create the gateway (len + 5 for the overwritten bytes + the jmp)
    void* gateway = VirtualAlloc(0, len + 5, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

    //Write the stolen bytes into the gateway
    memcpy(gateway, src, len);

    // Get the gateway to destination addy
    intptr_t  gatewayRelativeAddr = ((intptr_t)src - (intptr_t)gateway) - 5;

    // Add the jmp opcode to the end of the gateway
    *(char*)((intptr_t)gateway + len) = 0xE9;

    // Add the address to the jmp
    *(intptr_t*)((intptr_t)gateway + len + 1) = gatewayRelativeAddr;

    // Perform the detour
    Detour32(src, dst, len);

    return (char*)gateway;
}

typedef int(__stdcall* tMessageBoxA)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
tMessageBoxA oMessageBoxA = nullptr;

int __stdcall hkMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
    lpText = "hax0red";

    return oMessageBoxA(hWnd, lpText, lpCaption, uType);
}

int main()
{
    oMessageBoxA = (tMessageBoxA)GetProcAddress(GetModuleHandleA("user32.dll"), "MessageBoxA");

    oMessageBoxA = (tMessageBoxA)TrampHook32((char*)oMessageBoxA, (char*)hkMessageBoxA, 5);

    MessageBoxA(NULL, "Body Message", "Title Here", MB_OK);

    return 0;
}

【讨论】:

【解决方案2】:

感谢@GuidedHacking,我终于做了一个 64 工作代码。

重命名为 Detour64 和 TrampHook64。

#include <iostream>
#include <Windows.h>

bool Detour64(char* src, char* dst, const intptr_t len)
{
    if (len < 12) return false;

    DWORD  curProtection;
    VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &curProtection);

    intptr_t  absoluteAddress = (intptr_t)(dst);

    *src = (char)'\x48';
    *(src + 1) = (char)'\xb8';

    *(src + 2) = absoluteAddress % 256;
    *(src + 3) = (absoluteAddress/256) % 256;
    *(src + 4) = (absoluteAddress/65536) % 256;
    *(src + 5) = (absoluteAddress/(65536*256)) % 256;
    *(src + 6) = (absoluteAddress/((long long)65536*65536)) % 256;
    *(src + 7) = (absoluteAddress/((long long)65536*65536*256)) % 256;
    *(src + 8) = (absoluteAddress/((long long)65536*65536*65536))%256;
    *(src + 9) = (absoluteAddress/((long long)65536*65536*65536*256)) % 256;

    *(src + 10) = (char)'\xff';
    *(src + 11) = (char)'\xe0';

    VirtualProtect(src, len, curProtection, &curProtection);
    return true;
}

char* TrampHook64(char* src, char* dst, const intptr_t len)
{
    // Make sure the length is greater than 5
    if (len < 12) return 0;

    // Create the ga+teway (len + 5 for the overwritten bytes + the jmp)
    void* gateway = VirtualAlloc(0, len + 12, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);

    //Write the stolen bytes into the gateway
    memcpy(gateway, src, len);

    // Get the gateway to destination addy
    intptr_t  gatewayAbsoluteAddr = (intptr_t)src+len;    
 // Add the jmp opcode to the end of the gateway
    *((char*)gateway + len) = 0xFF;
    *((char*)gateway + len + 1) = 0x25;

    *((char*)gateway + len + 2) = 0;
    *((char*)gateway + len + 3) = 0;
    *((char*)gateway + len + 4) = 0;
    *((char*)gateway + len + 5) = 0;
    // Add the address to the jmp
    *((char*)gateway + len + 6) = gatewayAbsoluteAddr % 256;

    *((char*)gateway + len + 7) = (gatewayAbsoluteAddr / 256) % 256;

    *((char*)gateway + len + 8) = (gatewayAbsoluteAddr / 65536) % 256;

    *((char*)gateway + len + 9) = (gatewayAbsoluteAddr / (65536 * 256)) % 256;

    *((char*)gateway + len + 10) = (gatewayAbsoluteAddr / ((long long)65536 * 65536)) % 256;

    *((char*)gateway + len + 11) = (gatewayAbsoluteAddr / ((long long)65536 * 65536 * 256)) % 256;

    *((char*)gateway + len + 12) = (gatewayAbsoluteAddr / ((long long)65536 * 65536 * 65536)) % 256;
    *((char*)gateway + len + 13) = (gatewayAbsoluteAddr / ((long long)65536 * 65536 * 65536 * 256)) % 256;
   
    // Perform the detour
    Detour64(src, dst, len);

    return (char*)gateway;
}

typedef int(__stdcall* tMessageBoxA)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
tMessageBoxA oMessageBoxA = nullptr;

int __stdcall hkMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
    
    lpText = LPCTSTR("hax0red");
    
    return oMessageBoxA(hWnd, lpText, lpCaption, uType);
}

int main()
{

    HINSTANCE libr = LoadLibrary(L"User32.dll");
    
    oMessageBoxA = (tMessageBoxA)GetProcAddress(libr, "MessageBoxA");

    oMessageBoxA = (tMessageBoxA)TrampHook64((char*)oMessageBoxA, (char*)hkMessageBoxA, 14);
    
    MessageBoxA(NULL, "Body Message", "Title Here", MB_OK);

    return 0;
}

希望它永远是正确的。

【讨论】:

    【解决方案3】:

    关于@GuidedHacking 代码的小而重要的信息。一定要“测量”你通过的钩子长度,它并不总是 5,取决于你的原始函数的指令,它可能是 5,可能是 6,等等。你可以在 IDA 中看到它并放置正确的大小

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多