Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 微软的弯路有问题吗_C++_Dll_Code Injection_Detours - Fatal编程技术网

C++ 微软的弯路有问题吗

C++ 微软的弯路有问题吗,c++,dll,code-injection,detours,C++,Dll,Code Injection,Detours,我正试图用微软的迂回路线做一些基本的勾搭,但我无法让它工作。我基本上使用了此线程中发布的代码: 但是没有骰子。我更新了DLL代码中的发送/接收函数,将数据简单地记录到一个文件中,并尝试将主程序挂钩到“internet checkers”程序中,但从未创建日志文件,因此似乎没有插入DLL 我运行的是64位Windows 7、Visual Studio 10.0和Detours 3.0(我的环境似乎设置正确,没有任何问题)。我创建了一个DLL项目,粘贴在上面链接的DLL代码中,并更新了send/r

我正试图用微软的迂回路线做一些基本的勾搭,但我无法让它工作。我基本上使用了此线程中发布的代码:

但是没有骰子。我更新了DLL代码中的发送/接收函数,将数据简单地记录到一个文件中,并尝试将主程序挂钩到“internet checkers”程序中,但从未创建日志文件,因此似乎没有插入DLL

我运行的是64位Windows 7、Visual Studio 10.0和Detours 3.0(我的环境似乎设置正确,没有任何问题)。我创建了一个DLL项目,粘贴在上面链接的DLL代码中,并更新了send/recv:

FILE * pSendLogFile;
fopen_s(&pSendLogFile, "C:\\SendLog.txt", "a+");
fprintf(pSendLogFile, "%s\n", buf);
fclose(pSendLogFile);
并编译。然后创建另一个项目,从上面的链接粘贴到主代码中,将其设置为查找
chkrzm.exe
程序(checkers),并将DLL路径硬编码为:

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

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

仅供参考,我已经解决了这个问题。要查看哪些进程是32位的,只需按住ctrl-alt-delete键并转到任务管理器;32位进程旁边以*32列出。我的钩子也开始工作了;这是代码。我放弃了CreateRemoteThread方法,只使用了一个系统范围的钩子。我将以下代码缝合在一起:

这个程序只是在32位进程中反转文本(如上面最后一个链接所示)。例如,打开文本板并将鼠标悬停在菜单上;他们的文字应该颠倒过来

动态链接库:

#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;
    }
}
#包括
#包括
#包括
#包括
使用名称空间std;
//初始材料
#ifdef_管理
#pragma管理(推、关)
#恩迪夫
#pragma注释(lib,“Ws2_32.lib”)
#pragma注释(lib,“detours.lib”)
#pragma data_seg(“共享”)
HHOOK g_HHOOK=NULL;
#pragma data_seg()
//全球的
HINSTANCE g_HINSTANCE=NULL;
//ExtTextOut-原件
BOOL(WINAPI*Real_ExtTextOut)(HDC-HDC、int-X、int-Y、UINT选项、const-RECT*lprc、LPCTSTR-text、UINT-cbCount、const-int*lpspacingvalue)=ExtTextOut;
//ExtTextOut-被覆盖
BOOL WINAPI Mine_ExtTextOut(HDC HDC、int X、int Y、UINT选项、const RECT*lprc、LPCTSTR text、UINT cbCount、const int*lpspacingvalue)
{
如果(!text)
返回TRUE;
//安全地复制所提供的字符串
LPWSTR szTemp=(LPWSTR)LocalAlloc(0,(cbCount+1)*2);
memcpy(szTemp,text,cbCount*2);//这里不能使用strcpy
szTemp[cbCount]=L'\0';//追加终止null
//把它倒过来。。
wcsrev(szTemp);
//将其传递到windows。。。
BOOL rv=Real_ExtTextOut(hdc、X、Y、options、lprc、szTemp、cbCount、lpspacingvalue);
//清理
本地免费(szTemp);
返回TRUE;
}
//德尔曼
BOOL APIENTRY DllMain(句柄模块、DWORD ul_原因、LPVOID LPREFERED)
{
开关(ul\u呼叫原因\u)
{
案例DLL\u进程\u附加:
g_hInstance=(hInstance)hModule;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
绕道附加(&(PVOID&)Real_ExtTextOut,Mine_ExtTextOut);//
我运行的是64位Windows7,VisualStudio10.0

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


cmd>$Path/Detours Express 3.0>nmake test

您正在使用的示例代码缺少所有错误检查。您将无法诊断故障。您需要修复此问题,始终检查函数返回值,使用GetLastError()当函数失败时获取错误代码。嘿,谢谢你的提醒。我在主代码中添加了对GetLastError()的调用,看起来当我调用CreateRemoteThread()时,它正在退出,错误代码为5-“访问被拒绝”。我尝试以管理员的身份运行visual studio,但没有任何帮助;查看了更多内容,碰巧发现您在其他论坛上发布了一篇关于同一问题的旧帖子:“这是一个高度特权的操作,尽管有特权。请使用Technet论坛来解决安全问题。同时,您是否试图将32位代码注入64位进程?没有办法。”。“我如何知道进程是否为32/64位,以及我正在注入的dll是否为32/64位?我假设一些程序(例如textpad)是32位的,它们安装在program x86文件夹下……这是真的吗?谢谢您的进一步帮助。”。
#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;
}