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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
/*
Copyright (c) 2016. 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.
*/
#include "util.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <yara.h>
#include <yara/globals.h>
#include <yara/proc.h>
//
// Global variables used in test cases.
//
// Message for the latest compiler error
char compile_error[1024];
// Number of warnings produced by the last call to compile_rule
int warnings;
uint64_t yr_test_mem_block_size = 0;
uint64_t yr_test_mem_block_size_overlap = 0;
uint64_t yr_test_count_get_block = 0;
static uint64_t _yr_test_single_block_file_size(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_TEST_ITERATOR_CTX* context = (YR_TEST_ITERATOR_CTX*) iterator->context;
uint64_t file_size = context->current_block.size;
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} = %" PRIu64
" // test iterator; single memory block, blocking\n",
__FUNCTION__,
file_size);
return file_size;
}
static YR_MEMORY_BLOCK* _yr_test_single_block_get_first_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_TEST_ITERATOR_CTX* context = (YR_TEST_ITERATOR_CTX*) iterator->context;
context->current_block.size = context->buffer_size;
YR_MEMORY_BLOCK* result = &context->current_block;
yr_test_count_get_block++;
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} = %p // test iterator; single memory block, blocking\n",
__FUNCTION__,
result);
return result;
}
static YR_MEMORY_BLOCK* _yr_test_single_block_get_next_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_MEMORY_BLOCK* result = NULL;
yr_test_count_get_block++;
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} = %p // test iterator; single memory block, blocking\n",
__FUNCTION__,
result);
return result;
}
static const uint8_t* _yr_test_single_block_fetch_block_data(
YR_MEMORY_BLOCK* block)
{
YR_TEST_ITERATOR_CTX* context = (YR_TEST_ITERATOR_CTX*) block->context;
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} = %p // test iterator; single memory block, blocking\n",
__FUNCTION__,
context->buffer);
return context->buffer;
}
static uint64_t _yr_test_multi_block_file_size(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_TEST_ITERATOR_CTX* context = (YR_TEST_ITERATOR_CTX*) iterator->context;
uint64_t file_size = context->buffer_size;
YR_DEBUG_FPRINTF(
2, stderr, "- %s() {} = %" PRIu64 "\n", __FUNCTION__, file_size);
return file_size;
}
static YR_MEMORY_BLOCK* _yr_test_multi_block_get_next_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_TEST_ITERATOR_CTX* context = (YR_TEST_ITERATOR_CTX*) iterator->context;
YR_MEMORY_BLOCK* result;
uint64_t overlap;
yr_test_count_get_block++;
iterator->last_error = ERROR_SUCCESS;
if (context->block_not_ready_frequency > 0 &&
context->block_not_ready_frequency ==
context->blocks_returned_successfully)
{
overlap = 0;
context->blocks_returned_successfully = 0;
iterator->last_error = ERROR_BLOCK_NOT_READY;
result = NULL;
}
else if (0 == context->current_block.size)
{
overlap = 0;
context->blocks_returned_successfully++;
context->current_block.size = (context->buffer_size <
yr_test_mem_block_size)
? context->buffer_size
: yr_test_mem_block_size;
result = &context->current_block;
}
else
{
overlap = yr_test_mem_block_size_overlap;
context->blocks_returned_successfully++;
context->current_block.base += (0 == context->current_block.base) ? 0
: overlap;
context->current_block.base += (context->current_block.base <
context->buffer_size)
? yr_test_mem_block_size
: 0;
result = (context->current_block.base < context->buffer_size)
? &context->current_block
: NULL;
context->current_block.size =
((context->buffer_size - context->current_block.base) <
yr_test_mem_block_size)
? context->buffer_size - context->current_block.base + overlap
: yr_test_mem_block_size + overlap;
context->current_block.base -= overlap;
}
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} = %p // "
".base=(0x%" PRIx64 " or %" PRId64 ") of "
"(0x%" PRIx64 " or %" PRIu64 ") .size=%" PRIu64
", both including %" PRId64 " block overlap%s\n",
__FUNCTION__,
result,
context->current_block.base,
context->current_block.base,
context->buffer_size,
context->buffer_size,
context->current_block.size,
overlap,
overlap ? "" : " (note: 1st block overlap always 0)");
return result;
}
static YR_MEMORY_BLOCK* _yr_test_multi_block_get_first_block(
YR_MEMORY_BLOCK_ITERATOR* iterator)
{
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} // wrapping _yr_test_multi_block_get_next_block()\n",
__FUNCTION__);
YR_TEST_ITERATOR_CTX* context = (YR_TEST_ITERATOR_CTX*) iterator->context;
context->current_block.base = 0;
context->current_block.size = 0;
return _yr_test_multi_block_get_next_block(iterator);
}
static const uint8_t* _yr_test_multi_block_fetch_block_data(
YR_MEMORY_BLOCK* block)
{
YR_TEST_ITERATOR_CTX* context = (YR_TEST_ITERATOR_CTX*) block->context;
#if YR_DEBUG_VERBOSITY > 0
uint64_t overlap = context->current_block.base > 0
? yr_test_mem_block_size_overlap
: 0;
#endif
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s() {} = %p = %p + 0x%" PRIx64 " base including %" PRId64
" overlap%s\n",
__FUNCTION__,
context->buffer + context->current_block.base - overlap,
context->buffer,
context->current_block.base,
overlap,
overlap ? "" : " (note: 1st block overlap always 0)");
return context->buffer + context->current_block.base;
}
void init_test_iterator(
YR_MEMORY_BLOCK_ITERATOR* iterator,
YR_TEST_ITERATOR_CTX* ctx,
const uint8_t* buffer,
size_t buffer_size)
{
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s(buffer_size=%'lu) {} // yr_test_mem_block_size=%" PRId64 "\n",
__FUNCTION__,
buffer_size,
yr_test_mem_block_size);
ctx->buffer = buffer;
ctx->buffer_size = buffer_size;
ctx->current_block.base = 0;
ctx->current_block.size = 0;
ctx->current_block.context = ctx;
ctx->block_not_ready_frequency = 0;
ctx->blocks_returned_successfully = 0;
iterator->context = ctx;
iterator->last_error = ERROR_SUCCESS;
if (yr_test_mem_block_size)
{
// Come here for callbacks which dish up the buffer as multi blocks
ctx->current_block.fetch_data = _yr_test_multi_block_fetch_block_data;
iterator->first = _yr_test_multi_block_get_first_block;
iterator->next = _yr_test_multi_block_get_next_block;
iterator->file_size = _yr_test_multi_block_file_size;
}
else
{
// Come here for callbacks which dish up the buffer as a single block
ctx->current_block.fetch_data = _yr_test_single_block_fetch_block_data;
iterator->first = _yr_test_single_block_get_first_block;
iterator->next = _yr_test_single_block_get_next_block;
iterator->file_size = _yr_test_single_block_file_size;
}
}
char* top_srcdir;
void init_top_srcdir(void)
{
top_srcdir = getenv("TOP_SRCDIR");
if (!top_srcdir)
top_srcdir = ".";
}
char* prefix_top_srcdir(char* dir)
{
static char buf[4096];
snprintf(buf, sizeof(buf), "%s/%s", top_srcdir, dir);
return buf;
}
//
// A YR_CALLBACK_FUNC that counts the number of matching and non-matching rules
// during a scan. user_data must point to a COUNTERS structure.
//
int count(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data)
{
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s(message=%d AKA %s) {}\n",
__FUNCTION__,
message,
yr_debug_callback_message_as_string(message));
switch (message)
{
case CALLBACK_MSG_TOO_MANY_MATCHES:
(*(struct COUNTERS*) user_data).rules_warning++;
break;
case CALLBACK_MSG_RULE_MATCHING:
(*(struct COUNTERS*) user_data).rules_matching++;
break;
case CALLBACK_MSG_RULE_NOT_MATCHING:
(*(struct COUNTERS*) user_data).rules_not_matching++;
}
return CALLBACK_CONTINUE;
}
int count_non_matches(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data)
{
if (message == CALLBACK_MSG_RULE_NOT_MATCHING)
{
(*(int*) user_data)++;
}
return CALLBACK_CONTINUE;
}
//
// A YR_CALLBACK_FUNC that does nothing.
//
int do_nothing(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data)
{
return CALLBACK_CONTINUE;
}
static void _compiler_callback(
int error_level,
const char* file_name,
int line_number,
const YR_RULE* rule,
const char* message,
void* user_data)
{
if (error_level == YARA_ERROR_LEVEL_WARNING)
(*((int*) user_data))++;
snprintf(
compile_error,
sizeof(compile_error),
"line %d: %s",
line_number,
message);
}
int compile_rule(char* string, YR_RULES** rules)
{
bool strict_escape_flag = false;
return compile_rule_ex(string, rules, strict_escape_flag);
}
int compile_rule_ex(char* string, YR_RULES** rules, bool strict_escape_flag)
{
YR_COMPILER* compiler = NULL;
int result = ERROR_SUCCESS;
compile_error[0] = '\0';
warnings = 0;
if (yr_compiler_create(&compiler) != ERROR_SUCCESS)
{
perror("yr_compiler_create");
goto _exit;
}
compiler->strict_escape = strict_escape_flag;
yr_compiler_set_callback(compiler, _compiler_callback, &warnings);
// Define some variables that will be used in test cases.
yr_compiler_define_integer_variable(compiler, "var_zero", 0);
yr_compiler_define_integer_variable(compiler, "var_one", 1);
yr_compiler_define_boolean_variable(compiler, "var_true", 1);
yr_compiler_define_boolean_variable(compiler, "var_false", 0);
if (yr_compiler_add_string(compiler, string, NULL) != 0)
{
result = compiler->last_error;
goto _exit;
}
result = yr_compiler_get_rules(compiler, rules);
_exit:
yr_compiler_destroy(compiler);
return result;
}
int _scan_callback(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data)
{
YR_DEBUG_FPRINTF(
2,
stderr,
"- %s(message=%d AKA %s) {}\n",
__FUNCTION__,
message,
yr_debug_callback_message_as_string(message));
SCAN_CALLBACK_CTX* ctx = (SCAN_CALLBACK_CTX*) user_data;
YR_MODULE_IMPORT* mi;
switch (message)
{
case CALLBACK_MSG_RULE_MATCHING:
ctx->matches++;
break;
case CALLBACK_MSG_IMPORT_MODULE:
mi = (YR_MODULE_IMPORT*) message_data;
mi->module_data = ctx->module_data;
mi->module_data_size = ctx->module_data_size;
break;
}
return CALLBACK_CONTINUE;
}
int matches_blob_uses_default_iterator = 1;
int matches_blob(
char* rule,
uint8_t* blob,
size_t blob_size,
uint8_t* module_data,
size_t module_data_size)
{
YR_DEBUG_FPRINTF(
2, stderr, "+ %s(blob_size=%zu) {\n", __FUNCTION__, blob_size);
YR_RULES* rules;
if (blob == NULL)
{
blob = (uint8_t*) "dummy";
blob_size = 5;
}
if (compile_rule(rule, &rules) != ERROR_SUCCESS)
{
fprintf(
stderr, "failed to compile rule << %s >>: %s\n", rule, compile_error);
exit(EXIT_FAILURE);
}
SCAN_CALLBACK_CTX ctx = {
.matches = 0,
.module_data = module_data,
.module_data_size = module_data_size,
};
int flags = SCAN_FLAGS_NO_TRYCATCH;
YR_CALLBACK_FUNC callback = _scan_callback;
void* user_data = &ctx;
int timeout = 0;
int scan_result;
if (matches_blob_uses_default_iterator)
{
scan_result = yr_rules_scan_mem(
rules, blob, blob_size, flags, callback, user_data, timeout);
}
else
{
YR_SCANNER* scanner;
assert_true_expr(ERROR_SUCCESS == yr_scanner_create(rules, &scanner));
yr_scanner_set_callback(scanner, callback, user_data);
yr_scanner_set_timeout(scanner, timeout);
yr_scanner_set_flags(scanner, flags);
YR_MEMORY_BLOCK_ITERATOR iterator;
YR_TEST_ITERATOR_CTX iterator_ctx;
init_test_iterator(&iterator, &iterator_ctx, blob, blob_size);
scan_result = yr_scanner_scan_mem_blocks(scanner, &iterator);
yr_scanner_destroy(scanner);
}
if (scan_result != ERROR_SUCCESS)
{
fprintf(
stderr,
"failed to scan using rule << %s >>: error: %d\n",
rule,
scan_result);
exit(EXIT_FAILURE);
}
yr_rules_destroy(rules);
YR_DEBUG_FPRINTF(
2, stderr, "} = %u AKA ctx.matches // %s()\n", ctx.matches, __FUNCTION__);
return ctx.matches;
}
int matches_string(char* rule, char* string)
{
size_t len = 0;
if (string != NULL)
len = strlen(string);
return matches_blob(rule, (uint8_t*) string, len, NULL, 0);
}
typedef struct
{
char* expected;
int found;
} find_string_t;
static int capture_matches(
YR_SCAN_CONTEXT* context,
int message,
void* message_data,
void* user_data)
{
if (message == CALLBACK_MSG_RULE_MATCHING)
{
find_string_t* f = (find_string_t*) user_data;
YR_RULE* rule = (YR_RULE*) message_data;
YR_STRING* string;
yr_rule_strings_foreach(rule, string)
{
YR_MATCH* match;
yr_string_matches_foreach(context, string, match)
{
if (strlen(f->expected) == match->data_length &&
(match->data_length == 0 ||
strncmp(f->expected, (char*) (match->data), match->data_length) ==
0))
{
f->found++;
}
}
}
}
return CALLBACK_CONTINUE;
}
int capture_string(char* rule, char* string, char* expected_string)
{
YR_RULES* rules;
if (compile_rule(rule, &rules) != ERROR_SUCCESS)
{
fprintf(
stderr, "failed to compile rule << %s >>: %s\n", rule, compile_error);
exit(EXIT_FAILURE);
}
find_string_t f;
f.found = 0;
f.expected = expected_string;
int scan_result = yr_rules_scan_mem(
rules, (uint8_t*) string, strlen(string), 0, capture_matches, &f, 0);
if (scan_result != ERROR_SUCCESS)
{
fprintf(
stderr,
"failed to scan using rule << %s >>: error %d\n",
rule,
scan_result);
exit(EXIT_FAILURE);
}
yr_rules_destroy(rules);
return f.found;
}
int read_file(char* filename, char** buf)
{
int fd;
#if defined(_WIN32) || defined(__CYGWIN__)
if ((fd = open(filename, O_RDONLY | O_BINARY)) < 0)
#else
if ((fd = open(filename, O_RDONLY)) < 0)
#endif
return -1;
size_t sz = lseek(fd, 0, SEEK_END);
int rc = -1;
if (sz == -1)
goto _exit;
if (lseek(fd, 0, SEEK_SET) != 0)
goto _exit;
if ((*buf = malloc(sz)) == NULL)
goto _exit;
if ((rc = read(fd, *buf, sz)) != sz)
{
rc = -1;
free(*buf);
}
_exit:
close(fd);
return rc;
}
int _assert_atoms(RE_AST* re_ast, int expected_atom_count, atom* expected_atoms)
{
YR_ATOMS_CONFIG c;
YR_ATOM_LIST_ITEM* atoms;
YR_ATOM_LIST_ITEM* atom;
YR_ATOM_LIST_ITEM* next_atom;
int min_atom_quality;
int exit_code;
YR_MODIFIER modifier;
modifier.flags = 0;
c.get_atom_quality = yr_atoms_heuristic_quality;
yr_atoms_extract_from_re(&c, re_ast, modifier, &atoms, &min_atom_quality);
atom = atoms;
exit_code = EXIT_SUCCESS;
while (atom != NULL)
{
if (expected_atom_count == 0)
{
exit_code = EXIT_FAILURE;
break;
}
if (atom->atom.length != expected_atoms->length ||
memcmp(atom->atom.bytes, expected_atoms->data, atom->atom.length) != 0)
{
exit_code = EXIT_FAILURE;
break;
}
expected_atoms++;
expected_atom_count--;
atom = atom->next;
}
atom = atoms;
while (atom != NULL)
{
next_atom = atom->next;
yr_free(atom);
atom = next_atom;
}
return exit_code;
}
void assert_re_atoms(char* re, int expected_atom_count, atom* expected_atoms)
{
RE_AST* re_ast;
RE_ERROR re_error;
int exit_code;
yr_re_parse(re, &re_ast, &re_error, RE_PARSER_FLAG_NONE);
exit_code = _assert_atoms(re_ast, expected_atom_count, expected_atoms);
if (re_ast != NULL)
yr_re_ast_destroy(re_ast);
if (exit_code != EXIT_SUCCESS)
exit(exit_code);
}
void assert_hex_atoms(char* hex, int expected_atom_count, atom* expected_atoms)
{
RE_AST* re_ast;
RE_ERROR re_error;
int exit_code;
yr_re_parse_hex(hex, &re_ast, &re_error);
exit_code = _assert_atoms(re_ast, expected_atom_count, expected_atoms);
if (re_ast != NULL)
yr_re_ast_destroy(re_ast);
if (exit_code != EXIT_SUCCESS)
exit(exit_code);
}
|