libclamav/matcher.c
e3aaff8e
 /*
d0cba11e
  *  Copyright (C) 2015, 2017 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
c1206103
  *  Copyright (C) 2007-2013 Sourcefire, Inc.
3c333c78
  *  All Rights Reserved.
4addba22
  *
2023340a
  *  Authors: Tomasz Kojm
e3aaff8e
  *
  *  This program is free software; you can redistribute it and/or modify
bb34cb31
  *  it under the terms of the GNU General Public License version 2 as
  *  published by the Free Software Foundation.
e3aaff8e
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
e3aaff8e
  */
 
6d6e8271
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
e3aaff8e
 #include <string.h>
7ec67e94
 #include <ctype.h>
 #include <sys/types.h>
 #include <sys/stat.h>
b58fdfc2
 #ifdef	HAVE_UNISTD_H
7ec67e94
 #include <unistd.h>
b58fdfc2
 #endif
e3aaff8e
 
 #include "clamav.h"
 #include "others.h"
8000d078
 #include "matcher-ac.h"
 #include "matcher-bm.h"
7f5c687b
 #include "matcher-pcre.h"
888f5794
 #include "filetypes.h"
b68d11d2
 #include "matcher.h"
7ec67e94
 #include "pe.h"
01302683
 #include "elf.h"
 #include "execs.h"
c3a3be2d
 #include "special.h"
7770d314
 #include "scanners.h"
c9c463fe
 #include "str.h"
bedc58de
 #include "cltypes.h"
589d8d8e
 #include "default.h"
8af7ccd0
 #include "macho.h"
048d7677
 #include "fmap.h"
0f7ba617
 #include "pe_icons.h"
55094a9c
 #include "regex/regex.h"
02eabc6d
 #include "filtering.h"
 #include "perflogging.h"
1dae00eb
 #include "bytecode_priv.h"
 #include "bytecode_api_impl.h"
baeb6253
 #ifdef HAVE_YARA
d2554980
 #include "yara_clam.h"
b289385d
 #include "yara_exec.h"
baeb6253
 #endif
16b28d07
 
02eabc6d
 #ifdef CLI_PERF_LOGGING
 
 static inline void PERF_LOG_FILTER(int32_t pos, int32_t length, int8_t trie)
 {
     cli_perf_log_add(RAW_BYTES_SCANNED, length);
     cli_perf_log_add(FILTER_BYTES_SCANNED, length - pos);
     cli_perf_log_count2(TRIE_SCANNED, trie, length - pos);
 }
 
 static inline int PERF_LOG_TRIES(int8_t acmode, int8_t bm_called, int32_t length)
 {
     if (bm_called)
 	cli_perf_log_add(BM_SCANNED, length);
     if (acmode)
 	cli_perf_log_add(AC_SCANNED, length);
     return 0;
 }
 
 #else
cd94be7a
 static inline void PERF_LOG_FILTER(int32_t pos, uint32_t length, int8_t trie) {
     UNUSEDPARAM(pos);
     UNUSEDPARAM(length);
     UNUSEDPARAM(trie);
 }
 
 static inline int PERF_LOG_TRIES(int8_t acmode, int8_t bm_called, int32_t length) {
     UNUSEDPARAM(acmode);
     UNUSEDPARAM(bm_called);
     UNUSEDPARAM(length);
 
     return 0;
 }
02eabc6d
 #endif
53721687
 
2a94c08e
 static inline int matcher_run(const struct cli_matcher *root,
 			      const unsigned char *buffer, uint32_t length,
 			      const char **virname, struct cli_ac_data *mdata,
 			      uint32_t offset,
294558a5
 			      const struct cli_target_info *tinfo,
2a94c08e
 			      cli_file_t ftype,
 			      struct cli_matched_type **ftoffset,
461a7bd9
 			      unsigned int acmode,
10aaf4c3
                               unsigned int pcremode,
ffa9b060
 			      struct cli_ac_result **acres,
461a7bd9
 			      fmap_t *map,
6ad45a29
 			      struct cli_bm_off *offdata,
3c333c78
 			      struct cli_pcre_off *poffdata,
6ad45a29
 			      cli_ctx *ctx)
2a94c08e
 {
91e495f1
     int ret, saved_ret = CL_CLEAN;
02eabc6d
     int32_t pos = 0;
     struct filter_match_info info;
7ba110bd
     uint32_t orig_length, orig_offset;
     const unsigned char* orig_buffer;
6ad45a29
     unsigned int viruses_found = 0;
7ba110bd
 
02eabc6d
     if (root->filter) {
 	if(filter_search_ext(root->filter, buffer, length, &info) == -1) {
 	    /*  for safety always scan last maxpatlen bytes */
 	    pos = length - root->maxpatlen - 1;
 	    if (pos < 0) pos = 0;
 	    PERF_LOG_FILTER(pos, length, root->type);
 	} else {
 	    /* must not cut buffer for 64[4-4]6161, because we must be able to check
 	     * 64! */
 	    pos = info.first_match - root->maxpatlen - 1;
 	    if (pos < 0) pos = 0;
 	    PERF_LOG_FILTER(pos, length, root->type);
 	}
     } else {
 	PERF_LOG_FILTER(0, length, root->type);
     }
7ba110bd
 
     orig_length = length;
     orig_buffer = buffer;
     orig_offset = offset;
02eabc6d
     length -= pos;
     buffer += pos;
     offset += pos;
7ba110bd
     if (!root->ac_only) {
 	PERF_LOG_TRIES(0, 1, length);
 	if (root->bm_offmode) {
 	    /* Don't use prefiltering for BM offset mode, since BM keeps tracks
 	     * of offsets itself, and doesn't work if we skip chunks of input
 	     * data */
38dc186f
 	    ret = cli_bm_scanbuff(orig_buffer, orig_length, virname, NULL, root, orig_offset, tinfo, offdata, ctx);
7ba110bd
 	} else {
38dc186f
 	    ret = cli_bm_scanbuff(buffer, length, virname, NULL, root, offset, tinfo, offdata, ctx);
6ad45a29
 	}
38dc186f
 	if (ret != CL_CLEAN) {
 	    if (ret != CL_VIRUS)
 		return ret;
 
 	    /* else (ret == CL_VIRUS) */
d7979d4f
 	    if (SCAN_ALLMATCHES)
38dc186f
 		viruses_found = 1;
 	    else {
cbf5017a
 		ret = cli_append_virus(ctx, *virname);
 		if (ret != CL_CLEAN)
                     return ret;
6ad45a29
 	    }
7ba110bd
 	}
02eabc6d
     }
7ba110bd
     PERF_LOG_TRIES(acmode, 0, length);
aec1e3be
     ret = cli_ac_scanbuff(buffer, length, virname, NULL, acres, root, mdata, offset, ftype, ftoffset, acmode, ctx);
6565fe45
     if (ret != CL_CLEAN) {
167c0079
         if (ret == CL_VIRUS) {
d7979d4f
             if (SCAN_ALLMATCHES)
6565fe45
                 viruses_found = 1;
             else {
cbf5017a
                 ret = cli_append_virus(ctx, *virname);
                 if (ret != CL_CLEAN)
                     return ret;
6565fe45
             }
         } else if (ret > CL_TYPENO && acmode & AC_SCAN_VIR)
91e495f1
             saved_ret = ret;
6565fe45
         else
             return ret;
167c0079
     }
b81cbc26
 
0ef888f5
     if (root->bcomp_metas) {
d7d58a58
         ret = cli_bcomp_scanbuf(orig_buffer, orig_length, virname, acres, root, mdata, ctx);
18ff5029
         if (ret != CL_CLEAN) {
             if (ret == CL_VIRUS) {
d2f48a2c
                 if (SCAN_ALLMATCHES)
18ff5029
                     viruses_found = 1;
                 else {
                     ret = cli_append_virus(ctx, *virname);
                     if (ret != CL_CLEAN)
                         return ret;
                 }
             } else if (ret > CL_TYPENO && acmode & AC_SCAN_VIR)
                 saved_ret = ret;
             else
                 return ret;
         }
     }
 
3c333c78
     /* due to logical triggered, pcres cannot be evaluated until after full subsig matching */
     /* cannot save pcre execution state without possible evasion; must scan entire buffer */
     /* however, scanning the whole buffer may require the whole buffer being loaded into memory */
 #if HAVE_PCRE
a0efe9cf
     if (root->pcre_metas) {
045d184a
         int rc;
         uint64_t maxfilesize;
 
10aaf4c3
         if (map && (pcremode == PCRE_SCAN_FMAP)) {
03889c49
             if (offset+length >= map->len) {
045d184a
                 /* check that scanned map does not exceed pcre maxfilesize limit */
                 maxfilesize = (uint64_t)cl_engine_get_num(ctx->engine, CL_ENGINE_PCRE_MAX_FILESIZE, &rc);
                 if (rc != CL_SUCCESS)
                     return rc;
                 if (maxfilesize && (map->len > maxfilesize)) {
059ca614
                     cli_dbgmsg("matcher_run: pcre max filesize (map) exceeded (limit: %llu, needed: %llu)\n",
                                (long long unsigned)maxfilesize, (long long unsigned)map->len);
045d184a
                     return CL_EMAXSIZE;
                 }
 
03889c49
                 cli_dbgmsg("matcher_run: performing regex matching on full map: %u+%u(%u) >= %zu\n", offset, length, offset+length, map->len);
 
                 buffer = fmap_need_off_once(map, 0, map->len);
                 if (!buffer)
                     return CL_EMEM;
 
                 /* scan the full buffer */
2d785c96
                 ret = cli_pcre_scanbuf(buffer, map->len, virname, acres, root, mdata, poffdata, ctx);
03889c49
             }
a0efe9cf
         }
10aaf4c3
         else if (pcremode == PCRE_SCAN_BUFF) {
045d184a
             /* check that scanned buffer does not exceed pcre maxfilesize limit */
             maxfilesize = (uint64_t)cl_engine_get_num(ctx->engine, CL_ENGINE_PCRE_MAX_FILESIZE, &rc);
             if (rc != CL_SUCCESS)
                 return rc;
             if (maxfilesize && (length > maxfilesize)) {
059ca614
                 cli_dbgmsg("matcher_run: pcre max filesize (buf) exceeded (limit: %llu, needed: %u)\n", (long long unsigned)maxfilesize, length);
045d184a
                 return CL_EMAXSIZE;
             }
 
a0efe9cf
             cli_dbgmsg("matcher_run: performing regex matching on buffer with no map: %u+%u(%u)\n", offset, length, offset+length);
03889c49
             /* scan the specified buffer */
2d785c96
             ret = cli_pcre_scanbuf(buffer, length, virname, acres, root, mdata, poffdata, ctx);
a0efe9cf
         }
3c333c78
     }
 #endif /* HAVE_PCRE */
     /* end experimental fragment */
2d785c96
 
18ff5029
     if (ctx && !SCAN_ALLMATCHES && ret == CL_VIRUS) {
cbf5017a
         return cli_append_virus(ctx, *virname);
18ff5029
     }
     if (ctx && SCAN_ALLMATCHES && viruses_found) {
2d785c96
         return CL_VIRUS;
18ff5029
     }
     if (saved_ret && ret == CL_CLEAN) {
91e495f1
         return saved_ret;
18ff5029
     }
 
2a94c08e
     return ret;
 }
