/src/yara/libyara/modules/math/math.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2014-2021. The YARA Authors. All Rights Reserved. |
3 | | |
4 | | Redistribution and use in source and binary forms, with or without modification, |
5 | | are permitted provided that the following conditions are met: |
6 | | |
7 | | 1. Redistributions of source code must retain the above copyright notice, this |
8 | | list of conditions and the following disclaimer. |
9 | | |
10 | | 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | this list of conditions and the following disclaimer in the documentation and/or |
12 | | other materials provided with the distribution. |
13 | | |
14 | | 3. Neither the name of the copyright holder nor the names of its contributors |
15 | | may be used to endorse or promote products derived from this software without |
16 | | specific prior written permission. |
17 | | |
18 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19 | | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
22 | | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24 | | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
25 | | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | #include <math.h> |
31 | | #include <stdlib.h> |
32 | | #include <yara/mem.h> |
33 | | #include <yara/modules.h> |
34 | | #include <yara/strutils.h> |
35 | | #include <yara/utils.h> |
36 | | |
37 | | #define MODULE_NAME math |
38 | | |
39 | | #define PI 3.141592653589793 |
40 | | // This is more than enough space to hold the maximum signed 64bit integer as a |
41 | | // string in decimal, hex or octal, including the sign and NULL terminator. |
42 | 0 | #define INT64_MAX_STRING 30 |
43 | | |
44 | | // log2 is not defined by math.h in VC++ |
45 | | |
46 | | #if defined(_MSC_VER) && _MSC_VER < 1800 |
47 | | static double log2(double n) |
48 | | { |
49 | | return log(n) / log(2.0); |
50 | | } |
51 | | #endif |
52 | | |
53 | | uint32_t* get_distribution( |
54 | | int64_t offset, |
55 | | int64_t length, |
56 | | YR_SCAN_CONTEXT* context) |
57 | 0 | { |
58 | 0 | bool past_first_block = false; |
59 | |
|
60 | 0 | size_t i; |
61 | |
|
62 | 0 | uint32_t* data = (uint32_t*) yr_calloc(256, sizeof(uint32_t)); |
63 | |
|
64 | 0 | if (data == NULL) |
65 | 0 | return NULL; |
66 | | |
67 | 0 | YR_MEMORY_BLOCK* block = first_memory_block(context); |
68 | 0 | YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator; |
69 | |
|
70 | 0 | if (block == NULL || offset < 0 || length < 0 || offset < block->base) |
71 | 0 | { |
72 | 0 | yr_free(data); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | | |
76 | 0 | foreach_memory_block(iterator, block) |
77 | 0 | { |
78 | 0 | if (offset >= block->base && offset < block->base + block->size) |
79 | 0 | { |
80 | 0 | size_t data_offset = (size_t) (offset - block->base); |
81 | 0 | size_t data_len = (size_t) yr_min( |
82 | 0 | length, (size_t) (block->size - data_offset)); |
83 | |
|
84 | 0 | const uint8_t* block_data = yr_fetch_block_data(block); |
85 | |
|
86 | 0 | if (block_data == NULL) |
87 | 0 | { |
88 | 0 | yr_free(data); |
89 | 0 | return NULL; |
90 | 0 | } |
91 | | |
92 | 0 | offset += data_len; |
93 | 0 | length -= data_len; |
94 | |
|
95 | 0 | for (i = 0; i < data_len; i++) |
96 | 0 | { |
97 | 0 | uint8_t c = *(block_data + data_offset + i); |
98 | 0 | data[c]++; |
99 | 0 | } |
100 | |
|
101 | 0 | past_first_block = true; |
102 | 0 | } |
103 | 0 | else if (past_first_block) |
104 | 0 | { |
105 | | // If offset is not within current block and we already |
106 | | // past the first block then the we are trying to compute |
107 | | // the distribution over a range of non contiguous blocks. As |
108 | | // range contains gaps of undefined data the distribution is |
109 | | // undefined. |
110 | |
|
111 | 0 | yr_free(data); |
112 | 0 | return NULL; |
113 | 0 | } |
114 | | |
115 | 0 | if (block->base + block->size >= offset + length) |
116 | 0 | break; |
117 | 0 | } |
118 | | |
119 | 0 | if (!past_first_block) |
120 | 0 | { |
121 | 0 | yr_free(data); |
122 | 0 | return NULL; |
123 | 0 | } |
124 | 0 | return data; |
125 | 0 | } |
126 | | |
127 | | uint32_t* get_distribution_global(YR_SCAN_CONTEXT* context) |
128 | 0 | { |
129 | 0 | size_t i; |
130 | |
|
131 | 0 | int64_t expected_next_offset = 0; |
132 | |
|
133 | 0 | uint32_t* data = (uint32_t*) yr_calloc(256, sizeof(uint32_t)); |
134 | |
|
135 | 0 | if (data == NULL) |
136 | 0 | return NULL; |
137 | | |
138 | 0 | YR_MEMORY_BLOCK* block; |
139 | 0 | YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator; |
140 | 0 | foreach_memory_block(iterator, block) |
141 | 0 | { |
142 | 0 | if (expected_next_offset != block->base) |
143 | 0 | { |
144 | | // If offset is not directly after the current block then |
145 | | // we are trying to compute the distribution over a range of non |
146 | | // contiguous blocks. As the range contains gaps of |
147 | | // undefined data the distribution is undefined. |
148 | 0 | yr_free(data); |
149 | 0 | return NULL; |
150 | 0 | } |
151 | 0 | const uint8_t* block_data = yr_fetch_block_data(block); |
152 | |
|
153 | 0 | if (block_data == NULL) |
154 | 0 | { |
155 | 0 | yr_free(data); |
156 | 0 | return NULL; |
157 | 0 | } |
158 | | |
159 | 0 | for (i = 0; i < block->size; i++) |
160 | 0 | { |
161 | 0 | uint8_t c = *(block_data + i); |
162 | 0 | data[c] += 1; |
163 | 0 | } |
164 | 0 | expected_next_offset = block->base + block->size; |
165 | 0 | } |
166 | 0 | return data; |
167 | 0 | } |
168 | | |
169 | | define_function(string_entropy) |
170 | 0 | { |
171 | 0 | size_t i; |
172 | 0 | double entropy = 0.0; |
173 | |
|
174 | 0 | SIZED_STRING* s = sized_string_argument(1); |
175 | |
|
176 | 0 | uint32_t* data = (uint32_t*) yr_calloc(256, sizeof(uint32_t)); |
177 | |
|
178 | 0 | if (data == NULL) |
179 | 0 | return_float(YR_UNDEFINED); |
180 | |
|
181 | 0 | for (i = 0; i < s->length; i++) |
182 | 0 | { |
183 | 0 | uint8_t c = s->c_string[i]; |
184 | 0 | data[c] += 1; |
185 | 0 | } |
186 | |
|
187 | 0 | for (i = 0; i < 256; i++) |
188 | 0 | { |
189 | 0 | if (data[i] != 0) |
190 | 0 | { |
191 | 0 | double x = (double) (data[i]) / s->length; |
192 | 0 | entropy -= x * log2(x); |
193 | 0 | } |
194 | 0 | } |
195 | |
|
196 | 0 | yr_free(data); |
197 | 0 | return_float(entropy); |
198 | 0 | } |
199 | | |
200 | | define_function(data_entropy) |
201 | 0 | { |
202 | 0 | double entropy = 0.0; |
203 | |
|
204 | 0 | int64_t offset = integer_argument(1); // offset where to start |
205 | 0 | int64_t length = integer_argument(2); // length of bytes we want entropy on |
206 | |
|
207 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
208 | |
|
209 | 0 | size_t i; |
210 | |
|
211 | 0 | size_t total_len = 0; |
212 | |
|
213 | 0 | uint32_t* data = get_distribution(offset, length, context); |
214 | |
|
215 | 0 | if (data == NULL) |
216 | 0 | return_float(YR_UNDEFINED); |
217 | |
|
218 | 0 | for (i = 0; i < 256; i++) |
219 | 0 | { |
220 | 0 | total_len += data[i]; |
221 | 0 | } |
222 | |
|
223 | 0 | for (i = 0; i < 256; i++) |
224 | 0 | { |
225 | 0 | if (data[i] != 0) |
226 | 0 | { |
227 | 0 | double x = (double) (data[i]) / total_len; |
228 | 0 | entropy -= x * log2(x); |
229 | 0 | } |
230 | 0 | } |
231 | |
|
232 | 0 | yr_free(data); |
233 | 0 | return_float(entropy); |
234 | 0 | } |
235 | | |
236 | | define_function(string_deviation) |
237 | 0 | { |
238 | 0 | SIZED_STRING* s = sized_string_argument(1); |
239 | |
|
240 | 0 | double mean = float_argument(2); |
241 | 0 | double sum = 0.0; |
242 | |
|
243 | 0 | size_t i; |
244 | |
|
245 | 0 | for (i = 0; i < s->length; i++) sum += fabs(((double) s->c_string[i]) - mean); |
246 | |
|
247 | 0 | return_float(sum / s->length); |
248 | 0 | } |
249 | | |
250 | | define_function(data_deviation) |
251 | 0 | { |
252 | 0 | int64_t offset = integer_argument(1); |
253 | 0 | int64_t length = integer_argument(2); |
254 | |
|
255 | 0 | double mean = float_argument(3); |
256 | 0 | double sum = 0.0; |
257 | |
|
258 | 0 | size_t total_len = 0; |
259 | 0 | size_t i; |
260 | |
|
261 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
262 | |
|
263 | 0 | uint32_t* data = get_distribution(offset, length, context); |
264 | |
|
265 | 0 | if (data == NULL) |
266 | 0 | return_float(YR_UNDEFINED); |
267 | |
|
268 | 0 | for (i = 0; i < 256; i++) |
269 | 0 | { |
270 | 0 | total_len += data[i]; |
271 | 0 | sum += fabs(((double) i) - mean) * data[i]; |
272 | 0 | } |
273 | |
|
274 | 0 | yr_free(data); |
275 | 0 | return_float(sum / total_len); |
276 | 0 | } |
277 | | |
278 | | define_function(string_mean) |
279 | 0 | { |
280 | 0 | size_t i; |
281 | 0 | double sum = 0.0; |
282 | |
|
283 | 0 | SIZED_STRING* s = sized_string_argument(1); |
284 | |
|
285 | 0 | for (i = 0; i < s->length; i++) sum += (double) s->c_string[i]; |
286 | |
|
287 | 0 | return_float(sum / s->length); |
288 | 0 | } |
289 | | |
290 | | define_function(data_mean) |
291 | 0 | { |
292 | 0 | double sum = 0.0; |
293 | |
|
294 | 0 | int64_t offset = integer_argument(1); |
295 | 0 | int64_t length = integer_argument(2); |
296 | |
|
297 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
298 | |
|
299 | 0 | size_t total_len = 0; |
300 | 0 | size_t i; |
301 | |
|
302 | 0 | uint32_t* data = get_distribution(offset, length, context); |
303 | |
|
304 | 0 | if (data == NULL) |
305 | 0 | return_float(YR_UNDEFINED); |
306 | |
|
307 | 0 | for (i = 0; i < 256; i++) |
308 | 0 | { |
309 | 0 | total_len += data[i]; |
310 | 0 | sum += ((double) i) * data[i]; |
311 | 0 | } |
312 | |
|
313 | 0 | yr_free(data); |
314 | 0 | return_float(sum / total_len); |
315 | 0 | } |
316 | | |
317 | | define_function(data_serial_correlation) |
318 | 0 | { |
319 | 0 | int past_first_block = false; |
320 | |
|
321 | 0 | size_t total_len = 0; |
322 | 0 | size_t i; |
323 | |
|
324 | 0 | int64_t offset = integer_argument(1); |
325 | 0 | int64_t length = integer_argument(2); |
326 | |
|
327 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
328 | 0 | YR_MEMORY_BLOCK* block = first_memory_block(context); |
329 | 0 | YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator; |
330 | |
|
331 | 0 | if (block == NULL) |
332 | 0 | return_float(YR_UNDEFINED); |
333 | |
|
334 | 0 | double sccun = 0; |
335 | 0 | double sccfirst = 0; |
336 | 0 | double scclast = 0; |
337 | 0 | double scct1 = 0; |
338 | 0 | double scct2 = 0; |
339 | 0 | double scct3 = 0; |
340 | 0 | double scc = 0; |
341 | |
|
342 | 0 | if (offset < 0 || length < 0 || offset < block->base) |
343 | 0 | return_float(YR_UNDEFINED); |
344 | |
|
345 | 0 | foreach_memory_block(iterator, block) |
346 | 0 | { |
347 | 0 | if (offset >= block->base && offset < block->base + block->size) |
348 | 0 | { |
349 | 0 | size_t data_offset = (size_t) (offset - block->base); |
350 | 0 | size_t data_len = (size_t) yr_min( |
351 | 0 | length, (size_t) (block->size - data_offset)); |
352 | |
|
353 | 0 | const uint8_t* block_data = yr_fetch_block_data(block); |
354 | |
|
355 | 0 | if (block_data == NULL) |
356 | 0 | return_float(YR_UNDEFINED); |
357 | |
|
358 | 0 | total_len += data_len; |
359 | 0 | offset += data_len; |
360 | 0 | length -= data_len; |
361 | |
|
362 | 0 | for (i = 0; i < data_len; i++) |
363 | 0 | { |
364 | 0 | sccun = (double) *(block_data + data_offset + i); |
365 | 0 | if (i == 0) |
366 | 0 | { |
367 | 0 | sccfirst = sccun; |
368 | 0 | } |
369 | 0 | scct1 += scclast * sccun; |
370 | 0 | scct2 += sccun; |
371 | 0 | scct3 += sccun * sccun; |
372 | 0 | scclast = sccun; |
373 | 0 | } |
374 | |
|
375 | 0 | past_first_block = true; |
376 | 0 | } |
377 | 0 | else if (past_first_block) |
378 | 0 | { |
379 | | // If offset is not within current block and we already |
380 | | // past the first block then the we are trying to compute |
381 | | // the checksum over a range of non contiguous blocks. As |
382 | | // range contains gaps of undefined data the checksum is |
383 | | // undefined. |
384 | 0 | return_float(YR_UNDEFINED); |
385 | 0 | } |
386 | | |
387 | 0 | if (block->base + block->size >= offset + length) |
388 | 0 | break; |
389 | 0 | } |
390 | | |
391 | 0 | if (!past_first_block) |
392 | 0 | return_float(YR_UNDEFINED); |
393 | |
|
394 | 0 | scct1 += scclast * sccfirst; |
395 | 0 | scct2 *= scct2; |
396 | |
|
397 | 0 | scc = total_len * scct3 - scct2; |
398 | |
|
399 | 0 | if (scc == 0) |
400 | 0 | scc = -100000; |
401 | 0 | else |
402 | 0 | scc = (total_len * scct1 - scct2) / scc; |
403 | |
|
404 | 0 | return_float(scc); |
405 | 0 | } |
406 | | |
407 | | define_function(string_serial_correlation) |
408 | 0 | { |
409 | 0 | SIZED_STRING* s = sized_string_argument(1); |
410 | |
|
411 | 0 | double sccun = 0; |
412 | 0 | double scclast = 0; |
413 | 0 | double scct1 = 0; |
414 | 0 | double scct2 = 0; |
415 | 0 | double scct3 = 0; |
416 | 0 | double scc = 0; |
417 | |
|
418 | 0 | size_t i; |
419 | |
|
420 | 0 | for (i = 0; i < s->length; i++) |
421 | 0 | { |
422 | 0 | sccun = (double) s->c_string[i]; |
423 | 0 | scct1 += scclast * sccun; |
424 | 0 | scct2 += sccun; |
425 | 0 | scct3 += sccun * sccun; |
426 | 0 | scclast = sccun; |
427 | 0 | } |
428 | |
|
429 | 0 | if (s->length > 0) |
430 | 0 | { |
431 | 0 | scct1 += scclast * (double) s->c_string[0]; |
432 | 0 | } |
433 | 0 | scct2 *= scct2; |
434 | |
|
435 | 0 | scc = s->length * scct3 - scct2; |
436 | |
|
437 | 0 | if (scc == 0) |
438 | 0 | scc = -100000; |
439 | 0 | else |
440 | 0 | scc = (s->length * scct1 - scct2) / scc; |
441 | |
|
442 | 0 | return_float(scc); |
443 | 0 | } |
444 | | |
445 | | define_function(data_monte_carlo_pi) |
446 | 0 | { |
447 | 0 | int past_first_block = false; |
448 | 0 | int mcount = 0; |
449 | 0 | int inmont = 0; |
450 | |
|
451 | 0 | double INCIRC = pow(pow(256.0, 3.0) - 1, 2.0); |
452 | 0 | double mpi = 0; |
453 | |
|
454 | 0 | size_t i; |
455 | |
|
456 | 0 | int64_t offset = integer_argument(1); |
457 | 0 | int64_t length = integer_argument(2); |
458 | |
|
459 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
460 | 0 | YR_MEMORY_BLOCK* block = first_memory_block(context); |
461 | 0 | YR_MEMORY_BLOCK_ITERATOR* iterator = context->iterator; |
462 | |
|
463 | 0 | if (block == NULL) |
464 | 0 | return_float(YR_UNDEFINED); |
465 | |
|
466 | 0 | if (offset < 0 || length < 0 || offset < block->base) |
467 | 0 | return_float(YR_UNDEFINED); |
468 | |
|
469 | 0 | foreach_memory_block(iterator, block) |
470 | 0 | { |
471 | 0 | if (offset >= block->base && offset < block->base + block->size) |
472 | 0 | { |
473 | 0 | unsigned int monte[6]; |
474 | |
|
475 | 0 | size_t data_offset = (size_t) (offset - block->base); |
476 | 0 | size_t data_len = (size_t) yr_min( |
477 | 0 | length, (size_t) (block->size - data_offset)); |
478 | |
|
479 | 0 | const uint8_t* block_data = yr_fetch_block_data(block); |
480 | |
|
481 | 0 | if (block_data == NULL) |
482 | 0 | return_float(YR_UNDEFINED); |
483 | |
|
484 | 0 | offset += data_len; |
485 | 0 | length -= data_len; |
486 | |
|
487 | 0 | for (i = 0; i < data_len; i++) |
488 | 0 | { |
489 | 0 | monte[i % 6] = (unsigned int) *(block_data + data_offset + i); |
490 | |
|
491 | 0 | if (i % 6 == 5) |
492 | 0 | { |
493 | 0 | double mx = 0; |
494 | 0 | double my = 0; |
495 | 0 | int j; |
496 | |
|
497 | 0 | mcount++; |
498 | |
|
499 | 0 | for (j = 0; j < 3; j++) |
500 | 0 | { |
501 | 0 | mx = (mx * 256.0) + monte[j]; |
502 | 0 | my = (my * 256.0) + monte[j + 3]; |
503 | 0 | } |
504 | |
|
505 | 0 | if ((mx * mx + my * my) <= INCIRC) |
506 | 0 | inmont++; |
507 | 0 | } |
508 | 0 | } |
509 | |
|
510 | 0 | past_first_block = true; |
511 | 0 | } |
512 | 0 | else if (past_first_block) |
513 | 0 | { |
514 | | // If offset is not within current block and we already |
515 | | // past the first block then the we are trying to compute |
516 | | // the checksum over a range of non contiguous blocks. As |
517 | | // range contains gaps of undefined data the checksum is |
518 | | // undefined. |
519 | 0 | return_float(YR_UNDEFINED); |
520 | 0 | } |
521 | | |
522 | 0 | if (block->base + block->size >= offset + length) |
523 | 0 | break; |
524 | 0 | } |
525 | | |
526 | 0 | if (!past_first_block || mcount == 0) |
527 | 0 | return_float(YR_UNDEFINED); |
528 | |
|
529 | 0 | mpi = 4.0 * ((double) inmont / mcount); |
530 | |
|
531 | 0 | return_float(fabs((mpi - PI) / PI)); |
532 | 0 | } |
533 | | |
534 | | define_function(string_monte_carlo_pi) |
535 | 0 | { |
536 | 0 | SIZED_STRING* s = sized_string_argument(1); |
537 | |
|
538 | 0 | double INCIRC = pow(pow(256.0, 3.0) - 1, 2.0); |
539 | 0 | double mpi = 0; |
540 | |
|
541 | 0 | unsigned int monte[6]; |
542 | |
|
543 | 0 | int mcount = 0; |
544 | 0 | int inmont = 0; |
545 | |
|
546 | 0 | size_t i; |
547 | |
|
548 | 0 | for (i = 0; i < s->length; i++) |
549 | 0 | { |
550 | 0 | monte[i % 6] = (unsigned int) s->c_string[i]; |
551 | |
|
552 | 0 | if (i % 6 == 5) |
553 | 0 | { |
554 | 0 | double mx = 0; |
555 | 0 | double my = 0; |
556 | |
|
557 | 0 | int j; |
558 | |
|
559 | 0 | mcount++; |
560 | |
|
561 | 0 | for (j = 0; j < 3; j++) |
562 | 0 | { |
563 | 0 | mx = (mx * 256.0) + monte[j]; |
564 | 0 | my = (my * 256.0) + monte[j + 3]; |
565 | 0 | } |
566 | |
|
567 | 0 | if ((mx * mx + my * my) <= INCIRC) |
568 | 0 | inmont++; |
569 | 0 | } |
570 | 0 | } |
571 | |
|
572 | 0 | if (mcount == 0) |
573 | 0 | return_float(YR_UNDEFINED); |
574 | |
|
575 | 0 | mpi = 4.0 * ((double) inmont / mcount); |
576 | 0 | return_float(fabs((mpi - PI) / PI)); |
577 | 0 | } |
578 | | |
579 | | define_function(in_range) |
580 | 0 | { |
581 | 0 | double test = float_argument(1); |
582 | 0 | double lower = float_argument(2); |
583 | 0 | double upper = float_argument(3); |
584 | |
|
585 | 0 | return_integer((lower <= test && test <= upper) ? 1 : 0); |
586 | 0 | } |
587 | | |
588 | | // Undefine existing "min" and "max" macros in order to avoid conflicts with |
589 | | // function names. |
590 | | #undef min |
591 | | #undef max |
592 | | |
593 | | define_function(min) |
594 | 0 | { |
595 | 0 | uint64_t i = integer_argument(1); |
596 | 0 | uint64_t j = integer_argument(2); |
597 | |
|
598 | 0 | return_integer(i < j ? i : j); |
599 | 0 | } |
600 | | |
601 | | define_function(max) |
602 | 0 | { |
603 | 0 | uint64_t i = integer_argument(1); |
604 | 0 | uint64_t j = integer_argument(2); |
605 | |
|
606 | 0 | return_integer(i > j ? i : j); |
607 | 0 | } |
608 | | |
609 | | define_function(to_number) |
610 | 0 | { |
611 | 0 | return_integer(integer_argument(1) ? 1 : 0); |
612 | 0 | } |
613 | | |
614 | | define_function(yr_math_abs) |
615 | 0 | { |
616 | 0 | return_integer(llabs(integer_argument(1))); |
617 | 0 | } |
618 | | |
619 | | define_function(count_range) |
620 | 0 | { |
621 | 0 | int64_t byte = integer_argument(1); |
622 | 0 | int64_t offset = integer_argument(2); |
623 | 0 | int64_t length = integer_argument(3); |
624 | |
|
625 | 0 | if (byte < 0 || byte > 255) |
626 | 0 | return_integer(YR_UNDEFINED); |
627 | |
|
628 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
629 | |
|
630 | 0 | uint32_t* distribution = get_distribution(offset, length, context); |
631 | |
|
632 | 0 | if (distribution == NULL) |
633 | 0 | return_integer(YR_UNDEFINED); |
634 | |
|
635 | 0 | int64_t count = (int64_t) distribution[byte]; |
636 | 0 | yr_free(distribution); |
637 | 0 | return_integer(count); |
638 | 0 | } |
639 | | |
640 | | define_function(count_global) |
641 | 0 | { |
642 | 0 | int64_t byte = integer_argument(1); |
643 | |
|
644 | 0 | if (byte < 0 || byte > 255) |
645 | 0 | return_integer(YR_UNDEFINED); |
646 | |
|
647 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
648 | |
|
649 | 0 | uint32_t* distribution = get_distribution_global(context); |
650 | |
|
651 | 0 | if (distribution == NULL) |
652 | 0 | return_integer(YR_UNDEFINED); |
653 | |
|
654 | 0 | int64_t count = (int64_t) distribution[byte]; |
655 | 0 | yr_free(distribution); |
656 | 0 | return_integer(count); |
657 | 0 | } |
658 | | |
659 | | define_function(percentage_range) |
660 | 0 | { |
661 | 0 | int64_t byte = integer_argument(1); |
662 | 0 | int64_t offset = integer_argument(2); |
663 | 0 | int64_t length = integer_argument(3); |
664 | |
|
665 | 0 | if (byte < 0 || byte > 255) |
666 | 0 | return_float(YR_UNDEFINED); |
667 | |
|
668 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
669 | |
|
670 | 0 | uint32_t* distribution = get_distribution(offset, length, context); |
671 | |
|
672 | 0 | if (distribution == NULL) |
673 | 0 | return_float(YR_UNDEFINED); |
674 | |
|
675 | 0 | int64_t count = (int64_t) distribution[byte]; |
676 | 0 | int64_t total_count = 0; |
677 | 0 | int64_t i; |
678 | |
|
679 | 0 | for (i = 0; i < 256; i++) total_count += distribution[i]; |
680 | |
|
681 | 0 | yr_free(distribution); |
682 | 0 | return_float(((float) count) / ((float) total_count)); |
683 | 0 | } |
684 | | |
685 | | define_function(percentage_global) |
686 | 0 | { |
687 | 0 | int64_t byte = integer_argument(1); |
688 | |
|
689 | 0 | if (byte < 0 || byte > 255) |
690 | 0 | return_float(YR_UNDEFINED); |
691 | |
|
692 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
693 | |
|
694 | 0 | uint32_t* distribution = get_distribution_global(context); |
695 | |
|
696 | 0 | if (distribution == NULL) |
697 | 0 | return_float(YR_UNDEFINED); |
698 | |
|
699 | 0 | int64_t count = (int64_t) distribution[byte]; |
700 | 0 | int64_t total_count = 0; |
701 | 0 | int64_t i; |
702 | |
|
703 | 0 | for (i = 0; i < 256; i++) total_count += distribution[i]; |
704 | |
|
705 | 0 | yr_free(distribution); |
706 | 0 | return_float(((float) count) / ((float) total_count)); |
707 | 0 | } |
708 | | |
709 | | define_function(mode_range) |
710 | 0 | { |
711 | 0 | int64_t offset = integer_argument(1); |
712 | 0 | int64_t length = integer_argument(2); |
713 | |
|
714 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
715 | |
|
716 | 0 | uint32_t* distribution = get_distribution(offset, length, context); |
717 | |
|
718 | 0 | if (distribution == NULL) |
719 | 0 | return_integer(YR_UNDEFINED); |
720 | |
|
721 | 0 | int64_t most_common = 0; |
722 | 0 | size_t i; |
723 | |
|
724 | 0 | for (i = 0; i < 256; i++) |
725 | 0 | { |
726 | 0 | if (distribution[i] > distribution[most_common]) |
727 | 0 | most_common = (int64_t) i; |
728 | 0 | } |
729 | |
|
730 | 0 | yr_free(distribution); |
731 | 0 | return_integer(most_common); |
732 | 0 | } |
733 | | |
734 | | define_function(mode_global) |
735 | 0 | { |
736 | 0 | YR_SCAN_CONTEXT* context = yr_scan_context(); |
737 | |
|
738 | 0 | uint32_t* distribution = get_distribution_global(context); |
739 | |
|
740 | 0 | if (distribution == NULL) |
741 | 0 | return_integer(YR_UNDEFINED); |
742 | |
|
743 | 0 | int64_t most_common = 0; |
744 | 0 | size_t i; |
745 | |
|
746 | 0 | for (i = 0; i < 256; i++) |
747 | 0 | { |
748 | 0 | if (distribution[i] > distribution[most_common]) |
749 | 0 | most_common = (int64_t) i; |
750 | 0 | } |
751 | |
|
752 | 0 | yr_free(distribution); |
753 | 0 | return_integer(most_common); |
754 | 0 | } |
755 | | |
756 | | define_function(to_string) |
757 | 0 | { |
758 | 0 | int64_t i = integer_argument(1); |
759 | 0 | char str[INT64_MAX_STRING]; |
760 | 0 | snprintf(str, INT64_MAX_STRING, "%" PRId64, i); |
761 | 0 | return_string(&str); |
762 | 0 | } |
763 | | |
764 | | define_function(to_string_base) |
765 | 0 | { |
766 | 0 | int64_t i = integer_argument(1); |
767 | 0 | int64_t base = integer_argument(2); |
768 | 0 | char str[INT64_MAX_STRING]; |
769 | 0 | char* fmt; |
770 | 0 | switch (base) |
771 | 0 | { |
772 | 0 | case 10: |
773 | 0 | fmt = "%" PRId64; |
774 | 0 | break; |
775 | 0 | case 8: |
776 | 0 | fmt = "%" PRIo64; |
777 | 0 | break; |
778 | 0 | case 16: |
779 | 0 | fmt = "%" PRIx64; |
780 | 0 | break; |
781 | 0 | default: |
782 | 0 | return_string(YR_UNDEFINED); |
783 | 0 | } |
784 | 0 | snprintf(str, INT64_MAX_STRING, fmt, i); |
785 | 0 | return_string(&str); |
786 | 0 | } |
787 | | |
788 | 0 | begin_declarations |
789 | 0 | declare_float("MEAN_BYTES"); |
790 | 0 | declare_function("in_range", "fff", "i", in_range); |
791 | 0 | declare_function("deviation", "iif", "f", data_deviation); |
792 | 0 | declare_function("deviation", "sf", "f", string_deviation); |
793 | 0 | declare_function("mean", "ii", "f", data_mean); |
794 | 0 | declare_function("mean", "s", "f", string_mean); |
795 | 0 | declare_function("serial_correlation", "ii", "f", data_serial_correlation); |
796 | 0 | declare_function("serial_correlation", "s", "f", string_serial_correlation); |
797 | 0 | declare_function("monte_carlo_pi", "ii", "f", data_monte_carlo_pi); |
798 | 0 | declare_function("monte_carlo_pi", "s", "f", string_monte_carlo_pi); |
799 | 0 | declare_function("entropy", "ii", "f", data_entropy); |
800 | 0 | declare_function("entropy", "s", "f", string_entropy); |
801 | 0 | declare_function("min", "ii", "i", min); |
802 | 0 | declare_function("max", "ii", "i", max); |
803 | 0 | declare_function("to_number", "b", "i", to_number); |
804 | 0 | declare_function("abs", "i", "i", yr_math_abs); |
805 | 0 | declare_function("count", "iii", "i", count_range); |
806 | 0 | declare_function("count", "i", "i", count_global); |
807 | 0 | declare_function("percentage", "iii", "f", percentage_range); |
808 | 0 | declare_function("percentage", "i", "f", percentage_global); |
809 | 0 | declare_function("mode", "ii", "i", mode_range); |
810 | 0 | declare_function("mode", "", "i", mode_global); |
811 | 0 | declare_function("to_string", "i", "s", to_string); |
812 | 0 | declare_function("to_string", "ii", "s", to_string_base); |
813 | 0 | end_declarations |
814 | | |
815 | | int module_initialize(YR_MODULE* module) |
816 | 2 | { |
817 | 2 | return ERROR_SUCCESS; |
818 | 2 | } |
819 | | |
820 | | int module_finalize(YR_MODULE* module) |
821 | 0 | { |
822 | 0 | return ERROR_SUCCESS; |
823 | 0 | } |
824 | | |
825 | | int module_load( |
826 | | YR_SCAN_CONTEXT* context, |
827 | | YR_OBJECT* module_object, |
828 | | void* module_data, |
829 | | size_t module_data_size) |
830 | 0 | { |
831 | 0 | yr_set_float(127.5, module_object, "MEAN_BYTES"); |
832 | 0 | return ERROR_SUCCESS; |
833 | 0 | } |
834 | | |
835 | | int module_unload(YR_OBJECT* module_object) |
836 | 0 | { |
837 | 0 | return ERROR_SUCCESS; |
838 | 0 | } |