2013-05-18 8 views
1

MS Detours를 사용하여 FindNextFile()에 연결합니다. Detours 라이브러리를 성공적으로 구성했으며 "Detuors.dll"이라는 dll과 "FNFSend.exe"라는 응용 프로그램을 작성했습니다.응용 프로그램 MS Detours에 연결하고 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은이 파일을 후크하여 파일에 카운터를 씁니다.

그런 다음 detours 라이브러리 자체에서 제공하는 "withdll.exe"라는 도구를 사용하여 dll이 삽입 된 프로세스를 만들었습니다. 그래서 명령을 사용하여 응용 프로그램에 내 DLL을 주입 :

withdll /d:Detuors.dll "C : \ FNFSend.exe"파일이 이루어집니다 즉

가 주입 한 후, 함수가 성공적으로 걸려있다 갑자기 응용 프로그램이 충돌합니다.

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

가 친절하게 문제를 정류에 도움을 다음과 같이 Visual Studio에서 디버깅 후, 나는 "output.c"의 예외를 보았다.

답변

1

%s은 숫자 인쇄용으로는 format string이 아닙니다. 대신 %d을 사용하십시오.

%s을 지정하면 counter의 메모리를 문자열로 읽을 것을 fprintf에게 알리는 것입니다. fprintf을 호출하는 첫 번째 값은 1이므로 주소가 0x00000001 인 액세스 위반이있는 것입니다.

+0

감사합니다. 스티브! 효과가있다. – Faheem

관련 문제