Browse code

bb11343: Automatically appennding YARA. to all yara rule sigs.

Mickey Sola authored on 2015/07/25 02:24:11
Showing 2 changed files
... ...
@@ -3446,6 +3446,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3446 3446
     char *logic = NULL, *target_str = NULL;
3447 3447
     uint8_t has_short_string;
3448 3448
     char *exp_op = "|";
3449
+    char *newident = NULL;
3449 3450
 
3450 3451
     cli_yaramsg("load_oneyara: attempting to load %s\n", rule->identifier);
3451 3452
 
... ...
@@ -3462,8 +3463,17 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3462 3462
         return CL_SUCCESS;
3463 3463
     }
3464 3464
 
3465
-    if(engine->cb_sigload && engine->cb_sigload("yara", rule->identifier, ~options & CL_DB_OFFICIAL, engine->cb_sigload_ctx)) {
3466
-        cli_dbgmsg("cli_loadyara: skipping %s due to callback\n", rule->identifier);
3465
+    newident = cli_malloc(strlen(rule->identifier) + 5 + 1);
3466
+    if(!newident) {
3467
+	cli_errmsg("cli_loadyara(): newident == NULL\n");
3468
+	return CL_EMEM;
3469
+    }
3470
+
3471
+    sprintf(newident, "YARA.%s", rule->identifier);
3472
+
3473
+    if(engine->cb_sigload && engine->cb_sigload("yara", newident, ~options & CL_DB_OFFICIAL, engine->cb_sigload_ctx)) {
3474
+        cli_dbgmsg("cli_loadyara: skipping %s due to callback\n", newident);
3475
+        free(newident);
3467 3476
         (*sigs)--;
3468 3477
         return CL_SUCCESS;
3469 3478
     }
... ...
@@ -3488,11 +3498,12 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3488 3488
 
3489 3489
     if (RULE_IS_NULL(rule) || ((rule->g_flags) & RULE_GFLAGS_REQUIRE_EXECUTABLE)) {
3490 3490
 
3491
-        cli_warnmsg("load_oneyara: skipping %s due to unsupported rule gflags\n", rule->identifier);
3491
+        cli_warnmsg("load_oneyara: skipping %s due to unsupported rule gflags\n", newident);
3492 3492
 
3493 3493
         cli_yaramsg("RULE_IS_NULL                   %s\n", RULE_IS_NULL(rule) ? "yes" : "no");
3494 3494
         cli_yaramsg("RULE_GFLAGS_REQUIRE_EXECUTABLE %s\n", ((rule->g_flags) & RULE_GFLAGS_REQUIRE_EXECUTABLE) ? "yes" : "no");
3495 3495
 
3496
+        free(newident);
3496 3497
         (*sigs)--;
3497 3498
         return CL_SUCCESS;
3498 3499
     }
... ...
@@ -3513,9 +3524,10 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3513 3513
     */
3514 3514
 #endif
3515 3515
 
3516
-    if(engine->cb_sigload && engine->cb_sigload("yara", rule->identifier, ~options & CL_DB_OFFICIAL, engine->cb_sigload_ctx)) {
3517
-        cli_dbgmsg("load_oneyara: skipping %s due to callback\n", rule->identifier);
3516
+    if(engine->cb_sigload && engine->cb_sigload("yara", newident, ~options & CL_DB_OFFICIAL, engine->cb_sigload_ctx)) {
3517
+        cli_dbgmsg("load_oneyara: skipping %s due to callback\n", newident);
3518 3518
         (*sigs)--;
3519
+        free(newident);
3519 3520
         return CL_SUCCESS;
3520 3521
     }
3521 3522
 
... ...
@@ -3526,7 +3538,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3526 3526
 
3527 3527
         /* string type handler */
3528 3528
         if (STRING_IS_NULL(string)) {
3529
-            cli_warnmsg("load_oneyara: skipping NULL string %s\n", string->identifier);
3529
+            cli_warnmsg("load_oneyara: skipping NULL string %s\n", newident);
3530 3530
             //str_error++; /* kill the insertion? */
3531 3531
             continue;
3532 3532
 #ifdef YARA_FINISHED
... ...
@@ -3577,7 +3589,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3577 3577
             ytable_add_string(&ytable, substr);
3578 3578
             free(substr);
3579 3579
 #else
3580
-            cli_warnmsg("cli_loadyara: %s uses PCREs but support is disabled\n", rule->identifier);
3580
+            cli_warnmsg("cli_loadyara: %s uses PCREs but support is disabled\n", newident);
3581 3581
             str_error++;
3582 3582
             ret = CL_SUCCESS;
3583 3583
             break;
... ...
@@ -3590,7 +3602,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3590 3590
             size_t totsize = 2*length+1;
3591 3591
 
3592 3592
             if (length < CLI_DEFAULT_AC_MINDEPTH) {
3593
-                cli_warnmsg("load_oneyara: string is too short %s\n", string->identifier);
3593
+                cli_warnmsg("load_oneyara: string is too short %s\n", newident);
3594 3594
                 str_error++;
3595 3595
                 continue;
3596 3596
             }
... ...
@@ -3667,7 +3679,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3667 3667
         if (STRING_IS_REFERENCED(string) || STRING_IS_FAST_HEX_REGEXP(string) || STRING_IS_CHAIN_PART(string) ||
3668 3668
             STRING_IS_CHAIN_TAIL(string) || STRING_FITS_IN_ATOM(string)) {
3669 3669
 
3670
-            cli_warnmsg("load_oneyara: skipping unsupported string %s\n", rule->identifier);
3670
+            cli_warnmsg("load_oneyara: skipping unsupported string %s\n", newident);
3671 3671
 
3672 3672
             cli_yaramsg("STRING_IS_REFERENCED      %s\n", STRING_IS_REFERENCED(string) ? "yes" : "no");
3673 3673
             cli_yaramsg("STRING_IS_FAST_HEX_REGEXP %s\n", STRING_IS_FAST_HEX_REGEXP(string) ? "yes" : "no");
... ...
@@ -3701,21 +3713,24 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3701 3701
     }
3702 3702
 
3703 3703
     if (str_error > 0) {
3704
-        cli_warnmsg("load_oneyara: clamav cannot support %d input strings, skipping %s\n", str_error, rule->identifier);
3704
+        cli_warnmsg("load_oneyara: clamav cannot support %d input strings, skipping %s\n", str_error, newident);
3705 3705
         yara_malform++;
3706 3706
         ytable_delete(&ytable);
3707
+        free(newident);
3707 3708
         (*sigs)--;
3708 3709
         return ret;
3709 3710
     } else if (ytable.tbl_cnt == 0) {
3710
-        cli_warnmsg("load_oneyara: yara rule contains no supported strings, skipping %s\n", rule->identifier);
3711
+        cli_warnmsg("load_oneyara: yara rule contains no supported strings, skipping %s\n", newident);
3711 3712
         yara_malform++;
3712 3713
         ytable_delete(&ytable);
3714
+        free(newident);
3713 3715
         (*sigs)--;
3714 3716
         return CL_SUCCESS; /* TODO - kill signature instead? */
3715 3717
     } else if (ytable.tbl_cnt > MAX_LDB_SUBSIGS) {
3716
-        cli_warnmsg("load_oneyara: yara rule contains too many subsigs (%d, max: %d), skipping %s\n", ytable.tbl_cnt, MAX_LDB_SUBSIGS, rule->identifier);
3718
+        cli_warnmsg("load_oneyara: yara rule contains too many subsigs (%d, max: %d), skipping %s\n", ytable.tbl_cnt, MAX_LDB_SUBSIGS, newident);
3717 3719
         yara_malform++;
3718 3720
         ytable_delete(&ytable);
3721
+        free(newident);
3719 3722
         (*sigs)--;
3720 3723
         return CL_SUCCESS;
3721 3724
     }
... ...
@@ -3757,10 +3772,11 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3757 3757
         target_str = cli_strdup(YARATARGET0);
3758 3758
 
3759 3759
     memset(&tdb, 0, sizeof(tdb));
3760
-    if ((ret = init_tdb(&tdb, engine, target_str, rule->identifier)) != CL_SUCCESS) {
3760
+    if ((ret = init_tdb(&tdb, engine, target_str, newident)) != CL_SUCCESS) {
3761 3761
         ytable_delete(&ytable);
3762 3762
         free(logic);
3763 3763
         free(target_str);
3764
+        free(newident);
3764 3765
         (*sigs)--;
3765 3766
         if (ret == CL_BREAK)
3766 3767
             return CL_SUCCESS;
... ...
@@ -3777,6 +3793,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3777 3777
         FREE_TDB(tdb);
3778 3778
         ytable_delete(&ytable);
3779 3779
         free(logic);
3780
+        free(newident);
3780 3781
         return CL_EMEM;
3781 3782
     }
3782 3783
 
... ...
@@ -3791,6 +3808,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3791 3791
             FREE_TDB(tdb);
3792 3792
             ytable_delete(&ytable);
3793 3793
             mpool_free(engine->mempool, lsig);
3794
+            free(newident);
3794 3795
             return CL_EMEM;
3795 3796
         }
3796 3797
     } else {
... ...
@@ -3803,6 +3821,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3803 3803
             FREE_TDB(tdb);
3804 3804
             ytable_delete(&ytable);
3805 3805
             mpool_free(engine->mempool, lsig);
3806
+            free(newident);
3806 3807
             return CL_EMEM;
3807 3808
         }
3808 3809
     }
... ...
@@ -3818,6 +3837,7 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3818 3818
         FREE_TDB(tdb);
3819 3819
         ytable_delete(&ytable);
3820 3820
         mpool_free(engine->mempool, lsig);
3821
+        free(newident);
3821 3822
         return CL_EMEM;
3822 3823
     }