53721687
 
e06afe8e
 int cli_scanbuff(const unsigned char *buffer, uint32_t length, uint32_t offset, cli_ctx *ctx, cli_file_t ftype, struct cli_ac_data **acdata)
8000d078
 {
bedc58de
 	int ret = CL_CLEAN;
2c0fa85f
 	unsigned int i = 0, j = 0, viruses_found = 0;
4e9ab8ed
 	struct cli_ac_data mdata;
5612732c
 	struct cli_matcher *groot, *troot = NULL;
6ad45a29
 	const char *virname = NULL;
2ac2095a
 	const struct cl_engine *engine=ctx->engine;
e3aaff8e
 
5612732c
     if(!engine) {
 	cli_errmsg("cli_scanbuff: engine == NULL\n");
 	return CL_ENULLARG;
     }
 
     groot = engine->root[0]; /* generic signatures */
 
     if(ftype) {
2c0fa85f
         for(i = 1; i < CLI_MTARGETS; i++) {
             for (j = 0; j < cli_mtargets[i].target_count; ++j) {
                 if(cli_mtargets[i].target[j] == ftype) {
                     troot = ctx->engine->root[i];
                     break;
                 }
             }
             if (troot) break;
         }
5612732c
     }
 
     if(troot) {
 
aca9ea82
 	if(!acdata && (ret = cli_ac_initdata(&mdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
4e9ab8ed
 	    return ret;
5612732c
 
a80453e6
 	ret = matcher_run(troot, buffer, length, &virname, acdata ? (acdata[0]): (&mdata), offset, NULL, ftype, NULL, AC_SCAN_VIR, PCRE_SCAN_BUFF, NULL, *ctx->fmap, NULL, NULL, ctx);
5612732c
 
01386195
 	if(!acdata)
 	    cli_ac_freedata(&mdata);
5612732c
 
aec1e3be
 	if(ret == CL_EMEM)
5612732c
 	    return ret;
aec1e3be
 	if(ret == CL_VIRUS) {
 	    viruses_found = 1;
d7979d4f
 	    if(ctx && !SCAN_ALLMATCHES) {
aec1e3be
 		return ret;
 	    }
 	}
5612732c
     }
 
6ad45a29
     virname = NULL;
 
aca9ea82
     if(!acdata && (ret = cli_ac_initdata(&mdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)))
4e9ab8ed
 	return ret;
e3aaff8e
 
a80453e6
     ret = matcher_run(groot, buffer, length, &virname, acdata ? (acdata[1]): (&mdata), offset, NULL, ftype, NULL, AC_SCAN_VIR, PCRE_SCAN_BUFF, NULL, *ctx->fmap, NULL, NULL, ctx);
4e9ab8ed
 
01386195
     if(!acdata)
 	cli_ac_freedata(&mdata);
8000d078
 
aec1e3be
     if(viruses_found)
 	return CL_VIRUS;
8000d078
     return ret;
e3aaff8e
 }
 
33872a43
 /*
  * offdata[0]: type
  * offdata[1]: offset value
  * offdata[2]: max shift
  * offdata[3]: section number
  */
294558a5
 int cli_caloff(const char *offstr, const struct cli_target_info *info, unsigned int target, uint32_t *offdata, uint32_t *offset_min, uint32_t *offset_max)
7ec67e94
 {
33872a43
 	char offcpy[65];
bda5598b
 	unsigned int n, val;
33872a43
 	char *pt;
e06afe8e
 
33872a43
     if(!info) { /* decode offset string */
 	if(!offstr) {
 	    cli_errmsg("cli_caloff: offstr == NULL\n");
 	    return CL_ENULLARG;
 	}
e06afe8e
 
33872a43
 	if(!strcmp(offstr, "*")) {
 	    offdata[0] = *offset_max = *offset_min = CLI_OFF_ANY;
 	    return CL_SUCCESS;
 	}
e06afe8e
 
33872a43
 	if(strlen(offstr) > 64) {
 	    cli_errmsg("cli_caloff: Offset string too long\n");
 	    return CL_EMALFDB;
 	}
 	strcpy(offcpy, offstr);
841161e0
 
33872a43
 	if((pt = strchr(offcpy, ','))) {
 	    if(!cli_isnumber(pt + 1)) {
 		cli_errmsg("cli_caloff: Invalid offset shift value\n");
 		return CL_EMALFDB;
 	    }
 	    offdata[2] = atoi(pt + 1);
 	    *pt = 0;
 	} else {
 	    offdata[2] = 0;
 	}
841161e0
 
33872a43
 	*offset_max = *offset_min = CLI_OFF_NONE;
841161e0
 
33872a43
 	if(!strncmp(offcpy, "EP+", 3) || !strncmp(offcpy, "EP-", 3)) {
 	    if(offcpy[2] == '+')
 		offdata[0] = CLI_OFF_EP_PLUS;
 	    else
 		offdata[0] = CLI_OFF_EP_MINUS;
841161e0
 
33872a43
 	    if(!cli_isnumber(&offcpy[3])) {
 		cli_errmsg("cli_caloff: Invalid offset value\n");
 		return CL_EMALFDB;
 	    }
 	    offdata[1] = atoi(&offcpy[3]);
 
 	} else if(offcpy[0] == 'S') {
e067b3b4
 	    if(offcpy[1] == 'E') {
 		if(!cli_isnumber(&offcpy[2])) {
 		    cli_errmsg("cli_caloff: Invalid section number\n");
 		    return CL_EMALFDB;
 		}
 		offdata[0] = CLI_OFF_SE;
 		offdata[3] = atoi(&offcpy[2]);
 
 	    } else if(!strncmp(offstr, "SL+", 3)) {
33872a43
 		offdata[0] = CLI_OFF_SL_PLUS;
 		if(!cli_isnumber(&offcpy[3])) {
 		    cli_errmsg("cli_caloff: Invalid offset value\n");
 		    return CL_EMALFDB;
841161e0
 		}
33872a43
 		offdata[1] = atoi(&offcpy[3]);
 
 	    } else if(sscanf(offcpy, "S%u+%u", &n, &val) == 2) {
 		offdata[0] = CLI_OFF_SX_PLUS;
 		offdata[1] = val;
 		offdata[3] = n;
 	    } else {
 		cli_errmsg("cli_caloff: Invalid offset string\n");
 		return CL_EMALFDB;
 	    }
841161e0
 
33872a43
 	} else if(!strncmp(offcpy, "EOF-", 4)) {
 	    offdata[0] = CLI_OFF_EOF_MINUS;
 	    if(!cli_isnumber(&offcpy[4])) {
 		cli_errmsg("cli_caloff: Invalid offset value\n");
 		return CL_EMALFDB;
841161e0
 	    }
33872a43
 	    offdata[1] = atoi(&offcpy[4]);
d2ba6f98
 	} else if(!strncmp(offcpy, "VI", 2)) {
 	    /* versioninfo */
 	    offdata[0] = CLI_OFF_VERSION;
ab893605
 	} else if (strchr(offcpy, '$')) {
 	    if (sscanf(offcpy, "$%u$", &n) != 1) {
 		cli_errmsg("cli_caloff: Invalid macro($) in offset: %s\n", offcpy);
 		return CL_EMALFDB;
 	    }
 	    if (n >= 32) {
 		cli_errmsg("cli_caloff: at most 32 macro groups supported\n");
 		return CL_EMALFDB;
 	    }
 	    offdata[0] = CLI_OFF_MACRO;
 	    offdata[1] = n;
33872a43
 	} else {
 	    offdata[0] = CLI_OFF_ABSOLUTE;
 	    if(!cli_isnumber(offcpy)) {
 		cli_errmsg("cli_caloff: Invalid offset value\n");
 		return CL_EMALFDB;
 	    }
 	    *offset_min = offdata[1] = atoi(offcpy);
 	    *offset_max = *offset_min + offdata[2];
841161e0
 	}
4d1136fe
 
ab893605
 	if(offdata[0] != CLI_OFF_ANY && offdata[0] != CLI_OFF_ABSOLUTE &&
 	   offdata[0] != CLI_OFF_EOF_MINUS && offdata[0] != CLI_OFF_MACRO) {
33872a43
 	    if(target != 1 && target != 6 && target != 9) {
 		cli_errmsg("cli_caloff: Invalid offset type for target %u\n", target);
 		return CL_EMALFDB;
 	    }
 	}
7ec67e94
 
33872a43
     } else {
 	/* calculate relative offsets */
e067b3b4
 	*offset_min = CLI_OFF_NONE;
 	if(offset_max)
 	    *offset_max = CLI_OFF_NONE;
 	if(info->status == -1)
33872a43
 	    return CL_SUCCESS;
7ec67e94
 
33872a43
 	switch(offdata[0]) {
 	    case CLI_OFF_EOF_MINUS:
 		*offset_min = info->fsize - offdata[1];
 		break;
399bd596
 
33872a43
 	    case CLI_OFF_EP_PLUS:
 		*offset_min = info->exeinfo.ep + offdata[1];
 		break;
7ec67e94
 
33872a43
 	    case CLI_OFF_EP_MINUS:
 		*offset_min = info->exeinfo.ep - offdata[1];
 		break;
 
 	    case CLI_OFF_SL_PLUS:
 		*offset_min = info->exeinfo.section[info->exeinfo.nsections - 1].raw + offdata[1];
 		break;
 
 	    case CLI_OFF_SX_PLUS:
 		if(offdata[3] >= info->exeinfo.nsections)
006f5fe6
 		    *offset_min = CLI_OFF_NONE;
33872a43
 		else
 		    *offset_min = info->exeinfo.section[offdata[3]].raw + offdata[1];
 		break;
e067b3b4
 
 	    case CLI_OFF_SE:
 		if(offdata[3] >= info->exeinfo.nsections) {
 		    *offset_min = CLI_OFF_NONE;
 		} else {
 		    *offset_min = info->exeinfo.section[offdata[3]].raw;
b0d2122c
             if (offset_max)
 		        *offset_max = *offset_min + info->exeinfo.section[offdata[3]].rsz + offdata[2];
e067b3b4
 		}
 		break;
 
d2ba6f98
 	    case CLI_OFF_VERSION:
b0d2122c
         if (offset_max)
d2ba6f98
 		*offset_min = *offset_max = CLI_OFF_ANY;
 		break;
33872a43
 	    default:
 		cli_errmsg("cli_caloff: Not a relative offset (type: %u)\n", offdata[0]);
 		return CL_EARG;
841161e0
 	}
7ec67e94
 
e067b3b4
 	if(offset_max && *offset_max == CLI_OFF_NONE && *offset_min != CLI_OFF_NONE)
 	    *offset_max = *offset_min + offdata[2];
7ec67e94
     }
 
33872a43
     return CL_SUCCESS;
7ec67e94
 }
 
c1206103
 void cli_targetinfo(struct cli_target_info *info, unsigned int target, fmap_t *map)
294558a5
 {
 	int (*einfo)(fmap_t *, struct cli_exe_info *) = NULL;
 
 
     memset(info, 0, sizeof(struct cli_target_info));
     info->fsize = map->len;
04ec2e19
     cli_hashset_init_noalloc(&info->exeinfo.vinfo);
294558a5
 
     if(target == 1)
 	einfo = cli_peheader;
     else if(target == 6)
 	einfo = cli_elfheader;
     else if(target == 9)
 	einfo = cli_machoheader;
     else return;
 
     if(einfo(map, &info->exeinfo))
 	info->status = -1;
     else
 	info->status = 1;
 }
 
32b1e04e
 int cli_checkfp(unsigned char *digest, size_t size, cli_ctx *ctx)
db65451b
 {
cbf5017a
     return cli_checkfp_virus(digest, size, ctx, NULL);
 }
 
 int cli_checkfp_virus(unsigned char *digest, size_t size, cli_ctx *ctx, const char * vname)
 {
06bd94f0
     char md5[33];
     unsigned int i;
     const char *virname=NULL;
     fmap_t *map;
     const char *ptr;
     uint8_t shash1[SHA1_HASH_SIZE*2+1];
     uint8_t shash256[SHA256_HASH_SIZE*2+1];
     int have_sha1, have_sha256, do_dsig_check = 1;
3c29ca0b
     stats_section_t sections;
db65451b
 
b99de64c
     if(cli_hm_scan(digest, size, &virname, ctx->engine->hm_fp, CLI_HASH_MD5) == CL_VIRUS) {
06bd94f0
         cli_dbgmsg("cli_checkfp(md5): Found false positive detection (fp sig: %s), size: %d\n", virname, (int)size);
         return CL_CLEAN;
2296ab0f
     }
e37613ad
     else if(cli_hm_scan_wild(digest, &virname, ctx->engine->hm_fp, CLI_HASH_MD5) == CL_VIRUS) {
06bd94f0
         cli_dbgmsg("cli_checkfp(md5): Found false positive detection (fp sig: %s), size: *\n", virname);
         return CL_CLEAN;
e37613ad
     }
2296ab0f
 
640c07cf
     if(cli_debug_flag || ctx->engine->cb_hash) {
06bd94f0
         for(i = 0; i < 16; i++)
             sprintf(md5 + i * 2, "%02x", digest[i]);
         md5[32] = 0;
         cli_dbgmsg("FP SIGNATURE: %s:%u:%s\n", md5, (unsigned int) size,
cbf5017a
                    vname ? vname : "Name");
640c07cf
     }
 
cbf5017a
     if(vname)
         do_dsig_check = strncmp("W32S.", vname, 5);
16b28d07
 
b99de64c
     map = *ctx->fmap;
e37613ad
     have_sha1 = cli_hm_have_size(ctx->engine->hm_fp, CLI_HASH_SHA1, size)
06bd94f0
      || cli_hm_have_wild(ctx->engine->hm_fp, CLI_HASH_SHA1)
      || (cli_hm_have_size(ctx->engine->hm_fp, CLI_HASH_SHA1, 1) && do_dsig_check);
e37613ad
     have_sha256 = cli_hm_have_size(ctx->engine->hm_fp, CLI_HASH_SHA256, size)
06bd94f0
      || cli_hm_have_wild(ctx->engine->hm_fp, CLI_HASH_SHA256);
b99de64c
     if(have_sha1 || have_sha256) {
06bd94f0
         if((ptr = fmap_need_off_once(map, 0, size))) {
             if(have_sha1) {
3a8c4e48
                 cl_sha1(ptr, size, &shash1[SHA1_HASH_SIZE], NULL);
06bd94f0
 
                 if(cli_hm_scan(&shash1[SHA1_HASH_SIZE], size, &virname, ctx->engine->hm_fp, CLI_HASH_SHA1) == CL_VIRUS) {
                     cli_dbgmsg("cli_checkfp(sha1): Found false positive detection (fp sig: %s)\n", virname);
                     return CL_CLEAN;
                 }
                 if(cli_hm_scan_wild(&shash1[SHA1_HASH_SIZE], &virname, ctx->engine->hm_fp, CLI_HASH_SHA1) == CL_VIRUS) {
                     cli_dbgmsg("cli_checkfp(sha1): Found false positive detection (fp sig: %s)\n", virname);
                     return CL_CLEAN;
                 }
                 if(do_dsig_check && cli_hm_scan(&shash1[SHA1_HASH_SIZE], 1, &virname, ctx->engine->hm_fp, CLI_HASH_SHA1) == CL_VIRUS) {
                     cli_dbgmsg("cli_checkfp(sha1): Found false positive detection via catalog file\n");
                     return CL_CLEAN;
                 }
             }
 
             if(have_sha256) {
3a8c4e48
                 cl_sha256(ptr, size, &shash256[SHA256_HASH_SIZE], NULL);
06bd94f0
 
                 if(cli_hm_scan(&shash256[SHA256_HASH_SIZE], size, &virname, ctx->engine->hm_fp, CLI_HASH_SHA256) == CL_VIRUS) {
                     cli_dbgmsg("cli_checkfp(sha256): Found false positive detection (fp sig: %s)\n", virname);
                     return CL_CLEAN;
                 }
                 if(cli_hm_scan_wild(&shash256[SHA256_HASH_SIZE], &virname, ctx->engine->hm_fp, CLI_HASH_SHA256) == CL_VIRUS) {
                     cli_dbgmsg("cli_checkfp(sha256): Found false positive detection (fp sig: %s)\n", virname);
                     return CL_CLEAN;
                 }
             }
         }
b99de64c
     }
7dfd90ec
 
16b28d07
 #ifdef HAVE__INTERNAL__SHA_COLLECT
d7979d4f
     if(SCAN_DEV_COLLECT_SHA && (ctx->sha_collect > 0)) {
16b28d07
         if((ptr = fmap_need_off_once(map, 0, size))) {
b2e7c931
             if(!have_sha256)
                 cl_sha256(ptr, size, shash256+SHA256_HASH_SIZE, NULL);
06bd94f0
 
16b28d07
             for(i=0; i<SHA256_HASH_SIZE; i++)
                 sprintf((char *)shash256+i*2, "%02x", shash256[SHA256_HASH_SIZE+i]);
 
b2e7c931
             if(!have_sha1)
                 cl_sha1(ptr, size, shash1+SHA1_HASH_SIZE);
06bd94f0
 
16b28d07
             for(i=0; i<SHA1_HASH_SIZE; i++)
                 sprintf((char *)shash1+i*2, "%02x", shash1[SHA1_HASH_SIZE+i]);
 
d39cb658
             if (NULL == ctx->target_filepath) {
                 cli_errmsg("COLLECT:%s:%s:%u:%s:%s\n", shash256, shash1, size, vname?vname:"noname", "NO_IDEA");
             } else {
                 cli_errmsg("COLLECT:%s:%s:%u:%s:%s\n", shash256, shash1, size, vname?vname:"noname", ctx->target_filepath);
             }
16b28d07
         } else
             cli_errmsg("can't compute sha\n!");
06bd94f0
 
16b28d07
         ctx->sha_collect = -1;
     }
 #endif
 
3c29ca0b
     memset(&sections, 0x00, sizeof(stats_section_t));
     if(do_dsig_check || ctx->engine->cb_stats_add_sample) {
e8a1a905
         uint32_t flags = (do_dsig_check ? CL_CHECKFP_PE_FLAG_AUTHENTICODE : 0);
d4f90ad4
         if (!(ctx->engine->engine_options & ENGINE_OPTIONS_DISABLE_PE_STATS) && !(ctx->engine->dconf->stats & (DCONF_STATS_DISABLED | DCONF_STATS_PE_SECTION_DISABLED)))
e8a1a905
             flags |= CL_CHECKFP_PE_FLAG_STATS;
3c29ca0b
 
64ecd109
         switch(cli_checkfp_pe(ctx, &sections, flags)) {
06bd94f0
         case CL_CLEAN:
64ecd109
             cli_dbgmsg("cli_checkfp(pe): PE file whitelisted due to valid digital signature\n");
4ef79cfc
             if (sections.sections)
                 free(sections.sections);
06bd94f0
             return CL_CLEAN;
64ecd109
         default:
             break;
06bd94f0
         }
7dfd90ec
     }
06bd94f0
 
769f37a6
     if (ctx->engine->cb_hash)
cbf5017a
         ctx->engine->cb_hash(fmap_fd(*ctx->fmap), size, (const unsigned char *)md5, vname?vname:"noname", ctx->cb_ctx);
edbba730
 
f2571e34
     if (ctx->engine->cb_stats_add_sample)
cbf5017a
         ctx->engine->cb_stats_add_sample(vname?vname:"noname", digest, size, &sections, ctx->engine->stats_data);
f2571e34
 
e6bcbd5a
     if (sections.sections)
         free(sections.sections);
 
32b1e04e
     return CL_VIRUS;
db65451b
 }
 
453d8180
 static int matchicon(cli_ctx *ctx, struct cli_exe_info *exeinfo, const char *grp1, const char *grp2)
0f7ba617
 {
e7da4f65
     icon_groupset iconset;
0f7ba617
 
453d8180
     if(!ctx ||
        !ctx->engine ||
        !ctx->engine->iconcheck ||
        !ctx->engine->iconcheck->group_counts[0] ||
        !ctx->engine->iconcheck->group_counts[1] ||
        !exeinfo->res_addr
     ) return CL_CLEAN;
 
e7da4f65
     if (!(ctx->dconf->pe & PE_CONF_MATCHICON))
         return CL_CLEAN;
 
0f7ba617
     cli_icongroupset_init(&iconset);
     cli_icongroupset_add(grp1 ? grp1 : "*", &iconset, 0, ctx);
     cli_icongroupset_add(grp2 ? grp2 : "*", &iconset, 1, ctx);
453d8180
     return cli_scanicon(&iconset, exeinfo->res_addr, ctx, exeinfo->section, exeinfo->nsections, exeinfo->hdr_size);
0f7ba617
 }
 
1dae00eb
 int32_t cli_bcapi_matchicon(struct cli_bc_ctx *ctx , const uint8_t* grp1, int32_t grp1len,
 			    const uint8_t* grp2, int32_t grp2len)
 {
7882e721
     int ret;
1dae00eb
     char group1[128], group2[128];
762d46e8
     const char **oldvirname;
7882e721
     struct cli_exe_info info;
 
762d46e8
     if (!ctx->hooks.pedata->ep) {
1dae00eb
 	cli_dbgmsg("bytecode: matchicon only works with PE files\n");
 	return -1;
     }
661e8e3f
     if ((size_t) grp1len > sizeof(group1)-1 ||
 	(size_t) grp2len > sizeof(group2)-1)
1dae00eb
 	return -1;
e865b2d8
     oldvirname = ((cli_ctx*)ctx->ctx)->virname;
762d46e8
     ((cli_ctx*)ctx->ctx)->virname = NULL;
1dae00eb
     memcpy(group1, grp1, grp1len);
     memcpy(group2, grp2, grp2len);
     group1[grp1len] = 0;
762d46e8
     group2[grp2len] = 0;
7882e721
     memset(&info, 0, sizeof(info));
4abbeb3a
     if (ctx->bc->kind == BC_PE_UNPACKER || ctx->bc->kind == BC_PE_ALL) {
762d46e8
 	if(le16_to_host(ctx->hooks.pedata->file_hdr.Characteristics) & 0x2000 ||
 	   !ctx->hooks.pedata->dirs[2].Size)
 	    info.res_addr = 0;
 	else
 	    info.res_addr = le32_to_host(ctx->hooks.pedata->dirs[2].VirtualAddress);
     } else
 	info.res_addr = ctx->resaddr; /* from target_info */
a8935f90
     info.section = (struct cli_exe_section*)ctx->sections;
1dae00eb
     info.nsections = ctx->hooks.pedata->nsections;
     info.hdr_size = ctx->hooks.pedata->hdr_size;
762d46e8
     cli_dbgmsg("bytecode matchicon %s %s\n", group1, group2);
     ret = matchicon(ctx->ctx, &info, group1[0] ? group1 : NULL,
 		    group2[0] ? group2 : NULL);
e865b2d8
     ((cli_ctx*)ctx->ctx)->virname = oldvirname;
7882e721
     return ret;
1dae00eb
 }
 
453d8180
 
ffa9b060
 int cli_scandesc(int desc, cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli_matched_type **ftoffset, unsigned int acmode, struct cli_ac_result **acres)
e3aaff8e
 {
a0c38197
     int ret = CL_EMEM, empty;
49cc1e3c
     fmap_t *map = *ctx->fmap;
fc8e8ca2
 
a0c38197
     if((*ctx->fmap = fmap_check_empty(desc, 0, 0, &empty))) {
ffa9b060
 	ret = cli_fmap_scandesc(ctx, ftype, ftonly, ftoffset, acmode, acres, NULL);
742b3a0e
 	map->dont_cache_flag = (*ctx->fmap)->dont_cache_flag;
49cc1e3c
 	funmap(*ctx->fmap);
fc8e8ca2
     }
048d7677
     *ctx->fmap = map;
a0c38197
     if(empty)
 	return CL_CLEAN;
048d7677
     return ret;
fc8e8ca2
 }
cceea458
 
87b2a1a9
 static int intermediates_eval(cli_ctx *ctx, struct cli_ac_lsig *ac_lsig)
 {
     uint32_t i, icnt = ac_lsig->tdb.intermediates[0];
     int32_t j = -1;
 
     if (ctx->recursion < icnt)
         return 0;
 
     for (i = icnt; i > 0; i--) {
         if (ac_lsig->tdb.intermediates[i] == CL_TYPE_ANY)
             continue;
167c0079
         if (ac_lsig->tdb.intermediates[i] != cli_get_container_intermediate(ctx, j--))
87b2a1a9
             return 0;
     }
     return 1;
 }
 
9de40055
 static int lsig_eval(cli_ctx *ctx, struct cli_matcher *root, struct cli_ac_data *acdata, struct cli_target_info *target_info, const char *hash, uint32_t lsid)
60dbee52
 {
9de40055
     unsigned evalcnt = 0;
     uint64_t evalids = 0;
     fmap_t *map = *ctx->fmap;
     struct cli_ac_lsig *ac_lsig = root->ac_lsigtable[lsid];
     char * exp = ac_lsig->u.logic;
     char* exp_end = exp + strlen(exp);
b7999b89
     int rc;
60dbee52
 
b7999b89
     rc = cli_ac_chkmacro(root, acdata, lsid);
     if (rc != CL_SUCCESS)
         return rc;
9de40055
     if (cli_ac_chklsig(exp, exp_end, acdata->lsigcnt[lsid], &evalcnt, &evalids, 0) == 1) {
167c0079
         if(ac_lsig->tdb.container && ac_lsig->tdb.container[0] != cli_get_container(ctx, -1))
9de40055
             return CL_CLEAN;
87b2a1a9
         if(ac_lsig->tdb.intermediates && !intermediates_eval(ctx, ac_lsig))
             return CL_CLEAN;
9de40055
         if(ac_lsig->tdb.filesize && (ac_lsig->tdb.filesize[0] > map->len || ac_lsig->tdb.filesize[1] < map->len))
             return CL_CLEAN;
2445be8c
 
9de40055
         if(ac_lsig->tdb.ep || ac_lsig->tdb.nos) {
             if(!target_info || target_info->status != 1)
                 return CL_CLEAN;
             if(ac_lsig->tdb.ep && (ac_lsig->tdb.ep[0] > target_info->exeinfo.ep || ac_lsig->tdb.ep[1] < target_info->exeinfo.ep))
                 return CL_CLEAN;
             if(ac_lsig->tdb.nos && (ac_lsig->tdb.nos[0] > target_info->exeinfo.nsections || ac_lsig->tdb.nos[1] < target_info->exeinfo.nsections))
                 return CL_CLEAN;
         }
7770d314
 
9de40055
         if(hash && ac_lsig->tdb.handlertype) {
             if(memcmp(ctx->handlertype_hash, hash, 16)) {
                 ctx->recursion++;
                 memcpy(ctx->handlertype_hash, hash, 16);
                 if(cli_magic_scandesc_type(ctx, ac_lsig->tdb.handlertype[0]) == CL_VIRUS) {
                     ctx->recursion--;
                     return CL_VIRUS;
                 }
                 ctx->recursion--;
                 return CL_CLEAN;
             }
         }
         
         if(ac_lsig->tdb.icongrp1 || ac_lsig->tdb.icongrp2) {
             if(!target_info || target_info->status != 1)
                 return CL_CLEAN;
             if(matchicon(ctx, &target_info->exeinfo, ac_lsig->tdb.icongrp1, ac_lsig->tdb.icongrp2) == CL_VIRUS) {
                 if(!ac_lsig->bc_idx) {
cbf5017a
                     rc = cli_append_virus(ctx, ac_lsig->virname);
                     if (rc != CL_CLEAN)
                         return rc;
9de40055
                 } else if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, ac_lsig->bc_idx, acdata->lsigcnt[lsid], acdata->lsigsuboff_first[lsid], map) == CL_VIRUS) {
                     return CL_VIRUS;
                 }
             }
             return CL_CLEAN;
         }
         if(!ac_lsig->bc_idx) {
cbf5017a
             rc = cli_append_virus(ctx, ac_lsig->virname);
             if (rc != CL_CLEAN)
                 return rc;
9de40055
         }
         if(cli_bytecode_runlsig(ctx, target_info, &ctx->engine->bcs, ac_lsig->bc_idx, acdata->lsigcnt[lsid], acdata->lsigsuboff_first[lsid], map) == CL_VIRUS) {
             return CL_VIRUS;
         }
     }
     
     return CL_CLEAN;
 }
 
baeb6253
 #ifdef HAVE_YARA
9de40055
 static int yara_eval(cli_ctx *ctx, struct cli_matcher *root, struct cli_ac_data *acdata, struct cli_target_info *target_info, const char *hash, uint32_t lsid)
 {
d2554980
     struct cli_ac_lsig *ac_lsig = root->ac_lsigtable[lsid];
7665e02d
     int rc;
d0cba11e
     YR_SCAN_CONTEXT context;
 
     (void)hash;
c436e3b7
  
d0cba11e
     memset(&context, 0, sizeof(YR_SCAN_CONTEXT));
484edf66
     context.fmap = *ctx->fmap;
     context.file_size = (*ctx->fmap)->len;
c436e3b7
     if (target_info != NULL) {
         if (target_info->status == 1)   
             context.entry_point = target_info->exeinfo.ep;
     }
 
d2554980
     rc = yr_execute_code(ac_lsig, acdata, &context, 0, 0);
 
7665e02d
     if (rc == CL_VIRUS) {
         if (ac_lsig->flag & CLI_LSIG_FLAG_PRIVATE) {
             rc = CL_CLEAN;
         } else {
cbf5017a
             rc = cli_append_virus(ctx, ac_lsig->virname);
7665e02d
         }
     }
d2554980
     return rc;
9de40055
 }
baeb6253
 #endif
9de40055
 
 int cli_exp_eval(cli_ctx *ctx, struct cli_matcher *root, struct cli_ac_data *acdata, struct cli_target_info *target_info, const char *hash)
 {
     uint8_t viruses_found = 0;
     uint32_t i;
e85c7775
     int32_t rc = CL_SUCCESS;
9de40055
 
     for(i = 0; i < root->ac_lsigs; i++) {
b7999b89
         if (root->ac_lsigtable[i]->type == CLI_LSIG_NORMAL)
9de40055
             rc = lsig_eval(ctx, root, acdata, target_info, hash, i);
baeb6253
 #ifdef HAVE_YARA
b7999b89
         else if (root->ac_lsigtable[i]->type == CLI_YARA_NORMAL || root->ac_lsigtable[i]->type == CLI_YARA_OFFSET)
9de40055
             rc = yara_eval(ctx, root, acdata, target_info, hash, i);
baeb6253
 #endif
9de40055
         if (rc == CL_VIRUS) {
             viruses_found = 1;
d7979d4f
             if (SCAN_ALLMATCHES)
9de40055
                 continue;
             break;
         }
60dbee52
     }
9de40055
     if (viruses_found)
6ad45a29
 	return CL_VIRUS;
60dbee52
     return CL_CLEAN;
 }
cceea458
 
ffa9b060
 int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli_matched_type **ftoffset, unsigned int acmode, struct cli_ac_result **acres, unsigned char *refhash)
