-
Notifications
You must be signed in to change notification settings - Fork 2
/
patterngenerator.cpp
235 lines (184 loc) · 6.18 KB
/
patterngenerator.cpp
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
#include "patterngenerator.h"
#include "listingconsolerenderer.h"
#include <redasm/support/utils.h>
#include <redasm/support/hash.h>
#include <redasm/plugins/plugins.h>
#include <redasm/loaders/binary/binary.h>
#include <fstream>
#define PATTERN_OFFSET(offset) (offset / 2)
#define PATTERN_SIZE(size) PATTERN_OFFSET(size)
#define SIGNATURE_BYTES(pattern) PATTERN_SIZE(pattern.size())
PatternGenerator::PatternGenerator(): std::list<BytePattern>() { }
void PatternGenerator::setPrefix(const std::string &prefix) { m_prefix = prefix; }
void PatternGenerator::setSuffix(const std::string &suffix) { m_suffix = suffix; }
bool PatternGenerator::writePatternsSource(json &patterns)
{
for(auto it = this->begin(); it != this->end(); it++)
{
if(!this->isBytePatternValid(*it))
continue;
auto patternobj = json::object();
patternobj["name"] = it->name;
patternobj["pattern"] = it->pattern;
patternobj["symboltype"] = it->symboltype;
patterns.push_back(patternobj);
}
return true;
}
void PatternGenerator::push(const BytePattern &bp)
{
u16 checksum = bp.hash();
auto it = m_done.find(checksum);
if(it != m_done.end())
return;
m_done.insert(checksum);
this->emplace_back(bp);
}
bool PatternGenerator::writePatterns(REDasm::SignatureDB &sigdb)
{
for(auto it = this->begin(); it != this->end(); it++)
{
if(!this->isBytePatternValid(*it))
continue;
REDasm::Signature sig;
sig.symboltype(it->symboltype);
sig.size(SIGNATURE_BYTES(it->pattern));
sig.name(it->name);
if(!this->appendAllPatterns(&sig, *it))
return false;
sigdb << sig;
}
return true;
}
bool PatternGenerator::disassemble(const BytePattern& bytepattern)
{
REDasm::MemoryBuffer buffer = REDasm::bytes(bytepattern.pattern);
std::unique_ptr<REDasm::Disassembler> disassembler(this->createDisassembler(&buffer));
if(!disassembler)
return false;
std::unique_ptr<REDasm::Printer> printer(disassembler->createPrinter());
disassembler->disassemble();
ListingConsoleRenderer consolerenderer(disassembler.get());
consolerenderer.renderAll();
return true;
}
bool PatternGenerator::appendAllPatterns(REDasm::Signature *signature, const BytePattern &bytepattern) const
{
u64 offset = 0;
while(offset < bytepattern.pattern.size())
{
REDasm::SignaturePattern sigpatt;
bool wildcard = false;
std::string chunk = this->getChunk(bytepattern.pattern, offset, &wildcard);
if(chunk.empty())
{
std::cout << "ERROR: Empty chunk @ " << REDasm::hex(offset) << std::endl;
return false;
}
if(!wildcard)
{
sigpatt.offset(offset);
sigpatt.checksum(this->chunkChecksum(chunk));
sigpatt.size(PATTERN_SIZE(chunk.size()));
signature->patterns().push_back(sigpatt);
}
offset += chunk.size();
}
return true;
}
bool PatternGenerator::isBytePatternValid(const BytePattern &bytepattern) const
{
if(bytepattern.pattern.empty())
return false;
if(bytepattern.name.empty())
{
std::cout << "WARNING: Skipping anonymous pattern" << std::endl;
return false;
}
int wc = 0;
for(char ch : bytepattern.pattern)
{
if(ch == WILDCARD_CHARACTER)
wc++;
}
if((wc / static_cast<float>(bytepattern.pattern.size())) >= 0.50)
{
std::cout << "WARNING: Skipping unrealiable pattern: " << REDasm::quoted(bytepattern.name) << std::endl;
return false;
}
return true;
}
u16 PatternGenerator::chunkChecksum(const std::string &chunk) const
{
std::vector<u8> bytes(SIGNATURE_BYTES(chunk));
for(size_t i = 0, j = 0; i < chunk.size(); i += 2, j++)
REDasm::byte(chunk, &bytes[j], i);
return REDasm::Hash::crc16(bytes.data(), bytes.size());
}
std::string PatternGenerator::getChunk(const std::string &s, int offset, bool* wildcard) const
{
std::string chunk, hexbyte = s.substr(offset, 2);
if(hexbyte == WILDCARD_PATTERN)
{
*wildcard = true;
while((offset < s.size()) && (hexbyte == WILDCARD_PATTERN))
{
chunk += hexbyte;
offset += 2;
hexbyte = s.substr(offset, 2);
}
}
else
{
*wildcard = false;
while((offset < s.size()) && (hexbyte != WILDCARD_PATTERN))
{
chunk += hexbyte;
offset += 2;
hexbyte = s.substr(offset, 2);
}
}
return chunk;
}
std::string PatternGenerator::fullName(const std::string &name) const
{
std::string fullname = name;
if(!m_prefix.empty())
fullname = m_prefix + "." + fullname;
return fullname + m_suffix;
}
REDasm::Disassembler *PatternGenerator::createDisassembler(REDasm::AbstractBuffer *buffer)
{
if(buffer->empty())
{
std::cout << "ERROR: Pattern is empty" << std::endl;
return NULL;
}
const REDasm::AssemblerPlugin_Entry* assemblerentry = REDasm::getAssembler(this->assembler());
if(!assemblerentry)
{
std::cout << "ERROR: Invalid Assembler: " << REDasm::quoted(this->assembler()) << std::endl;
return NULL;
}
address_t baseaddress = 1u << (assemblerentry->bits() - 1);
REDasm::BinaryLoader* loader = new REDasm::BinaryLoader(buffer);
loader->build(assemblerentry->name(), 0, baseaddress, baseaddress);
return new REDasm::Disassembler(assemblerentry->init(), loader); // Takes ownership
}
void PatternGenerator::pushPattern(u32 symboltype, const std::string &name, const std::string &pattern)
{
this->push_back({ symboltype , this->fullName(name), pattern });
}
std::string PatternGenerator::subPattern(const std::string &pattern, size_t pos, size_t len) { return pattern.substr(pos, len); }
void PatternGenerator::wildcard(std::string &pattern, size_t pos, size_t n)
{
pos *= 2;
if(pos >= pattern.size())
{
if(pos > pattern.size())
std::cout << "WARNING: " << pos << " >= " << pattern.size() << std::endl;
return;
}
for(size_t i = 0; i < n; i++, pos += 2)
pattern.replace(pos, 2, WILDCARD_PATTERN);
}