aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/core/memory.cpp
blob: 603dcd84a5ce54af876fb89ea277f493c98acad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "memory.h"

CMemoryAddress::CMemoryAddress() : m_nAddress(0) {}
CMemoryAddress::CMemoryAddress(const uintptr_t nAddress) : m_nAddress(nAddress) {}
CMemoryAddress::CMemoryAddress(const void* pAddress) : m_nAddress(reinterpret_cast<uintptr_t>(pAddress)) {}

// operators
CMemoryAddress::operator uintptr_t() const
{
	return m_nAddress;
}

CMemoryAddress::operator void*() const
{
	return reinterpret_cast<void*>(m_nAddress);
}

CMemoryAddress::operator bool() const
{
	return m_nAddress != 0;
}

bool CMemoryAddress::operator==(const CMemoryAddress& other) const
{
	return m_nAddress == other.m_nAddress;
}

bool CMemoryAddress::operator!=(const CMemoryAddress& other) const
{
	return m_nAddress != other.m_nAddress;
}

bool CMemoryAddress::operator==(const uintptr_t& addr) const
{
	return m_nAddress == addr;
}

bool CMemoryAddress::operator!=(const uintptr_t& addr) const
{
	return m_nAddress != addr;
}

CMemoryAddress CMemoryAddress::operator+(const CMemoryAddress& other) const
{
	return Offset(other.m_nAddress);
}

CMemoryAddress CMemoryAddress::operator-(const CMemoryAddress& other) const
{
	return CMemoryAddress(m_nAddress - other.m_nAddress);
}

CMemoryAddress CMemoryAddress::operator+(const uintptr_t& addr) const
{
	return Offset(addr);
}

CMemoryAddress CMemoryAddress::operator-(const uintptr_t& addr) const
{
	return CMemoryAddress(m_nAddress - addr);
}

CMemoryAddress CMemoryAddress::operator*() const
{
	return Deref();
}

// traversal
CMemoryAddress CMemoryAddress::Offset(const uintptr_t nOffset) const
{
	return CMemoryAddress(m_nAddress + nOffset);
}

CMemoryAddress CMemoryAddress::Deref(const int nNumDerefs) const
{
	uintptr_t ret = m_nAddress;
	for (int i = 0; i < nNumDerefs; i++)
		ret = *reinterpret_cast<uintptr_t*>(ret);

	return CMemoryAddress(ret);
}

// patching
void CMemoryAddress::Patch(const uint8_t* pBytes, const size_t nSize)
{
	if (nSize)
		WriteProcessMemory(GetCurrentProcess(), reinterpret_cast<LPVOID>(m_nAddress), pBytes, nSize, NULL);
}

void CMemoryAddress::Patch(const std::initializer_list<uint8_t> bytes)
{
	uint8_t* pBytes = new uint8_t[bytes.size()];

	int i = 0;
	for (const uint8_t& byte : bytes)
		pBytes[i++] = byte;

	Patch(pBytes, bytes.size());
	delete[] pBytes;
}

inline std::vector<uint8_t> HexBytesToString(const char* pHexString)
{
	std::vector<uint8_t> ret;

	int size = strlen(pHexString);
	for (int i = 0; i < size; i++)
	{
		// If this is a space character, ignore it
		if (isspace(pHexString[i]))
			continue;

		if (i < size - 1)
		{
			BYTE result = 0;
			for (int j = 0; j < 2; j++)
			{
				int val = 0;
				char c = *(pHexString + i + j);
				if (c >= 'a')
				{
					val = c - 'a' + 0xA;
				}
				else if (c >= 'A')
				{
					val = c - 'A' + 0xA;
				}
				else if (isdigit(c))
				{
					val = c - '0';
				}
				else
				{
					assert_msg(false, "Failed to parse invalid hex string.");
					val = -1;
				}

				result += (j == 0) ? val * 16 : val;
			}
			ret.push_back(result);
		}

		i++;
	}

	return ret;
}

void CMemoryAddress::Patch(const char* pBytes)
{
	std::vector<uint8_t> vBytes = HexBytesToString(pBytes);
	Patch(vBytes.data(), vBytes.size());
}

void CMemoryAddress::NOP(const size_t nSize)
{
	uint8_t* pBytes = new uint8_t[nSize];

	memset(pBytes, 0x90, nSize);
	Patch(pBytes, nSize);

	delete[] pBytes;
}

bool CMemoryAddress::IsMemoryReadable(const size_t nSize)
{
	static SYSTEM_INFO sysInfo;
	if (!sysInfo.dwPageSize)
		GetSystemInfo(&sysInfo);

	MEMORY_BASIC_INFORMATION memInfo;
	if (!VirtualQuery(reinterpret_cast<LPCVOID>(m_nAddress), &memInfo, sizeof(memInfo)))
		return false;

	return memInfo.RegionSize >= nSize && memInfo.State & MEM_COMMIT && !(memInfo.Protect & PAGE_NOACCESS);
}

