首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >堆栈溢出,当我使用Detours拦截CreateFileW时

堆栈溢出,当我使用Detours拦截CreateFileW时
EN

Stack Overflow用户
提问于 2022-06-01 06:34:55
回答 1查看 100关注 0票数 1

我想拦截win32 api CreateFileW,但遇到了“堆栈溢出”错误。我不知道发生了什么,有人能帮我吗?

错误:在KernelBase.dll: 0xC00000FD:堆栈溢出(参数:0x000000000001,0x00000094A6A03F0)中,0x00007FFA76204170 (KernelBase.dll)引发异常。0x00007FFA76204170 (KernelBase.dll)处的未处理异常( detoursExample.exe: 0xC00000FD:堆栈溢出(参数:0x000000000001,0x0000000094A6A03F0))。

代码语言:javascript
复制
#include <Windows.h>
#include <string>
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>

#include"detours.h"  
#pragma comment(lib,"detours.lib") 

void myCreateFile(LPCWSTR pathName) {
    // Open a handle to the file
    HANDLE hFile = CreateFileW(
        pathName, // L".\\NewFile.txt",     // Filename
        GENERIC_WRITE,          // Desired access
        FILE_SHARE_READ,        // Share mode
        NULL,                   // Security attributes
        CREATE_NEW,             // Creates a new file, only if it doesn't already exist
        FILE_ATTRIBUTE_NORMAL,  // Flags and attributes
        NULL);                  // Template file handle

    if (hFile == INVALID_HANDLE_VALUE)
    {
        // Failed to open/create file
        throw("failed to open/create file\n");
        return ;
    }

    // Write data to the file
    std::string strText = "Hello World!"; // For C use LPSTR (char*) or LPWSTR (wchar_t*)
    DWORD bytesWritten;
    WriteFile(
        hFile,            // Handle to the file
        strText.c_str(),  // Buffer to write
        strText.size(),   // Buffer size
        &bytesWritten,    // Bytes written
        nullptr);         // Overlapped

     // Close the handle once we don't need it.
    CloseHandle(hFile);
}

                                      
HANDLE (*oldCreateFile)(LPCWSTR,    
     DWORD,
     DWORD,
     LPSECURITY_ATTRIBUTES,
     DWORD,
     DWORD,
     HANDLE) = CreateFileW;

HANDLE WINAPI newCreateFile(
    _In_ LPCWSTR lpFileName,
    _In_ DWORD dwDesiredAccess,
    _In_ DWORD dwShareMode,
    _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
    _In_ DWORD dwCreationDisposition,
    _In_ DWORD dwFlagsAndAttributes,
    _In_opt_ HANDLE hTemplateFile
) {
    printf("hook success!\n");
    return CreateFileW(
        //L".\\newFiles.txt", // L".\\NewFile.txt",     // Filename
        lpFileName,
        dwDesiredAccess,          // Desired access
        dwShareMode,        // Share mode
        lpSecurityAttributes,                   // Security attributes
        dwCreationDisposition,             // Creates a new file, only if it doesn't already exist
        dwFlagsAndAttributes,  // Flags and attributes
        hTemplateFile);
}

void hook() {
    DetourRestoreAfterWith();                              
    DetourTransactionBegin();                             
    DetourUpdateThread(GetCurrentThread());                
    //DetourAttach((void**)&poldsystem, newsystem);         
    DetourAttach((void**)&oldCreateFile, newCreateFile);         
    DetourTransactionCommit();                             
}

int main() {
    hook();
    myCreateFile(L".\\text.txt");
    getchar();
    return 0;

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-01 06:50:04

newCreateFile中,您需要调用oldCreateFile,而不是CreateFileW。你这样做的方式,你的钩子最终会永远呼唤自己。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72457380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档