cceea458
 {
b2e7c931
     const unsigned char *buff;
d0cba11e
     int ret = CL_CLEAN, type = CL_CLEAN, compute_hash[CLI_HASH_AVAIL_TYPES];
2c0fa85f
     unsigned int i = 0, j = 0, bm_offmode = 0;
d0cba11e
     uint32_t maxpatlen, bytes, offset = 0;
b2e7c931
     struct cli_ac_data gdata, tdata;
     struct cli_bm_off toff;
3c333c78
     struct cli_pcre_off gpoff, tpoff;
b2e7c931
     unsigned char digest[CLI_HASH_AVAIL_TYPES][32];
     struct cli_matcher *groot = NULL, *troot = NULL;
     struct cli_target_info info;
     fmap_t *map = *ctx->fmap;
     struct cli_matcher *hdb, *fp;
     const char *virname = NULL;
     uint32_t viruses_found = 0;
da6e06dd
     void *md5ctx, *sha1ctx, *sha256ctx;
888f5794
 
3c91998b
     if(!ctx->engine) {
b2e7c931
         cli_errmsg("cli_scandesc: engine == NULL\n");
         return CL_ENULLARG;
     }
 
da6e06dd
     md5ctx = cl_hash_init("md5");
f077c617
     if (!(md5ctx))
         return CL_EMEM;
 
da6e06dd
     sha1ctx = cl_hash_init("sha1");
f077c617
     if (!(sha1ctx)) {
da6e06dd
         cl_hash_destroy(md5ctx);
f077c617
         return CL_EMEM;
     }
 
da6e06dd
     sha256ctx = cl_hash_init("sha256");
f077c617
     if (!(sha256ctx)) {
da6e06dd
         cl_hash_destroy(md5ctx);
         cl_hash_destroy(sha1ctx);
f077c617
         return CL_EMEM;
     }
 
73218de2
     if(!ftonly)
b2e7c931
         groot = ctx->engine->root[0]; /* generic signatures */
5612732c
 
     if(ftype) {
b2e7c931
         for(i = 1; i < CLI_MTARGETS; i++) {
2c0fa85f
             for (j = 0; j < cli_mtargets[i].target_count; ++j) {
                 if(cli_mtargets[i].target[j] == ftype) {
                     troot = ctx->engine->root[i];
                     break;
                 }
b2e7c931
             }
2c0fa85f
             if (troot) break;
b2e7c931
         }
5612732c
     }
 
73218de2
     if(ftonly) {
a1cbd793
         if(!troot) {
da6e06dd
             cl_hash_destroy(md5ctx);
             cl_hash_destroy(sha1ctx);
             cl_hash_destroy(sha256ctx);
b2e7c931
             return CL_CLEAN;
a1cbd793
         }
73218de2
 
b2e7c931
         maxpatlen = troot->maxpatlen;
73218de2
     } else {
b2e7c931
         if(troot)
             maxpatlen = MAX(troot->maxpatlen, groot->maxpatlen);
         else
             maxpatlen = groot->maxpatlen;
73218de2
     }
5612732c
 
c1206103
     cli_targetinfo(&info, i, map);
294558a5
 
b2e7c931
     if(!ftonly) {
5e572e2f
         if((ret = cli_ac_initdata(&gdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)) || (ret = cli_ac_caloff(groot, &gdata, &info))) {
b2e7c931
             if(info.exeinfo.section)
                 free(info.exeinfo.section);
 
             cli_hashset_destroy(&info.exeinfo.vinfo);
da6e06dd
             cl_hash_destroy(md5ctx);
             cl_hash_destroy(sha1ctx);
             cl_hash_destroy(sha256ctx);
b2e7c931
             return ret;
         }
5e572e2f
         if((ret = cli_pcre_recaloff(groot, &gpoff, &info, ctx))) {
             cli_ac_freedata(&gdata);
             if(info.exeinfo.section)
                 free(info.exeinfo.section);
 
             cli_hashset_destroy(&info.exeinfo.vinfo);
             cl_hash_destroy(md5ctx);
             cl_hash_destroy(sha1ctx);
             cl_hash_destroy(sha256ctx);
             return ret;
 
         }
b2e7c931
     }
e3aaff8e
 
5612732c
     if(troot) {
5e572e2f
         if((ret = cli_ac_initdata(&tdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN)) || (ret = cli_ac_caloff(troot, &tdata, &info))) {
3c333c78
             if(!ftonly) {
b2e7c931
                 cli_ac_freedata(&gdata);
3c333c78
                 cli_pcre_freeoff(&gpoff);
             }
b2e7c931
             if(info.exeinfo.section)
                 free(info.exeinfo.section);
 
             cli_hashset_destroy(&info.exeinfo.vinfo);
da6e06dd
             cl_hash_destroy(md5ctx);
             cl_hash_destroy(sha1ctx);
             cl_hash_destroy(sha256ctx);
b2e7c931
             return ret;
         }
         if(troot->bm_offmode) {
             if(map->len >= CLI_DEFAULT_BM_OFFMODE_FSIZE) {
                 if((ret = cli_bm_initoff(troot, &toff, &info))) {
3c333c78
                     if(!ftonly) {
b2e7c931
                         cli_ac_freedata(&gdata);
3c333c78
                         cli_pcre_freeoff(&gpoff);
                     }
b2e7c931
 
                     cli_ac_freedata(&tdata);
                     if(info.exeinfo.section)
                         free(info.exeinfo.section);
 
                     cli_hashset_destroy(&info.exeinfo.vinfo);
da6e06dd
                     cl_hash_destroy(md5ctx);
                     cl_hash_destroy(sha1ctx);
                     cl_hash_destroy(sha256ctx);
b2e7c931
                     return ret;
                 }
 
                 bm_offmode = 1;
             }
         }
5e572e2f
         if ((ret = cli_pcre_recaloff(troot, &tpoff, &info, ctx))) {
             if(!ftonly) {
                 cli_ac_freedata(&gdata);
                 cli_pcre_freeoff(&gpoff);
             }
 
             cli_ac_freedata(&tdata);
             if(bm_offmode)
                 cli_bm_freeoff(&toff);
             if(info.exeinfo.section)
                 free(info.exeinfo.section);
 
             cli_hashset_destroy(&info.exeinfo.vinfo);
             cl_hash_destroy(md5ctx);
             cl_hash_destroy(sha1ctx);
             cl_hash_destroy(sha256ctx);
             return ret;
         }
5612732c
     }
 
c32c98e5
     hdb = ctx->engine->hm_hdb;
     fp = ctx->engine->hm_fp;
 
     if(!ftonly && hdb) {
b2e7c931
         if(!refhash) {
5c89a90b
             if(cli_hm_have_size(hdb, CLI_HASH_MD5, map->len) || cli_hm_have_size(fp, CLI_HASH_MD5, map->len)
                || cli_hm_have_wild(hdb, CLI_HASH_MD5) || cli_hm_have_wild(fp, CLI_HASH_MD5)) {
b2e7c931
                 compute_hash[CLI_HASH_MD5] = 1;
             } else {
                 compute_hash[CLI_HASH_MD5] = 0;
             }
         } else {
             compute_hash[CLI_HASH_MD5] = 0;
             memcpy(digest[CLI_HASH_MD5], refhash, 16);
         }
 
         if(cli_hm_have_size(hdb, CLI_HASH_SHA1, map->len) || cli_hm_have_wild(hdb, CLI_HASH_SHA1)
             || cli_hm_have_size(fp, CLI_HASH_SHA1, map->len) || cli_hm_have_wild(fp, CLI_HASH_SHA1) ) {
             compute_hash[CLI_HASH_SHA1] = 1;
         } else {
             compute_hash[CLI_HASH_SHA1] = 0;
         }
c32c98e5
 
b2e7c931
         if(cli_hm_have_size(hdb, CLI_HASH_SHA256, map->len) || cli_hm_have_wild(hdb, CLI_HASH_SHA256)
             || cli_hm_have_size(fp, CLI_HASH_SHA256, map->len) || cli_hm_have_wild(fp, CLI_HASH_SHA256)) {
             compute_hash[CLI_HASH_SHA256] = 1;
         } else {
             compute_hash[CLI_HASH_SHA256] = 0;
         }
c32c98e5
     }
e3aaff8e
 
084d19aa
     while(offset < map->len) {
b2e7c931
         bytes = MIN(map->len - offset, SCANBUFF);
         if(!(buff = fmap_need_off_once(map, offset, bytes)))
             break;
         if(ctx->scanned)
             *ctx->scanned += bytes / CL_COUNT_PRECISION;
 
         if(troot) {
                 virname = NULL;
a80453e6
                 ret = matcher_run(troot, buff, bytes, &virname, &tdata, offset, &info, ftype, ftoffset, acmode, PCRE_SCAN_FMAP, acres, map, bm_offmode ? &toff : NULL, &tpoff, ctx);
b2e7c931
 
             if (virname) {
                 /* virname already appended by matcher_run */
                 viruses_found = 1;
             }
d7979d4f
             if((ret == CL_VIRUS && !SCAN_ALLMATCHES) || ret == CL_EMEM) {
3c333c78
                 if(!ftonly) {
b2e7c931
                     cli_ac_freedata(&gdata);
3c333c78
                     cli_pcre_freeoff(&gpoff);
                 }
b2e7c931
 
                 cli_ac_freedata(&tdata);
                 if(bm_offmode)
                     cli_bm_freeoff(&toff);
5e572e2f
                 cli_pcre_freeoff(&tpoff);
b2e7c931
 
                 if(info.exeinfo.section)
                     free(info.exeinfo.section);
 
                 cli_hashset_destroy(&info.exeinfo.vinfo);
da6e06dd
                 cl_hash_destroy(md5ctx);
                 cl_hash_destroy(sha1ctx);
                 cl_hash_destroy(sha256ctx);
b2e7c931
                 return ret;
             }
         }
e3aaff8e
 
b2e7c931
         if(!ftonly) {
6ad45a29
             virname = NULL;
a80453e6
             ret = matcher_run(groot, buff, bytes, &virname, &gdata, offset, &info, ftype, ftoffset, acmode, PCRE_SCAN_FMAP, acres, map, NULL, &gpoff, ctx);
83fa5305
 
b2e7c931
             if (virname) {
                 /* virname already appended by matcher_run */
                 viruses_found = 1;
             }
d7979d4f
             if((ret == CL_VIRUS && !SCAN_ALLMATCHES) || ret == CL_EMEM) {
b2e7c931
                 cli_ac_freedata(&gdata);
3c333c78
                 cli_pcre_freeoff(&gpoff);
b2e7c931
                 if(troot) {
                     cli_ac_freedata(&tdata);
                     if(bm_offmode)
                         cli_bm_freeoff(&toff);
5e572e2f
                     cli_pcre_freeoff(&tpoff);
b2e7c931
                 }
db65451b
 
b2e7c931
                 if(info.exeinfo.section)
                     free(info.exeinfo.section);
294558a5
 
b2e7c931
                 cli_hashset_destroy(&info.exeinfo.vinfo);
ab71073f
                 cl_hash_destroy(md5ctx);
                 cl_hash_destroy(sha1ctx);
                 cl_hash_destroy(sha256ctx);
b2e7c931
                 return ret;
             } else if((acmode & AC_SCAN_FT) && ret >= CL_TYPENO) {
                 if(ret > type)
                     type = ret;
             }
e3aaff8e
 
05fa78fd
             /* if (bytes <= (maxpatlen * (offset!=0))), it means the last window finished the file hashing *
              *   since the last window is responsible for adding intersection between windows (maxpatlen)  */
             if(hdb && (bytes > (maxpatlen * (offset!=0)))) {
b2e7c931
                 const void *data = buff + maxpatlen * (offset!=0);
                 uint32_t data_len = bytes - maxpatlen * (offset!=0);
c32c98e5
 
b2e7c931
                 if(compute_hash[CLI_HASH_MD5])
cd94be7a
                     cl_update_hash(md5ctx, (void *)data, data_len);
b2e7c931
                 if(compute_hash[CLI_HASH_SHA1])
cd94be7a
                     cl_update_hash(sha1ctx, (void *)data, data_len);
b2e7c931
                 if(compute_hash[CLI_HASH_SHA256])
cd94be7a
                     cl_update_hash(sha256ctx, (void *)data, data_len);
b2e7c931
             }
         }
0710165d
 
b2e7c931
         if(bytes < SCANBUFF)
             break;
 
         offset += bytes - maxpatlen;
8000d078
     }
084ee140
 
c32c98e5
     if(!ftonly && hdb) {
b2e7c931
         enum CLI_HASH_TYPE hashtype, hashtype2;
 
a1cbd793
         if(compute_hash[CLI_HASH_MD5]) {
da6e06dd
             cl_finish_hash(md5ctx, digest[CLI_HASH_MD5]);
             md5ctx = NULL;
a1cbd793
         }
b2e7c931
         if(refhash)
             compute_hash[CLI_HASH_MD5] = 1;
a1cbd793
         if(compute_hash[CLI_HASH_SHA1]) {
da6e06dd
             cl_finish_hash(sha1ctx, digest[CLI_HASH_SHA1]);
             sha1ctx = NULL;
a1cbd793
         }
         if(compute_hash[CLI_HASH_SHA256]) {
da6e06dd
             cl_finish_hash(sha256ctx, digest[CLI_HASH_SHA256]);
             sha256ctx = NULL;
a1cbd793
         }
b2e7c931
 
         virname = NULL;
         for(hashtype = CLI_HASH_MD5; hashtype < CLI_HASH_AVAIL_TYPES; hashtype++) {
             const char * virname_w = NULL;
             int found = 0;
 
             /* If no hash, skip to next type */
             if(!compute_hash[hashtype])
                 continue;
 
             /* Do hash scan */
             if((ret = cli_hm_scan(digest[hashtype], map->len, &virname, hdb, hashtype)) == CL_VIRUS) {
                 found += 1;
             }
d7979d4f
             if(!found || SCAN_ALLMATCHES) {
b2e7c931
                 if ((ret = cli_hm_scan_wild(digest[hashtype], &virname_w, hdb, hashtype)) == CL_VIRUS)
                     found += 2;
             }
e37613ad
 
b2e7c931
             /* If found, do immediate hash-only FP check */
             if (found && fp) {
                 for(hashtype2 = CLI_HASH_MD5; hashtype2 < CLI_HASH_AVAIL_TYPES; hashtype2++) {
                     if(!compute_hash[hashtype2])
                         continue;
                     if(cli_hm_scan(digest[hashtype2], map->len, NULL, fp, hashtype2) == CL_VIRUS) {
                         found = 0;
                         ret = CL_CLEAN;
                         break;
                     }
                     else if(cli_hm_scan_wild(digest[hashtype2], NULL, fp, hashtype2) == CL_VIRUS) {
                         found = 0;
                         ret = CL_CLEAN;
                         break;
                     }
                 }
             }
e37613ad
 
b2e7c931
             /* If matched size-based hash ... */
             if (found % 2) {
                 viruses_found = 1;
cbf5017a
                 ret = cli_append_virus(ctx, virname);
d7979d4f
                 if (!SCAN_ALLMATCHES || ret != CL_CLEAN)
b2e7c931
                     break;
                 virname = NULL;
             }
             /* If matched size-agnostic hash ... */
             if (found > 1) {
                 viruses_found = 1;
cbf5017a
                 ret = cli_append_virus(ctx, virname_w);
d7979d4f
                 if (!SCAN_ALLMATCHES || ret != CL_CLEAN)
b2e7c931
                     break;
cbf5017a
              }
b2e7c931
         }
b240ee01
     }
 
da6e06dd
     cl_hash_destroy(md5ctx);
     cl_hash_destroy(sha1ctx);
     cl_hash_destroy(sha256ctx);
a1cbd793
 
c7aeeb46
     if(troot) {
d7979d4f
         if(ret != CL_VIRUS || SCAN_ALLMATCHES)
9de40055
             ret = cli_exp_eval(ctx, troot, &tdata, &info, (const char *)refhash);
b2e7c931
         if (ret == CL_VIRUS)
             viruses_found++;
 
         cli_ac_freedata(&tdata);
         if(bm_offmode)
             cli_bm_freeoff(&toff);
5e572e2f
         cli_pcre_freeoff(&tpoff);
18ff5029
 
c7aeeb46
     }
 
     if(groot) {
d7979d4f
         if(ret != CL_VIRUS || SCAN_ALLMATCHES)
9de40055
             ret = cli_exp_eval(ctx, groot, &gdata, &info, (const char *)refhash);
b2e7c931
         cli_ac_freedata(&gdata);
3c333c78
         cli_pcre_freeoff(&gpoff);
c7aeeb46
     }
 
2445be8c
     if(info.exeinfo.section)
b2e7c931
         free(info.exeinfo.section);
 
04ec2e19
     cli_hashset_destroy(&info.exeinfo.vinfo);
2445be8c
 
dfa92896
     if (SCAN_ALLMATCHES && viruses_found) {
b2e7c931
         return CL_VIRUS;
18ff5029
     }
     if(ret == CL_VIRUS) {
b2e7c931
         return CL_VIRUS;
18ff5029
     }
fc8e8ca2
 
6038397e
     return (acmode & AC_SCAN_FT) ? type : CL_CLEAN;
fc8e8ca2
 }
