【问题标题】:Hooking ReadFile WinAPI挂钩 ReadFile WinAPI
【发布时间】:2013-12-02 10:11:38
【问题描述】:

我正在使用 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);
}

这必须很简单。如果其加密文件应用程序将动态解密字节(简单的异或)。

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

【问题讨论】:

    标签: c++ winapi visual-c++ hook


    【解决方案1】:

    这里有个大问题:

    pNumberOfBytesRead = &readedCount;
    

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

    您应该改为使用取消引用运算符* 并将其作为值分配给它

    *pNumberOfBytesRead = readedCount;
    

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

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多