aboutsummaryrefslogtreecommitdiff
path: root/MemDriverLib/PatternScanner.cpp
blob: a274b6132748d6188298bc07bd68b8c952c81a81 (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
#include "stdafx.h"

#include "DLLHelper.h"
#include "PatternScanner.h"

#include <algorithm>
#include <stdexcept>
#include <sstream>
#include <string>
#include <vector>
#include <Windows.h>


static bool map_file_loadlib(SymbolResolver& symres, MODULE_DATA& module, PVOID * const buffer,
	SIZE_T * const size, PVOID const user_ptr);
static bool map_file_loadlib_cleanup(SymbolResolver& symres, MODULE_DATA& module, PVOID buffer,
	PVOID const user_ptr);

const struct map_file_data map_loadlib = map_file_data(map_file_loadlib, map_file_loadlib_cleanup, true);

bool map_file_loadlib(SymbolResolver& symres, MODULE_DATA& module, PVOID * const buffer,
	SIZE_T * const size, PVOID const user_ptr)
{
	HMODULE hMod;
	struct loadlib_user_data * const user_data = (struct loadlib_user_data * const) user_ptr;
	std::vector<DLL_DIRECTORY_COOKIE> dir_cookies;

	if (user_data) {
		if (user_data->additionalDllSearchDirectories.size() == 1) {
			SetDllDirectoryA(user_data->additionalDllSearchDirectories[0].c_str());
		}
		else {
			for (auto& searchDir : user_data->additionalDllSearchDirectories) {
				dir_cookies.push_back(AddDllDirectory(std::wstring(searchDir.begin(),
					searchDir.end()).c_str()));
			}
			if (!SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_USER_DIRS)) {
				return false;
			}
		}
	}

	hMod = symres.LoadLibrary(module.FullDllPath);

	if (user_data) {
		if (dir_cookies.size() > 1) {
			SetDllDirectoryA("");
		}
		else
			for (auto& searchDir : dir_cookies) {
				RemoveDllDirectory(searchDir);
			}
	}

	if (!hMod) {
		*buffer = NULL;
		*size = 0;
		return false;
	}
	else {
		*buffer = hMod;
		*size = module.SizeOfImage;
		return true;
	}
}

bool map_file_loadlib_cleanup(SymbolResolver& symres, MODULE_DATA& module, PVOID buffer, PVOID const user_ptr)
{
	return symres.FreeLibrary((HMODULE)buffer);
}

PatternScanner::PatternScanner(SymbolResolver& symres,
	struct map_file_data const * const mfd, PVOID map_file_user_data)
	: m_symbolResolver(symres), mfd(mfd), map_file_user_data(map_file_user_data)
{
	if (!mfd) {
		throw std::runtime_error("MapFileData was NULL");
	}
}

PatternScanner::~PatternScanner()
{
}

static void findAndReplaceAll(std::string& data, const char * const search, const char * const replace)
{
	std::string toSearch(search);
	std::string replaceStr(replace);

	size_t pos = data.find(toSearch);

	while (pos != std::string::npos)
	{
		data.replace(pos, toSearch.size(), replaceStr);
		pos = data.find(toSearch, pos + replaceStr.size());
	}
}

bool PatternScanner::checkPattern(MODULE_DATA& module, const char * const pattern, std::string& result)
{
	const char * const hexalnum = "0123456789abcdefABCDEF ?";
	std::string str_pattern(pattern);

	std::size_t found = str_pattern.find_first_not_of(hexalnum);
	if (found != std::string::npos) {
		std::stringstream err_str;
		err_str << "Found an invalid character at " << found
			<< " (allowed characters: \"" << hexalnum << "\")";
		throw std::runtime_error(err_str.str());
		return false;
	}

	findAndReplaceAll(str_pattern, " ", "");
	if (str_pattern.length() % 2 != 0) {
		std::stringstream err_str;
		err_str << "Pattern length is not a multiple of 2";
		throw std::runtime_error(err_str.str());
		return false;
	}

	result = str_pattern;
	return true;
}

#include <iostream>
bool PatternScanner::doScan(std::string& pattern, UINT8 *buf, SIZE_T size, std::vector<UINT64>& foundOffsets)
{
	//std::wcout << "BLAAAAAAAAAAAAA" << std::endl;
	//std::wstring bla(str_pattern.begin(), str_pattern.end());
	//std::wcout << bla << std::endl;
	std::cout << pattern << std::endl;
	return false;
}

bool PatternScanner::Scan(MODULE_DATA& module, const char * const pattern)
{
	bool result;
	std::string validPattern;
	IMAGE_NT_HEADERS *ntHeader;
	IMAGE_SECTION_HEADER *secHeader;
	UINT8 *mappedBuffer = NULL;
	SIZE_T mappedSize = 0;
	std::vector<UINT64> foundOffsets;

	if (!checkPattern(module, pattern, validPattern)) {
		return false;
	}

	if (!mfd->mapfile(m_symbolResolver, module, (PVOID *)&mappedBuffer, &mappedSize, map_file_user_data))
	{
		return false;
	}

	if (mfd->in_memory_module) {
		if (!VerifyPeHeader(mappedBuffer, mappedSize, &ntHeader) || !ntHeader) {
			return false;
		}

		DWORD nBytes = 0, virtualSize;
		secHeader = IMAGE_FIRST_SECTION(ntHeader);
		for (SIZE_T i = 0; ntHeader->FileHeader.NumberOfSections; i++)
		{
			if (nBytes >= ntHeader->OptionalHeader.SizeOfImage)
				break;

			std::cout << "Sec: " << secHeader->Name << std::endl;

			virtualSize = secHeader->VirtualAddress;
			secHeader++;
			virtualSize = secHeader->VirtualAddress - virtualSize;
			nBytes += virtualSize;
		}
		result = false;
	}
	else {
		result = doScan(validPattern, mappedBuffer, mappedSize, foundOffsets);
	}

	if (!mfd->mapcleanup(m_symbolResolver, module, mappedBuffer, map_file_user_data))
	{
		return false;
	}

	return result;
}