Browse code

added structures for yara string table

Kevin Lin authored on 2015/02/12 08:38:06
Showing 1 changed files
... ...
@@ -2733,10 +2733,10 @@ static int cli_loadopenioc(FILE *fs, const char *dbname, struct cl_engine *engin
2733 2733
 }
2734 2734
 
2735 2735
 #ifndef _WIN32
2736
-#define YARA_DEBUG 1
2737
-#if YARA_DEBUG == 2
2736
+#define YARA_DEBUG 2
2737
+#if (YARA_DEBUG == 2)
2738 2738
 #define cli_yaramsg(...) cli_errmsg(__VA_ARGS__)
2739
-#elseif YARA_DEBUG == 1
2739
+#elif (YARA_DEBUG == 1)
2740 2740
 #define cli_yaramsg(...) cli_dbgmsg(__VA_ARGS__)
2741 2741
 #else
2742 2742
 #define cli_yaramsg(...) 
... ...
@@ -2811,22 +2811,27 @@ static char *parse_yara_hex_string(YR_STRING *string)
2811 2811
     return res;
2812 2812
 }
2813 2813
 
2814
-int ytable_add(char ***ytablep, uint32_t *ytbl_cntp)
2815
-{
2816
-}
2814
+struct cli_ytable_entry {
2815
+    char *offset;
2816
+    char *hexstr;
2817
+    char *sigopts;
2818
+};
2817 2819
 
2818
-uint32_t ytable_lookup(char **ytable, uint32_t ytbl_cnt)
2819
-{
2820
-}
2820
+struct cli_ytable {
2821
+    struct cli_ytable_entry **table;
2822
+    uint32_t tbl_cnt;
2823
+};
2821 2824
 
