【问题标题】:Having trouble with microsoft detours微软走弯路遇到麻烦
【发布时间】:2011-12-24 10:59:39
【问题描述】:

我正在尝试使用 microsoft detours 进行一些基本的挂钩,但我无法让它工作。我基本上使用了该线程中发布的代码:

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

但没有骰子。我更新了 DLL 代码中的发送/接收函数以简单地将数据记录到文件中,并且我尝试将主程序挂接到“互联网检查程序”程序中,但从未创建过日志文件,所以看起来 dll没有注入。

我正在运行 Windows 7 64 位、Visual Studio 10.0、Detours 3.0(我的环境似乎设置正确,构建或任何问题都没有问题)。我创建了一个 DLL 项目,从上面的链接粘贴到 DLL 代码中,发送/接收更新如下:

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";

并运行它,但没有骰子。知道为什么我不能让它工作吗?

【问题讨论】:

  • 您使用的示例代码缺少所有错误检查。您将无法诊断故障。您需要解决这个问题,始终检查函数返回值,在函数失败时使用 GetLastError() 获取错误代码。
  • 您好,感谢您的提醒。我在主代码中添加了对 GetLastError () 的调用,看起来当我调用 CreateRemoteThread () 时,它以错误代码 5 退出 - “访问被拒绝”。我尝试以管理员身份运行 Visual Studio,但这没有帮助;又看了一些,巧合的是你在其他论坛上发现了一篇关于同一问题的旧帖子:
  • "这是一个高权限的操作,获得的特权。使用 Technet 论坛追查安全问题。同时,您是否试图将 32 位代码注入 64 位进程?没办法。”我如何知道进程是否为 32/64 位,以及我注入的 dll 是否为 32/64 位?我假设一些程序,例如。 textpad,在程序x86文件夹下安装自己是32位......是真的吗?感谢您提供进一步的帮助。

标签: c++ dll code-injection detours


【解决方案1】:

仅供参考,解决了这个问题。要查看哪些进程是 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 位进程中反转文本(如上面最后一个链接所示)。例如。打开文本板并将鼠标悬停在菜单上;他们的文字应该被颠倒。

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;
}

然而,当试图绕过 Java 应用程序中的 ExtTextOut 时,Java 应用程序会崩溃;需要调查一下。

【讨论】:

    【解决方案2】:

    我正在运行 Windows 7 64 位,Visual Studio 10.0

    您必须在 WIN7 上以管理员用户身份运行 MS DETOUR INJECT。 要验证有效的 detour 代码,请使用 detour 3.0 的示例,使用 make target test。

    cmd&gt;$Path/Detours Express 3.0&gt;nmake test

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 2020-06-15
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      相关资源
      最近更新 更多