forked from avast/retdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodifications.cpp
319 lines (281 loc) · 7.85 KB
/
modifications.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
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
/**
* @file src/pat2yara/modifications.cpp
* @brief Rule manipulation functions for yara patterns filter.
* @copyright (c) 2017 Avast Software, licensed under the MIT license
*/
#include <set>
#include "pat2yara/compare.h"
#include "pat2yara/modifications.h"
#include "pat2yara/utils.h"
#include "yaramod/builder/yara_expression_builder.h"
#include "yaramod/builder/yara_file_builder.h"
#include "yaramod/builder/yara_rule_builder.h"
#include "yaramod/types/rule.h"
using namespace yaramod;
namespace
{
/**
* Delphi (template) function represenation.
*
* E.g. Class<Type>.Method will be stored as class-method pair, ignoring type,
* if funtion is template function Function<Type> it will be stored as Function
* with '<' character in method position to differ with non-template functions.
*/
using TemplatePair = std::pair<std::string, std::string>;
/**
* Split (template) function definition to TemplatePair type.
*
* @param name function definition (name attribute)
*
* @return new TemplatePair
*/
TemplatePair getTemplatePair(
const std::string &name)
{
const auto first = name.find_first_of('<');
const auto last = name.find_last_of('>');
if (first == std::string::npos || last == std::string::npos) {
// Simple non-template function.
return TemplatePair(name, "");
}
std::string firstName = name.substr(0, first);
if (last == name.size() - 1) {
// Rightmost '>' is last character - template function.
return TemplatePair(firstName, "<");
}
// Skip dot symbol too so +2.
return TemplatePair(firstName, name.substr(last + 2, std::string::npos));
}
} // anonymous namespace
/**
* Create architecture rule from another rule.
*
* @param rule source rule (usually first rule from input file)
*
* @return private empty rule with architecture information only
*/
std::unique_ptr<Rule> createArchitectureRule(
const yaramod::Rule* rule)
{
YaraRuleBuilder builder;
builder.withName("architecture");
builder.withModifier(Rule::Modifier::Private);
for (const Meta &meta : rule->getMetas()) {
const auto &key = meta.getKey();
const auto &value = meta.getValue().getPureText();
if (key == "architecture") {
builder.withStringMeta("architecture", value);
continue;
}
if (key == "endianness") {
builder.withStringMeta("endianness", value);
continue;
}
if (key == "bitWidth") {
const auto width = std::stoi(meta.getValue().getText());
builder.withIntMeta("bits", width);
}
}
return builder.get();
}
/**
* Create log-file rule from another rule.
*
* @param rule source rule (usually rule that is being removed)
* @param reason reason of rule being removed
*
* @return rule for log-file
*/
std::unique_ptr<Rule> createLogRule(
const yaramod::Rule *rule,
const std::string &reason = "")
{
YaraRuleBuilder ruleBuilder;
ruleBuilder.withName(rule->getName());
for (const Meta &meta : rule->getMetas()) {
const auto &key = meta.getKey();
const auto &value = meta.getValue().getPureText();
if (key == "name") {
ruleBuilder.withStringMeta("name", value);
continue;
}
if (key == "refs") {
ruleBuilder.withStringMeta("refs", value);
continue;
}
if (key == "source") {
ruleBuilder.withStringMeta("source", value);
}
}
if (!reason.empty()) {
ruleBuilder.withStringMeta("reason", reason);
}
ruleBuilder.withHexString("$1", getHexPattern(rule, "$1"));
ruleBuilder.withCondition(boolVal(false).get());
return ruleBuilder.get();
}
/**
* Create new HexString with limited size.
*
* @param hexString original HexString
* @param limit maximal size in bytes
*
* @return shortened HexString
*/
std::shared_ptr<HexString> cutHexString(
const std::shared_ptr<HexString> &hexString,
std::size_t limit)
{
// Multiply limit by two as HexString works with nibbles.
std::size_t nibbleLimit = limit * 2;
if (hexString->getLength() > nibbleLimit) {
const auto old = hexString->getUnits();
std::vector<std::shared_ptr<HexStringUnit>> newUnits(old.begin(),
old.begin() + nibbleLimit);
// Create new hexadecimal string.
return std::make_shared<HexString>(
hexString->getTokenStream(),
newUnits);
}
// String is already shorter than limit.
return hexString;
}
/**
* Cut string before limit but on whitespace.
*
* @param inputString original string
* @param limit maximal string size
*
* @return shortened string
*/
std::string cutStringWhitespace(
const std::string &inputString,
std::size_t limit)
{
if (inputString.size() < limit) {
// No cut necessary.
return inputString;
}
const auto last = inputString.find_last_of(' ', limit);
if (last == std::string::npos) {
// Cut anyway (should not happend in correct input).
return inputString.substr(0, limit);
}
// Cut on position of last within limit.
return inputString.substr(0, last);
}
/**
* Add necessary meta attributes to new rule builder.
*
* @param builder target rule builder
* @param rule source rule
*/
void filterMetaSection(
YaraRuleBuilder &builder,
const Rule* rule)
{
for (const Meta &meta : rule->getMetas()) {
const auto &key = meta.getKey();
const auto &value = meta.getValue().getPureText();
if (key == "name") {
builder.withStringMeta("name", value);
continue;
}
if (key == "note") {
builder.withStringMeta("note", value);
continue;
}
if (key == "refs") {
builder.withStringMeta("refs",
cutStringWhitespace(value, YARA_BUF_SIZE));
continue;
}
if (key == "size") {
builder.withIntMeta("size", std::stoi(meta.getValue().getText()));
}
}
}
/**
* Copy rule to new builder.
*
* Only metas copied are name, size, note and references (cut to refs).
*
* @param builder target rule builder
* @param rule source rule
*/
void copyRuleToBuilder(
YaraRuleBuilder &builder,
const Rule* rule)
{
filterMetaSection(builder, rule);
builder.withName(rule->getName());
builder.withHexString("$1", getHexPattern(rule, "$1"));
builder.withCondition(stringRef("$1").get());
}
/**
* Pack Delphi template names to simple format Class<T>.Method or similar.
* @param builder target for final rule
* @param alternativeRules rule with name and its alternatives
*/
void packDelhpi(
yaramod::YaraFileBuilder &builder,
const RuleRelations &alternativeRules)
{
if (!alternativeRules.hasEquals()) {
return;
}
// Pointer to main rule.
const auto* mainRule = alternativeRules.getRule();
// Strip types from templates.
std::set<TemplatePair> templates;
templates.insert(getTemplatePair(getName(mainRule)));
for (const auto &rule : alternativeRules.getEquals()) {
templates.insert(getTemplatePair(getName(rule)));
}
// Create declarations.
std::vector<std::string> functions;
for (auto it = templates.cbegin(), e = templates.cend(); it != e; ++it) {
if (it->second.empty()) {
functions.emplace_back(it->first);
}
else if (it->second == "<") {
functions.emplace_back(it->first + "<T>");
}
else {
functions.emplace_back(it->first + "<T>." + it->second);
}
}
YaraRuleBuilder newRule;
newRule.withName(mainRule->getName());
newRule.withStringMeta("name", functions[0]);
// Copy meta section but without name.
for (const Meta &meta : mainRule->getMetas()) {
const auto &key = meta.getKey();
const auto &value = meta.getValue().getPureText();
if (key == "note") {
newRule.withStringMeta("note", value);
continue;
}
if (key == "refs") {
// Already shortened - no cut necessary.
newRule.withStringMeta("refs", value);
continue;
}
if (key == "size") {
newRule.withIntMeta("size", std::stoi(meta.getValue().getText()));
}
}
newRule.withHexString("$1", getHexPattern(mainRule, "$1"));
newRule.withCondition(mainRule->getCondition());
// Collect alternative templates if any.
std::string alternatives;
for (std::size_t i = 1; i < functions.size(); ++i) {
alternatives += functions[i] + " ";
}
if (!alternatives.empty()) {
alternatives.pop_back();
newRule.withStringMeta("altNames",
cutStringWhitespace(alternatives, YARA_BUF_SIZE));
}
builder.withRule(newRule.get());
}