-
Notifications
You must be signed in to change notification settings - Fork 45
/
SigMaker.h
196 lines (168 loc) · 4.32 KB
/
SigMaker.h
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
// Common and shared
#pragma once
#include "StdAfx.h"
#include <vector>
#include "Settings.h"
// Minimal signature byte length
static const UINT32 MIN_SIG_SIZE = 5;
// Signature container
struct SIG
{
std::vector<BYTE> bytes;
std::vector<BYTE> mask; // 0xFF = keep, 0 = wildcard/skip
// ------------------------------------------------------------------------
// Append one or more bytes at address to the signature
void AddBytes(ea_t ea, UINT32 size)
{
size_t len = bytes.size();
bytes.resize(len + size);
mask.resize(len + size);
PBYTE bytesPtr = &bytes[len];
PBYTE maskPtr = &mask[len];
// get_db_byte() loop faster than get_bytes(), etc.
while (size--)
{
*bytesPtr = get_db_byte(ea);
*maskPtr = 0xFF;
ea++, bytesPtr++, maskPtr++;
};
}
// Append one or more wildcards to a signature
void AddWildcards(UINT32 size)
{
size_t len = bytes.size();
bytes.resize(len + size);
mask.resize(len + size);
PBYTE bytesPtr = &bytes[len];
PBYTE maskPtr = &mask[len];
while (size--)
{
*bytesPtr++ = 0xCC;
*maskPtr++ = 0;
};
}
// ------------------------------------------------------------------------
// Output the sig as a "F8 66 4B ?? ?? ?? 88" format string
void ToIdaString(__out qstring &string) const
{
size_t count = bytes.size();
if (count > 0)
{
string.reserve(count * SIZESTR("?? "));
for (size_t i = 0; i < count; i++)
{
if (mask[i])
string.cat_sprnt("%02X ", bytes[i]);
else
string.cat_sprnt("?? ");
}
// Remove the final ' ' space
string.remove_last();
}
}
// Convert mask to a "code" style mask string; "xxxxxxx????xxx"
void ToMaskString(__out qstring &maskStr) const
{
int count = (int) mask.size();
maskStr.resize(count + 1);
for (int i = 0; i < count; i++)
{
if (mask[i])
maskStr[i] = 'x';
else
maskStr[i] = '?';
}
}
// Convert byte pattern to '\x' "code" style encoding; "\x45\xAA\xCC\xCC\xCC\x9A\xFA"
void ToCodeString(__out qstring &string) const
{
size_t count = bytes.size();
if (count > 0)
{
string.reserve(count * SIZESTR("\\xCC"));
for (size_t i = 0; i < count; i++)
{
if (mask[i])
string.cat_sprnt("\\x%02X", bytes[i]);
else
string += "\\xCC";
}
}
}
// Convert signature to a "inline" byte style C string. E.g. "{0x33,0x9A,0xFA,0xAE,0xAE,0xAE,0xAE,0x45,0x68}"
void ToInlineString(__out qstring &string) const
{
size_t count = bytes.size();
if (count > 0)
{
string = "const BYTE name_me[]={";
for (size_t i = 0; i < count; i++)
{
if (mask[i])
string.cat_sprnt("0x%02X,", bytes[i]);
else
string.cat_sprnt("0x%02X,", settings.maskByte);
}
string.remove_last();
string += "};";
}
}
// Right trim wildcards from signiture if they exist
void trim()
{
size_t len = 0;
for (size_t i = (bytes.size() - 1); i > 0; i--)
{
if (!mask[i])
len++;
else
break;
}
if (len)
{
size_t newSize = (bytes.size() - len);
bytes.resize(newSize);
mask.resize(newSize);
}
}
// Return wildcard/mask count
size_t wildcards() const
{
size_t count = 0;
// TODO: Vectorize this functions for speed?
size_t size = bytes.size();
for (size_t i = 0; i < size; ++i)
{
if (!mask[i])
count++;
}
return count;
}
// Return TRUE is there is one or more wildcard/mask bytes
__inline BOOL hasMask() const
{
return memchr(mask.data(), 0, bytes.size()) != NULL;
}
// ------------------------------------------------------------------------
SIG& operator+=(const SIG &rhs)
{
// Append another sig to me
bytes.insert(bytes.end(), rhs.bytes.begin(), rhs.bytes.end());
mask.insert(mask.end(), rhs.mask.begin(), rhs.mask.end());
return *this;
}
};
// Search.cpp
enum SSTATUS
{
NOT_FOUND, // Signature not found error
UNIQUE, // Unique, single instance found
NOT_UNIQUE // Not unique, more than one instance found
};
SSTATUS SearchSignature(const SIG &sig);
void SearchCleanup();
// Signature.cpp
void CreateFunctionSig();
void CreateAddressSig();
void CreateAddressRangeSig();
void OutputSignature(const SIG &sig, ea_t address, UINT32 offset);