C++封裝長途注入類CreateRemoteThreadEx實例。本站提示廣大學習愛好者:(C++封裝長途注入類CreateRemoteThreadEx實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C++封裝長途注入類CreateRemoteThreadEx實例正文
本文實例講述了C++封裝長途注入類CreateRemoteThreadEx的辦法,分享給年夜家供年夜家參考。詳細辦法以下:
起首,類初始化時傳入要注入的DLL文件名
只應用兩個函數
// 注入DLL到指定的地址空間
BOOL InjectModuleInto(DWORD dwProcessId);
// 從指定的地址空間卸載DLL
BOOL EjectModuleFrom(DWORD dwProcessId);
.h頭文件以下:
#pragma once
#include <windows.h> //在頭文件中包括
class CRemThreadInject
{
public:
CRemThreadInject(LPSTR lpDllName);
~CRemThreadInject(void);
protected:
char m_szDllName[MAX_PATH];
static BOOL EnableDebugPrivilege(BOOL bEnable);
public:
// 注入DLL到指定的地址空間
BOOL InjectModuleInto(DWORD dwProcessId);
// 從指定的地址空間卸載DLL
BOOL EjectModuleFrom(DWORD dwProcessId);
};
.cpp源文件以下:
#include "RemThreadInject.h"
#include <tlhelp32.h>
CRemThreadInject::CRemThreadInject(LPSTR lpDllName)
{
memcpy(m_szDllName, lpDllName, MAX_PATH);
EnableDebugPrivilege(TRUE);
}
CRemThreadInject::~CRemThreadInject(void)
{
EnableDebugPrivilege(FALSE);
}
BOOL CRemThreadInject::EnableDebugPrivilege(BOOL bEnable)
{
HANDLE hToken = INVALID_HANDLE_VALUE;
//OpenProcessToken
if (0 == ::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
{
return FALSE;
}
LUID luid;
//
::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnable)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
if ( !AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL) )
{
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
return FALSE;
}
::CloseHandle(hToken);
return TRUE;
}
// 注入DLL到指定的地址空間
BOOL CRemThreadInject::InjectModuleInto(DWORD dwProcessId)
{
//
if (::GetCurrentProcessId() == dwProcessId)
{
return FALSE;
}
BOOL bFound;
/************************************************************************/
/* 遍歷模塊 */
/************************************************************************/
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
// Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
if( hModuleSnap == INVALID_HANDLE_VALUE )
{
return( FALSE );
}
me32.dwSize = sizeof( MODULEENTRY32 );
if( !Module32First( hModuleSnap, &me32 ) )
{
CloseHandle( hModuleSnap ); // Must clean up the snapshot object!
return( FALSE );
}
do
{
if (stricmp(me32.szModule, m_szDllName) == 0)
{
bFound = TRUE;
break;
}
} while( Module32Next( hModuleSnap, &me32 ) );
// Do not forget to clean up the snapshot object.
CloseHandle( hModuleSnap );
if (bFound) //假如曾經加載了模塊,就不再加載
{
return FALSE;
}
//假如沒加載,翻開過程,長途注入
HANDLE hProcess = ::OpenProcess(PROCESS_CREATE_THREAD |PROCESS_VM_OPERATION |PROCESS_VM_WRITE, FALSE, dwProcessId);
if (hProcess == NULL)
{
return FALSE;
}
HMODULE hKernerl32 = GetModuleHandle("kernel32.dll");
LPTHREAD_START_ROUTINE pfnLoadLibraryA = (LPTHREAD_START_ROUTINE)::GetProcAddress(hKernerl32, "LoadLibraryA");
int cbSize = strlen(m_szDllName)+1;
LPVOID lpRemoteDllName = ::VirtualAllocEx(hProcess, 0, cbSize, MEM_COMMIT, PAGE_READWRITE);
::WriteProcessMemory(hProcess, lpRemoteDllName, m_szDllName, cbSize, NULL);
HANDLE hRemoteThread = ::CreateRemoteThreadEx(hProcess, NULL, 0, pfnLoadLibraryA, lpRemoteDllName, 0, NULL, NULL);
if (NULL == hRemoteThread)
{
::CloseHandle(hProcess);
return FALSE;
}
//期待目的線程運轉停止,即LoadLibraryA函數前往
::WaitForSingleObject(hRemoteThread, INFINITE);
::CloseHandle(hRemoteThread);
::CloseHandle(hProcess);
return TRUE;
}
// 從指定的地址空間卸載DLL
BOOL CRemThreadInject::EjectModuleFrom(DWORD dwProcessId)
{
//
if (::GetCurrentProcessId() == dwProcessId)
{
return FALSE;
}
BOOL bFound;
/************************************************************************/
/* 遍歷模塊 */
/************************************************************************/
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
MODULEENTRY32 me32;
// Take a snapshot of all modules in the specified process.
hModuleSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcessId );
if( hModuleSnap == INVALID_HANDLE_VALUE )
{
return( FALSE );
}
me32.dwSize = sizeof( MODULEENTRY32 );
if( !Module32First( hModuleSnap, &me32 ) )
{
CloseHandle( hModuleSnap ); // Must clean up the snapshot object!
return( FALSE );
}
do
{
if (stricmp(me32.szModule, m_szDllName) == 0)
{
bFound = TRUE;
break;
}
} while( Module32Next( hModuleSnap, &me32 ) );
// Do not forget to clean up the snapshot object.
CloseHandle( hModuleSnap );
if (!bFound) //假如沒有加載模塊,就不克不及卸載
{
return FALSE;
}
//假如加載了,翻開過程,長途注入
HANDLE hProcess = ::OpenProcess(PROCESS_CREATE_THREAD |PROCESS_VM_OPERATION |PROCESS_VM_WRITE, FALSE, dwProcessId);
if (hProcess == NULL)
{
return FALSE;
}
HMODULE hKernerl32 = GetModuleHandle("kernel32.dll");
LPTHREAD_START_ROUTINE pfnFreeLibrary = (LPTHREAD_START_ROUTINE)::GetProcAddress(hKernerl32, "FreeLibrary");
int cbSize = strlen(m_szDllName)+1;
LPVOID lpRemoteDllName = ::VirtualAllocEx(hProcess, 0, cbSize, MEM_COMMIT, PAGE_READWRITE);
::WriteProcessMemory(hProcess, lpRemoteDllName, m_szDllName, cbSize, NULL);
HANDLE hRemoteThread = ::CreateRemoteThreadEx(hProcess, NULL, 0, pfnFreeLibrary, lpRemoteDllName, 0, NULL, NULL);
if (NULL == hRemoteThread)
{
::CloseHandle(hProcess);
return FALSE;
}
//期待目的線程運轉停止,即LoadLibraryA函數前往
::WaitForSingleObject(hRemoteThread, INFINITE);
::CloseHandle(hRemoteThread);
::CloseHandle(hProcess);
return TRUE;
}
願望本文所述對年夜家的C++法式設計有所贊助。