2013-05-15 85 views
3

好吧,我在VB.net中做了一個DLL注入器。它適用於除我以外的任何DLL。所以我知道問題出在DLL上。這裏是注射器的代碼:C++ DLL注入後不執行函數

Private Function Inject(ByVal pID As Integer, ByVal dllLocation As String) As Boolean 
    Dim hProcess As Integer = OpenProcess(&H1F0FFF, 1, pID) 
    If hProcess = 0 Then 
     Return False 
     MessageBox.Show("Could not open process!") 
    End If 
    Dim dllBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(dllLocation) 
    Dim allocAddress As Integer = VirtualAllocEx(hProcess, 0, dllBytes.Length, &H1000, &H4) 
    If allocAddress = Nothing Then 
     Return False 
     MessageBox.Show("Could not allocate the address!") 
    End If 
    Dim kernelMod As Integer = GetModuleHandle("kernel32.dll") 
    Dim loadLibAddr = GetProcAddress(kernelMod, "LoadLibraryA") 
    If (kernelMod = 0) Then 
     MessageBox.Show("Could not get the Module") 
     Return False 
    End If 
    If (loadLibAddr = 0) Then 
     MessageBox.Show("get the Process address!") 
     Return False 
    End If 
    WriteProcessMemory(hProcess, allocAddress, dllBytes, dllBytes.Length, 0) 
    Dim libThread As Integer = CreateRemoteThread(hProcess, 0, 0, loadLibAddr, allocAddress, 0, 0) 

    If libThread = 0 Then 
     Return False 
     MessageBox.Show("Error Creating thread!") 
    Else 
     WaitForSingleObject(libThread, 5000) 
     CloseHandle(libThread) 
    End If 
    CloseHandle(hProcess) 
    Threading.Thread.Sleep(1000) 
    Return True 
End Function 

這寫入進程內存並創建一個遠程線程。

現在我的項目有兩個文件:頭文件和CPP文件。

頁眉:

#ifdef MAINLIB_EXPORTS 
#define MAINLIB_API __declspec(dllexport) 
#else 
#define MAINLIB_API __declspec(dllexport) 
#endif 

extern "C" MAINLIB_API DWORD TestFunction(); 

而CPP:

#define WIN32_LEAN_AND_MEAN 

#include <windows.h> 
#include <stdio.h> 
#include "dll.h" 
#include "Urlmon.h" 

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 
{ 
     hModule; 
     lpReserved; 

    switch (ul_reason_for_call) 
     { 
       case DLL_PROCESS_ATTACH: 
       case DLL_THREAD_ATTACH: 
       case DLL_THREAD_DETACH: 
       case DLL_PROCESS_DETACH: 
         break; 
    } 

    return TRUE; 
} 

DWORD TestFunction() 
{  
     MessageBox(0, TEXT("LOL"), TEXT("LMAO"), MB_OK); 
     return 1; 
} 

從我瞭解的是,本應在注射運行TestFunction。但事實並非如此。任何解決方案/有用的網頁我可以使用?

回答

3

代碼中沒有任何內容指定需要調用TestFunction。一旦DLL連接到進程,只調用DllMain和需要初始化的全局對象。處理DLL_PROCESS_ATTACH時,您需要致電TestFunction

DWORD TestFunction(); 

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) 
{ 
    hModule; 
    lpReserved; 

    switch (ul_reason_for_call) 
    { 
    case DLL_PROCESS_ATTACH: 
     TestFunction(); // < call TestFunction ONCE when dll is loaded 
     break; 

    case DLL_THREAD_ATTACH: 
    case DLL_THREAD_DETACH: 
    case DLL_PROCESS_DETACH: 
     break; 
    } 

    return TRUE; 
} 

DWORD TestFunction() 
{  
     MessageBox(0, TEXT("LOL"), TEXT("LMAO"), MB_OK); 
     return 1; 
}