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"
31 #include "util-unittest-helper.h"
32 
33 #include "detect-filesha1.h"
34 
35 #ifndef HAVE_NSS
36 
37 static int DetectFileSha1SetupNoSupport (DetectEngineCtx *a, Signature *b, const char *c)
38 {
39  SCLogError(SC_ERR_NO_SHA1_SUPPORT, "no SHA-1 calculation support built in, needed for filesha1 keyword");
40  return -1;
41 }
42 
43 /**
44  * \brief Registration function for keyword: filesha1
45  */
47 {
48  sigmatch_table[DETECT_FILESHA1].name = "filesha1";
50  sigmatch_table[DETECT_FILESHA1].Setup = DetectFileSha1SetupNoSupport;
54 
55  SCLogDebug("registering filesha1 rule option");
56  return;
57 }
58 
59 #else /* HAVE_NSS */
60 
61 static int DetectFileSha1Setup (DetectEngineCtx *, Signature *, const char *);
62 static void DetectFileSha1RegisterTests(void);
63 static int g_file_match_list_id = 0;
64 
65 /**
66  * \brief Registration function for keyword: filesha1
67  */
68 void DetectFileSha1Register(void)
69 {
70  sigmatch_table[DETECT_FILESHA1].name = "filesha1";
71  sigmatch_table[DETECT_FILESHA1].desc = "match file SHA-1 against list of SHA-1 checksums";
72  sigmatch_table[DETECT_FILESHA1].url = DOC_URL DOC_VERSION "/rules/file-keywords.html#filesha1";
74  sigmatch_table[DETECT_FILESHA1].Setup = DetectFileSha1Setup;
76  sigmatch_table[DETECT_FILESHA1].RegisterTests = DetectFileSha1RegisterTests;
77 
78  g_file_match_list_id = DetectBufferTypeRegister("files");
79 
80  SCLogDebug("registering filesha1 rule option");
81  return;
82 }
83 
84 /**
85  * \brief this function is used to parse filesha1 options
86  * \brief into the current signature
87  *
88  * \param de_ctx pointer to the Detection Engine Context
89  * \param s pointer to the Current Signature
90  * \param str pointer to the user provided "filesha1" option
91  *
92  * \retval 0 on Success
93  * \retval -1 on Failure
94  */
95 static int DetectFileSha1Setup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
96 {
97  return DetectFileHashSetup(de_ctx, s, str, DETECT_FILESHA1, g_file_match_list_id);
98 }
99 
100 #ifdef UNITTESTS
101 static int SHA1MatchLookupString(ROHashTable *hash, const char *string)
102 {
103  uint8_t sha1[20];
104  if (ReadHashString(sha1, string, "file", 88, 40) == 1) {
105  void *ptr = ROHashLookup(hash, &sha1, (uint16_t)sizeof(sha1));
106  if (ptr == NULL)
107  return 0;
108  else
109  return 1;
110  }
111  return 0;
112 }
113 
114 static int SHA1MatchTest01(void)
115 {
116  ROHashTable *hash = ROHashInit(4, 20);
117  if (hash == NULL) {
118  return 0;
119  }
120  if (LoadHashTable(hash, "447661c5de965bd4d837b50244467e37bddc184d", "file", 1, DETECT_FILESHA1) != 1)
121  return 0;
122  if (LoadHashTable(hash, "75a9af1e34dc0bb2f7fcde9d56b2503072ac35dd", "file", 2, DETECT_FILESHA1) != 1)
123  return 0;
124  if (LoadHashTable(hash, "53224a297bbb30631670fdcd2d295d87a1d328e9", "file", 3, DETECT_FILESHA1) != 1)
125  return 0;
126  if (LoadHashTable(hash, "3395856ce81f2b7382dee72602f798b642f14140", "file", 4, DETECT_FILESHA1) != 1)
127  return 0;
128  if (LoadHashTable(hash, "65559245709fe98052eb284577f1fd61c01ad20d", "file", 5, DETECT_FILESHA1) != 1)
129  return 0;
130  if (LoadHashTable(hash, "0931fd4e05e6ea81c75f8488ecc1db9e66f22cbb", "file", 6, DETECT_FILESHA1) != 1)
131  return 0;
132 
133  if (ROHashInitFinalize(hash) != 1) {
134  return 0;
135  }
136 
137  if (SHA1MatchLookupString(hash, "447661c5de965bd4d837b50244467e37bddc184d") != 1)
138  return 0;
139  if (SHA1MatchLookupString(hash, "75a9af1e34dc0bb2f7fcde9d56b2503072ac35dd") != 1)
140  return 0;
141  if (SHA1MatchLookupString(hash, "53224a297bbb30631670fdcd2d295d87a1d328e9") != 1)
142  return 0;
143  if (SHA1MatchLookupString(hash, "3395856ce81f2b7382dee72602f798b642f14140") != 1)
144  return 0;
145  if (SHA1MatchLookupString(hash, "65559245709fe98052eb284577f1fd61c01ad20d") != 1)
146  return 0;
147  if (SHA1MatchLookupString(hash, "0931fd4e05e6ea81c75f8488ecc1db9e66f22cbb") != 1)
148  return 0;
149  /* Shouldn't match */
150  if (SHA1MatchLookupString(hash, "3333333333333333333333333333333333333333") == 1)
151  return 0;
152 
153  ROHashFree(hash);
154  return 1;
155 }
156 #endif
157 
158 void DetectFileSha1RegisterTests(void)
159 {
160 #ifdef UNITTESTS
161  UtRegisterTest("SHA1MatchTest01", SHA1MatchTest01);
162 #endif
163 }
164 
165 #endif /* HAVE_NSS */
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
#define SCLogDebug(...)
Definition: util-debug.h:335
void DetectFileSha1Register(void)
Registration function for keyword: filesha1.
void DetectFileHashFree(void *)
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
main detection engine ctx
Definition: detect.h:720
#define str(s)
void * ROHashLookup(ROHashTable *table, void *data, uint16_t size)
Definition: util-rohash.c:113
int(* FileMatch)(ThreadVars *, DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1137
#define SIGMATCH_NOT_BUILT
Definition: detect.h:1334
void(* Free)(void *)
Definition: detect.h:1151
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
ROHashTable * ROHashInit(uint8_t hash_bits, uint16_t item_size)
initialize a new rohash
Definition: util-rohash.c:64
int ReadHashString(uint8_t *, const char *, const char *, int, uint16_t)
const char * desc
Definition: detect.h:1162
int DetectBufferTypeRegister(const char *name)
int ROHashInitFinalize(ROHashTable *table)
create final hash data structure
Definition: util-rohash.c:183
void ROHashFree(ROHashTable *table)
Definition: util-rohash.c:92
int LoadHashTable(ROHashTable *, const char *, const char *, int, uint32_t)
const char * url
Definition: detect.h:1163
int DetectFileHashSetup(DetectEngineCtx *, Signature *, const char *, uint32_t, int)
#define DOC_URL
Definition: suricata.h:86
int DetectFileHashMatch(ThreadVars *, DetectEngineThreadCtx *, Flow *, uint8_t, File *, const Signature *, const SigMatchCtx *)
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1154
void(* RegisterTests)(void)
Definition: detect.h:1152