Yufan
CInjectDLL a class for DLL injection
to inject use Inject()
to uninject use Uninject()
*/
#pragma once
#include "stdafx.h"
class CInjectDLL
{
public:
CInjectDLL(void);
BOOL Inject(const DWORD dwRemoteProcessID, const LPCTSTR& lpwszRemoteDllFullPath);
BOOL Inject(const LPCWSTR wszProcessName, const LPCTSTR& lpwszRemoteDllFullPath);
BOOL Uninject(const DWORD dwRemoteProcessID, const LPCTSTR& lpwszRemoteDllFullPath);
BOOL Uninject(const LPCWSTR wszProcessName, const LPCTSTR& lpwszRemoteDllFullPath);
~CInjectDLL(void);
private:
BOOL AdjustProcessTokenPrivilege();
BOOL GetProcessID(const wchar_t* wszProcessName, DWORD& dwProcID);
};
Yufan
CInjectDLL a class for DLL injection
to inject use Inject()
to uninject use Uninject()
*/
#pragma once
#include "StdAfx.h"
#include "InjectDLL.h"
#include <Windows.h>
#include <string>
#include "tlhelp32.h"
CInjectDLL::CInjectDLL(void)
{
}
CInjectDLL::~CInjectDLL(void)
{
}
BOOL CInjectDLL::Inject(const DWORD dwRemoteProcessID, const LPCTSTR& lpwszRemoteDllFullPath)
{
std::wstring wstrRemoteDllFullPath = lpwszRemoteDllFullPath;
BOOL bRet;
if (!AdjustProcessTokenPrivilege())
{
OutputDebugString(_T("AdjustProcessTokenPrivilege failn"));
}
HANDLE hRemoteProgress = ::OpenProcess(PROCESS\_ALL\_ACCESS, FALSE, dwRemoteProcessID);
if (hRemoteProgress == NULL)
{
OutputDebugString(_T("OpenProcess failn"));
return FALSE;
}
DWORD dwMemSize = sizeof(wchar_t)*wstrRemoteDllFullPath.length()+1;
wchar\_t* wszDllPath = reinterpret\_cast<wchar\_t*>(::VirtualAllocEx(hRemoteProgress, NULL, dwMemSize, MEM\_COMMIT, PAGE_READWRITE));
if (wszDllPath == NULL)
{
OutputDebugString(_T("VirtualAllocEx failn"));
::CloseHandle(hRemoteProgress);
return FALSE;
}
bRet = ::WriteProcessMemory(hRemoteProgress, wszDllPath, wstrRemoteDllFullPath.c_str(), dwMemSize, NULL);
if (!bRet)
{
OutputDebugString(_T("WriteProcessMemory failn"));
::CloseHandle(hRemoteProgress);
return FALSE;
}
FARPROC pfnFunAddr = ::GetProcAddress(::GetModuleHandle(_T("Kernel32")),"LoadLibraryW");
if (pfnFunAddr == NULL)
{
OutputDebugString(_T("GetProcAddress failn"));
::CloseHandle(hRemoteProgress);
return FALSE;
}
HANDLE hCreateThread;
hCreateThread = ::CreateRemoteThread(hRemoteProgress, NULL, 0, (LPTHREAD\_START\_ROUTINE) pfnFunAddr, wszDllPath, 0, NULL);
if (hCreateThread == NULL)
{
OutputDebugString(_T("CreateRemoteThread failn"));
::CloseHandle(hRemoteProgress);
::CloseHandle(hCreateThread);
return FALSE;
}
::VirtualFreeEx(hRemoteProgress, reinterpret\_cast<LPVOID>(wszDllPath), dwMemSize, MEM\_COMMIT);
::CloseHandle(hCreateThread);
::CloseHandle(hRemoteProgress);
return TRUE;
}
BOOL CInjectDLL::Inject(const wchar_t* wszProcessName, const LPCTSTR& lpwszRemoteDllFullPath)
{
DWORD dwProcID;
GetProcessID(wszProcessName,dwProcID);
if (Inject(dwProcID, lpwszRemoteDllFullPath))
return TRUE;
else
return FALSE;
}
BOOL CInjectDLL::Uninject(const DWORD dwRemoteProcessID, const LPCTSTR& lpwszRemoteDllFullPath)
{
std::wstring wstrRemoteDllFullPath = lpwszRemoteDllFullPath;
HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwRemoteProcessID);
MODULEENTRY32 Me32 = {0};
Me32.dwSize = sizeof(MODULEENTRY32);
BOOL bRet = ::Module32First(hSnap, &Me32);
while (bRet)
{
if (wcscmp(Me32.szExePath, wstrRemoteDllFullPath.c_str()) == 0)
{
break;
}
bRet = ::Module32Next(hSnap, &Me32);
}
::CloseHandle(hSnap);
HANDLE hRemoteProgress = ::OpenProcess(PROCESS\_ALL\_ACCESS, FALSE, dwRemoteProcessID);
if (hRemoteProgress == NULL)
{
OutputDebugString(_T("OpenProcess failn"));
return FALSE;
}
FARPROC pfnFunAddr = ::GetProcAddress(::GetModuleHandle(_T("Kernel32")),"FreeLibrary");
if (pfnFunAddr == NULL)
{
OutputDebugString(_T("GetProcAddress failn"));
::CloseHandle(hRemoteProgress);
return FALSE;
}
HANDLE hCreateThread;
hCreateThread = ::CreateRemoteThread(hRemoteProgress, NULL, 0, (LPTHREAD\_START\_ROUTINE)pfnFunAddr, Me32.hModule, 0, NULL);
if (hCreateThread == NULL)
{
OutputDebugString(_T("CreateRemoteThread failn"));
::CloseHandle(hRemoteProgress);
::CloseHandle(hCreateThread);
return FALSE;
}
::CloseHandle(hCreateThread);
::CloseHandle(hRemoteProgress);
return TRUE;
}
BOOL CInjectDLL::Uninject(const wchar_t* wszProcessName, const LPCTSTR& lpwszRemoteDllFullPath)
{
DWORD dwProcID;
GetProcessID(wszProcessName,dwProcID);
if (Uninject(dwProcID, lpwszRemoteDllFullPath))
return TRUE;
else
return FALSE;
}
BOOL CInjectDLL::AdjustProcessTokenPrivilege()
{
LUID luidTmp;
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN\_ADJUST\_PRIVILEGES | TOKEN_QUERY, &hToken))
{
OutputDebugString(_T("AdjustProcessTokenPrivilege OpenProcessToken Failed ! n"));
return FALSE;
}
if(!LookupPrivilegeValue(NULL, SE\_DEBUG\_NAME, &luidTmp))
{
OutputDebugString(_T("AdjustProcessTokenPrivilege LookupPrivilegeValue Failed ! n"));
CloseHandle(hToken);
return FALSE;
}
tkp.PrivilegeCount = 1;
tkp.Privileges\[0\].Luid = luidTmp;
tkp.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED;
if(!AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(tkp), NULL, NULL))
{
OutputDebugString(_T("AdjustProcessTokenPrivilege AdjustTokenPrivileges Failed ! n"));
CloseHandle(hToken);
return FALSE;
}
return TRUE;
}
BOOL CInjectDLL::GetProcessID(const wchar_t* wszProcessName, DWORD& dwProcID)
{
HANDLE hSnapShot = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, dwProcID );
PROCESSENTRY32 pe = {sizeof(pe)};
BOOL bOk = ::Process32First( hSnapShot, &pe );
while( bOk )
{
if (wcsstr(pe.szExeFile, wszProcessName)!=NULL)
{
dwProcID = pe.th32ProcessID;
return TRUE;
}
bOk = ::Process32Next( hSnapShot, &pe );
}
::CloseHandle(hSnapShot);
return FALSE;
}