DLL注入器的问题

时间:2015-01-17 18:13:25

标签: c++ windows dll

我最近一直在学习C ++,而且我正处于可以构建足够程序的阶段。

目前我只是尝试将一个DLL注入我选择的进程中,我遇到了麻烦,因为当它注入时工作正常,但是当我没有DLL时,它仍然像注入一样。

请帮助。

我的来源:

#include <iostream>
#include <Windows.h>
#include <TlHelp32.h>

using namespace std;
bool InjectDLL(DWORD ProcessID);

char FileToInject[] = "DLL.dll";
char ProcessName[] = "ac_client.exe";
typedef HINSTANCE (*fpLoadLibrary)(char*);

int main()
{
    DWORD processId = NULL;

    PROCESSENTRY32 pe32 = {sizeof(PROCESSENTRY32)};
    HANDLE hProcSnap;

    while(!processId)
    {
        system("CLS");
        cout << "|Coded by Proton|" << endl;
        cout << "Waiting for AssaultCube ..." << endl;
        hProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

        if(Process32First(hProcSnap, &pe32))
        {
            do
            {
                if(!strcmp(pe32.szExeFile, ProcessName))
                {
                    processId = pe32.th32ProcessID;
                    break;
                }
            }
            while(Process32Next(hProcSnap, &pe32));
        }
        Sleep(1000);
    }


    while(!InjectDLL(processId))
    {
        system("CLS");
        cout << "DLL failed to inject." << endl;
        Sleep(1000);
    }

    cout << "DLL Injected." << endl << endl;
    cout << "Closing Injector in 5 seconds ..." << endl;

    CloseHandle(hProcSnap);
    Sleep(5000);

    return 0;
}

bool InjectDLL(DWORD ProcessID)
{
    HANDLE hProc;
    LPVOID paramAddr;

    HINSTANCE hDll = LoadLibrary("KERNEL32");

    fpLoadLibrary LoadLibraryAddr = (fpLoadLibrary)GetProcAddress(hDll, "LoadLibraryA");

    hProc = OpenProcess (PROCESS_ALL_ACCESS, false, ProcessID);

    char dllPath[250] = "C:\\";

    strcat(dllPath, FileToInject);

    paramAddr = VirtualAllocEx(hProc, 0, strlen(dllPath)+1, MEM_COMMIT, PAGE_READWRITE);
    bool memoryWritten = WriteProcessMemory(hProc, paramAddr, dllPath, strlen(dllPath)+1, NULL);

    CreateRemoteThread(hProc, 0, 0, (LPTHREAD_START_ROUTINE)LoadLibraryAddr, paramAddr, 0, 0);

    CloseHandle(hProc);

    return memoryWritten;
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

请勿忽略CreateRemoteThread的返回值。

使用线程句柄检查它是否仍在运行(WaitForSingleObject超时),如果退出,请检查其返回码。