CModule::CModule(const HMODULE pModule)
{
	MODULEINFO mInfo {0};

	if (pModule && pModule != INVALID_HANDLE_VALUE)
		GetModuleInformation(GetCurrentProcess(), pModule, &mInfo, sizeof(MODULEINFO));

	m_nModuleSize = static_cast<size_t>(mInfo.SizeOfImage);
	m_pModuleBase = reinterpret_cast<uintptr_t>(mInfo.lpBaseOfDll);
	m_nAddress = m_pModuleBase;

	if (!m_nModuleSize || !m_pModuleBase)
		return;

	m_pDOSHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(m_pModuleBase);
	m_pNTHeaders = reinterpret_cast<IMAGE_NT_HEADERS64*>(m_pModuleBase + m_pDOSHeader->e_lfanew);

	const IMAGE_SECTION_HEADER* hSection = IMAGE_FIRST_SECTION(m_pNTHeaders); // Get first image section.

	for (WORD i = 0; i < m_pNTHeaders->FileHeader.NumberOfSections; i++) // Loop through the sections.
	{
		const IMAGE_SECTION_HEADER& hCurrentSection = hSection[i]; // Get current section.

		ModuleSections_t moduleSection = ModuleSections_t(
		    std::string(reinterpret_cast<const char*>(hCurrentSection.Name)),
		    static_cast<uintptr_t>(m_pModuleBase + hCurrentSection.VirtualAddress),
		    hCurrentSection.SizeOfRawData);

		if (!strcmp((const char*)hCurrentSection.Name, ".text"))
			m_ExecutableCode = moduleSection;
		else if (!strcmp((const char*)hCurrentSection.Name, ".pdata"))
			m_ExceptionTable = moduleSection;
		else if (!strcmp((const char*)hCurrentSection.Name, ".data"))
			m_RunTimeData = moduleSection;
		else if (!strcmp((const char*)hCurrentSection.Name, ".rdata"))
			m_ReadOnlyData = moduleSection;

		m_vModuleSections.push_back(moduleSection); // Push back a struct with the section data.
	}
}

CModule::CModule(const char* pModuleName) : CModule(GetModuleHandleA(pModuleName)) {}

CMemoryAddress CModule::GetExport(const char* pExportName)
{
	return CMemoryAddress(reinterpret_cast<uintptr_t>(GetProcAddress(reinterpret_cast<HMODULE>(m_nAddress), pExportName)));
}

CMemoryAddress CModule::FindPattern(const uint8_t* pPattern, const char* pMask)
{
	if (!m_ExecutableCode.IsSectionValid())
		return CMemoryAddress();

	uint64_t nBase = static_cast<uint64_t>(m_ExecutableCode.m_pSectionBase);
	uint64_t nSize = static_cast<uint64_t>(m_ExecutableCode.m_nSectionSize);

	const uint8_t* pData = reinterpret_cast<uint8_t*>(nBase);
	const uint8_t* pEnd = pData + static_cast<uint32_t>(nSize) - strlen(pMask);

	int nMasks[64]; // 64*16 = enough masks for 1024 bytes.
	int iNumMasks = static_cast<int>(ceil(static_cast<float>(strlen(pMask)) / 16.f));

	memset(nMasks, '\0', iNumMasks * sizeof(int));
	for (intptr_t i = 0; i < iNumMasks; ++i)
	{
		for (intptr_t j = strnlen(pMask + i * 16, 16) - 1; j >= 0; --j)
		{
			if (pMask[i * 16 + j] == 'x')
			{
				_bittestandset(reinterpret_cast<LONG*>(&nMasks[i]), j);
			}
		}
	}
	__m128i xmm1 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pPattern));
	__m128i xmm2, xmm3, msks;
	for (; pData != pEnd; _mm_prefetch(reinterpret_cast<const char*>(++pData + 64), _MM_HINT_NTA))
	{
		if (pPattern[0] == pData[0])
		{
			xmm2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>(pData));
			msks = _mm_cmpeq_epi8(xmm1, xmm2);
			if ((_mm_movemask_epi8(msks) & nMasks[0]) == nMasks[0])
			{
				for (uintptr_t i = 1; i < static_cast<uintptr_t>(iNumMasks); ++i)
				{
					xmm2 = _mm_loadu_si128(reinterpret_cast<const __m128i*>((pData + i * 16)));
					xmm3 = _mm_loadu_si128(reinterpret_cast<const __m128i*>((pPattern + i * 16)));
					msks = _mm_cmpeq_epi8(xmm2, xmm3);
					if ((_mm_movemask_epi8(msks) & nMasks[i]) == nMasks[i])
					{
						if ((i + 1) == iNumMasks)
						{
							return CMemoryAddress(const_cast<uint8_t*>(pData));
						}
					}
					else
						goto CONTINUE;
				}

				return CMemoryAddress((&*(const_cast<uint8_t*>(pData))));
			}
		}

	CONTINUE:;
	}

	return CMemoryAddress();
}

inline std::pair<std::vector<uint8_t>, std::string> MaskedBytesFromPattern(const char* pPatternString)
{
	std::vector<uint8_t> vRet;
	std::string sMask;

	int size = strlen(pPatternString);
	for (int i = 0; i < size; i++)
	{
		// If this is a space character, ignore it
		if (isspace(pPatternString[i]))
			continue;

		if (pPatternString[i] == '?')
		{
			// Add a wildcard
			vRet.push_back(0);
			sMask.append("?");
		}
		else if (i < size - 1)
		{
			BYTE result = 0;
			for (int j = 0; j < 2; j++)
			{
				int val = 0;
				char c = *(pPatternString + i + j);
				if (c >= 'a')
				{
					val = c - 'a' + 0xA;
				}
				else if (c >= 'A')
				{
					val = c - 'A' + 0xA;
				}
				else if (isdigit(c))
				{
					val = c - '0';
				}
				else
				{
					assert_msg(false, "Failed to parse invalid pattern string.");
					val = -1;
				}

				result += (j == 0) ? val * 16 : val;
			}

			vRet.push_back(result);
			sMask.append("x");
		}

		i++;
	}

	return std::make_pair(vRet, sMask);
}

CMemoryAddress CModule::FindPattern(const char* pPattern)
{
	const auto pattern = MaskedBytesFromPattern(pPattern);
	return FindPattern(pattern.first.data(), pattern.second.c_str());
}