2011-12-24 75 views
1

我正在嘗試用微軟的彎路做一些基本的連接,但我無法讓它工作。我已經基本上用的是張貼在這個線程的代碼:遇到微軟的彎路問題

How can I hook Windows functions in C/C++?

,但沒有骰子。我更新了DLL代碼中的發送/接收函數以簡單地將數據記錄到文件中,並且我嘗試將主程序掛接到「互聯網檢查程序」程序中,但日誌文件永遠不會被創建,所以看起來dll沒有被注射。我正在運行Windows 7 64位,Visual Studio 10.0,Detours 3.0(我的環境似乎設置正確,沒有問題建設或任何其他)。我創建了一個DLL項目,從上面的鏈接DLL的代碼粘貼,用發送/ recv的更新,例如:

FILE * pSendLogFile; 
fopen_s(&pSendLogFile, "C:\\SendLog.txt", "a+"); 
fprintf(pSendLogFile, "%s\n", buf); 
fclose(pSendLogFile); 

和編譯。然後創建另一個項目,從上面的鏈接在主粘貼代碼,將其設置爲查找chkrzm.exe程序(跳棋),以及硬編碼了DLL路徑:

fullPath = "C:\\Users\\PM\\Documents\\Programs\\C Code\\Test\\DLLTester2\\Debug\\DLLTester2.dll"; 

並運行它,但沒有骰子。任何想法爲什麼我不能得到這個工作?

+2

使用的是缺少所有錯誤檢查的示例代碼。你將無法診斷失敗。您需要修復該問題,始終檢查函數返回值,並在函數失敗時使用GetLastError()來獲取錯誤代碼。 – 2011-12-24 15:01:26

+0

嘿,謝謝你的擡頭。我用調用GetLastError()來調用主代碼,它看起來像我調用CreateRemoteThread()時,它正在退出,出現錯誤代碼5 - 「訪問被拒絕」。我嘗試以管理員身份運行visual studio,但這並沒有幫助;在一些其他論壇上發現了一篇關於同一問題的舊帖子: – 2011-12-25 05:21:07

+0

「這是一項非常特權的操作,因爲它具有特權,使用Technet論壇來解決安全問題。試圖將32位代碼注入64位進程?不行。「我怎麼知道過程是32/64位,如果我注入的DLL是32/64位?我認爲一些程序,例如。在程序x86文件夾下安裝自己的文本板是32位的...是真的嗎?感謝您的進一步協助。 – 2011-12-25 05:24:26

回答

2

僅供參考,已經解決了。要查看哪些進程是32位的,只需按Ctrl-Alt-Delete並轉到任務管理器; 32位進程在其旁邊以* 32列出。也讓我的鉤子工作;這裏是代碼。我放棄了CreateRemoteThread方法,只使用了系統範圍的鉤子。

How to hook external process with SetWindowsHookEx and WH_KEYBOARD http://www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-4 http://www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-7

這個程序簡單地反轉以32位處理文本(如圖中最後一個環節以上):我從縫合的代碼在一起。例如。打開textpad並將鼠標懸停在菜單上;他們的文字應該顛倒過來。

該DLL:

#include <windows.h> 
#include <detours.h> 
#include <stdio.h> 
#include <iostream> 
using namespace std; 


// Initial stuff 
#ifdef _MANAGED 
#pragma managed(push, off) 
#endif 

#pragma comment(lib, "Ws2_32.lib") 
#pragma comment(lib, "detours.lib") 

#pragma data_seg("Shared") 
HHOOK g_hHook = NULL; 
#pragma data_seg() 


// Globals 
HINSTANCE g_hInstance = NULL; 


// ExtTextOut - original 
BOOL (WINAPI * Real_ExtTextOut)(HDC hdc, int X, int Y, UINT options, const RECT* lprc, LPCTSTR text, UINT cbCount, const INT* lpSpacingValues) = ExtTextOut; 

