2014-09-14

C# 注入DLL (Inject DLL by CSharp)

解說:http://www.javaworld.com.tw/roller/qing/entry/2007_5_18_DLL_injection
參考這篇 http://blog.csdn.net/intcry/article/details/6318596 程式碼

















看完一定會再找其他的,看這篇.http://wenku.baidu.com/view/cfdcc20ef12d2af90242e626.html
然後先去了解createremotethread是在幹什的。

通常都是注入一個非託管的dll(不是運行在CLR上),下面這是一個C++上的標準DLL,帶有DllMain(入口函數),不是普通的類庫DLL,而C#無法產生一個帶有入口函數的DLL。

以下這是要注入的非託管DLL代碼(打開txt檔寫入資料)
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) {
/* open file */
FILE *file;
fopen_s(&file, "C:\temp.txt", "a+");
switch(Reason) {
case DLL_PROCESS_ATTACH:
fprintf(file, "DLL attach function called.n");
break;
case DLL_PROCESS_DETACH:
fprintf(file, "DLL detach function called.n");
break;
case DLL_THREAD_ATTACH:
fprintf(file, "DLL thread attach function called.n");
break;
case DLL_THREAD_DETACH:
fprintf(file, "DLL thread detach function called.n");
break;
}
/* close file */
fclose(file);
return TRUE;
}
DllMain entry point :
An optional entry point into a dynamic-link library (DLL). When the system starts or terminates a process or thread, it calls the entry-point function for each loaded DLL using the first thread of the process. The system also calls the entry-point function for a DLL when it is loaded or unloaded using the LoadLibrary and FreeLibraryfunctions.


總結:
用什麼來寫加載DLL不會差太多。
要注入非託管DLL,直接注入。
要注入託管DLL,先注入非託管的DLL來加載CLR再去加載託管DLL。

完整的C++程式碼和說明提供C++使用者參考。


沒有留言:

張貼留言