Browse code

Handle yara entrypoint keyword in prototype.

Steven Morgan authored on 2014/12/15 12:23:04
Showing 4 changed files
... ...
@@ -2723,7 +2723,9 @@ static int cli_loadopenioc(FILE *fs, const char *dbname, struct cl_engine *engin
2723 2723
 }
2724 2724
 
2725 2725
 #ifndef _WIN32
2726
-#define YARATARGET "Target:0;"
2726
+#define YARATARGET0 "Target:0;"
2727
+#define YARATARGET1 "Target:1;"
2728
+#define EPSTR "EP+0:"
2727 2729
 static char *parse_yara_hex_string(YR_STRING *string);
2728 2730
 
2729 2731
 static char *parse_yara_hex_string(YR_STRING *string)
... ...
@@ -2835,7 +2837,7 @@ static int cli_loadyara(FILE *fs, const char *dbname, struct cl_engine *engine,
2835 2835
 
2836 2836
         /* First find out how long our dynamically-build ldb string should be */
2837 2837
         allstringsize = 0;
2838
-        totsize = strlen(rule->id) + 2 + strlen(YARATARGET);
2838
+        totsize = strlen(rule->id) + 2 + strlen(YARATARGET0);
2839 2839
         STAILQ_FOREACH(string, &rule->strings, link) {
2840 2840
             nstrings++;
2841 2841
             if (STRING_IS_HEX(string)) {
... ...
@@ -2881,6 +2883,11 @@ static int cli_loadyara(FILE *fs, const char *dbname, struct cl_engine *engine,
2881 2881
             totsize += nstrings-10;
2882 2882
         totsize++;
2883 2883
 
2884
+#ifdef YARA_PROTO
2885
+        if (rule->g_flags & RULE_EP && nstrings == 1)
2886
+            totsize += strlen(EPSTR);
2887
+#endif
2888
+
2884 2889
         rulestr = cli_calloc(totsize, sizeof(char));
2885 2890
         if (!rulestr) {
2886 2891
             free(rule->id);
... ...
@@ -2894,11 +2901,15 @@ static int cli_loadyara(FILE *fs, const char *dbname, struct cl_engine *engine,
2894 2894
             exp_op = "&";
2895 2895
         else {
2896 2896
             exp_op = "|";
2897
-            if (!(rule->g_flags & RULE_ANY && rule->g_flags & RULE_THEM) && nstrings > 1)
2897
+            if ((!(rule->g_flags & RULE_ANY && rule->g_flags & RULE_THEM) && nstrings > 1) &&
2898
+                !(rule->g_flags & RULE_EP && nstrings == 1))
2898 2899
                 yara_complex++;
2899 2900
         }
2901
+        if (rule->g_flags & RULE_EP)
2902
+            sprintf(rulestr, "%s;%s(", rule->id, YARATARGET1);
2903
+        else
2900 2904
 #endif
2901
-        sprintf(rulestr, "%s;%s(", rule->id, YARATARGET);
2905
+        sprintf(rulestr, "%s;%s(", rule->id, YARATARGET0);
2902 2906
         for (i=0; i<nstrings; i++) {
2903 2907
             size_t len=strlen(rulestr);
2904 2908
             snprintf(rulestr+len, totsize-len, "%u%s", i, (i+1 == nstrings) ? "" : exp_op);
... ...
@@ -2915,6 +2926,12 @@ static int cli_loadyara(FILE *fs, const char *dbname, struct cl_engine *engine,
2915 2915
 #if 0
2916 2916
             cli_errmsg("%i:", ++dcount);
2917 2917
 #endif
2918
+#ifdef YARA_PROTO
2919
+            if (rule->g_flags & RULE_EP && nstrings == 1) {
2920
+                size_t len = strlen(rulestr);
2921
+                snprintf(rulestr+len, totsize-len, "%s", EPSTR);
2922
+            }
2923
+#endif
2918 2924
 
2919 2925
             if (STRING_IS_HEX(string)) {
2920 2926
                 char *substr;
... ...
@@ -445,6 +445,7 @@ struct RE {
445 445
 #define RULE_ALL  2
446 446
 #define RULE_ONE  4
447 447
 #define RULE_THEM 8
448
+#define RULE_EP   16
448 449
 #endif
449 450
 
450 451
 /* YARA to ClamAV function mappings */
... ...
@@ -1508,9 +1508,12 @@ primary_expression
1508 1508
       }
1509 1509
     | _ENTRYPOINT_
1510 1510
       {
1511
+#ifndef YARA_PROTO
1511 1512
         yywarning(yyscanner,
1512 1513
             "Using deprecated \"entrypoint\" keyword. Use the \"entry_point\" " "function from PE module instead.");
1513
-
1514
+#else
1515
+        compiler->current_rule_flags |= RULE_EP;
1516
+#endif
1514 1517
         compiler->last_result = yr_parser_emit(
1515 1518
             yyscanner, OP_ENTRYPOINT, NULL);
1516 1519
 
... ...
@@ -608,7 +608,7 @@ YR_STRING* yr_parser_reduce_string_declaration(
608 608
       string->g_flags |= STRING_GFLAGS_CHAIN_TAIL | STRING_GFLAGS_CHAIN_PART;
609 609
       string->chain_gap_min = min_gap;
610 610
       string->chain_gap_max = max_gap;
611
-    }
611
+    
612 612
 
613 613
     // Use "aux_string" from now on, we want to keep the value of "string"
614 614
     // because it will returned.