// ExtTextOut - overridden 
BOOL WINAPI Mine_ExtTextOut(HDC hdc, int X, int Y, UINT options, const RECT* lprc, LPCTSTR text, UINT cbCount, const INT* lpSpacingValues) 
{ 
    if (!text) 
     return TRUE; 

    // Make a copy of the supplied string..safely 
    LPWSTR szTemp = (LPWSTR)LocalAlloc(0, (cbCount+1) * 2); 
    memcpy(szTemp, text, cbCount*2); // can't use strcpy here 
    szTemp[cbCount] = L'\0'; // append terminating null 

    // Reverse it.. 
    wcsrev(szTemp); 

    // Pass it on to windows... 
    BOOL rv = Real_ExtTextOut(hdc, X, Y, options, lprc, szTemp, cbCount, lpSpacingValues); 

    // Cleanup 
    LocalFree(szTemp); 

    return TRUE; 
} 


// DLLMain 
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) 
{ 
    switch (ul_reason_for_call) 
    { 
     case DLL_PROCESS_ATTACH: 
      g_hInstance = (HINSTANCE) hModule; 

      DetourTransactionBegin(); 
      DetourUpdateThread(GetCurrentThread()); 
      DetourAttach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut); // <- magic 
      DetourTransactionCommit(); 
      break; 

     case DLL_PROCESS_DETACH: 
      DetourTransactionBegin(); 
      DetourUpdateThread(GetCurrentThread()); 
      DetourDetach(&(PVOID&)Real_ExtTextOut, Mine_ExtTextOut); 
      DetourTransactionCommit(); 
      break; 
    } 

    return TRUE; 
} 


// CBT Hook - dll is hooked into all processes (only 32 bit processes on my machine) 
LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) 
{ 
    if (nCode < 0) 
     return CallNextHookEx(g_hHook, nCode, wParam, lParam); 

    // Return 0 to allow window creation/destruction/activation to proceed as normal. 
    return 0; 
} 


// Install hook 
extern "C" __declspec(dllexport) bool install() 
{ 
    g_hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC) CBTProc, g_hInstance, 0); 

    return g_hHook != NULL; 
} 


// Uninstall hook 
extern "C" __declspec(dllexport) void uninstall() 
{ 
    if (g_hHook) 
    { 
     UnhookWindowsHookEx(g_hHook); 
     g_hHook = NULL; 
    } 
} 

主程序:

#include <Windows.h> 
#include <stdio.h> 
#include <tchar.h> 
#include <iostream> 
using namespace std; 


// Main 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    // Load dll 
    HINSTANCE hinst = LoadLibrary(_T("C:\\Users\\PM\\Documents\\Programs\\C Code\\Test\\DLLTesterFinal\\Debug\\DLLTesterFinal.dll")); 

    if (hinst) 
    { 
     // Get functions 
     typedef bool (*Install)(); 
     typedef void (*Uninstall)(); 
     Install install = (Install) GetProcAddress(hinst, "install"); 
     Uninstall uninstall = (Uninstall) GetProcAddress(hinst, "uninstall"); 
     cout << "GetLastError1: " << GetLastError() << endl << endl; 

     // Install hook 
     bool hookInstalledSuccessfully = install(); 
     cout << "GetLastError2: " << GetLastError() << endl; 
     cout << "Hook installed successfully? " << hookInstalledSuccessfully << endl << endl; 

     // At this point, go to a 32-bit process (eg. textpad, chrome) and hover over menus; their text should get reversed 
     cout << "Text should now be reversed in 32-bit processes" << endl; 
     system ("Pause"); 

     // Uninstall hook 
     uninstall(); 
     cout << endl << "GetLastError3: " << GetLastError() << endl; 
     cout << "Done" << endl; 
     system ("Pause"); 
    } 

    return 0; 
} 

但是在試圖迂迴ExtTextOut在Java應用程序時,Java應用程序崩潰;需要調查一下。

1

我運行Windows 7 64位,Visual Studio的10.0

您必須運行MS進樣DETOUR作爲WIN7管理員用戶。 驗證工作繞道代碼使用樣本繞道3.0使用目標測試。

cmd>$Path/Detours Express 3.0>nmake test