2003-07-29 15:48:06 +00:00
|
|
|
/*
|
2013-12-05 15:09:19 -08:00
|
|
|
* Copyright (C) 2007-2013 Sourcefire, Inc.
|
2008-02-01 00:17:44 +00:00
|
|
|
*
|
2008-04-02 15:24:51 +00:00
|
|
|
* Authors: Tomasz Kojm
|
2003-07-29 15:48:06 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2007-03-31 20:31:04 +00:00
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
2003-07-29 15:48:06 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-04-09 19:59:28 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2003-07-29 15:48:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __MATCHER_H
|
|
|
|
#define __MATCHER_H
|
|
|
|
|
2006-11-20 00:03:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#include "clamav.h"
|
2006-01-15 15:46:36 +00:00
|
|
|
#include "filetypes.h"
|
2006-02-15 00:41:40 +00:00
|
|
|
#include "others.h"
|
2006-11-20 00:03:16 +00:00
|
|
|
#include "execs.h"
|
|
|
|
#include "cltypes.h"
|
2003-07-29 15:48:06 +00:00
|
|
|
|
2010-06-18 15:41:39 +02:00
|
|
|
struct cli_target_info {
|
|
|
|
off_t fsize;
|
|
|
|
struct cli_exe_info exeinfo;
|
|
|
|
int status; /* 0 == not initialised, 1 == initialised OK, -1 == error */
|
|
|
|
};
|
|
|
|
|
2007-03-28 21:38:07 +00:00
|
|
|
#include "matcher-ac.h"
|
|
|
|
#include "matcher-bm.h"
|
2011-01-07 02:59:41 +01:00
|
|
|
#include "matcher-hash.h"
|
2014-08-22 14:39:17 -04:00
|
|
|
#include "matcher-pcre.h"
|
|
|
|
#include "regex_pcre.h"
|
2009-09-01 13:49:36 +02:00
|
|
|
#include "fmap.h"
|
2008-10-17 17:00:13 +00:00
|
|
|
#include "mpool.h"
|
|
|
|
|
2015-02-05 20:52:18 -08:00
|
|
|
#define CLI_MATCH_METADATA 0xff00
|
|
|
|
#define CLI_MATCH_WILDCARD 0x0f00
|
2007-09-26 22:18:44 +00:00
|
|
|
#define CLI_MATCH_CHAR 0x0000
|
2015-02-05 20:52:18 -08:00
|
|
|
#define CLI_MATCH_NOCASE 0x1000
|
2007-03-28 21:38:07 +00:00
|
|
|
#define CLI_MATCH_IGNORE 0x0100
|
2009-09-11 16:05:50 +02:00
|
|
|
#define CLI_MATCH_SPECIAL 0x0200
|
2007-03-28 21:38:07 +00:00
|
|
|
#define CLI_MATCH_NIBBLE_HIGH 0x0300
|
|
|
|
#define CLI_MATCH_NIBBLE_LOW 0x0400
|
|
|
|
|
2008-07-25 19:00:25 +00:00
|
|
|
struct cli_lsig_tdb {
|
|
|
|
#define CLI_TDB_UINT 0
|
|
|
|
#define CLI_TDB_RANGE 1
|
|
|
|
#define CLI_TDB_STR 2
|
|
|
|
#define CLI_TDB_RANGE2 3
|
2010-01-12 14:41:44 +01:00
|
|
|
#define CLI_TDB_FTYPE 4
|
2008-07-25 19:00:25 +00:00
|
|
|
uint32_t *val, *range;
|
|
|
|
char *str;
|
|
|
|
uint32_t cnt[3];
|
2010-06-09 13:42:47 +03:00
|
|
|
uint32_t subsigs;
|
2008-07-25 19:00:25 +00:00
|
|
|
|
|
|
|
const uint32_t *target;
|
2009-12-30 00:20:02 +01:00
|
|
|
const uint32_t *engine, *nos, *ep, *filesize;
|
2010-08-24 12:28:16 +02:00
|
|
|
const uint32_t *container, *handlertype;
|
2009-12-14 17:16:46 +01:00
|
|
|
/*
|
2008-07-25 19:00:25 +00:00
|
|
|
const uint32_t *sectoff, *sectrva, *sectvsz, *sectraw, *sectrsz,
|
|
|
|
*secturva, *sectuvsz, *secturaw, *sectursz;
|
2009-12-14 17:16:46 +01:00
|
|
|
*/
|
|
|
|
const char *icongrp1, *icongrp2;
|
2010-02-08 13:45:03 +02:00
|
|
|
uint32_t *macro_ptids;
|
2008-10-19 16:16:49 +00:00
|
|
|
#ifdef USE_MPOOL
|
2009-01-26 19:47:02 +00:00
|
|
|
mpool_t *mempool;
|
2008-10-19 16:16:49 +00:00
|
|
|
#endif
|
2008-07-25 19:00:25 +00:00
|
|
|
};
|
|
|
|
|
2009-09-21 23:44:32 +03:00
|
|
|
struct cli_bc;
|
2008-07-25 19:00:25 +00:00
|
|
|
struct cli_ac_lsig {
|
|
|
|
uint32_t id;
|
2010-06-09 13:42:47 +03:00
|
|
|
unsigned bc_idx;
|
2008-07-25 19:00:25 +00:00
|
|
|
char *logic;
|
|
|
|
const char *virname;
|
|
|
|
struct cli_lsig_tdb tdb;
|
|
|
|
};
|
|
|
|
|
2007-03-28 21:38:07 +00:00
|
|
|
struct cli_matcher {
|
2009-08-14 14:38:13 +02:00
|
|
|
unsigned int type;
|
|
|
|
|
2007-03-28 21:38:07 +00:00
|
|
|
/* Extended Boyer-Moore */
|
2007-11-06 17:16:07 +00:00
|
|
|
uint8_t *bm_shift;
|
2009-09-01 11:19:31 +02:00
|
|
|
struct cli_bm_patt **bm_suffix, **bm_pattab;
|
2007-04-30 14:12:38 +00:00
|
|
|
uint32_t *soff, soff_len; /* for PE section sigs */
|
2009-09-01 11:19:31 +02:00
|
|
|
uint32_t bm_offmode, bm_patterns, bm_reloff_num, bm_absoff_num;
|
2007-03-28 21:38:07 +00:00
|
|
|
|
2011-01-14 16:12:43 +01:00
|
|
|
/* HASH */
|
2011-01-07 02:59:41 +01:00
|
|
|
struct cli_hash_patt hm;
|
2013-03-08 18:10:07 -05:00
|
|
|
struct cli_hash_wild hwild;
|
2011-01-07 02:59:41 +01:00
|
|
|
|
2007-03-28 21:38:07 +00:00
|
|
|
/* Extended Aho-Corasick */
|
2015-02-10 09:23:51 -08:00
|
|
|
uint32_t ac_partsigs, ac_nodes, ac_lists, ac_patterns, ac_lsigs;
|
2008-07-25 19:00:25 +00:00
|
|
|
struct cli_ac_lsig **ac_lsigtable;
|
2007-03-28 21:38:07 +00:00
|
|
|
struct cli_ac_node *ac_root, **ac_nodetable;
|
2015-02-10 09:23:51 -08:00
|
|
|
struct cli_ac_list **ac_listtable;
|
2007-04-28 18:40:59 +00:00
|
|
|
struct cli_ac_patt **ac_pattable;
|
2009-08-14 14:38:13 +02:00
|
|
|
struct cli_ac_patt **ac_reloff;
|
2009-08-21 15:55:10 +02:00
|
|
|
uint32_t ac_reloff_num, ac_absoff_num;
|
2008-01-24 13:24:02 +00:00
|
|
|
uint8_t ac_mindepth, ac_maxdepth;
|
2010-02-10 11:39:47 +02:00
|
|
|
struct filter *filter;
|
2008-01-24 13:24:02 +00:00
|
|
|
|
|
|
|
uint16_t maxpatlen;
|
2015-02-09 14:22:45 -08:00
|
|
|
uint8_t ac_only;
|
2014-08-22 14:39:17 -04:00
|
|
|
|
|
|
|
/* Perl-Compiled Regular Expressions */
|
2014-08-25 15:07:30 -04:00
|
|
|
#if HAVE_PCRE
|
2014-08-29 14:57:09 -04:00
|
|
|
uint32_t pcre_metas;
|
|
|
|
struct cli_pcre_meta **pcre_metatable;
|
2014-09-09 17:14:12 -04:00
|
|
|
uint32_t pcre_reloff_num, pcre_absoff_num;
|
2014-08-25 15:07:30 -04:00
|
|
|
#endif
|
2014-08-22 14:39:17 -04:00
|
|
|
|
2008-10-18 00:16:23 +00:00
|
|
|
#ifdef USE_MPOOL
|
2009-01-26 19:47:02 +00:00
|
|
|
mpool_t *mempool;
|
2008-10-18 00:16:23 +00:00
|
|
|
#endif
|
2007-03-28 21:38:07 +00:00
|
|
|
};
|
|
|
|
|
2010-01-07 18:26:12 +01:00
|
|
|
struct cli_cdb
|
|
|
|
{
|
|
|
|
char *virname; /* virus name */
|
|
|
|
cli_file_t ctype; /* container type */
|
|
|
|
regex_t name; /* filename regex */
|
|
|
|
size_t csize[2]; /* container size (min, max); if csize[0] != csize[1]
|
|
|
|
* then value of 0 makes the field ignored
|
|
|
|
*/
|
|
|
|
size_t fsizec[2]; /* file size in container */
|
|
|
|
size_t fsizer[2]; /* real file size */
|
|
|
|
int encrypted; /* file is encrypted; 2 == ignore */
|
2010-01-08 15:20:33 +01:00
|
|
|
int filepos[2]; /* file position in container */
|
|
|
|
int res1; /* reserved / format specific */
|
2010-01-07 18:26:12 +01:00
|
|
|
void *res2; /* reserved / format specific */
|
|
|
|
|
|
|
|
struct cli_cdb *next;
|
|
|
|
};
|
|
|
|
|
2014-02-21 16:10:32 -05:00
|
|
|
#define CLI_MAX_TARGETS 2 /* maximum filetypes for a specific target */
|
2008-02-01 00:17:44 +00:00
|
|
|
struct cli_mtarget {
|
2014-02-21 16:10:32 -05:00
|
|
|
cli_file_t target[CLI_MAX_TARGETS];
|
2008-02-01 00:17:44 +00:00
|
|
|
const char *name;
|
|
|
|
uint8_t idx; /* idx of matcher */
|
|
|
|
uint8_t ac_only;
|
2010-02-10 11:39:47 +02:00
|
|
|
uint8_t enable_prefiltering;
|
2014-02-21 16:10:32 -05:00
|
|
|
uint8_t target_count; /* must be synced with non-zero values in the target array */
|
2008-02-01 00:17:44 +00:00
|
|
|
};
|
|
|
|
|
2014-04-29 17:15:07 -04:00
|
|
|
#define CLI_MTARGETS 14
|
2008-02-01 00:17:44 +00:00
|
|
|
static const struct cli_mtarget cli_mtargets[CLI_MTARGETS] = {
|
2014-02-21 16:10:32 -05:00
|
|
|
{ {0, 0}, "GENERIC", 0, 0, 1, 1 },
|
|
|
|
{ {CL_TYPE_MSEXE, 0}, "PE", 1, 0, 1, 1 },
|
|
|
|
{ {CL_TYPE_MSOLE2, 0}, "OLE2", 2, 1, 0, 1 },
|
|
|
|
{ {CL_TYPE_HTML, 0}, "HTML", 3, 1, 0, 1 },
|
|
|
|
{ {CL_TYPE_MAIL, 0}, "MAIL", 4, 1, 1, 1 },
|
|
|
|
{ {CL_TYPE_GRAPHICS, 0}, "GRAPHICS", 5, 1, 0, 1 },
|
|
|
|
{ {CL_TYPE_ELF, 0}, "ELF", 6, 1, 0, 1 },
|
|
|
|
{ {CL_TYPE_TEXT_ASCII, 0}, "ASCII", 7, 1, 1, 1 },
|
|
|
|
{ {CL_TYPE_ERROR, 0}, "NOT USED", 8, 1, 0, 1 },
|
|
|
|
{ {CL_TYPE_MACHO, CL_TYPE_MACHO_UNIBIN}, "MACH-O", 9, 1, 0, 2 },
|
|
|
|
{ {CL_TYPE_PDF, 0}, "PDF", 10, 1, 0, 1 },
|
|
|
|
{ {CL_TYPE_SWF, 0}, "FLASH", 11, 1, 0, 1 },
|
2014-04-29 17:15:07 -04:00
|
|
|
{ {CL_TYPE_JAVA, 0}, "JAVA", 12, 1, 0, 1 },
|
|
|
|
{ {CL_TYPE_INTERNAL, 0}, "INTERNAL", 13, 1, 0, 1 }
|
2008-02-01 00:17:44 +00:00
|
|
|
};
|
2004-09-14 01:33:32 +00:00
|
|
|
|
2009-08-14 14:38:13 +02:00
|
|
|
#define CLI_OFF_ANY 0xffffffff
|
|
|
|
#define CLI_OFF_NONE 0xfffffffe
|
|
|
|
#define CLI_OFF_ABSOLUTE 1
|
|
|
|
#define CLI_OFF_EOF_MINUS 2
|
|
|
|
#define CLI_OFF_EP_PLUS 3
|
|
|
|
#define CLI_OFF_EP_MINUS 4
|
|
|
|
#define CLI_OFF_SL_PLUS 5
|
|
|
|
#define CLI_OFF_SX_PLUS 6
|
2010-01-04 14:56:04 +01:00
|
|
|
#define CLI_OFF_VERSION 7
|
2010-02-08 13:45:03 +02:00
|
|
|
#define CLI_OFF_MACRO 8
|
2011-12-09 16:54:43 +01:00
|
|
|
#define CLI_OFF_SE 9
|
2009-08-14 14:38:13 +02:00
|
|
|
|
2009-04-06 20:01:09 +00:00
|
|
|
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);
|
2005-09-23 02:23:36 +00:00
|
|
|
|
2010-06-10 12:24:26 +02:00
|
|
|
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);
|
|
|
|
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);
|
2011-01-03 16:42:04 +01:00
|
|
|
int cli_lsig_eval(cli_ctx *ctx, struct cli_matcher *root, struct cli_ac_data *acdata, struct cli_target_info *target_info, const char *hash);
|
2010-06-18 15:41:39 +02:00
|
|
|
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);
|
2006-12-02 00:09:02 +00:00
|
|
|
|
2010-02-14 12:29:37 +01:00
|
|
|
int cli_checkfp(unsigned char *digest, size_t size, cli_ctx *ctx);
|
2009-04-23 13:24:21 +00:00
|
|
|
|
2011-03-11 20:30:45 +01:00
|
|
|
int cli_matchmeta(cli_ctx *ctx, const char *fname, size_t fsizec, size_t fsizer, int encrypted, unsigned int filepos, int res1, void *res2);
|
2010-01-07 18:26:12 +01:00
|
|
|
|
2013-12-05 15:09:19 -08:00
|
|
|
void cli_targetinfo(struct cli_target_info *info, unsigned int target, fmap_t *map);
|
|
|
|
|
2003-07-29 15:48:06 +00:00
|
|
|
#endif
|