2013-05-18 37 views
1

我使用MS走彎路掛鉤FindNextFile()注入。我已成功配置Detours庫並編寫了一個名爲「Detuors.dll」的dll和一個名爲「FNFSend.exe」的應用程序。以下是代碼:應用程序崩潰時MS走彎路掛鉤,並與Withdll.exe

DLL:

#include <cstdio> 
#include <stdio.h> 
#include <windows.h> 
#include "detours.h" 
#pragma comment (lib,"detours.lib") 

//Prototypes 
extern "C" __declspec(dllexport) BOOL (WINAPI *pFNF)(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData) = FindNextFile; 
extern "C" __declspec(dllexport) BOOL WINAPI MyFNF(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData); 

//Log File 
FILE* pFNFLogFile; 
int counter = 0; 

INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) 
{ 
    switch(Reason) 
    { 
     case DLL_PROCESS_ATTACH: 
      DisableThreadLibraryCalls(hDLL); 
      DetourTransactionBegin(); 
      DetourUpdateThread(GetCurrentThread()); 
      DetourAttach(&(PVOID&)pFNF, MyFNF); 
      if(DetourTransactionCommit() == NO_ERROR) 
       OutputDebugString("FNF() detoured successfully"); 
      else 
       OutputDebugString("FNF() not detoured"); 
      break; 
     case DLL_PROCESS_DETACH: 
      DetourTransactionBegin(); //Detach 
      DetourUpdateThread(GetCurrentThread()); 
      DetourDetach(&(PVOID&)pFNF, MyFNF); 
      DetourTransactionCommit(); 
      break; 
     case DLL_THREAD_ATTACH: 
      DisableThreadLibraryCalls(hDLL); 
      DetourTransactionBegin(); 
      DetourUpdateThread(GetCurrentThread()); 
      DetourAttach(&(PVOID&)pFNF, MyFNF); 
      if(DetourTransactionCommit() == NO_ERROR) 
       OutputDebugString("FNF() detoured successfully"); 
      else 
       OutputDebugString("FNF() not detoured"); 
      break; 
     case DLL_THREAD_DETACH: 
      break; 
    } 
    return TRUE; 
} 

//Open file, write contents, close it 
extern "C" __declspec(dllexport) int WINAPI MyFNF(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData) 
{ 
    counter ++; 
    fopen_s(&pFNFLogFile, "C:\\FNFLog.txt", "a+"); 
    fprintf(pFNFLogFile, "%s\n", counter); 
    fclose(pFNFLogFile); 
    return pFNF(hFindFile, lpFindFileData); 
} 

無論是無誤差地成功編譯代碼。該應用程序遞歸調用FindNextFile(),並將該DLL掛鉤並將計數器寫入文件。

然後我用命名由彎路庫本身提供創建一個進程在它注入一個dll「withdll.exe」的工具。所以我注入我的DLL到應用程序中使用命令:

withdll /d:Detuors.dll「C:\ FNFSend.exe」

注射後,該功能被成功勾搭,即文件在製作但是突然間應用程序崩潰了。在visual studio中進行調試後,我看到「output.c」中的異常如下:

Unhandled exception at 0x6265984f (msvcr90d.dll) in FNFSend.exe: 0xC0000005: 
Access violation reading location 0x00000001. 

請幫助解決問題。

回答

1

%s不是有效format string打印出來的數字。改爲使用%d

通過指定%s您告訴fprintf以字符串形式讀取地址爲counter的存儲器。您嘗試撥打fprintf的第一個值爲1,這就是爲什麼地址0x00000001有訪問衝突的原因。

+0

謝謝史蒂夫!有效 – Faheem