首先我想說的是,我並不想破解遊戲。我實際上是由我嘗試注入的公司僱用的。 :)
我想知道如何從一個已經注入的DLL中調用一個函數。
因此,我使用CreateRemoteThread()成功注入並加載了我的DLL。下面你可以看到注射的一個片段:從注入的DLL中調用函數
private static bool Inject(Process pToBeInjected, string sDllPath,out string sError, out IntPtr hwnd, out IntPtr hLibModule)
{
IntPtr zeroPtr = (IntPtr)0;
hLibModule = zeroPtr;
IntPtr hProcess = NativeUtils.OpenProcess(
(0x2 | 0x8 | 0x10 | 0x20 | 0x400), //create thread, query info, operation ,write, and read
1,
(uint)pToBeInjected.Id);
hwnd = hProcess;
IntPtr loadLibH = NativeUtils.GetProcAddress(NativeUtils.GetModuleHandle("kernel32.dll"),"LoadLibraryA");
IntPtr dllAddress = NativeUtils.VirtualAllocEx(
hProcess,
(IntPtr)null,
(IntPtr)sDllPath.Length, //520 bytes should be enough
(uint)NativeUtils.AllocationType.Commit |
(uint)NativeUtils.AllocationType.Reserve,
(uint)NativeUtils.MemoryProtection.ExecuteReadWrite);
byte[] bytes = CalcBytes(sDllPath);
IntPtr ipTmp = IntPtr.Zero;
NativeUtils.WriteProcessMemory(
hProcess,
dllAddress,
bytes,
(uint)bytes.Length,
out ipTmp);
IntPtr hThread = NativeUtils.CreateRemoteThread(
hProcess,
(IntPtr)null,
(IntPtr)0,
loadLibH, //handle to LoabLibrary function
dllAddress,//Address of the dll in remote process
0,
(IntPtr)null);
uint retV= NativeUtils.WaitForSingleObject(hThread, NativeUtils.INFINITE_WAIT);
bool exitR = NativeUtils.GetExitCodeThread(hThread, out hLibModule);
return true;
}
注:錯誤檢查和簡潔取出釋放資源,但是請放心,我檢查所有的指針和自由我的資源。
上面的函數退出後,我有一個非零的模塊句柄到我的DLL由LoadLibrary通過hLibModule
返回,這意味着DLL正確加載。
我的DLL是一個C#類庫,旨在顯示一個消息框(用於測試)。我已經嘗試過測試該功能,並彈出消息框。它看起來像這樣:
public class Class1
{
public static void ThreadFunc(IntPtr param)
{
IntPtr libPtr = LoadLibrary("user32.dll");
MessageBox(IntPtr.Zero, "I'm ALIVE!!!!", "InjectedDll", 0);
}
[DllImport("kernel32", SetLastError = true)]
public static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);
}
我從Visual Studio編譯它,DLL出現在Debug文件夾中。然後我將DLL的完整路徑傳遞給注入器。
注入到目標進程後,我不知道如何從注入的DLL中調用我的ThreadFunc
,因此它永遠不會執行。
我不能使用GetProcAddress(hLibModule,"ThreadFunc")
,因爲我沒有進程,所以答案肯定在於調用CreateRemoteThread()。另外,我已經讀過DllMain不再允許使用.NET DLL,所以我也無法獲得任何自由執行。
有沒有人有任何想法如何從注入的DLL調用函數?
預先感謝您。
我找到了一個鏈接,使用您給我的關鍵字。這是我需要做的嗎?它說我需要編寫一個引導DLL,然後注入。他們還表示,任何注射技術都可以發揮作用。如果我使用上述技術注入引導DLL,那麼我將如何獲得引導DLL運行?謝謝回答。 –
prettyCode
2010-11-03 06:46:15
是的,這就是方法。按照他的注射代碼鏈接。 – 2010-11-03 10:22:00