c ++ Injector运行但不注入DLL

c++ Injector runs but is not injecting DLL

我在 windows10 上使用 Visual Studios 2017,我编写了一个 C++ 程序来打开一个进程的句柄。程序 运行s 并打开进程。我 运行 调试器中的程序,看起来所有功能都通过了。但是我知道 DLL 没有注入,因为当我 运行 这个应用程序在 32 位游戏上时。我可以删除我使用的 test.dll ,这意味着它没有被使用。关于为什么这个 运行 很好但没有注射的任何建议?

PS remThread 的值为 0x0000011c

// DLL Injector.cpp : Defines the entry point for the console application.

#include "stdafx.h"
int InjectDLL(DWORD, string*);
int getDLLpath(string*);
int getPID(int*);
int getProc(HANDLE*, DWORD);
void enableSeDebug();

int main()
{
    //Escalate privlege 
    enableSeDebug();

    system("title Dll Injector");
    string dllPath = "";
    int PID = -1;

    getDLLpath(&dllPath);
    getPID(&PID);


    InjectDLL(PID, &dllPath);
    system("pause");
    return 0;
}

int getDLLpath(string* dllPath)
{
    cout << "Please enter the path to your DLL file\n";
    cin >> *dllPath;
    return 1;
}

int getPID(int* PID)
{
    cout << "Please enter the PID to your target process\n";
    cin >> *PID;
    return 1;
}

int getProc(HANDLE* handleToProc, DWORD pid)
{

    //Create a handle to the process
    *handleToProc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
    if (*handleToProc != 0)
    {
        cout << "Unable to open process.\n";
        return -1;
    }
    else
    {
        cout << "process opened.\n";
        return 1;
    }
}


int InjectDLL(DWORD PID, string* dllPath)
{

    HANDLE handleToProc;
    LPVOID LoadLibAddr;
    LPVOID baseAddr;
    HANDLE remThread;

    //Get handle to process
    if (getProc(&handleToProc, PID) < 0)
        return -1;

    //Load kernel32 library
    LoadLibAddr = (LPVOID)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA");

    if (!LoadLibAddr)
        return -1;

    //Allocate memory for DLL injection
    baseAddr = VirtualAllocEx(handleToProc, NULL, dllPath->length(), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

    if (!baseAddr)
        return -1;

    //Write dll path
    if (!WriteProcessMemory(handleToProc, baseAddr, dllPath, dllPath->length(), NULL))
        return -1;

    //Create remote thread
    remThread = CreateRemoteThread(handleToProc, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibAddr, baseAddr, 0, NULL);

    if (!remThread)
        return -1;

    //Wait untill DLL exits then deallocate memmory
    WaitForSingleObject(remThread, INFINITE);

    //Freing memmory
    VirtualFreeEx(handleToProc, baseAddr, dllPath->length(), MEM_RELEASE);

    //Closing handles
    if (CloseHandle(remThread) == 0)
    {
        cout << "Failed to close handle to remote thread.\n";
        return -1;
    }

    if (CloseHandle(handleToProc) == 0)
    {
        cout << "Failed to close handle to target process.\n";
        return -1;
    }

    return 1;
}



void enableSeDebug()
{
    /////////////////////////////////////////////////////////
    //   Note: Enabling SeDebugPrivilege adapted from sample
    //     MSDN @ http://msdn.microsoft.com/en-us/library/aa446619%28VS.85%29.aspx
    // Enable SeDebugPrivilege
    HANDLE hToken = NULL;
    TOKEN_PRIVILEGES tokenPriv;
    LUID luidDebug;
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken) != FALSE)
    {
        if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luidDebug) != FALSE)
        {
            tokenPriv.PrivilegeCount = 1;
            tokenPriv.Privileges[0].Luid = luidDebug;
            tokenPriv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
            if (AdjustTokenPrivileges(hToken, FALSE, &tokenPriv, 0, NULL, NULL) != FALSE)
        {
                // Always successful, even in the cases which lead to OpenProcess failure
                cout << "SUCCESSFULLY CHANGED TOKEN PRIVILEGES" << endl;
            }
            else
            {
                cout << "FAILED TO CHANGE TOKEN PRIVILEGES, CODE: " << GetLastError() << endl;
            }
        }
    }
    CloseHandle(hToken);
    // Enable SeDebugPrivilege
}

上面的代码可以打开一个 32 位进程并且 运行s 没有函数调用失败但是 DLL 没有注入?

嗯,我认为 getProc 中的测试应该是:

if (*handleToProc == 0)
    ...

加上其他人在关于测试和报告的评论中所说的 GetLastError

可能还有其他问题,我只是粗略浏览了一下代码

在这里,您不得不将其重写为更多 "proper" 代码。 (不是亲寿)。您在条件检查方面遇到了一些问题。一切都适合我。

#include <windows.h>
#include <iostream>
#include <tlhelp32.h>
#include <string>
using namespace std;

void getPID(DWORD&);
int  getProc(HANDLE&, DWORD);

int main()
{
    cout << "title Dll Injector" << endl;
    string dllPath = "";
    DWORD PID = NULL;
    HANDLE handleToProc = nullptr;

    getPID(PID);

    if (getProc(handleToProc, PID) < 0)
        return -1;


    //Lets check is the handle valid
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (hSnapshot) {
        PROCESSENTRY32 pe32;
        pe32.dwSize = sizeof(PROCESSENTRY32);
        if (Process32First(hSnapshot, &pe32)) {
            do {
                if (pe32.th32ProcessID == GetProcessId(handleToProc))
                {
                    wstring name = wstring(pe32.szExeFile);
                    wcout << "Process id: " << pe32.th32ProcessID << ", Exe filename: " << name << endl;
                }
            } while (Process32Next(hSnapshot, &pe32));
        }
        CloseHandle(hSnapshot);
    }

    //Cleanup is a must!
    CloseHandle(handleToProc);
    return 0;
}

//Create a handle to process
int getProc(HANDLE& handleToProc, DWORD pid)
{
    //Create a handle to the process
    handleToProc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
    if (handleToProc == nullptr)
    {
        DWORD error = GetLastError();

        cout << "Unable to open process. Error: " << error << "\n";
        return -1;
    }
    else
    {
        cout << "process opened.\n";
        return 0;
    }
}

//Get PID from user
void getPID(DWORD& PID)
{
    cout << "Please enter the PID to your target process\n";
    cin >> PID;
}

问题出在指向句柄赋值的指针上,稍后检查句柄是否有效。

我能够解决我的问题。检查 getProc 时我需要做

int getProc(HANDLE* handleToProc, DWORD pid)
{

  //Create a handle to the process
  *handleToProc = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
  if (*handleToProc == NULL) //I changed this line to check for NULL
  {
      cout << "Unable to open process.\n";
      return -1;
  }
  else
  {
      cout << "process opened.\n";
      return 1;
  }
}

我需要更改的第二部分是我的 dllPath,它是一个需要成为 char* 的字符串。因此,当我编写 DLL 字符串时,它是一种不同的编码。使用 char* 指针解决了这个问题,现在注入器可以工作了。