Hooking ReadFile WinAPI

Hooking ReadFile WinAPI

本文关键字:WinAPI ReadFile Hooking      更新时间:2023-10-16

Im使用mhook C++lib来挂接WinAPI。我有个问题。。这是一个挂钩函数:

BOOL WINAPI HookedReadFile(
    _In_         HANDLE hFile,
    _Out_        LPVOID lpBuffer,
    _In_         DWORD nNumberOfBytesToRead,
    _Out_opt_    LPDWORD lpNumberOfBytesRead,
    _Inout_opt_  LPOVERLAPPED lpOverlapped)
{
    if (inWork && hFile == CryptedFileHandle)
    {
        DWORD readedCount = 0;
        DWORD toReadCount = nNumberOfBytesToRead;
        LPBYTE Buf = new BYTE[toReadCount];
        BOOL result = OriginalReadFile(hFile, Buf, toReadCount, &readedCount, NULL);
        if (result && readedCount > 0)
        {
                    // decryption routine will be here
            std::copy(Buf, Buf + readedCount, (LPBYTE)lpBuffer);
    }
    lpNumberOfBytesRead = &readedCount;
    delete[] Buf;
    return result;
} else
    return OriginalReadFile(hFile, lpBuffer, nNumberOfBytesToRead,      lpNumberOfBytesRead, lpOverlapped);
}

这一定很简单。如果它的加密文件应用程序将即时解密字节(简单的xor)。

但它不起作用。我认为问题出在std::copy(Buf, Buf + readedCount, (LPBYTE)lpBuffer);中,因为程序无法正常读取此文件。

这里有一个主要问题:

pNumberOfBytesRead = &readedCount;

在这里,使pNumberOfBytesRead指向局部变量readedCount。除了试图"返回"指向局部变量的指针(在下一个大括号处超出范围)的问题外,您还忘记了参数,甚至指针,都是按值传递的,所以您只更改pNumberOfBytesRead的本地副本。

相反,您应该使用解引用运算符*并将其指定为值

*pNumberOfBytesRead = readedCount;

至于std::copy调用,它看起来是合法的。也许你有什么问题是在某些代码中你没有向我们展示?您是否尝试过在调试器中逐步执行代码?