CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/exploits/CVE-2010-0232/common/ResourceLoader.c
Views: 11784
1
/*!
2
* @file ResourceLoader.c
3
* @brief Helper functions for loading embedded resources.
4
*/
5
#include <Windows.h>
6
#include "common.h"
7
8
/*!
9
* @brief Load a resource from the given module as a raw array of bytes.
10
* @param hModule Handle to the module containing the resource.
11
* @param uResourceId ID of the resource to load.
12
* @param lpType The type of resource being loaded.
13
* @param pBuffer Pointer to the buffer that will receive the loaded resource.
14
* @param pBufferSize Pointer to the variable that will receive the size of \c pBuffer.
15
* @returns Indication of success or failure.
16
*/
17
DWORD resource_extract_raw(HMODULE hModule, UINT uResourceId, LPCSTR lpType, LPBYTE* pBuffer, LPDWORD pBufferSize)
18
{
19
DWORD dwResult = FALSE;
20
DWORD dwResourceSize = 0;
21
LPBYTE pResource = NULL;
22
HRSRC hResource = NULL;
23
HGLOBAL hResData = NULL;
24
LPVOID lpResData = NULL;
25
26
*pBuffer = NULL;
27
*pBufferSize = 0;
28
29
do
30
{
31
if ((hResource = FindResourceA(hModule, MAKEINTRESOURCEA(uResourceId), lpType)) == NULL) {
32
dwResult = GetLastError();
33
dprintf("[RES] Unable to find resource %d type %s", uResourceId, lpType);
34
break;
35
}
36
37
if ((dwResourceSize = SizeofResource(hModule, hResource)) == 0) {
38
dwResult = GetLastError();
39
dprintf("[RES] Unable to find resource size for %d type %s", uResourceId, lpType);
40
break;
41
}
42
43
if ((pResource = (LPBYTE)malloc(dwResourceSize)) == NULL) {
44
dwResult = ERROR_NOT_ENOUGH_MEMORY;
45
dprintf("[RES] Unable to allocate memory for resource %d type %s size %u", uResourceId, lpType, dwResourceSize);
46
break;
47
}
48
49
if ((hResData = LoadResource(hModule, hResource)) == NULL) {
50
dwResult = GetLastError();
51
dprintf("[RES] Unable to load resource for %d type %s", uResourceId, lpType);
52
break;
53
}
54
55
if ((lpResData = LockResource(hResData)) == NULL) {
56
dwResult = GetLastError();
57
dprintf("[RES] Unable to lock resource for %d type %s", uResourceId, lpType);
58
break;
59
}
60
61
memcpy_s(pResource, dwResourceSize, lpResData, dwResourceSize);
62
63
// Locked resource don't need to be unlocked. If we get here, we've won!
64
dwResult = ERROR_SUCCESS;
65
*pBuffer = lpResData;
66
*pBufferSize = dwResourceSize;
67
68
} while (0);
69
70
if (dwResult != ERROR_SUCCESS && pResource != NULL) {
71
free(pResource);
72
}
73
74
return dwResult;
75
}
76
77
/*!
78
* @brief Free up memory that was allocated when loading the resource.
79
* @param lpBuffer Pointer to the allocated buffer.
80
* @returns \c ERROR_SUCCESS
81
*/
82
DWORD resource_destroy(LPBYTE lpBuffer)
83
{
84
if (lpBuffer != NULL)
85
{
86
free(lpBuffer);
87
}
88
return ERROR_SUCCESS;
89
}
90