3823 3824
 
... ...
@@ -3835,13 +3855,14 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3835 3835
                     (ytable.table[i]->sigopts & ACPATT_OPTION_WIDE) ? "w" : "",
3836 3836
                     (ytable.table[i]->sigopts & ACPATT_OPTION_ASCII) ? "a" : "");
3837 3837
 
3838
-        if((ret = cli_sigopts_handler(root, rule->identifier, ytable.table[i]->hexstr, ytable.table[i]->sigopts, 0, 0, ytable.table[i]->offset, target, lsigid, options)) != CL_SUCCESS) {
3838
+        if((ret = cli_sigopts_handler(root, newident, ytable.table[i]->hexstr, ytable.table[i]->sigopts, 0, 0, ytable.table[i]->offset, target, lsigid, options)) != CL_SUCCESS) {
3839 3839
             root->ac_lsigs--;
3840 3840
             FREE_TDB(tdb);
3841 3841
             ytable_delete(&ytable);
3842 3842
             mpool_free(engine->mempool, lsig);
3843 3843
 
3844 3844
             yara_malform++;
3845
+            free(newident);
3845 3846
             return ret;
3846 3847
         }
3847 3848
     }
... ...
@@ -3851,7 +3872,8 @@ static int load_oneyara(YR_RULE *rule, int chkpua, struct cl_engine *engine, uns
3851 3851
 
3852 3852
     rule->lsigid = root->ac_lsigs - 1;
3853 3853
     yara_loaded++;
3854
-    cli_yaramsg("load_oneyara: successfully loaded %s\n", rule->identifier);
3854
+    cli_yaramsg("load_oneyara: successfully loaded %s\n", newident);
3855
+    free(newident);
3855 3856
     return CL_SUCCESS;
3856 3857
 }
