繁体   English   中英

挂钩和取消挂钩一个文件 DLL

[英]Hook and unhook one file DLL

我尝试将文件 DLL 挂接到控制台应用程序中。 这段代码

#include "pch.h"
#include <vector>
#include <string>
#include <windows.h>
#include <Tlhelp32.h>

using std::vector;
using std::string;

int main(void)
{
while (true)
{
    vector<string>processNames;
    PROCESSENTRY32 pe32;
    pe32.dwSize = sizeof(PROCESSENTRY32);
    HANDLE hTool32 = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    BOOL bProcess = Process32First(hTool32, &pe32);
    if (bProcess == TRUE)
    {
        while ((Process32Next(hTool32, &pe32)) == TRUE)
        {
            processNames.push_back(pe32.szExeFile);
            if (strcmp(pe32.szExeFile, "ConsoleApplication4.exe") == 0)
            {
                char* DirPath = new char[MAX_PATH];
                char* FullPath = new char[MAX_PATH];
                GetCurrentDirectory(MAX_PATH, DirPath);
                sprintf_s(FullPath, MAX_PATH, "%s\\..\\ConsoleApplication1\\ConsoleApplication1.dll", DirPath);

                FILE *pFile;
                if (fopen_s(&pFile, FullPath, "r") || !pFile)
                {
                    OutputDebugString("[Hook] File name or file does not exist");
                    OutputDebugString(FullPath);
                    return -1;
                }
                fclose(pFile);

                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
                if (!hProcess)
                {
                    OutputDebugString("[Hook] Open process fail");
                    return -1;
                }

                //attach
                LPVOID LoadLibraryAddr = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
                if (!LoadLibraryAddr)
                {
                    OutputDebugString("[Hook] Load LoadLibraryA fail");
                    return -1;
                }

                LPVOID LLParam = (LPVOID)VirtualAllocEx(hProcess, NULL, strlen(FullPath), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);

                if (!WriteProcessMemory(hProcess, LLParam, FullPath, strlen(FullPath), NULL))
                {
                    OutputDebugString("[Hook] Write process fail");
                    return -1;
                }

                HANDLE hHandle = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, LLParam, NULL, NULL);
                if (!hHandle)
                {
                    OutputDebugString("[Hook] Hooked fail");
                    return -1;
                }

                system("pause");

                //detach
                LoadLibraryAddr = (LPVOID)GetProcAddress(GetModuleHandle("kernel32.dll"), "FreeLibrary");
                if (!LoadLibraryAddr)
                {
                    OutputDebugString("[Hook] Load FreeLibrary fail");
                    return -1;
                }

                hHandle = CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, LLParam, NULL, NULL);
                if (!hHandle)
                {
                    OutputDebugString("[Hook] detach fail");
                    return -1;
                }

                CloseHandle(hProcess);
                delete[] DirPath;
                delete[] FullPath;

                system("pause");

                return 0;
            }
        }
    }
    CloseHandle(hTool32);
}
return 0;
}

我对此有一些疑问: - 为什么此代码无法分离文件 dll? - 为什么我更改 LoadLibraryA -> LoadLibrary :加载 LoadLibrary 失败? - 为什么我更改 LoadLibraryA -> LoadLibraryW:文件 dll 没有附加? - 在 Mutibyte 中运行的代码很好,但转换为 Unicode,文件 dll 没有附加?

谢谢,

这适用于多字节但不能使用 unicode 字符集编译的原因是因为您正在传递一个常规 c 字符串,它是一个常规字符数组。 当您将构建类型设置为使用多字节 LoadLibrary() 时,将解析为 LoadLibraryA() 的 ansi 版本。 如果您想在您的项目属性中使用 Unicode,它将解析为 LoadLibraryW(),您将需要传递一个 unicode 字符数组,通常是 wchar_t[]。

即使在 unicode 模式下编译,您仍然可以调用 LoadLibraryA() 并传递 ac 字符串,但您必须专门调用该函数的 A (ansi) 版本,而不是依赖于为您解析它们的 #ifdef 预处理器语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM