I am using visual studios 2017 on windows 10 and I wrote a c++ program to open a handle to a process. The program runs and opens the process. I ran the program in the debugger and it looks like all the functions passed. However I know the DLL is not injecting because when I run this application on a 32 bit game. I can delete the test.dll I used which means it is not being used. Any suggestions on why this runs fine but is not injecting?
PS remThread is value 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
}
The above code can open a 32 bit process and runs without a function call failing but the DLL is not injecting?
I was able to resolve my issue. When checking getProc I needed to do
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;
}
}
The second part I needed to change was my dllPath which was a string needed to be a char*. So when I wrote the DLL string it was a different encoding. Using a char* pointer fixed this and now the injector works.