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
|
#include "PatternFind.h"
#include <dbghelp.h>
#include <psapi.h>
#pragma comment(lib, "dbghelp")
#pragma comment(lib, "psapi")
BOOL GetModuleSize(HMODULE hModule, LPVOID* lplpBase, LPDWORD lpdwSize) {
if (hModule == GetModuleHandle(NULL)) {
PIMAGE_NT_HEADERS pImageNtHeaders = ImageNtHeader((PVOID)hModule);
if (pImageNtHeaders == NULL)
return FALSE;
*lplpBase = (LPVOID)hModule;
*lpdwSize = pImageNtHeaders->OptionalHeader.SizeOfImage;
} else {
MODULEINFO ModuleInfo;
if (!GetModuleInformation(GetCurrentProcess(), hModule, &ModuleInfo, sizeof(MODULEINFO)))
return FALSE;
*lplpBase = ModuleInfo.lpBaseOfDll;
*lpdwSize = ModuleInfo.SizeOfImage;
}
return TRUE;
}
DWORD PFAPI GetPatternCB(char *szPattern) {
DWORD cb = 0;
bool first_nibble = false;
for (DWORD i=0; i<strlen(szPattern); i++) {
char c = toupper(szPattern[i]);
if (c != ' ') {
if (c == '?') {
if (!first_nibble) cb++;
else return 0;
} else {
if (!isxdigit(c)) return 0;
if (first_nibble) cb++;
first_nibble ^= true;
}
}
}
if (first_nibble) return 0;
return cb;
}
BOOL PFAPI GeneratePatternMask(char *szPattern, char *buffer) {
bool first_nibble = false;
for (DWORD i=0; i<strlen(szPattern); i++) {
char c = toupper(szPattern[i]);
if (c != ' ') {
if (c == '?') {
if (!first_nibble) strcat_s(buffer, MAX_PATTERN, "?");
else return FALSE;
} else {
if (!isxdigit(c)) return FALSE;
if (first_nibble) strcat_s(buffer, MAX_PATTERN, "x");
first_nibble ^= true;
}
}
}
if (first_nibble) return FALSE;
return TRUE;
}
BOOL PFAPI GeneratePatternBytes(char *szPattern, LPBYTE buffer) {
bool first_nibble = false;
DWORD cb = 0;
for (DWORD i=0; i<strlen(szPattern); i++) {
char c = toupper(szPattern[i]);
if (c != ' ') {
if (c == '?') {
if (!first_nibble) {
buffer[cb] = 0x00;
cb++;
}
else return FALSE;
} else {
if (!isxdigit(c)) return FALSE;
if (first_nibble) {
char byte[3] = {0};
byte[0] = szPattern[i-1];
byte[1] = c;
byte[2] = '\0';
buffer[cb] = (BYTE)strtol(byte, NULL, 16);
cb++;
}
first_nibble ^= true;
}
}
}
if (first_nibble) return FALSE;
return TRUE;
}
VOID PFAPI SearchPattern(PFSEARCH *ppf, LPVOID lpvBase, DWORD dwSize) {
ppf->lpvResult = 0;
DWORD dwBase = reinterpret_cast<DWORD>(lpvBase);
for (DWORD i=dwBase; i<dwBase+dwSize; i++) {
bool found = true;
for (DWORD j=0; j<ppf->dwLength; j++) {
if (ppf->szMask[j] == 'x') {
if (*reinterpret_cast<BYTE*>(i+j) != ppf->lpbData[j]) {
found = false;
break;
}
}
}
if (found) {
ppf->lpvResult = reinterpret_cast<LPVOID>(i);
return;
}
}
}
DWORD PFAPI FindPattern(char *szPattern, PFSEARCH *ppf, LPVOID lpvBase, DWORD dwSize) {
ZeroMemory(ppf, sizeof(PFSEARCH));
bool invalid = false;
ppf->dwLength = GetPatternCB(szPattern);
invalid = invalid || !ppf->dwLength;
invalid = invalid || !GeneratePatternMask(szPattern, ppf->szMask);
invalid = invalid || !GeneratePatternBytes(szPattern, ppf->lpbData);
if (invalid)
return PF_INVALID;
if (ppf->dwLength > MAX_PATTERN)
return PF_OVERFLOW;
SearchPattern(ppf, lpvBase, dwSize);
if (!ppf->lpvResult)
return PF_NOT_FOUND;
return PF_NONE;
}
|