|
|
|
@ -45,6 +45,7 @@ |
|
|
|
|
#include "cltypes.h" |
|
|
|
|
#include "default.h" |
|
|
|
|
#include "macho.h" |
|
|
|
|
#include "fmap.h" |
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
@ -76,7 +77,7 @@ int cli_scanbuff(const unsigned char *buffer, uint32_t length, uint32_t offset, |
|
|
|
|
if(!acdata && (ret = cli_ac_initdata(&mdata, troot->ac_partsigs, troot->ac_lsigs, troot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN))) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
if(troot->ac_only || (ret = cli_bm_scanbuff(buffer, length, virname, troot, offset, -1)) != CL_VIRUS) |
|
|
|
|
if(troot->ac_only || (ret = cli_bm_scanbuff(buffer, length, virname, troot, offset, NULL)) != CL_VIRUS) |
|
|
|
|
ret = cli_ac_scanbuff(buffer, length, virname, NULL, NULL, troot, acdata ? (acdata[0]) : (&mdata), offset, ftype, NULL, AC_SCAN_VIR, NULL); |
|
|
|
|
|
|
|
|
|
if(!acdata) |
|
|
|
@ -89,7 +90,7 @@ int cli_scanbuff(const unsigned char *buffer, uint32_t length, uint32_t offset, |
|
|
|
|
if(!acdata && (ret = cli_ac_initdata(&mdata, groot->ac_partsigs, groot->ac_lsigs, groot->ac_reloff_num, CLI_DEFAULT_AC_TRACKLEN))) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
if(groot->ac_only || (ret = cli_bm_scanbuff(buffer, length, virname, groot, offset, -1)) != CL_VIRUS) |
|
|
|
|
if(groot->ac_only || (ret = cli_bm_scanbuff(buffer, length, virname, groot, offset, NULL)) != CL_VIRUS) |
|
|
|
|
ret = cli_ac_scanbuff(buffer, length, virname, NULL, NULL, groot, acdata ? (acdata[1]) : (&mdata), offset, ftype, NULL, AC_SCAN_VIR, NULL); |
|
|
|
|
|
|
|
|
|
if(!acdata) |
|
|
|
@ -104,9 +105,9 @@ int cli_scanbuff(const unsigned char *buffer, uint32_t length, uint32_t offset, |
|
|
|
|
* offdata[2]: max shift |
|
|
|
|
* offdata[3]: section number |
|
|
|
|
*/ |
|
|
|
|
int cli_caloff(const char *offstr, struct cli_target_info *info, int fd, unsigned int target, uint32_t *offdata, uint32_t *offset_min, uint32_t *offset_max) |
|
|
|
|
int cli_caloff(const char *offstr, struct cli_target_info *info, struct F_MAP *map, unsigned int target, uint32_t *offdata, uint32_t *offset_min, uint32_t *offset_max) |
|
|
|
|
{ |
|
|
|
|
int (*einfo)(int, struct cli_exe_info *) = NULL; |
|
|
|
|
int (*einfo)(struct F_MAP *, struct cli_exe_info *) = NULL; |
|
|
|
|
char offcpy[65]; |
|
|
|
|
unsigned int n, val; |
|
|
|
|
char *pt; |
|
|
|
@ -206,14 +207,8 @@ int cli_caloff(const char *offstr, struct cli_target_info *info, int fd, unsigne |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if((offdata[0] == CLI_OFF_EOF_MINUS)) { |
|
|
|
|
if(!info->fsize) { |
|
|
|
|
if(fstat(fd, &sb) == -1) { |
|
|
|
|
cli_errmsg("cli_caloff: fstat(%d) failed\n", fd); |
|
|
|
|
return CL_ESTAT; |
|
|
|
|
} |
|
|
|
|
info->fsize = sb.st_size; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!info->fsize) |
|
|
|
|
info->fsize = map->len; |
|
|
|
|
} else if(!info->status) { |
|
|
|
|
if(target == 1) |
|
|
|
|
einfo = cli_peheader; |
|
|
|
@ -227,20 +222,12 @@ int cli_caloff(const char *offstr, struct cli_target_info *info, int fd, unsigne |
|
|
|
|
return CL_EMALFDB; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if((pos = lseek(fd, 0, SEEK_CUR)) == -1) { |
|
|
|
|
cli_errmsg("cli_caloff: lseek(%d) failed\n", fd); |
|
|
|
|
return CL_ESEEK; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
lseek(fd, 0, SEEK_SET); |
|
|
|
|
if(einfo(fd, &info->exeinfo)) { |
|
|
|
|
if(einfo(map, &info->exeinfo)) { |
|
|
|
|
/* einfo *may* fail */ |
|
|
|
|
lseek(fd, pos, SEEK_SET); |
|
|
|
|
info->status = -1; |
|
|
|
|
*offset_min = *offset_max = 0; |
|
|
|
|
return CL_SUCCESS; |
|
|
|
|
} |
|
|
|
|
lseek(fd, pos, SEEK_SET); |
|
|
|
|
info->status = 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -303,7 +290,7 @@ int cli_checkfp(int fd, cli_ctx *ctx) |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(cli_bm_scanbuff(digest, 16, &virname, ctx->engine->md5_fp, 0, -1) == CL_VIRUS) { |
|
|
|
|
if(cli_bm_scanbuff(digest, 16, &virname, ctx->engine->md5_fp, 0, NULL) == CL_VIRUS) { |
|
|
|
|
cli_dbgmsg("cli_checkfp(): Found false positive detection (fp sig: %s)\n", virname); |
|
|
|
|
free(digest); |
|
|
|
|
lseek(fd, pos, SEEK_SET); |
|
|
|
@ -318,185 +305,15 @@ int cli_checkfp(int fd, cli_ctx *ctx) |
|
|
|
|
|
|
|
|
|
int cli_scandesc(int desc, cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli_matched_type **ftoffset, unsigned int acmode) |
|
|
|
|
{ |
|
|
|
|
unsigned char *buffer, *buff, *endbl, *upt; |
|
|
|
|
int ret = CL_CLEAN, type = CL_CLEAN, bytes; |
|
|
|
|
unsigned int i, evalcnt; |
|
|
|
|
uint32_t buffersize, length, maxpatlen, shift = 0, offset = 0; |
|
|
|
|
uint64_t evalids; |
|
|
|
|
struct cli_ac_data gdata, tdata; |
|
|
|
|
cli_md5_ctx md5ctx; |
|
|
|
|
unsigned char digest[16]; |
|
|
|
|
struct cli_matcher *groot = NULL, *troot = NULL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!ctx->engine) { |
|
|
|
|
cli_errmsg("cli_scandesc: engine == NULL\n"); |
|
|
|
|
return CL_ENULLARG; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!ftonly) |
|
|
|
|
groot = ctx->engine->root[0]; /* generic signatures */ |
|
|
|
|
|
|
|
|
|
if(ftype) { |
|
|
|
|
for(i = 1; i < CLI_MTARGETS; i++) { |
|
|
|
|
if(cli_mtargets[i].target == ftype) { |
|
|
|
|
troot = ctx->engine->root[i]; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(ftonly) { |
|
|
|
|
if(!troot) |
|
|
|
|
return CL_CLEAN; |
|
|
|
|
|
|
|
|
|
maxpatlen = troot->maxpatlen; |
|
|
|
|
} else { |
|
|
|
|
if(troot) |
|
|
|
|
maxpatlen = MAX(troot->maxpatlen, groot->maxpatlen); |
|
|
|
|
else |
|
|
|
|
maxpatlen = groot->maxpatlen; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* prepare the buffer */ |
|
|
|
|
buffersize = maxpatlen + SCANBUFF; |
|
|
|
|
if(!(buffer = (unsigned char *) cli_calloc(buffersize, sizeof(unsigned char)))) { |
|
|
|
|
cli_dbgmsg("cli_scandesc(): unable to cli_calloc(%u)\n", buffersize); |
|
|
|
|
return CL_EMEM; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!ftonly) |
|
|
|
|
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, desc))) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
if(troot) { |
|
|
|
|
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, desc))) { |
|
|
|
|
if(!ftonly) |
|
|
|
|
cli_ac_freedata(&gdata); |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!ftonly && ctx->engine->md5_hdb) |
|
|
|
|
cli_md5_init(&md5ctx); |
|
|
|
|
|
|
|
|
|
buff = buffer; |
|
|
|
|
buff += maxpatlen; /* pointer to read data block */ |
|
|
|
|
endbl = buff + SCANBUFF - maxpatlen; /* pointer to the last block
|
|
|
|
|
* length of maxpatlen |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
upt = buff; |
|
|
|
|
while((bytes = cli_readn(desc, buff + shift, SCANBUFF - shift)) > 0) { |
|
|
|
|
|
|
|
|
|
if(ctx->scanned) |
|
|
|
|
*ctx->scanned += bytes / CL_COUNT_PRECISION; |
|
|
|
|
|
|
|
|
|
length = shift + bytes; |
|
|
|
|
if(upt == buffer) |
|
|
|
|
length += maxpatlen; |
|
|
|
|
|
|
|
|
|
if(troot) { |
|
|
|
|
if(troot->ac_only || (ret = cli_bm_scanbuff(upt, length, ctx->virname, troot, offset, desc)) != CL_VIRUS) |
|
|
|
|
ret = cli_ac_scanbuff(upt, length, ctx->virname, NULL, NULL, troot, &tdata, offset, ftype, ftoffset, acmode, NULL); |
|
|
|
|
|
|
|
|
|
if(ret == CL_VIRUS) { |
|
|
|
|
free(buffer); |
|
|
|
|
if(!ftonly) |
|
|
|
|
cli_ac_freedata(&gdata); |
|
|
|
|
cli_ac_freedata(&tdata); |
|
|
|
|
|
|
|
|
|
if(cli_checkfp(desc, ctx)) |
|
|
|
|
return CL_CLEAN; |
|
|
|
|
else |
|
|
|
|
return CL_VIRUS; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!ftonly) { |
|
|
|
|
if(groot->ac_only || (ret = cli_bm_scanbuff(upt, length, ctx->virname, groot, offset, desc)) != CL_VIRUS) |
|
|
|
|
ret = cli_ac_scanbuff(upt, length, ctx->virname, NULL, NULL, groot, &gdata, offset, ftype, ftoffset, acmode, NULL); |
|
|
|
|
|
|
|
|
|
if(ret == CL_VIRUS) { |
|
|
|
|
free(buffer); |
|
|
|
|
cli_ac_freedata(&gdata); |
|
|
|
|
if(troot) |
|
|
|
|
cli_ac_freedata(&tdata); |
|
|
|
|
if(cli_checkfp(desc, ctx)) |
|
|
|
|
return CL_CLEAN; |
|
|
|
|
else |
|
|
|
|
return CL_VIRUS; |
|
|
|
|
|
|
|
|
|
} else if((acmode & AC_SCAN_FT) && ret >= CL_TYPENO) { |
|
|
|
|
if(ret > type) |
|
|
|
|
type = ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(ctx->engine->md5_hdb) |
|
|
|
|
cli_md5_update(&md5ctx, buff + shift, bytes); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(bytes + shift == SCANBUFF) { |
|
|
|
|
memmove(buffer, endbl, maxpatlen); |
|
|
|
|
offset += SCANBUFF; |
|
|
|
|
|
|
|
|
|
if(upt == buff) { |
|
|
|
|
upt = buffer; |
|
|
|
|
offset -= maxpatlen; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
shift = 0; |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
shift += bytes; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
free(buffer); |
|
|
|
|
|
|
|
|
|
if(troot) { |
|
|
|
|
for(i = 0; i < troot->ac_lsigs; i++) { |
|
|
|
|
evalcnt = 0; |
|
|
|
|
evalids = 0; |
|
|
|
|
if(cli_ac_chklsig(troot->ac_lsigtable[i]->logic, troot->ac_lsigtable[i]->logic + strlen(troot->ac_lsigtable[i]->logic), tdata.lsigcnt[i], &evalcnt, &evalids, 0) == 1) { |
|
|
|
|
if(ctx->virname) |
|
|
|
|
*ctx->virname = troot->ac_lsigtable[i]->virname; |
|
|
|
|
ret = CL_VIRUS; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
cli_ac_freedata(&tdata); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(groot) { |
|
|
|
|
if(ret != CL_VIRUS) for(i = 0; i < groot->ac_lsigs; i++) { |
|
|
|
|
evalcnt = 0; |
|
|
|
|
evalids = 0; |
|
|
|
|
if(cli_ac_chklsig(groot->ac_lsigtable[i]->logic, groot->ac_lsigtable[i]->logic + strlen(groot->ac_lsigtable[i]->logic), gdata.lsigcnt[i], &evalcnt, &evalids, 0) == 1) { |
|
|
|
|
if(ctx->virname) |
|
|
|
|
*ctx->virname = groot->ac_lsigtable[i]->virname; |
|
|
|
|
ret = CL_VIRUS; |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
cli_ac_freedata(&gdata); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(ret == CL_VIRUS) { |
|
|
|
|
lseek(desc, 0, SEEK_SET); |
|
|
|
|
if(cli_checkfp(desc, ctx)) |
|
|
|
|
return CL_CLEAN; |
|
|
|
|
else |
|
|
|
|
return CL_VIRUS; |
|
|
|
|
} |
|
|
|
|
int ret = CL_EMEM; |
|
|
|
|
struct F_MAP *map = *ctx->fmap; |
|
|
|
|
|
|
|
|
|
if(!ftonly && ctx->engine->md5_hdb) { |
|
|
|
|
cli_md5_final(digest, &md5ctx); |
|
|
|
|
if(cli_bm_scanbuff(digest, 16, ctx->virname, ctx->engine->md5_hdb, 0, -1) == CL_VIRUS && (cli_bm_scanbuff(digest, 16, NULL, ctx->engine->md5_fp, 0, -1) != CL_VIRUS)) |
|
|
|
|
return CL_VIRUS; |
|
|
|
|
if(!(*ctx->fmap = fmap(desc, 0, 0))) { |
|
|
|
|
ret = cli_fmap_scandesc(ctx, ftype, ftonly, ftoffset, acmode); |
|
|
|
|
fmunmap(map); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return (acmode & AC_SCAN_FT) ? type : CL_CLEAN; |
|
|
|
|
*ctx->fmap = map; |
|
|
|
|
return ret; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -543,11 +360,11 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!ftonly) |
|
|
|
|
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, map->fd))) |
|
|
|
|
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, map))) |
|
|
|
|
return ret; |
|
|
|
|
|
|
|
|
|
if(troot) { |
|
|
|
|
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, map->fd))) { |
|
|
|
|
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, map))) { |
|
|
|
|
if(!ftonly) |
|
|
|
|
cli_ac_freedata(&gdata); |
|
|
|
|
return ret; |
|
|
|
@ -566,7 +383,7 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli |
|
|
|
|
*ctx->scanned += bytes / CL_COUNT_PRECISION; |
|
|
|
|
|
|
|
|
|
if(troot) { |
|
|
|
|
if(troot->ac_only || (ret = cli_bm_scanbuff(buff, bytes, ctx->virname, troot, offset, map->fd)) != CL_VIRUS) |
|
|
|
|
if(troot->ac_only || (ret = cli_bm_scanbuff(buff, bytes, ctx->virname, troot, offset, map)) != CL_VIRUS) |
|
|
|
|
ret = cli_ac_scanbuff(buff, bytes, ctx->virname, NULL, NULL, troot, &tdata, offset, ftype, ftoffset, acmode, NULL); |
|
|
|
|
|
|
|
|
|
if(ret == CL_VIRUS) { |
|
|
|
@ -582,7 +399,7 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(!ftonly) { |
|
|
|
|
if(groot->ac_only || (ret = cli_bm_scanbuff(buff, bytes, ctx->virname, groot, offset, map->fd)) != CL_VIRUS) |
|
|
|
|
if(groot->ac_only || (ret = cli_bm_scanbuff(buff, bytes, ctx->virname, groot, offset, map)) != CL_VIRUS) |
|
|
|
|
ret = cli_ac_scanbuff(buff, bytes, ctx->virname, NULL, NULL, groot, &gdata, offset, ftype, ftoffset, acmode, NULL); |
|
|
|
|
|
|
|
|
|
if(ret == CL_VIRUS) { |
|
|
|
@ -645,7 +462,7 @@ int cli_fmap_scandesc(cli_ctx *ctx, cli_file_t ftype, uint8_t ftonly, struct cli |
|
|
|
|
|
|
|
|
|
if(!ftonly && ctx->engine->md5_hdb) { |
|
|
|
|
cli_md5_final(digest, &md5ctx); |
|
|
|
|
if(cli_bm_scanbuff(digest, 16, ctx->virname, ctx->engine->md5_hdb, 0, -1) == CL_VIRUS && (cli_bm_scanbuff(digest, 16, NULL, ctx->engine->md5_fp, 0, -1) != CL_VIRUS)) |
|
|
|
|
if(cli_bm_scanbuff(digest, 16, ctx->virname, ctx->engine->md5_hdb, 0, NULL) == CL_VIRUS && (cli_bm_scanbuff(digest, 16, NULL, ctx->engine->md5_fp, 0, NULL) != CL_VIRUS)) |
|
|
|
|
return CL_VIRUS; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|