2822
-void ytable_delete(char **ytable, uint32_t ytbl_cnt)
2825
+void ytable_delete(struct cli_ytable *ytable)
2823 2826
 {
2824 2827
     uint32_t i;
2828
+    if (!ytable)
2829
+        return;
2825 2830
 
2826
-    if (ytable) {
2827
-        for (i = 0; i < ytbl_cnt; ++i)
2828
-            free(ytable[i]);
2829
-        free(ytable);
2831
+    if (ytable->table) {
2832
+        for (i = 0; i < ytable->tbl_cnt; ++i)
2833
+            free(ytable->table[i]);
2834
+        free(ytable->table);
2830 2835
     }
2831 2836
 }
2832 2837
 
... ...
@@ -2849,10 +2854,8 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2849 2849
     size_t lsize;
2850 2850
     char *logic = NULL;
2851 2851
     char *exp_op = "|";
2852
-    char *offset;
2853 2852
 
2854
-    char **ytable = NULL;
2855
-    uint32_t ytbl_cnt = 0;
2853
+    struct cli_ytable ytable = { 0 };
2856 2854
 
2857 2855
     cli_yaramsg("called load_oneyara()\n");
2858 2856
 
... ...
@@ -2873,10 +2876,17 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2873 2873
     */
2874 2874
 
2875 2875
     /*** rule specific checks ***/
2876
-    if (RULE_IS_PRIVATE(rule) || RULE_IS_GLOBAL(rule) || RULE_IS_NULL(rule) ||
2877
-        ((rule->g_flags) & RULE_GFLAGS_REQUIRE_FILE) || ((rule->g_flags) & RULE_GFLAGS_REQUIRE_EXECUTABLE)) {
2876
+    if (RULE_IS_PRIVATE(rule) || !RULE_IS_GLOBAL(rule) || RULE_IS_NULL(rule) ||
2877
+        !((rule->g_flags) & RULE_GFLAGS_REQUIRE_FILE) || ((rule->g_flags) & RULE_GFLAGS_REQUIRE_EXECUTABLE)) {
2878 2878
 
2879 2879
         cli_warnmsg("load_oneyara: skipping %s due to unsupported rule gflags\n", rule->id);
2880
+
2881
+        cli_yaramsg("RULE_IS_PRIVATE                %s\n", RULE_IS_PRIVATE(rule) ? "yes" : "no");
2882
+        cli_yaramsg("RULE_IS_GLOBAL                 %s\n", RULE_IS_GLOBAL(rule) ? "yes" : "no");
2883
+        cli_yaramsg("RULE_IS_NULL                   %s\n", RULE_IS_NULL(rule) ? "yes" : "no");
2884
+        cli_yaramsg("RULE_GFLAGS_REQUIRE_FILE       %s\n", ((rule->g_flags) & RULE_GFLAGS_REQUIRE_FILE) ? "yes" : "no");
2885
+        cli_yaramsg("RULE_GFLAGS_REQUIRE_EXECUTABLE %s\n", ((rule->g_flags) & RULE_GFLAGS_REQUIRE_EXECUTABLE) ? "yes" : "no");
2886
+
2880 2887
         (*sigs)--;
2881 2888
         return CL_SUCCESS;
2882 2889
     }
... ...
@@ -2917,6 +2927,7 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2917 2917
             continue;
2918 2918
         }
2919 2919
 
2920
+
2920 2921
         /* modifier handler */
2921 2922
         if (STRING_IS_NO_CASE(string)) {
2922 2923
         }
... ...
@@ -2927,13 +2938,26 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2927 2927
         if (STRING_IS_FULL_WORD(string)) {
2928 2928
         }
2929 2929
 
2930
+
2930 2931
         /* special modifier handler */
2931
-        if (STRING_IS_ANONYMOUS(string)) { /* empty */ }
2932
+        if (STRING_IS_ANONYMOUS(string))
2933
+            cli_yaramsg("STRING_IS_ANONYMOUS       %s\n", STRING_IS_SINGLE_MATCH(string) ? "yes" : "no");
2932 2934
 
2933 2935
         /* unsupported(?) modifier handler */
2934
-        if (STRING_IS_REFERENCED(string) || STRING_IS_SINGLE_MATCH(string) || STRING_IS_FAST_HEX_REGEXP(string) ||
2935
-            STRING_IS_CHAIN_PART(string) || STRING_IS_CHAIN_TAIL(string) || STRING_FITS_IN_ATOM(string)) {
2936
+        if (STRING_IS_SINGLE_MATCH(string))
2937
+            cli_yaramsg("STRING_IS_SINGLE_MATCH    %s\n", STRING_IS_SINGLE_MATCH(string) ? "yes" : "no");
2938
+
2939
+        if (STRING_IS_REFERENCED(string) || STRING_IS_FAST_HEX_REGEXP(string) || STRING_IS_CHAIN_PART(string) ||
2940
+            STRING_IS_CHAIN_TAIL(string) || STRING_FITS_IN_ATOM(string)) {
2941
+
2936 2942
             cli_warnmsg("load_oneyara: skipping unsupported string %s\n", rule->id);
2943
+
2944
+            cli_yaramsg("STRING_IS_REFERENCED      %s\n", STRING_IS_REFERENCED(string) ? "yes" : "no");
2945
+            cli_yaramsg("STRING_IS_FAST_HEX_REGEXP %s\n", STRING_IS_FAST_HEX_REGEXP(string) ? "yes" : "no");
2946
+            cli_yaramsg("STRING_IS_CHAIN_PART      %s\n", STRING_IS_CHAIN_PART(string) ? "yes" : "no");
2947
+            cli_yaramsg("STRING_IS_CHAIN_TAIL      %s\n", STRING_IS_CHAIN_TAIL(string) ? "yes" : "no");
2948
+            cli_yaramsg("STRING_FITS_IN_ATOM       %s\n", STRING_FITS_IN_ATOM(string) ? "yes" : "no");
2949
+
2937 2950
             str_error++;
2938 2951
             free(substr);
2939 2952
             continue;
... ...
@@ -2941,13 +2965,13 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2941 2941
     }
2942 2942
 
2943 2943
     if (str_error > 0) {
2944
-        cli_errmsg("load_oneyara: clamav does not support %d input strings\n", str_error);
2945
-        ytable_delete(ytable, ytbl_cnt);
2944
+        cli_warnmsg("load_oneyara: clamav does not support %d input strings\n", str_error);
2945
+        ytable_delete(&ytable);
2946 2946
         (*sigs)--;
2947 2947
         return CL_SUCCESS; /* TODO - kill signature instead? */
2948
-    } else if (ytbl_cnt == 0) {
2949
-        cli_errmsg("load_oneyara: yara contains no supported strings\n");
2950
-        ytable_delete(ytable, ytbl_cnt);
2948
+    } else if (ytable.tbl_cnt == 0) {
2949
+        cli_warnmsg("load_oneyara: yara contains no supported strings\n");
2950
+        ytable_delete(&ytable);
2951 2951
         (*sigs)--;
2952 2952
         return CL_SUCCESS; /* TODO - kill signature instead? */
2953 2953
     }
... ...
@@ -2955,11 +2979,11 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2955 2955
     /*** conditional verification step (ex. do we define too many strings versus used?)  ***/
2956 2956
     /*** additional string table population (ex. offsets), second translation table pass ***/
2957 2957
 
2958
-    lsize = 3*ytbl_cnt;
2958
+    lsize = 3*ytable.tbl_cnt;
2959 2959
     logic = cli_calloc(lsize, sizeof(char));
2960 2960
     if (!logic) {
2961 2961
         cli_errmsg("load_oneyara: cannot allocate memory for logic statement\n");
2962
-        ytable_delete(ytable, ytbl_cnt);
2962
+        ytable_delete(&ytable);
2963 2963
         return CL_EMEM;
2964 2964
     }
2965 2965
 
... ...
@@ -2967,14 +2991,14 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2967 2967
         exp_op = "&";
2968 2968
     else {
2969 2969
         exp_op = "|";
2970
-        if ((!(rule->g_flags & RULE_ANY && rule->g_flags & RULE_THEM) && ytbl_cnt > 1) &&
2971
-            !(rule->g_flags & RULE_EP && ytbl_cnt == 1))
2970
+        if ((!(rule->g_flags & RULE_ANY && rule->g_flags & RULE_THEM) && ytable.tbl_cnt > 1) &&
2971
+            !(rule->g_flags & RULE_EP && ytable.tbl_cnt == 1))
2972 2972
             yara_complex++;
2973 2973
     }
2974 2974
 
2975
-    for (i=0; i<ytbl_cnt; i++) {
2975
+    for (i=0; i<ytable.tbl_cnt; i++) {
2976 2976
         size_t len=strlen(logic);
2977
-        snprintf(logic+len, lsize-len, "%u%s", i, (i+1 == ytbl_cnt) ? "" : exp_op);
2977
+        snprintf(logic+len, lsize-len, "%u%s", i, (i+1 == ytable.tbl_cnt) ? "" : exp_op);
2978 2978
     }    
2979 2979
 
2980 2980
     /*** END CONDITIONAL HANDLING ***/
... ...
@@ -2988,7 +3012,7 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
2988 2988
         FREE_TDB(tdb);
2989 2989
         if(ret == 1) {
2990 2990
             cli_dbgmsg("load_oneyara: Not supported attribute(s) in logical signature for %s, skipping\n", rule->id);
2991
-            ytable_delete(ytable, ytbl_cnt);
2991
+            ytable_delete(&ytable);
2992 2992
             free(logic);
2993 2993
             (*sigs)--;
2994 2994
             return CL_SUCCESS;
... ...
@@ -3000,13 +3024,13 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3000 3000
         if(tdb.engine[0] > cl_retflevel()) {
3001 3001
             cli_dbgmsg("load_oneyara: Signature for %s not loaded (required f-level: %u)\n", rule->id, tdb.engine[0]);
3002 3002
             FREE_TDB(tdb);
3003
-            ytable_delete(ytable, ytbl_cnt);
3003
+            ytable_delete(&ytable);
3004 3004
             free(logic);
3005 3005
             (*sigs)--;
3006 3006
             return CL_SUCCESS;
3007 3007
         } else if(tdb.engine[1] < cl_retflevel()) {
3008 3008
             FREE_TDB(tdb);
3009
-            ytable_delete(ytable, ytbl_cnt);
3009
+            ytable_delete(&ytable);
3010 3010
             free(logic);
3011 3011
             (*sigs)--;
3012 3012
             return CL_SUCCESS;
... ...
@@ -3016,13 +3040,13 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3016 3016
     if(!tdb.target) {
3017 3017
         cli_errmsg("load_oneyara: No target specified in TDB\n");
3018 3018
         FREE_TDB(tdb);
3019
-        ytable_delete(ytable, ytbl_cnt);
3019
+        ytable_delete(&ytable);
3020 3020
         free(logic);
3021 3021
         return CL_EMALFDB;
3022 3022
     } else if(tdb.target[0] >= CLI_MTARGETS) {
3023 3023
         cli_dbgmsg("load_oneyara: Not supported target type in logical signature for %s, skipping\n", rule->id);
3024 3024
         FREE_TDB(tdb);
3025
-        ytable_delete(ytable, ytbl_cnt);
3025
+        ytable_delete(&ytable);
3026 3026
         free(logic);
3027 3027
         (*sigs)--;
3028 3028
         return CL_SUCCESS;
... ...
@@ -3031,7 +3055,7 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3031 3031
     if((tdb.icongrp1 || tdb.icongrp2) && tdb.target[0] != 1) {
3032 3032
         cli_errmsg("load_oneyara: IconGroup is only supported in PE (target 1) signatures\n");
3033 3033
         FREE_TDB(tdb);
3034
-        ytable_delete(ytable, ytbl_cnt);
3034
+        ytable_delete(&ytable);
3035 3035
         free(logic);
3036 3036
         return CL_EMALFDB;
3037 3037
     }
... ...
@@ -3039,7 +3063,7 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3039 3039
     if((tdb.ep || tdb.nos) && tdb.target[0] != 1 && tdb.target[0] != 6 && tdb.target[0] != 9) {
3040 3040
         cli_errmsg("load_oneyara: EntryPoint/NumberOfSections is only supported in PE/ELF/Mach-O signatures\n");
3041 3041
         FREE_TDB(tdb);
3042
-        ytable_delete(ytable, ytbl_cnt);
3042
+        ytable_delete(&ytable);
3043 3043
         free(logic);
3044 3044
         return CL_EMALFDB;
3045 3045
     }
... ...
@@ -3051,7 +3075,7 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3051 3051
     if(!lsig) {
3052 3052
         cli_errmsg("load_oneyara: Can't allocate memory for lsig\n");
3053 3053
         FREE_TDB(tdb);
3054
-        ytable_delete(ytable, ytbl_cnt);
3054
+        ytable_delete(&ytable);
3055 3055
         free(logic);
3056 3056
         return CL_EMEM;
3057 3057
     }
... ...
@@ -3062,7 +3086,7 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3062 3062
         if(!lsig->u.logic) {
3063 3063
             cli_errmsg("load_oneyara: Can't allocate memory for lsig->logic\n");
3064 3064
             FREE_TDB(tdb);
3065
-            ytable_delete(ytable, ytbl_cnt);
3065
+            ytable_delete(&ytable);
3066 3066
             free(logic);
3067 3067
             mpool_free(engine->mempool, lsig);
3068 3068
             return CL_EMEM;
... ...
@@ -3070,7 +3094,7 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3070 3070
     } else {
3071 3071
             cli_errmsg("load_oneyara: Unsupported logic type\n");
3072 3072
             FREE_TDB(tdb);
3073
-            ytable_delete(ytable, ytbl_cnt);
3073
+            ytable_delete(&ytable);
3074 3074
             free(logic);
3075 3075
             mpool_free(engine->mempool, lsig);
3076 3076
             return CL_EMEM;
... ...
@@ -3085,30 +3109,27 @@ static int load_oneyara(YR_RULE *rule, struct cl_engine *engine, unsigned int op
3085 3085
         root->ac_lsigs--;
3086 3086
         cli_errmsg("cli_loadldb: Can't realloc root->ac_lsigtable\n");
3087 3087
         FREE_TDB(tdb);
3088
-        ytable_delete(ytable, ytbl_cnt);
3088
+        ytable_delete(&ytable);
3089 3089
         mpool_free(engine->mempool, lsig);
3090 3090
         return CL_EMEM;
3091 3091
     }
3092 3092
 
3093 3093
     newtable[root->ac_lsigs - 1] = lsig;
3094 3094
     root->ac_lsigtable = newtable;
3095
-    tdb.subsigs = ytbl_cnt;
3095
+    tdb.subsigs = ytable.tbl_cnt;
3096 3096
 
3097 3097
     /*** loading step - put things into the AC trie ***/
3098
-    for (i = 0; i < ytbl_cnt; ++i) {
3098
+    for (i = 0; i < ytable.tbl_cnt; ++i) {
3099 3099
         lsigid[1] = i;
3100 3100
 
3101
-        /* TODO - offsets as separate table or integrated into ytable[i]? */
3102
-        offset = "*";
3103
-
3104 3101
         /* TODO - options as separate table or integrated into ytable[i]? */
3105 3102
 
3106
-        if((ret = cli_parse_add(root, rule->id, ytable[i], NULL, 0, 0, offset, target, lsigid, options)))
3103
+        if((ret = cli_parse_add(root, rule->id, ytable.table[i]->hexstr, ytable.table[i]->sigopts, 0, 0, ytable.table[i]->offset, target, lsigid, options)))
3107 3104
             return ret;
3108 3105
     }
3109 3106
 
3110 3107
     memcpy(&lsig->tdb, &tdb, sizeof(tdb));
3111
-    ytable_delete(ytable, ytbl_cnt);
3108
+    ytable_delete(&ytable);
3112 3109
     return CL_SUCCESS;
3113 3110
 }
3114 3111
 
... ...
@@ -3162,7 +3183,7 @@ static int cli_loadyara(FILE *fs, struct cl_engine *engine, unsigned int *signo,
3162 3162
     if(signo)
3163 3163
         *signo += sigs;
3164 3164
 
3165
-    cli_yaramsg("Successfully loaded %u of %u yara signatures from %s\n", sigs, rules, dbname);
3165
+    cli_yaramsg("cli_loadyara: loaded %u of %u yara signatures from %s\n", sigs, rules, dbname);
3166 3166
 
3167 3167
     return CL_SUCCESS;
3168 3168
 }