forked from Velocidex/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.h
325 lines (262 loc) · 14.2 KB
/
modules.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
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
320
321
322
323
324
325
/*
Copyright (c) 2014. The YARA Authors. All Rights Reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef YR_MODULES_H
#define YR_MODULES_H
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <yara_error.h>
#include <yara_exec.h>
#include <yara_libyara.h>
#include <yara_limits.h>
#include <yara_object.h>
#include <yara_re.h>
#include <yara_types.h>
#include <yara_utils.h>
// Concatenation that macro-expands its arguments.
#define YR_CONCAT(arg1, arg2) _YR_CONCAT(arg1, arg2) // expands the arguments.
#define _YR_CONCAT(arg1, arg2) arg1##arg2 // do the actual concatenation.
#define module_declarations YR_CONCAT(MODULE_NAME, __declarations)
#define module_load YR_CONCAT(MODULE_NAME, __load)
#define module_unload YR_CONCAT(MODULE_NAME, __unload)
#define module_initialize YR_CONCAT(MODULE_NAME, __initialize)
#define module_finalize YR_CONCAT(MODULE_NAME, __finalize)
#define begin_declarations \
int module_declarations(YR_OBJECT* module) \
{ \
YR_OBJECT* stack[64]; \
int stack_top = 0; \
stack[stack_top] = module;
#define end_declarations \
return ERROR_SUCCESS; \
}
#define begin_struct(name) \
{ \
YR_OBJECT* structure; \
FAIL_ON_ERROR(yr_object_create( \
OBJECT_TYPE_STRUCTURE, name, stack[stack_top], &structure)); \
assertf( \
stack_top < sizeof(stack) / sizeof(stack[0]) - 1, \
"too many nested structures"); \
stack[++stack_top] = structure; \
}
#define begin_struct_array(name) \
{ \
YR_OBJECT* structure; \
YR_OBJECT* array; \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_ARRAY, name, stack[stack_top], &array)); \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_STRUCTURE, name, array, &structure)); \
assertf( \
stack_top < sizeof(stack) / sizeof(stack[0]) - 1, \
"too many nested structures"); \
stack[++stack_top] = structure; \
}
#define begin_struct_dictionary(name) \
{ \
YR_OBJECT* structure; \
YR_OBJECT* array; \
FAIL_ON_ERROR(yr_object_create( \
OBJECT_TYPE_DICTIONARY, name, stack[stack_top], &array)); \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_STRUCTURE, name, array, &structure)); \
assertf( \
stack_top < sizeof(stack) / sizeof(stack[0]) - 1, \
"too many nested structures"); \
stack[++stack_top] = structure; \
}
#define end_struct(name) \
{ \
assert(stack[stack_top]->type == OBJECT_TYPE_STRUCTURE); \
assertf( \
strcmp(stack[stack_top]->identifier, name) == 0, \
"unbalanced begin_struct/end_struct"); \
stack_top--; \
}
#define end_struct_array(name) \
end_struct \
(name)
#define end_struct_dictionary(name) \
end_struct \
(name)
#define declare_integer(name) \
{ \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_INTEGER, name, stack[stack_top], NULL)); \
}
#define declare_integer_array(name) \
{ \
YR_OBJECT* array; \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_ARRAY, name, stack[stack_top], &array)); \
FAIL_ON_ERROR(yr_object_create(OBJECT_TYPE_INTEGER, name, array, NULL)); \
}
#define declare_integer_dictionary(name) \
{ \
YR_OBJECT* dict; \
FAIL_ON_ERROR(yr_object_create( \
OBJECT_TYPE_DICTIONARY, name, stack[stack_top], &dict)); \
FAIL_ON_ERROR(yr_object_create(OBJECT_TYPE_INTEGER, name, dict, NULL)); \
}
#define declare_float(name) \
{ \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_FLOAT, name, stack[stack_top], NULL)); \
}
#define declare_float_array(name) \
{ \
YR_OBJECT* array; \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_ARRAY, name, stack[stack_top], &array)); \
FAIL_ON_ERROR(yr_object_create(OBJECT_TYPE_FLOAT, name, array, NULL)); \
}
#define declare_float_dictionary(name) \
{ \
YR_OBJECT* dict; \
FAIL_ON_ERROR(yr_object_create( \
OBJECT_TYPE_DICTIONARY, name, stack[stack_top], &dict)); \
FAIL_ON_ERROR(yr_object_create(OBJECT_TYPE_FLOAT, name, dict, NULL)); \
}
#define declare_string(name) \
{ \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_STRING, name, stack[stack_top], NULL)); \
}
#define declare_string_array(name) \
{ \
YR_OBJECT* array; \
FAIL_ON_ERROR( \
yr_object_create(OBJECT_TYPE_ARRAY, name, stack[stack_top], &array)); \
FAIL_ON_ERROR(yr_object_create(OBJECT_TYPE_STRING, name, array, NULL)); \
}
#define declare_string_dictionary(name) \
{ \
YR_OBJECT* dict; \
FAIL_ON_ERROR(yr_object_create( \
OBJECT_TYPE_DICTIONARY, name, stack[stack_top], &dict)); \
FAIL_ON_ERROR(yr_object_create(OBJECT_TYPE_STRING, name, dict, NULL)); \
}
#define declare_function(name, args_fmt, ret_fmt, func) \
{ \
YR_OBJECT* function; \
FAIL_ON_ERROR(yr_object_function_create( \
name, args_fmt, ret_fmt, func, stack[stack_top], &function)); \
}
#define define_function(func) \
int func( \
YR_VALUE* __args, \
YR_SCAN_CONTEXT* __context, \
YR_OBJECT_FUNCTION* __function_obj)
#define sized_string_argument(n) (__args[n - 1].ss)
#define string_argument(n) (sized_string_argument(n)->c_string)
#define integer_argument(n) (__args[n - 1].i)
#define float_argument(n) (__args[n - 1].d)
#define regexp_argument(n) ((RE*) (__args[n - 1].re))
#define module() yr_object_get_root((YR_OBJECT*) __function_obj)
#define parent() (__function_obj->parent)
#define scan_context() (__context)
#define foreach_memory_block(iterator, block) \
for (block = iterator->first(iterator); block != NULL; \
block = iterator->next(iterator))
#define first_memory_block(context) \
(context)->iterator->first((context)->iterator)
#define is_undefined(object, ...) \
yr_object_has_undefined_value(object, __VA_ARGS__)
#define get_object(object, ...) yr_object_lookup(object, 0, __VA_ARGS__)
#define get_integer(object, ...) yr_object_get_integer(object, __VA_ARGS__)
#define get_float(object, ...) yr_object_get_float(object, __VA_ARGS__)
#define get_string(object, ...) yr_object_get_string(object, __VA_ARGS__)
#define set_integer(value, object, ...) \
yr_object_set_integer(value, object, __VA_ARGS__)
#define set_float(value, object, ...) \
yr_object_set_float(value, object, __VA_ARGS__)
#define set_sized_string(value, len, object, ...) \
yr_object_set_string(value, len, object, __VA_ARGS__)
#define set_string(value, object, ...) \
set_sized_string( \
value, (value == NULL) ? 0 : strlen(value), object, __VA_ARGS__)
#define return_integer(integer) \
{ \
assertf( \
__function_obj->return_obj->type == OBJECT_TYPE_INTEGER, \
"return type differs from function declaration"); \
return yr_object_set_integer((integer), __function_obj->return_obj, NULL); \
}
#define return_float(double_) \
{ \
double d = (double) (double_); \
assertf( \
__function_obj->return_obj->type == OBJECT_TYPE_FLOAT, \
"return type differs from function declaration"); \
return yr_object_set_float( \
(d != (double) YR_UNDEFINED) ? d : NAN, \
__function_obj->return_obj, \
NULL); \
}
#define return_string(string) \
{ \
char* s = (char*) (string); \
assertf( \
__function_obj->return_obj->type == OBJECT_TYPE_STRING, \
"return type differs from function declaration"); \
return yr_object_set_string( \
(s != (char*) YR_UNDEFINED) ? s : NULL, \
(s != (char*) YR_UNDEFINED) ? strlen(s) : 0, \
__function_obj->return_obj, \
NULL); \
}
typedef int (*YR_EXT_INITIALIZE_FUNC)(YR_MODULE* module);
typedef int (*YR_EXT_FINALIZE_FUNC)(YR_MODULE* module);
typedef int (*YR_EXT_DECLARATIONS_FUNC)(YR_OBJECT* module_object);
typedef int (*YR_EXT_LOAD_FUNC)(
YR_SCAN_CONTEXT* context,
YR_OBJECT* module_object,
void* module_data,
size_t module_data_size);
typedef int (*YR_EXT_UNLOAD_FUNC)(YR_OBJECT* module_object);
struct YR_MODULE
{
char* name;
YR_EXT_DECLARATIONS_FUNC declarations;
YR_EXT_LOAD_FUNC load;
YR_EXT_UNLOAD_FUNC unload;
YR_EXT_INITIALIZE_FUNC initialize;
YR_EXT_FINALIZE_FUNC finalize;
};
struct YR_MODULE_IMPORT
{
const char* module_name;
void* module_data;
size_t module_data_size;
};
int yr_modules_initialize(void);
int yr_modules_finalize(void);
int yr_modules_do_declarations(
const char* module_name,
YR_OBJECT* main_structure);
int yr_modules_load(const char* module_name, YR_SCAN_CONTEXT* context);
int yr_modules_unload_all(YR_SCAN_CONTEXT* context);
#endif