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 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
|
/*
Copyright (c) 2007-2013. 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.
*/
#if !defined(_WIN32) && !defined(__CYGWIN__)
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <inttypes.h>
#else
#include <windows.h>
#define PRIx64 "llx"
#define PRId64 "lld"
#endif
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <yara.h>
#include "args.h"
#include "threading.h"
#include "config.h"
#define ERROR_COULD_NOT_CREATE_THREAD 100
#ifndef MAX_PATH
#define MAX_PATH 256
#endif
#ifndef min
#define min(x, y) ((x < y) ? (x) : (y))
#endif
#ifdef _MSC_VER
#define snprintf _snprintf
#define strdup _strdup
#endif
#define MAX_QUEUED_FILES 64
typedef struct _MODULE_DATA
{
const char* module_name;
YR_MAPPED_FILE mapped_file;
struct _MODULE_DATA* next;
} MODULE_DATA;
typedef struct _THREAD_ARGS
{
YR_RULES* rules;
time_t start_time;
} THREAD_ARGS;
typedef struct _QUEUED_FILE {
char* path;
} QUEUED_FILE;
#define MAX_ARGS_TAG 32
#define MAX_ARGS_IDENTIFIER 32
#define MAX_ARGS_EXT_VAR 32
#define MAX_ARGS_MODULE_DATA 32
char* tags[MAX_ARGS_TAG + 1];
char* identifiers[MAX_ARGS_IDENTIFIER + 1];
char* ext_vars[MAX_ARGS_EXT_VAR + 1];
char* modules_data[MAX_ARGS_EXT_VAR + 1];
int recursive_search = FALSE;
int show_module_data = FALSE;
int show_tags = FALSE;
int show_specified_tags = FALSE;
int show_specified_rules = FALSE;
int show_strings = FALSE;
int show_meta = FALSE;
int show_namespace = FALSE;
int show_version = FALSE;
int show_help = FALSE;
int ignore_warnings = FALSE;
int fast_scan = FALSE;
int negate = FALSE;
int count = 0;
int limit = 0;
int timeout = 1000000;
int stack_size = DEFAULT_STACK_SIZE;
int threads = 8;
#define USAGE_STRING \
"Usage: yara [OPTION]... RULES_FILE FILE | DIR | PID"
args_option_t options[] =
{
OPT_STRING_MULTI('t', "tag", &tags, MAX_ARGS_TAG,
"print only rules tagged as TAG", "TAG"),
OPT_STRING_MULTI('i', "identifier", &identifiers, MAX_ARGS_IDENTIFIER,
"print only rules named IDENTIFIER", "IDENTIFIER"),
OPT_BOOLEAN('n', "negate", &negate,
"print only not satisfied rules (negate)", NULL),
OPT_BOOLEAN('D', "print-module-data", &show_module_data,
"print module data"),
OPT_BOOLEAN('g', "print-tags", &show_tags,
"print tags"),
OPT_BOOLEAN('m', "print-meta", &show_meta,
"print metadata"),
OPT_BOOLEAN('s', "print-strings", &show_strings,
"print matching strings"),
OPT_BOOLEAN('e', "print-namespace", &show_namespace,
"print rules' namespace"),
OPT_INTEGER('p', "threads", &threads,
"use the specified NUMBER of threads to scan a directory", "NUMBER"),
OPT_INTEGER('l', "max-rules", &limit,
"abort scanning after matching a NUMBER of rules", "NUMBER"),
OPT_STRING_MULTI('d', NULL, &ext_vars, MAX_ARGS_EXT_VAR,
"define external variable", "VAR=VALUE"),
OPT_STRING_MULTI('x', NULL, &modules_data, MAX_ARGS_MODULE_DATA,
"pass FILE's content as extra data to MODULE", "MODULE=FILE"),
OPT_INTEGER('a', "timeout", &timeout,
"abort scanning after the given number of SECONDS", "SECONDS"),
OPT_INTEGER('k', "stack-size", &stack_size,
"set maximum stack size (default=16384)", "SLOTS"),
OPT_BOOLEAN('r', "recursive", &recursive_search,
"recursively search directories"),
OPT_BOOLEAN('f', "fast-scan", &fast_scan,
"fast matching mode"),
OPT_BOOLEAN('w', "no-warnings", &ignore_warnings,
"disable warnings"),
OPT_BOOLEAN('v', "version", &show_version,
"show version information"),
OPT_BOOLEAN('h', "help", &show_help,
"show this help and exit"),
OPT_END()
};
// file_queue is size-limited queue stored as a circular array, files are
// removed from queue_head position and new files are added at queue_tail
// position. The array has room for one extra element to avoid queue_head
// being equal to queue_tail in a full queue. The only situation where
// queue_head == queue_tail is when queue is empty.
QUEUED_FILE file_queue[MAX_QUEUED_FILES + 1];
int queue_head;
int queue_tail;
SEMAPHORE used_slots;
SEMAPHORE unused_slots;
MUTEX queue_mutex;
MUTEX output_mutex;
MODULE_DATA* modules_data_list = NULL;
int file_queue_init()
{
int result;
queue_tail = 0;
queue_head = 0;
result = mutex_init(&queue_mutex);
if (result != 0)
return result;
result = semaphore_init(&used_slots, 0);
if (result != 0)
return result;
return semaphore_init(&unused_slots, MAX_QUEUED_FILES);
}
void file_queue_destroy()
{
mutex_destroy(&queue_mutex);
semaphore_destroy(&unused_slots);
semaphore_destroy(&used_slots);
}
void file_queue_finish()
{
int i;
for (i = 0; i < MAX_THREADS; i++)
semaphore_release(&used_slots);
}
void file_queue_put(
const char* file_path)
{
semaphore_wait(&unused_slots);
mutex_lock(&queue_mutex);
file_queue[queue_tail].path = strdup(file_path);
queue_tail = (queue_tail + 1) % (MAX_QUEUED_FILES + 1);
mutex_unlock(&queue_mutex);
semaphore_release(&used_slots);
}
char* file_queue_get()
{
char* result;
semaphore_wait(&used_slots);
mutex_lock(&queue_mutex);
if (queue_head == queue_tail) // queue is empty
{
result = NULL;
}
else
{
result = file_queue[queue_head].path;
queue_head = (queue_head + 1) % (MAX_QUEUED_FILES + 1);
}
mutex_unlock(&queue_mutex);
semaphore_release(&unused_slots);
return result;
}
#if defined(_WIN32) || defined(__CYGWIN__)
int is_directory(
const char* path)
{
DWORD attributes = GetFileAttributes(path);
if (attributes != INVALID_FILE_ATTRIBUTES &&
attributes & FILE_ATTRIBUTE_DIRECTORY)
return TRUE;
else
return FALSE;
}
void scan_dir(
const char* dir,
int recursive,
time_t start_time,
YR_RULES* rules,
YR_CALLBACK_FUNC callback)
{
static char path_and_mask[MAX_PATH];
snprintf(path_and_mask, sizeof(path_and_mask), "%s\\*", dir);
WIN32_FIND_DATA FindFileData;
HANDLE hFind = FindFirstFile(path_and_mask, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
char full_path[MAX_PATH];
snprintf(full_path, sizeof(full_path), "%s\\%s",
dir, FindFileData.cFileName);
if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
file_queue_put(full_path);
}
else if (recursive &&
strcmp(FindFileData.cFileName, ".") != 0 &&
strcmp(FindFileData.cFileName, "..") != 0)
{
scan_dir(full_path, recursive, start_time, rules, callback);
}
} while (FindNextFile(hFind, &FindFileData));
FindClose(hFind);
}
}
#else
int is_directory(
const char* path)
{
struct stat st;
if (stat(path,&st) == 0)
return S_ISDIR(st.st_mode);
return 0;
}
void scan_dir(
const char* dir,
int recursive,
time_t start_time,
YR_RULES* rules,
YR_CALLBACK_FUNC callback)
{
DIR* dp = opendir(dir);
if (dp)
{
struct dirent* de = readdir(dp);
while (de && difftime(time(NULL), start_time) < timeout)
{
char full_path[MAX_PATH];
struct stat st;
snprintf(full_path, sizeof(full_path), "%s/%s", dir, de->d_name);
int err = lstat(full_path, &st);
if (err == 0)
{
if(S_ISREG(st.st_mode))
{
file_queue_put(full_path);
}
else if(recursive &&
S_ISDIR(st.st_mode) &&
!S_ISLNK(st.st_mode) &&
strcmp(de->d_name, ".") != 0 &&
strcmp(de->d_name, "..") != 0)
{
scan_dir(full_path, recursive, start_time, rules, callback);
}
}
de = readdir(dp);
}
closedir(dp);
}
}
#endif
void print_string(
uint8_t* data,
int length)
{
char* str = (char*) (data);
for (int i = 0; i < length; i++)
{
if (str[i] >= 32 && str[i] <= 126)
printf("%c", str[i]);
else
printf("\\x%02X", (uint8_t) str[i]);
}
printf("\n");
}
static char cescapes[] =
{
0 , 0 , 0 , 0 , 0 , 0 , 0 , 'a',
'b', 't', 'n', 'v', 'f', 'r', 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
};
void print_escaped(
uint8_t* data,
size_t length)
{
size_t i;
for (i = 0; i < length; i++)
{
switch (data[i])
{
case '\"':
case '\'':
case '\\':
printf("\\%c", data[i]);
break;
default:
if (data[i] >= 127)
printf("\\%03o", data[i]);
else if (data[i] >= 32)
putchar(data[i]);
else if (cescapes[data[i]] != 0)
printf("\\%c", cescapes[data[i]]);
else
printf("\\%03o", data[i]);
}
}
}
void print_hex_string(
uint8_t* data,
int length)
{
for (int i = 0; i < min(32, length); i++)
printf("%02X ", (uint8_t) data[i]);
if (length > 32)
printf("...");
printf("\n");
}
void print_scanner_error(
int error)
{
switch (error)
{
case ERROR_SUCCESS:
break;
case ERROR_COULD_NOT_ATTACH_TO_PROCESS:
fprintf(stderr, "can not attach to process (try running as root)\n");
break;
case ERROR_INSUFICIENT_MEMORY:
fprintf(stderr, "not enough memory\n");
break;
case ERROR_SCAN_TIMEOUT:
fprintf(stderr, "scanning timed out\n");
break;
case ERROR_COULD_NOT_OPEN_FILE:
fprintf(stderr, "could not open file\n");
break;
case ERROR_UNSUPPORTED_FILE_VERSION:
fprintf(stderr, "rules were compiled with a newer version of YARA.\n");
break;
case ERROR_CORRUPT_FILE:
fprintf(stderr, "corrupt compiled rules file.\n");
break;
case ERROR_EXEC_STACK_OVERFLOW:
fprintf(stderr, "stack overflow while evaluating condition "
"(see --stack-size argument).\n");
break;
case ERROR_INVALID_EXTERNAL_VARIABLE_TYPE:
fprintf(stderr, "invalid type for external variable.\n");
break;
default:
fprintf(stderr, "internal error: %d\n", error);
break;
}
}
void print_compiler_error(
int error_level,
const char* file_name,
int line_number,
const char* message,
void* user_data)
{
if (error_level == YARA_ERROR_LEVEL_ERROR)
{
fprintf(stderr, "%s(%d): error: %s\n", file_name, line_number, message);
}
else
{
if (!ignore_warnings)
fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
}
}
int handle_message(
int message,
YR_RULE* rule,
void* data)
{
const char* tag;
int show = TRUE;
if (tags[0] != NULL)
{
// The user specified one or more -t <tag> arguments, let's show this rule
// only if it's tagged with some of the specified tags.
show = FALSE;
for (int i = 0; !show && tags[i] != NULL; i++)
{
yr_rule_tags_foreach(rule, tag)
{
if (strcmp(tag, tags[i]) == 0)
{
show = TRUE;
break;
}
}
}
}
if (identifiers[0] != NULL)
{
// The user specified one or more -i <identifier> arguments, let's show
// this rule only if it's identifier is among of the provided ones.
show = FALSE;
for (int i = 0; !show && identifiers[i] != NULL; i++)
{
if (strcmp(identifiers[i], rule->identifier) == 0)
{
show = TRUE;
break;
}
}
}
int is_matching = (message == CALLBACK_MSG_RULE_MATCHING);
show = show && ((!negate && is_matching) || (negate && !is_matching));
if (show)
{
mutex_lock(&output_mutex);
if (show_namespace)
printf("%s:", rule->ns->name);
printf("%s ", rule->identifier);
if (show_tags)
{
printf("[");
yr_rule_tags_foreach(rule, tag)
{
// print a comma except for the first tag
if (tag != rule->tags)
printf(",");
printf("%s", tag);
}
printf("] ");
}
// Show meta-data.
if (show_meta)
{
YR_META* meta;
printf("[");
yr_rule_metas_foreach(rule, meta)
{
if (meta != rule->metas)
printf(",");
if (meta->type == META_TYPE_INTEGER)
{
printf("%s=%" PRId64, meta->identifier, meta->integer);
}
else if (meta->type == META_TYPE_BOOLEAN)
{
printf("%s=%s", meta->identifier, meta->integer ? "true" : "false");
}
else
{
printf("%s=\"", meta->identifier);
print_escaped((uint8_t*) (meta->string), strlen(meta->string));
putchar('"');
}
}
printf("] ");
}
printf("%s\n", (char*) data);
// Show matched strings.
if (show_strings)
{
YR_STRING* string;
yr_rule_strings_foreach(rule, string)
{
YR_MATCH* match;
yr_string_matches_foreach(string, match)
{
printf("0x%" PRIx64 ":%s: ",
match->base + match->offset,
string->identifier);
if (STRING_IS_HEX(string))
print_hex_string(match->data, match->data_length);
else
print_string(match->data, match->data_length);
}
}
}
mutex_unlock(&output_mutex);
}
if (is_matching)
count++;
if (limit != 0 && count >= limit)
return CALLBACK_ABORT;
return CALLBACK_CONTINUE;
}
int callback(
int message,
void* message_data,
void* user_data)
{
YR_MODULE_IMPORT* mi;
YR_OBJECT* object;
MODULE_DATA* module_data;
switch(message)
{
case CALLBACK_MSG_RULE_MATCHING:
case CALLBACK_MSG_RULE_NOT_MATCHING:
return handle_message(message, (YR_RULE*) message_data, user_data);
case CALLBACK_MSG_IMPORT_MODULE:
mi = (YR_MODULE_IMPORT*) message_data;
module_data = modules_data_list;
while (module_data != NULL)
{
if (strcmp(module_data->module_name, mi->module_name) == 0)
{
mi->module_data = module_data->mapped_file.data;
mi->module_data_size = module_data->mapped_file.size;
break;
}
module_data = module_data->next;
}
return CALLBACK_CONTINUE;
case CALLBACK_MSG_MODULE_IMPORTED:
if (show_module_data)
{
object = (YR_OBJECT*) message_data;
mutex_lock(&output_mutex);
yr_object_print_data(object, 0, 1);
printf("\n");
mutex_unlock(&output_mutex);
}
return CALLBACK_CONTINUE;
}
return CALLBACK_ERROR;
}
#if defined(_WIN32) || defined(__CYGWIN__)
DWORD WINAPI scanning_thread(LPVOID param)
#else
void* scanning_thread(void* param)
#endif
{
int result = ERROR_SUCCESS;
THREAD_ARGS* args = (THREAD_ARGS*) param;
char* file_path = file_queue_get();
int flags = 0;
if (fast_scan)
flags |= SCAN_FLAGS_FAST_MODE;
while (file_path != NULL)
{
int elapsed_time = (int) difftime(time(NULL), args->start_time);
if (elapsed_time < timeout)
{
result = yr_rules_scan_file(
args->rules,
file_path,
flags,
callback,
file_path,
timeout - elapsed_time);
if (result != ERROR_SUCCESS)
{
mutex_lock(&output_mutex);
fprintf(stderr, "error scanning %s: ", file_path);
print_scanner_error(result);
mutex_unlock(&output_mutex);
}
free(file_path);
file_path = file_queue_get();
}
else
{
file_path = NULL;
}
}
yr_finalize_thread();
return 0;
}
int is_integer(
const char *str)
{
if (*str == '-')
str++;
while(*str)
{
if (!isdigit(*str))
return FALSE;
str++;
}
return TRUE;
}
int is_float(
const char *str)
{
int has_dot = FALSE;
if (*str == '-') // skip the minus sign if present
str++;
if (*str == '.') // float can't start with a dot
return FALSE;
while(*str)
{
if (*str == '.')
{
if (has_dot) // two dots, not a float
return FALSE;
has_dot = TRUE;
}
else if (!isdigit(*str))
{
return FALSE;
}
str++;
}
return has_dot; // to be float must contain a dot
}
int define_external_variables(
YR_RULES* rules,
YR_COMPILER* compiler)
{
int result = ERROR_SUCCESS;
for (int i = 0; ext_vars[i] != NULL; i++)
{
char* equal_sign = strchr(ext_vars[i], '=');
if (!equal_sign)
{
fprintf(stderr, "error: wrong syntax for `-d` option.\n");
return ERROR_SUCCESS;
}
// Replace the equal sign with null character to split the external
// variable definition (i.e: myvar=somevalue) in two strings: identifier
// and value.
*equal_sign = '\0';
char* identifier = ext_vars[i];
char* value = equal_sign + 1;
if (is_float(value))
{
if (rules != NULL)
result = yr_rules_define_float_variable(
rules,
identifier,
atof(value));
if (compiler != NULL)
result = yr_compiler_define_float_variable(
compiler,
identifier,
atof(value));
}
else if (is_integer(value))
{
if (rules != NULL)
result = yr_rules_define_integer_variable(
rules,
identifier,
atoi(value));
if (compiler != NULL)
result = yr_compiler_define_integer_variable(
compiler,
identifier,
atoi(value));
}
else if (strcmp(value, "true") == 0 || strcmp(value, "false") == 0)
{
if (rules != NULL)
result = yr_rules_define_boolean_variable(
rules,
identifier,
strcmp(value, "true") == 0);
if (compiler != NULL)
result = yr_compiler_define_boolean_variable(
compiler,
identifier,
strcmp(value, "true") == 0);
}
else
{
if (rules != NULL)
result = yr_rules_define_string_variable(
rules,
identifier,
value);
if (compiler != NULL)
result = yr_compiler_define_string_variable(
compiler,
identifier,
value);
}
}
return result;
}
int load_modules_data()
{
for (int i = 0; modules_data[i] != NULL; i++)
{
char* equal_sign = strchr(modules_data[i], '=');
if (!equal_sign)
{
fprintf(stderr, "error: wrong syntax for `-x` option.\n");
return FALSE;
}
*equal_sign = '\0';
MODULE_DATA* module_data = (MODULE_DATA*) malloc(sizeof(MODULE_DATA));
if (module_data != NULL)
{
module_data->module_name = modules_data[i];
int result = yr_filemap_map(equal_sign + 1, &module_data->mapped_file);
if (result != ERROR_SUCCESS)
{
free(module_data);
fprintf(stderr, "error: could not open file \"%s\".\n", equal_sign + 1);
return FALSE;
}
module_data->next = modules_data_list;
modules_data_list = module_data;
}
}
return TRUE;
}
void unload_modules_data()
{
MODULE_DATA* module_data = modules_data_list;
while(module_data != NULL)
{
MODULE_DATA* next_module_data = module_data->next;
yr_filemap_unmap(&module_data->mapped_file);
free(module_data);
module_data = next_module_data;
}
modules_data_list = NULL;
}
#define exit_with_code(code) { result = code; goto _exit; }
int main(
int argc,
const char** argv)
{
YR_COMPILER* compiler = NULL;
YR_RULES* rules = NULL;
int result;
argc = args_parse(options, argc, argv);
if (show_version)
{
printf("%s\n", PACKAGE_STRING);
return EXIT_SUCCESS;
}
if (show_help)
{
printf(
"YARA %s, the pattern matching swiss army knife.\n"
"%s\n\n"
"Mandatory arguments to long options are mandatory for "
"short options too.\n\n", PACKAGE_VERSION, USAGE_STRING);
args_print_usage(options, 35);
printf("\nSend bug reports and suggestions to: %s.\n", PACKAGE_BUGREPORT);
return EXIT_SUCCESS;
}
if (argc != 2)
{
// After parsing the command-line options we expect two additional
// arguments, the rules file and the target file, directory or pid to
// be scanned.
fprintf(stderr, "yara: wrong number of arguments\n");
fprintf(stderr, "%s\n\n", USAGE_STRING);
fprintf(stderr, "Try `--help` for more options\n");
return EXIT_FAILURE;
}
if (!load_modules_data())
exit_with_code(EXIT_FAILURE);
result = yr_initialize();
if (result != ERROR_SUCCESS)
{
fprintf(stderr, "error: initialization error (%d)\n", result);
exit_with_code(EXIT_FAILURE);
}
if (stack_size != DEFAULT_STACK_SIZE)
{
// If the user chose a different stack size than default,
// modify the yara config here.
yr_set_configuration(YR_CONFIG_STACK_SIZE, &stack_size);
}
// Try to load the rules file as a binary file containing
// compiled rules first
result = yr_rules_load(argv[0], &rules);
// Accepted result are ERROR_SUCCESS or ERROR_INVALID_FILE
// if we are passing the rules in source form, if result is
// different from those exit with error.
if (result != ERROR_SUCCESS &&
result != ERROR_INVALID_FILE)
{
print_scanner_error(result);
exit_with_code(EXIT_FAILURE);
}
if (result == ERROR_SUCCESS)
{
result = define_external_variables(rules, NULL);
if (result != ERROR_SUCCESS)
{
print_scanner_error(result);
exit_with_code(EXIT_FAILURE);
}
}
else
{
// Rules file didn't contain compiled rules, let's handle it
// as a text file containing rules in source form.
if (yr_compiler_create(&compiler) != ERROR_SUCCESS)
exit_with_code(EXIT_FAILURE);
result = define_external_variables(NULL, compiler);
if (result != ERROR_SUCCESS)
{
print_scanner_error(result);
exit_with_code(EXIT_FAILURE);
}
yr_compiler_set_callback(compiler, print_compiler_error, NULL);
FILE* rule_file = fopen(argv[0], "r");
if (rule_file == NULL)
{
fprintf(stderr, "error: could not open file: %s\n", argv[0]);
exit_with_code(EXIT_FAILURE);
}
int errors = yr_compiler_add_file(compiler, rule_file, NULL, argv[0]);
fclose(rule_file);
if (errors > 0)
exit_with_code(EXIT_FAILURE);
result = yr_compiler_get_rules(compiler, &rules);
yr_compiler_destroy(compiler);
compiler = NULL;
if (result != ERROR_SUCCESS)
exit_with_code(EXIT_FAILURE);
}
mutex_init(&output_mutex);
if (is_integer(argv[1]))
{
int pid = atoi(argv[1]);
int flags = 0;
if (fast_scan)
flags |= SCAN_FLAGS_FAST_MODE;
result = yr_rules_scan_proc(
rules,
pid,
flags,
callback,
(void*) argv[1],
timeout);
if (result != ERROR_SUCCESS)
{
print_scanner_error(result);
exit_with_code(EXIT_FAILURE);
}
}
else if (is_directory(argv[1]))
{
if (file_queue_init() != 0)
{
print_scanner_error(ERROR_INTERNAL_FATAL_ERROR);
exit_with_code(EXIT_FAILURE);
}
THREAD thread[MAX_THREADS];
THREAD_ARGS thread_args;
time_t start_time = time(NULL);
thread_args.rules = rules;
thread_args.start_time = start_time;
for (int i = 0; i < threads; i++)
{
if (create_thread(&thread[i], scanning_thread, (void*) &thread_args))
{
print_scanner_error(ERROR_COULD_NOT_CREATE_THREAD);
exit_with_code(EXIT_FAILURE);
}
}
scan_dir(
argv[1],
recursive_search,
start_time,
rules,
callback);
file_queue_finish();
// Wait for scan threads to finish
for (int i = 0; i < threads; i++)
thread_join(&thread[i]);
file_queue_destroy();
}
else
{
int flags = 0;
if (fast_scan)
flags |= SCAN_FLAGS_FAST_MODE;
result = yr_rules_scan_file(
rules,
argv[1],
flags,
callback,
(void*) argv[1],
timeout);
if (result != ERROR_SUCCESS)
{
fprintf(stderr, "error scanning %s: ", argv[1]);
print_scanner_error(result);
exit_with_code(EXIT_FAILURE);
}
}
#ifdef PROFILING_ENABLED
yr_rules_print_profiling_info(rules);
#endif
result = EXIT_SUCCESS;
_exit:
unload_modules_data();
if (compiler != NULL)
yr_compiler_destroy(compiler);
if (rules != NULL)
yr_rules_destroy(rules);
yr_finalize();
return result;
}
|