55094a9c
 
b33354e5
 int cli_matchmeta(cli_ctx *ctx, const char *fname, size_t fsizec, size_t fsizer, int encrypted, unsigned int filepos, int res1, void *res2)
55094a9c
 {
 	const struct cli_cdb *cdb;
6ad45a29
 	unsigned int viruses_found = 0;
cbf5017a
         int ret = CL_CLEAN;
55094a9c
 
059ca614
     cli_dbgmsg("CDBNAME:%s:%llu:%s:%llu:%llu:%d:%u:%u:%p\n",
167c0079
 	       cli_ftname(cli_get_container(ctx, -1)), (long long unsigned)fsizec, fname, (long long unsigned)fsizec, (long long unsigned)fsizer,
059ca614
 	       encrypted, filepos, res1, res2);
e86311ab
 
     if (ctx->engine && ctx->engine->cb_meta)
167c0079
 	if (ctx->engine->cb_meta(cli_ftname(cli_get_container(ctx, -1)), fsizec, fname, fsizer, encrypted, filepos, ctx->cb_ctx) == CL_VIRUS) {
e86311ab
 	    cli_dbgmsg("inner file blacklisted by callback: %s\n", fname);
6ad45a29
 
cbf5017a
 	    ret = cli_append_virus(ctx, "Detected.By.Callback");
6ad45a29
 	    viruses_found++;
d7979d4f
 	    if(!SCAN_ALLMATCHES || ret != CL_CLEAN)
cbf5017a
 		return ret;
e86311ab
 	}
 
b0d2122c
     if(!ctx->engine || !(cdb = ctx->engine->cdb))
55094a9c
 	return CL_CLEAN;
 
     do {
167c0079
 	if(cdb->ctype != CL_TYPE_ANY && cdb->ctype != cli_get_container(ctx, -1))
55094a9c
 	    continue;
 
 	if(cdb->encrypted != 2 && cdb->encrypted != encrypted)
 	    continue;
 
15f413d1
 	if(cdb->res1 && (cdb->ctype == CL_TYPE_ZIP || cdb->ctype == CL_TYPE_RAR) && cdb->res1 != res1)
 	    continue;
55094a9c
 
d0cba11e
     #define CDBRANGE(field, val)                                              \
         if (field[0] != CLI_OFF_ANY)                                          \
         {                                                                     \
             if (field[0] == field[1] && field[0] != val)                      \
                 continue;                                                     \
             else if (field[0] != field[1] && ((field[0] && field[0] > val) || \
                                             (field[1] && field[1] < val)))    \
                 continue;                                                     \
         }
55094a9c
 
d0cba11e
     CDBRANGE(cdb->csize, cli_get_container_size(ctx, -1));
15f413d1
 	CDBRANGE(cdb->fsizec, fsizec);
 	CDBRANGE(cdb->fsizer, fsizer);
 	CDBRANGE(cdb->filepos, filepos);
55094a9c
 
 	if(cdb->name.re_magic && (!fname || cli_regexec(&cdb->name, fname, 0, NULL, 0) == REG_NOMATCH))
 	    continue;
 
cbf5017a
 	ret = cli_append_virus(ctx, cdb->virname);
6ad45a29
 	viruses_found++;
d7979d4f
 	if(!SCAN_ALLMATCHES || ret != CL_CLEAN)
cbf5017a
 	    return ret;
55094a9c
 
     } while((cdb = cdb->next));
 
d7979d4f
     if (SCAN_ALLMATCHES && viruses_found)
6ad45a29
 	return CL_VIRUS;
55094a9c
     return CL_CLEAN;
 }