Code:
Option Explicit
Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Const INFINITE As Long = &HFFFFFFFF
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const SYNCHRONIZE As Long = &H100000
Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
Private Declare Function CreateRemoteThread Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lStartAddress As Long, ByVal lParameter As Long, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByVal lpBuffer As Long, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Const MEM_COMMIT As Long = &H1000
Private Const MEM_DECOMMIT As Long = &H4000
Private Const PAGE_EXECUTE_READWRITE As Long = &H40
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Public Function InjectDll(ByVal lPID As Long, ByVal sDllPath As String) As Long
Dim hProcess As Long, hThread As Long
Dim pMem As Long, pLoadLib As Long
Dim lRet As Long, lTemp As Long
pLoadLib = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryW")
If pLoadLib = 10 Then Exit Function
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, lPID)
If hProcess = 0 Then Exit Function
pMem = VirtualAllocEx(hProcess, 0, LenB(sDllPath), MEM_COMMIT, PAGE_EXECUTE_READWRITE)
If pMem = 0 Then
CloseHandle hProcess
Exit Function
End If
lRet = WriteProcessMemory(hProcess, pMem, StrPtr(sDllPath), LenB(sDllPath), lTemp)
If lRet = 0 Or lTemp = 0 Then
10:
VirtualFreeEx hProcess, pMem, LenB(sDllPath), MEM_DECOMMIT
CloseHandle hProcess
InjectDll = 0
Exit Function
End If
hThread = CreateRemoteThread(hProcess, 0, 0, pLoadLib, pMem, 0, lTemp)
If hThread = 0 Then GoTo 10
WaitForSingleObject hThread, INFINITE
GetExitCodeThread hThread, InjectDll
VirtualFreeEx hProcess, pMem, LenB(sDllPath), MEM_DECOMMIT
CloseHandle hThread
CloseHandle hProcess
End Function