Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function GetModuleHandle Lib "kernel32" 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 CreateRemoteThread Lib "kernel32" (ByVal hProcess As Long, lpThreadAttributes As Any, ByVal dwStackSize As Long, lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Const KRN_LOAD As String = "LoadLibraryA"
Private Const KRN_FREE As String = "FreeLibrary"
Private Const KRN_DLL As String = "Kernel32"
Private Const GET_HMOD As String = "GetModuleHandleA"
Private Const PROCESS_QUERY_INFORMATION As Long = &H400
Private Const PROCESS_CREATE_THREAD As Long = &H2
Private Const PROCESS_VM_OPERATION As Long = &H8
Private Const PROCESS_VM_READ As Long = &H10
Private Const PROCESS_VM_WRITE As Long = &H20
Private Const MEM_RELEASE = &H8000
Private Const MEM_FREE = &H10000
Private Const MEM_COMMIT = &H1000
Public Const PAGE_READWRITE = &H4
Public Function LoadModuleInProcess(ByVal ProcessId As Long, ByVal ModulePathName As String) As Long
Dim hKernel32 As Long
Dim hThread As Long
Dim hVirtual As Long
Dim hProcess As Long
Dim hFunc As Long
'Ouverture du process
hProcess = OpenProcess(PROCESS_CREATE_THREAD Or PROCESS_VM_OPERATION Or PROCESS_VM_WRITE Or PROCESS_VM_READ, False, ProcessId)
If hProcess Then
'On recupere le Handle de Kernel32 qui contient la fontion LoadLibrary
hKernel32 = GetModuleHandle(KRN_DLL)
'On recupere l'adresse de la fonction LoadLibrary
hFunc = GetProcAddress(hKernel32, KRN_LOAD)
'On alloue une zone memoire dans le process de destination
hVirtual = VirtualAllocEx(hProcess, ByVal 0&, Len(ModulePathName), MEM_COMMIT, PAGE_READWRITE)
'On ecrit le chemin du module dans la zone memoire que l'on vient d'allouer
If WriteProcessMemory(hProcess, ByVal hVirtual, ByVal ModulePathName, Len(ModulePathName), ByVal 0&) Then
'Creation d'un Thread dans le Process,
'ce Thread va faire appel a LoadLibrary avec pour parametre hVirtual
'qui pointe vers la zone contenant le nom du module a charger
hThread = CreateRemoteThread(hProcess, ByVal 0&, 0, ByVal hFunc, ByVal hVirtual, 0, ByVal 0&)
If hThread Then
'Maitenant on attend la fin du Thread
'Ici on attend 2,5 secondes ce qui est plus que suffisant pour
'charger un module
WaitForSingleObject hThread, &H9C4
'On recupere code de sortie du Thread qui est le code de
'retour de la fonction LoadLibrary et qui contient le Handle
'du module chargé si tout s'est bien passé
GetExitCodeThread hThread, LoadModuleInProcess
'On ferme le Handle du Thread
CloseHandle hThread
End If
End If
'On libere la zone allouée dans le Process
VirtualFreeEx hProcess, hVirtual, Len(ModulePathName), MEM_RELEASE
'On ferme le Handle du Process
CloseHandle hProcess
End If
End Function
'Pour decharger un module c'est pareil sauf qu'on a besoin du Handle du module
'Si on a charger le module nous meme avec la fonction ci dessus c'est pas un probleme,
'sinon il faut le recuperer.
'Pour ca y a 2 methodes possible:
'
'Methode 1: On enumere les modules du process
'Methode 2: On fait un GetModuleHandle dans le process
'
'Ici tant qu'a faire on va rester dans le meme bain et utiliser la methode 2
'La fonction est un peu moins commentée car elle est tres semblable a la premiere
Public Function UnLoadModuleFromProcess(ByVal ProcessId As Long, Optional ByVal ModulePathName As String = "", Optional ByVal hMod As Long = 0) As Long
Dim hKernel32 As Long
Dim hThread As Long
Dim hProcess As Long
Dim hFunc As Long
Dim hVirtual As Long
hProcess = OpenProcess(PROCESS_CREATE_THREAD Or PROCESS_VM_OPERATION Or PROCESS_VM_WRITE Or PROCESS_VM_READ, False, ProcessId)
If hProcess Then
hKernel32 = GetModuleHandle(KRN_DLL)
hFunc = GetProcAddress(hKernel32, GET_HMOD)
' Si on a pas le handle du module on le recupere en faisant appel
' a GetModuleHandle depuis le process cible
If Not hMod Then
hVirtual = VirtualAllocEx(hProcess, ByVal 0&, Len(ModulePathName), MEM_COMMIT, PAGE_READWRITE)
If WriteProcessMemory(hProcess, ByVal hVirtual, ByVal ModulePathName, Len(ModulePathName), ByVal 0&) Then
hThread = CreateRemoteThread(hProcess, ByVal 0&, 0, ByVal hFunc, ByVal hVirtual, 0, ByVal 0&)
If hThread Then
WaitForSingleObject hThread, &H9C4
'on recupere le handle du Module dans hMod
GetExitCodeThread hThread, hMod
CloseHandle hThread
End If
End If
End If
'Si on le handle
If hMod Then
hFunc = GetProcAddress(hKernel32, KRN_FREE)
hThread = CreateRemoteThread(hProcess, ByVal 0&, 0, ByVal hFunc, ByVal hMod, 0, ByVal 0&)
If hThread Then
WaitForSingleObject hThread, &H9C4
'on recupere le code de retour de FreeLibrary (si = 1 le module a bien été dechargé)
GetExitCodeThread hThread, UnLoadModuleFromProcess
CloseHandle hThread
End If
End If
CloseHandle hProcess
End If
End Function
Seul les admins et l'auteur du code lui même peuvent modifier ce code.