内存加载DLL三大法宝

xingyun86 2018-7-16 2478

//文件读到内存中然后使用内存加载分两种方式实现C++和C
int read_dll_memory_load (TCHAR str_dll_path[],char str_export_fun[]) //用C++的方式把文件读到内存加载
{  
	filebuf *pbuf;  
	ifstream filestr;  
	long size;  
	char * buffer;  
 
	filestr.open (str_dll_path, ios::binary); // 要读入整个文件,必须采用二进制打开  
 
	pbuf=filestr.rdbuf(); // 获取filestr对应buffer对象的指针   
 
	size=pbuf->pubseekoff (0,ios::end,ios::in);  // 调用buffer对象方法获取文件大小 
	pbuf->pubseekpos (0,ios::in);  
 
	buffer=new char[size];  // 分配内存空间 
 
	pbuf->sgetn (buffer,size);  // 获取文件内容 
 
	filestr.close();  
 
	HMEMORYMODULE module=MemoryLoadLibrary(buffer);//从内存中加载文件
 
	FARPROC mc=MemoryGetProcAddress(module, str_export_fun);
 
	if (mc==NULL)
	{
		OutputDebugStringA("MemoryGetProcAddress NULL");
 
		return 0;
	}
 
	typedef int (WINAPI* FN_Execute) (int nType);
 
	FN_Execute execute=(FN_Execute)mc;
 
	execute(0);
 
	MemoryFreeLibrary(module);
 
	delete []buffer;  
 
	return 0;  
}
 
UINT read_dll_memory_load_c(TCHAR str_dll_path[],char str_export_fun[])
{
	HANDLE hFile = CreateFile(str_dll_path, GENERIC_READ , 0, NULL,OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
 
	if (hFile==NULL||hFile==INVALID_HANDLE_VALUE)
	{
		return 0;
	}
 
	DWORD  dwFileSize= 0;
 
	dwFileSize=GetFileSize(hFile,  NULL);
 
	CloseHandle(hFile);
 
	char * buff=new char[dwFileSize];
 
	FILE* fp = _tfopen(str_dll_path, _T("rb"));
 
	if (fp)
	{
		dwFileSize=fread(buff, 1, dwFileSize, fp);
	}
 
	fclose(fp);
 
 
	HMEMORYMODULE hm=MemoryLoadLibrary(buff);
 
	if (hm==NULL)
	{
		return 0;
	}
 
	FARPROC mc=MemoryGetProcAddress(hm, str_export_fun);
 
	if (mc==NULL)
	{
		OutputDebugStringA("MemoryGetProcAddress NULL");
 
		return 0;
	}
 
	typedef int (WINAPI* FN_Execute) (int nType);
 
	FN_Execute execute=(FN_Execute)mc;
 
	execute(0);
 
	MemoryFreeLibrary(hm);
 
	delete []buff;  
 
	return 0;
}

参考网址:
https://blog.didierstevens.com/2010/02/16/memoryloadlibrary-from-c-program-to-shellcode/

上传的附件:
×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回