suricata
detect-filesha1.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2016 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Victor Julien <victor@inliniac.net>
22 * \author Duarte Silva <duarte.silva@serializing.me>
23 *
24 */
25
26#include "suricata-common.h"
27
28#include "detect-engine.h"
30#include "util-unittest.h"
32
33#include "detect-filesha1.h"
34
35static int DetectFileSha1Setup (DetectEngineCtx *, Signature *, const char *);
36#ifdef UNITTESTS
37static void DetectFileSha1RegisterTests(void);
38#endif
39static int g_file_match_list_id = 0;
40
41/**
42 * \brief Registration function for keyword: filesha1
43 */
45{
47 sigmatch_table[DETECT_FILESHA1].desc = "match file SHA-1 against list of SHA-1 checksums";
48 sigmatch_table[DETECT_FILESHA1].url = "/rules/file-keywords.html#filesha1";
50 sigmatch_table[DETECT_FILESHA1].Setup = DetectFileSha1Setup;
52#ifdef UNITTESTS
53 sigmatch_table[DETECT_FILESHA1].RegisterTests = DetectFileSha1RegisterTests;
54#endif
55 g_file_match_list_id = DetectBufferTypeRegister("files");
56
57 SCLogDebug("registering filesha1 rule option");
58}
59
60/**
61 * \brief this function is used to parse filesha1 options
62 * \brief into the current signature
63 *
64 * \param de_ctx pointer to the Detection Engine Context
65 * \param s pointer to the Current Signature
66 * \param str pointer to the user provided "filesha1" option
67 */
68static int DetectFileSha1Setup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
69{
70 return DetectFileHashSetup(de_ctx, s, str, DETECT_FILESHA1, g_file_match_list_id);
71}
72
73#ifdef UNITTESTS
74static int SHA1MatchLookupString(ROHashTable *hash, const char *string)
75{
76 uint8_t sha1[20];
77 if (ReadHashString(sha1, string, "file", 88, 40) == 1) {
78 void *ptr = ROHashLookup(hash, &sha1, (uint16_t)sizeof(sha1));
79 if (ptr == NULL)
80 return 0;
81 else
82 return 1;
83 }
84 return 0;
85}
86
87static int SHA1MatchTest01(void)
88{
89 ROHashTable *hash = ROHashInit(4, 20);
90 FAIL_IF_NULL(hash);
91 FAIL_IF(LoadHashTable(hash, "447661c5de965bd4d837b50244467e37bddc184d", "file", 1,
92 DETECT_FILESHA1) != 1);
93 FAIL_IF(LoadHashTable(hash, "75a9af1e34dc0bb2f7fcde9d56b2503072ac35dd", "file", 2,
94 DETECT_FILESHA1) != 1);
95 FAIL_IF(LoadHashTable(hash, "53224a297bbb30631670fdcd2d295d87a1d328e9", "file", 3,
96 DETECT_FILESHA1) != 1);
97 FAIL_IF(LoadHashTable(hash, "3395856ce81f2b7382dee72602f798b642f14140", "file", 4,
98 DETECT_FILESHA1) != 1);
99 FAIL_IF(LoadHashTable(hash, "65559245709fe98052eb284577f1fd61c01ad20d", "file", 5,
100 DETECT_FILESHA1) != 1);
101 FAIL_IF(LoadHashTable(hash, "0931fd4e05e6ea81c75f8488ecc1db9e66f22cbb", "file", 6,
102 DETECT_FILESHA1) != 1);
103
104 FAIL_IF(ROHashInitFinalize(hash) != 1);
105
106 FAIL_IF(SHA1MatchLookupString(hash, "447661c5de965bd4d837b50244467e37bddc184d") != 1);
107 FAIL_IF(SHA1MatchLookupString(hash, "75a9af1e34dc0bb2f7fcde9d56b2503072ac35dd") != 1);
108 FAIL_IF(SHA1MatchLookupString(hash, "53224a297bbb30631670fdcd2d295d87a1d328e9") != 1);
109 FAIL_IF(SHA1MatchLookupString(hash, "3395856ce81f2b7382dee72602f798b642f14140") != 1);
110 FAIL_IF(SHA1MatchLookupString(hash, "65559245709fe98052eb284577f1fd61c01ad20d") != 1);
111 FAIL_IF(SHA1MatchLookupString(hash, "0931fd4e05e6ea81c75f8488ecc1db9e66f22cbb") != 1);
112 /* Shouldn't match */
113 FAIL_IF(SHA1MatchLookupString(hash, "3333333333333333333333333333333333333333") == 1);
114
115 ROHashFree(hash);
116 PASS;
117}
118
119static void DetectFileSha1RegisterTests(void)
120{
121 UtRegisterTest("SHA1MatchTest01", SHA1MatchTest01);
122}
123#endif
int DetectBufferTypeRegister(const char *name)
int ReadHashString(uint8_t *hash, const char *string, const char *filename, int line_no, uint16_t expected_len)
Read the bytes of a hash from an hexadecimal string.
int LoadHashTable(ROHashTable *hash_table, const char *string, const char *filename, int line_no, uint32_t type)
Store a hash into the hash table.
void DetectFileHashFree(DetectEngineCtx *de_ctx, void *ptr)
this function will free memory associated with DetectFileHashData
int DetectFileHashMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags, File *file, const Signature *s, const SigMatchCtx *m)
Match the specified file hash.
int DetectFileHashSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str, uint16_t type, int list)
this function is used to parse filemd5, filesha1 and filesha256 options
void DetectFileSha1Register(void)
Registration function for keyword: filesha1.
SigTableElmt * sigmatch_table
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
main detection engine ctx
Definition detect.h:932
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * name
Definition detect.h:1459
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition detect.h:1429
Signature container.
Definition detect.h:668
#define str(s)
#define SCLogDebug(...)
Definition util-debug.h:275
void * ROHashLookup(ROHashTable *table, void *data, uint16_t size)
void ROHashFree(ROHashTable *table)
Definition util-rohash.c:91
int ROHashInitFinalize(ROHashTable *table)
create final hash data structure
ROHashTable * ROHashInit(uint8_t hash_bits, uint16_t item_size)
initialize a new rohash
Definition util-rohash.c:64