windows.h: 1. Resource release

When installing a virus and Trojan horse, in order to make the program concise and not easy to find, only put the exe on the user's computer, which requires the use of resource release technology.

As the name implies, it is to load the resources that need to be used into the program first, and then release it from the program to the local computer when the program is running, which is equivalent to a thief hiding the tool in his pocket.

After entering the user’s house and ensuring your own safety, you can take out all the crime tools to reduce the probability of successful infiltration. Here is how to use this technology.

Use the text document 520.txt as a resource and load it into the program

First introduce a few related functions that will be used later, these functions are all defined in windows.h

1. FindResource function

HRSRC FindResource(
    HMODULE hModule,  //处理包含资源的可执行文件模块,若为NULL,则从当前进程模块中加载资源
    LPCWSTR lpName,   //指定资源的名称
    LPCWSTR lpType)   //指定资源类型

//如果函数运行成功,则返回指定资源信息的句柄,可以把句柄传递给loadResource函数来加载资源
//如果运行失败,则返回值为NULL
    

HMODULE is a long macro definition of usign type, and   LPCWSTR is a 32-bit pointer to a unicode encoded string, and the pointed string is of type wchar, not char type

2. SizeofResource function

DWORD SizeofResource(
    HMODULE hModule,  //与FindResource中的HMODULE参数相同
    HRSRC hResInfo)   //资源句柄,此句柄必须由函数FindResource或FindResourceEx 来创建

//若执行成功,返回资源的字节数
//若运行失败,则返回值为零

3. LoadResource function

HGLOBAL LoadResource(
    HMODULE hModule,
    HRSRC hResInfo)
//返回资源数据句柄,若失败返回NULL

4. LockResource function

LPVOID LockResource(
    HGLOBAL hResData)
//返回资源数据第一个字节的指针,若失败返回NULL

The overall function for releasing resources is written as follows:

#include <tchar.h>
#include <Windows.h>
#include<iostream>
using namespace std;

//定义函数showError
void ShowError(const char* content) {
	cout << content << endl;
}

//资源释放
BOOL FreeMyResource(UINT uiResourceName, LPCWSTR lpszResourceType, char* lpszSaveFileName) {
	//获取指定模块里的资源
	HRSRC hRsrc = ::FindResource(NULL, MAKEINTRESOURCE(uiResourceName), lpszResourceType);
	if (NULL == hRsrc) {
		//没有找到相关资源
		ShowError("FindResource");
		return FALSE;
	}
	//获取资源大小
	DWORD dwSize = ::SizeofResource(NULL, hRsrc);
	if (NULL >= dwSize) {
		//资源大小小于0
		ShowError("SizeofResource");
		return FALSE;
	}
	//将资源加载到内存中
	HGLOBAL hGlobal = ::LoadResource(NULL, hRsrc);
	if (NULL == hGlobal) {
		//执行失败
		ShowError("SizeofResource");
		return FALSE;
	}
	//锁定资源
	LPVOID lpVoid = ::LockResource(hGlobal);
	if (NULL == lpVoid) {
		//锁定失败
		ShowError("LockResource");
		return FALSE;
	}
	//保存资源为文件
	FILE *fp = NULL;
	fopen_s(&fp, lpszSaveFileName, "wb+");
	if (NULL == fp) {
		ShowError("FREEResource");
		return FALSE;
	}
	fwrite(lpVoid, sizeof(char), dwSize, fp);
	fclose(fp);
	return TRUE;
}

 Use the MFC project to test the functions as follows:

Create an MFC project, set the button to release the resource, and bind the button binding function with the function written above

First download and install the MFC component in the VS installer

New MFC project

 

In the resource file, click Add Resource -> Custom Resource Type

Import the resource file:

Modify the ui and binding function in the resource view:

Click Resource Release and find that a txt file is generated in the current directory:

 

Send the .exe file to the XP computer to execute the result:

 

 

okk ###

Guess you like

Origin blog.csdn.net/mid_Faker/article/details/112561913