DLL内存加载
動態(tài)加載dll
功能:
? ? ?把一個處于內(nèi)存里的dll直接加載并且使用。
用途:
? ? ?免殺(靜態(tài)文件查殺),外掛(防止游戲自己hook了loadlibrary等函數(shù)),以及其他。
原理:?
? ? 假設(shè)目前處于內(nèi)存里的dll是A,然后開辟一個新的內(nèi)存空間B,根據(jù)A的文件頭等相關(guān)信息,把B看做是加載內(nèi)存。
然后把數(shù)據(jù)拷貝到B里,并且對齊相關(guān)節(jié),然后修正iat等相關(guān)。然后在手動調(diào)用一次dllmain函數(shù),這樣dll就被從內(nèi)存A
加載到內(nèi)存B里了。之后再調(diào)用函數(shù)的時候,直接根據(jù)函數(shù)名,在INT或者其他位置找到函數(shù)地址,這個過程就是模擬了
GetProcAddress函數(shù)的功能。
整理了一個內(nèi)存加載dll相關(guān)的類以及測試項目代碼:(http://download.csdn.net/detail/u013761036/9686863)
下面是相關(guān)測試代碼:
#include "stdafx.h" #include <string> #include <windows.h> #include <shlwapi.h> #include "MemLoadDll.h" #pragma comment(lib, "shlwapi.lib") using namespace std; #pragma warning(disable : 4996)unsigned char bMemory[1024*1024*5] = {0};DWORD dwLoadDll2Memory(string strDllPath){FILE *fpLoadDll; char cCache[1024]; if((fpLoadDll = fopen(strDllPath.c_str(),"rb")) == NULL) { return 0;} DWORD dwNowReadId = 0;while (1) { ZeroMemory(cCache ,sizeof(cCache));DWORD dwReadSize = fread(cCache,1,1024 ,fpLoadDll);DWORD dwErrorCode = GetLastError();if(dwReadSize == 0){break;}for(int i = 1 ;i <= dwReadSize ;i ++){bMemory[dwNowReadId++] = cCache[i-1];}} fclose(fpLoadDll); return dwNowReadId; }VOID SetCurrentDir(){WCHAR wcLocalPath[MAX_PATH*2] = {0};GetModuleFileName(0 ,wcLocalPath ,MAX_PATH);PathRemoveFileSpec(wcLocalPath);SetCurrentDirectory(wcLocalPath); }int _tmain(int argc, _TCHAR* argv[]) { //mark : After loading a function related to the memory will be released, that is, only one function can be loaded to performSetCurrentDir();DWORD dwFileLength = dwLoadDll2Memory("TestDll.dll");CMemLoadDll *clLoadClass = new CMemLoadDll();BOOL bLoadDllResult = clLoadClass->MemLoadLibrary(bMemory ,dwFileLength); if(bLoadDllResult){typedef VOID (*TYPEPRINTFMSE)(const string &strMessage);TYPEPRINTFMSE _PrintfMse = (TYPEPRINTFMSE)clLoadClass->MemGetProcAddress("PrintfMse");if(_PrintfMse){_PrintfMse("Memory load function executed successfully!");}else{// getprocaddress error}}else{//loadlibrary error}delete clLoadClass;return 0; }
功能:
? ? ?把一個處于內(nèi)存里的dll直接加載并且使用。
用途:
? ? ?免殺(靜態(tài)文件查殺),外掛(防止游戲自己hook了loadlibrary等函數(shù)),以及其他。
原理:?
? ? 假設(shè)目前處于內(nèi)存里的dll是A,然后開辟一個新的內(nèi)存空間B,根據(jù)A的文件頭等相關(guān)信息,把B看做是加載內(nèi)存。
然后把數(shù)據(jù)拷貝到B里,并且對齊相關(guān)節(jié),然后修正iat等相關(guān)。然后在手動調(diào)用一次dllmain函數(shù),這樣dll就被從內(nèi)存A
加載到內(nèi)存B里了。之后再調(diào)用函數(shù)的時候,直接根據(jù)函數(shù)名,在INT或者其他位置找到函數(shù)地址,這個過程就是模擬了
GetProcAddress函數(shù)的功能。
整理了一個內(nèi)存加載dll相關(guān)的類以及測試項目代碼:(http://download.csdn.net/detail/u013761036/9686863)
下面是相關(guān)測試代碼:
#include "stdafx.h" #include <string> #include <windows.h> #include <shlwapi.h> #include "MemLoadDll.h" #pragma comment(lib, "shlwapi.lib") using namespace std; #pragma warning(disable : 4996)unsigned char bMemory[1024*1024*5] = {0};DWORD dwLoadDll2Memory(string strDllPath){FILE *fpLoadDll; char cCache[1024]; if((fpLoadDll = fopen(strDllPath.c_str(),"rb")) == NULL) { return 0;} DWORD dwNowReadId = 0;while (1) { ZeroMemory(cCache ,sizeof(cCache));DWORD dwReadSize = fread(cCache,1,1024 ,fpLoadDll);DWORD dwErrorCode = GetLastError();if(dwReadSize == 0){break;}for(int i = 1 ;i <= dwReadSize ;i ++){bMemory[dwNowReadId++] = cCache[i-1];}} fclose(fpLoadDll); return dwNowReadId; }VOID SetCurrentDir(){WCHAR wcLocalPath[MAX_PATH*2] = {0};GetModuleFileName(0 ,wcLocalPath ,MAX_PATH);PathRemoveFileSpec(wcLocalPath);SetCurrentDirectory(wcLocalPath); }int _tmain(int argc, _TCHAR* argv[]) { //mark : After loading a function related to the memory will be released, that is, only one function can be loaded to performSetCurrentDir();DWORD dwFileLength = dwLoadDll2Memory("TestDll.dll");CMemLoadDll *clLoadClass = new CMemLoadDll();BOOL bLoadDllResult = clLoadClass->MemLoadLibrary(bMemory ,dwFileLength); if(bLoadDllResult){typedef VOID (*TYPEPRINTFMSE)(const string &strMessage);TYPEPRINTFMSE _PrintfMse = (TYPEPRINTFMSE)clLoadClass->MemGetProcAddress("PrintfMse");if(_PrintfMse){_PrintfMse("Memory load function executed successfully!");}else{// getprocaddress error}}else{//loadlibrary error}delete clLoadClass;return 0; }
總結(jié)
- 上一篇: Intel汇编程序设计-高级过程(上)
- 下一篇: WindowsPE 第五章 导出表