3857 3858
 
... ...
@@ -228,8 +228,8 @@ EOF
228 228
     if test_run 1 $CLAMSCAN --gen-json --quiet -dtest-db/test.yara $TESTFILES --log=clamscan6.log; then
229 229
 	scan_failed clamscan6.log "clamscan YARA at-offset test failed"
230 230
     fi
231
-    grep "clam.tar.gz: yara_at_offset.UNOFFICIAL FOUND" clamscan6.log || die "YARA at-offset test1 failed"
232
-    grep "clam_cache_emax.tgz: yara_at_offset.UNOFFICIAL FOUND" clamscan6.log || die "YARA at-offset test2 failed"
231
+    grep "clam.tar.gz: YARA.yara_at_offset.UNOFFICIAL FOUND" clamscan6.log || die "YARA at-offset test1 failed"
232
+    grep "clam_cache_emax.tgz: YARA.yara_at_offset.UNOFFICIAL FOUND" clamscan6.log || die "YARA at-offset test2 failed"
233 233
     NINFECTED=`grep "Infected files" clamscan6.log | cut -f2 -d: | sed -e 's/ //g'`
234 234
     if test "x$NINFECTED" != x2; then
235 235
 	scan_failed clamscan7.log "clamscan: unexpected YARA offset match."
... ...
@@ -241,8 +241,8 @@ EOF
241 241
     if test_run 1 $CLAMSCAN --gen-json --quiet -dtest-db/test.yara $TESTFILES --log=clamscan7.log; then
242 242
 	scan_failed clamscan7.log "clamscan YARA in-range test failed"
243 243
     fi
244
-    grep "clam.tar.gz: yara_in_range.UNOFFICIAL FOUND" clamscan7.log || die "YARA in-range test1 failed"
245
-    grep "clam_cache_emax.tgz: yara_in_range.UNOFFICIAL FOUND" clamscan7.log || die "YARA in-range test2 failed"
244
+    grep "clam.tar.gz: YARA.yara_in_range.UNOFFICIAL FOUND" clamscan7.log || die "YARA in-range test1 failed"
245
+    grep "clam_cache_emax.tgz: YARA.yara_in_range.UNOFFICIAL FOUND" clamscan7.log || die "YARA in-range test2 failed"
246 246
     NINFECTED=`grep "Infected files" clamscan7.log | cut -f2 -d: | sed -e 's/ //g'`
247 247
     if test "x$NINFECTED" != x2; then
248 248
 	scan_failed clamscan7.log "clamscan: unexpected YARA range match."