能力值:
( LV2,RANK:10 )
|
-
-
2 楼
使用LdrLoadDll和内存自己加载方式,均不成功
|
能力值:
( LV2,RANK:10 )
|
-
-
3 楼
看看导入表,就知道为啥不成功了。
|
能力值:
( LV2,RANK:10 )
|
-
-
4 楼
HMODULE __stdcall MyLoadLibraryA(const char *szDllName)
{
void *pFileAddr;//文件隐射基地址
void *pImageBase;//加载DLL的基地址,作为返回数
IMAGE_DOS_HEADER *pDosHead;
IMAGE_OPTIONAL_HEADER *pOPHead;
IMAGE_FILE_HEADER *pFileHead;
DWORD dwSecNum;
IMAGE_SECTION_HEADER *pSecInfo;
IMAGE_IMPORT_DESCRIPTOR *pIID;
LPTHREAD_START_ROUTINE OEP;
IMAGE_BASE_RELOCATION *pIBR;
DWORD dwDllRVA; //实际装载地址与建议装载地址差。
DWORD i = 0;
do
{
HANDLE hFile = ::CreateFileA(szDllName,GENERIC_READ,0,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile==INVALID_HANDLE_VALUE) break;
HANDLE hMaping = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL);
pFileAddr = MapViewOfFile(hMaping,FILE_MAP_READ,0,0,0);
pDosHead = (IMAGE_DOS_HEADER*)pFileAddr;
pFileHead = (IMAGE_FILE_HEADER*)(pDosHead->e_lfanew+4 +(DWORD)pFileAddr);
pOPHead = (IMAGE_OPTIONAL_HEADER*)((DWORD)pFileHead +
sizeof(_IMAGE_FILE_HEADER));
//申请内存,建立DLL内存空间,并COPY DLL
pImageBase = VirtualAlloc(NULL,pOPHead->SizeOfImage,MEM_COMMIT
,PAGE_EXECUTE_READWRITE);
if (pImageBase == NULL) break;
dwDllRVA = (DWORD)pImageBase - pOPHead->ImageBase;
pSecInfo = (IMAGE_SECTION_HEADER*)((DWORD)pOPHead + pFileHead->SizeOfOptionalHeader) ;
dwSecNum = pFileHead->NumberOfSections;
//COPY文件头
DWORD dwFileSize = pOPHead->SizeOfHeaders;
memcpy(pImageBase,pFileAddr,dwFileSize);
//COPY区段
for (i = 0; i < dwSecNum; i++)
{
void *des,*sour;
des = (void*)(pSecInfo->VirtualAddress + (DWORD)pImageBase);
sour = (void*)(pSecInfo->PointerToRawData + (DWORD)pFileAddr);
memcpy(des,sour,pSecInfo->Misc.VirtualSize);
pSecInfo++;
}
OEP = (LPTHREAD_START_ROUTINE)(pOPHead->AddressOfEntryPoint +
(DWORD)pImageBase);
// 修正重定位表。
pIBR = (IMAGE_BASE_RELOCATION*)(pOPHead->DataDirectory[5].VirtualAddress + (DWORD)pImageBase);
while (pIBR->VirtualAddress)
{
WORD *pTypeOffset = (WORD*)((DWORD)pIBR+ 8);
for (i = 0; i < pIBR->SizeOfBlock - 8; i += 2)
{
//pTypeOffset[i] & 0Xf000 == 0x3000 结果不对,郁闷。
if (pTypeOffset[i] >>12 == 3)
{
DWORD nTmp =((DWORD) pTypeOffset[i]) & 0xfff;
DWORD *pReloc = (DWORD*)(nTmp +(DWORD) pImageBase + pIBR->VirtualAddress);
*pReloc = *pReloc + dwDllRVA;
}
}
pIBR = (IMAGE_BASE_RELOCATION*)(pIBR->SizeOfBlock + (DWORD)pIBR);
}
//填充IAT表
pIID = (IMAGE_IMPORT_DESCRIPTOR*)((pOPHead->DataDirectory[1].VirtualAddress)
+ (DWORD)pImageBase);
while (pIID->Name != 0)
{
char *pDllName;
char *pFacName;
HMODULE hDll;
IMAGE_THUNK_DATA32 *pIDT,*pIAT;
PIMAGE_IMPORT_BY_NAME *pImByName;
pDllName = (char*)((DWORD)(pIID->Name) + (DWORD)pImageBase);
hDll = LoadLibraryA((LPCSTR)pDllName);
pIDT = (IMAGE_THUNK_DATA32*)(pIID->OriginalFirstThunk +
(DWORD)pImageBase);
pIAT = (IMAGE_THUNK_DATA32*)(pIID->FirstThunk + (DWORD)pImageBase);
while (*(DWORD*)pIDT != 0)
{
pImByName = NULL;
pImByName = (PIMAGE_IMPORT_BY_NAME*)((DWORD)(pIDT->u1.AddressOfData)
+ (DWORD)pImageBase);
pFacName =(char*)pImByName+2;
pIAT->u1.Function = (DWORD)GetProcAddress(hDll,pFacName);
pIDT++ ;
}
pIID++;
}
//调用DLLMAIN
_asm
{
pushad;
push 0;
push DLL_PROCESS_ATTACH;
push pImageBase;
call OEP;
add esp,0x4; //DLLMAIN未平衡堆栈。
popad
}
} while (false);
return (HMODULE)pImageBase;
}
|
能力值:
( LV2,RANK:10 )
|
-
-
5 楼
HMODULE LoadDll( LPCSTR lpFileName )
{
HMODULE hntdll = NULL;
if (hntdll == NULL) { hntdll = GetModuleHandleA("ntdll.dll"); }
if (_LdrLoadDll == NULL) { _LdrLoadDll = (fLdrLoadDll) GetProcAddress ( hntdll, "LdrLoadDll"); }
if (_RtlInitUnicodeString == NULL)
{ _RtlInitUnicodeString = (fRtlInitUnicodeString) GetProcAddress ( hntdll, "RtlInitUnicodeString"); }
int StrLen = lstrlenA(lpFileName);
BSTR WideStr = SysAllocStringLen(NULL, StrLen);
MultiByteToWideChar(CP_ACP, 0, lpFileName, StrLen, WideStr, StrLen);
UNICODE_STRING usDllName;
_RtlInitUnicodeString(&usDllName, WideStr);
SysFreeString(WideStr);
HANDLE DllHandle;
_LdrLoadDll(0, 0, &usDllName, &DllHandle);
return (HMODULE)DllHandle;
}
|
能力值:
( LV2,RANK:10 )
|
-
-
6 楼
导入表需要修正?
|
能力值:
( LV2,RANK:10 )
|
-
-
7 楼
不懂,MARK下
|
能力值:
( LV6,RANK:90 )
|
-
-
8 楼
我没有加载,应该是缺dll文件吧 memmgr.dll msvcp80.dll msvcr80.dll 从这里看 这个dll应该是vs2005写的 应该是圣域2 的辅助dll吧 还是什么的吧
|
能力值:
( LV2,RANK:10 )
|
-
-
9 楼
楼上正解 !
|
能力值:
( LV8,RANK:120 )
|
-
-
10 楼
还缺别的dll 就和exe缺少必要的dll 打不开一样
|
能力值:
( LV2,RANK:10 )
|
-
-
11 楼
哪位大侠给我点提示?什么原因加载不了?
|
能力值:
( LV2,RANK:10 )
|
-
-
12 楼
大家别误会,纯属技术讨论。
|