suricata
detect-filesha256.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-filesha256.h"
34 
35 #ifndef HAVE_NSS
36 
37 static int DetectFileSha256SetupNoSupport (DetectEngineCtx *a, Signature *b, const char *c)
38 {
39  SCLogError(SC_ERR_NO_SHA256_SUPPORT, "no SHA-256 calculation support built in, needed for filesha256 keyword");
40  return -1;
41 }
42 
43 /**
44  * \brief Registration function for keyword: filesha256
45  */
47 {
48  sigmatch_table[DETECT_FILESHA256].name = "filesha256";
49  sigmatch_table[DETECT_FILESHA256].Setup = DetectFileSha256SetupNoSupport;
51 
52  SCLogDebug("registering filesha256 rule option");
53  return;
54 }
55 
56 #else /* HAVE_NSS */
57 
58 static int DetectFileSha256Setup (DetectEngineCtx *, Signature *, const char *);
59 #ifdef UNITTESTS
60 static void DetectFileSha256RegisterTests(void);
61 #endif
62 static int g_file_match_list_id = 0;
63 
64 /**
65  * \brief Registration function for keyword: filesha256
66  */
67 void DetectFileSha256Register(void)
68 {
69  sigmatch_table[DETECT_FILESHA256].name = "filesha256";
70  sigmatch_table[DETECT_FILESHA256].desc = "match file SHA-256 against list of SHA-256 checksums";
71  sigmatch_table[DETECT_FILESHA256].url = "/rules/file-keywords.html#filesha256";
73  sigmatch_table[DETECT_FILESHA256].Setup = DetectFileSha256Setup;
75 #ifdef UNITTESTS
76  sigmatch_table[DETECT_FILESHA256].RegisterTests = DetectFileSha256RegisterTests;
77 #endif
78 
79  g_file_match_list_id = DetectBufferTypeRegister("files");
80 
81  SCLogDebug("registering filesha256 rule option");
82  return;
83 }
84 
85 /**
86  * \brief this function is used to parse filesha256 options
87  * \brief into the current signature
88  *
89  * \param de_ctx pointer to the Detection Engine Context
90  * \param s pointer to the Current Signature
91  * \param str pointer to the user provided "filesha256" option
92  *
93  * \retval 0 on Success
94  * \retval -1 on Failure
95  */
96 static int DetectFileSha256Setup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
97 {
98  return DetectFileHashSetup(de_ctx, s, str, DETECT_FILESHA256, g_file_match_list_id);
99 }
100 
101 #ifdef UNITTESTS
102 static int SHA256MatchLookupString(ROHashTable *hash, const char *string)
103 {
104  uint8_t sha256[32];
105  if (ReadHashString(sha256, string, "file", 88, 64) == 1) {
106  void *ptr = ROHashLookup(hash, &sha256, (uint16_t)sizeof(sha256));
107  if (ptr == NULL)
108  return 0;
109  else
110  return 1;
111  }
112  return 0;
113 }
114 
115 static int SHA256MatchTest01(void)
116 {
117  ROHashTable *hash = ROHashInit(4, 32);
118  if (hash == NULL) {
119  return 0;
120  }
121  if (LoadHashTable(hash, "9c891edb5da763398969b6aaa86a5d46971bd28a455b20c2067cb512c9f9a0f8", "file", 1, DETECT_FILESHA256) != 1)
122  return 0;
123  if (LoadHashTable(hash, "6eee51705f34b6cfc7f0c872a7949ec3e3172a908303baf5d67d03b98f70e7e3", "file", 2, DETECT_FILESHA256) != 1)
124  return 0;
125  if (LoadHashTable(hash, "b12c7d57507286bbbe36d7acf9b34c22c96606ffd904e3c23008399a4a50c047", "file", 3, DETECT_FILESHA256) != 1)
126  return 0;
127  if (LoadHashTable(hash, "ca496e1ddadc290050339dd75ce8830ad3028ce1556a5368874a4aec3aee114b", "file", 4, DETECT_FILESHA256) != 1)
128  return 0;
129  if (LoadHashTable(hash, "275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f", "file", 5, DETECT_FILESHA256) != 1)
130  return 0;
131  if (LoadHashTable(hash, "d765e722e295969c0a5c2d90f549db8b89ab617900bf4698db41c7cdad993bb9", "file", 6, DETECT_FILESHA256) != 1)
132  return 0;
133 
134  if (ROHashInitFinalize(hash) != 1) {
135  return 0;
136  }
137 
138  if (SHA256MatchLookupString(hash, "9c891edb5da763398969b6aaa86a5d46971bd28a455b20c2067cb512c9f9a0f8") != 1)
139  return 0;
140  if (SHA256MatchLookupString(hash, "6eee51705f34b6cfc7f0c872a7949ec3e3172a908303baf5d67d03b98f70e7e3") != 1)
141  return 0;
142  if (SHA256MatchLookupString(hash, "b12c7d57507286bbbe36d7acf9b34c22c96606ffd904e3c23008399a4a50c047") != 1)
143  return 0;
144  if (SHA256MatchLookupString(hash, "ca496e1ddadc290050339dd75ce8830ad3028ce1556a5368874a4aec3aee114b") != 1)
145  return 0;
146  if (SHA256MatchLookupString(hash, "275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f") != 1)
147  return 0;
148  if (SHA256MatchLookupString(hash, "d765e722e295969c0a5c2d90f549db8b89ab617900bf4698db41c7cdad993bb9") != 1)
149  return 0;
150  /* Shouldn't match */
151  if (SHA256MatchLookupString(hash, "3333333333333333333333333333333333333333333333333333333333333333") == 1)
152  return 0;
153 
154  ROHashFree(hash);
155  return 1;
156 }
157 
158 void DetectFileSha256RegisterTests(void)
159 {
160  UtRegisterTest("SHA256MatchTest01", SHA256MatchTest01);
161 }
162 #endif
163 #endif /* HAVE_NSS */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1201
ReadHashString
int ReadHashString(uint8_t *, const char *, const char *, int, uint16_t)
SigTableElmt_::name
const char * name
Definition: detect.h:1211
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1187
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1205
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
ROHashTable_
Definition: util-rohash.h:29
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
util-unittest.h
SIGMATCH_NOT_BUILT
#define SIGMATCH_NOT_BUILT
Definition: detect.h:1386
util-unittest-helper.h
DetectFileHashMatch
int DetectFileHashMatch(DetectEngineThreadCtx *, Flow *, uint8_t, File *, const Signature *, const SigMatchCtx *)
ROHashInitFinalize
int ROHashInitFinalize(ROHashTable *table)
create final hash data structure
Definition: util-rohash.c:183
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectFileHashSetup
int DetectFileHashSetup(DetectEngineCtx *, Signature *, const char *, uint32_t, int)
ROHashLookup
void * ROHashLookup(ROHashTable *table, void *data, uint16_t size)
Definition: util-rohash.c:113
LoadHashTable
int LoadHashTable(ROHashTable *, const char *, const char *, int, uint32_t)
DetectFileSha256Register
void DetectFileSha256Register(void)
Registration function for keyword: filesha256.
Definition: detect-filesha256.c:46
detect-file-hash-common.h
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:836
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
ROHashInit
ROHashTable * ROHashInit(uint8_t hash_bits, uint16_t item_size)
initialize a new rohash
Definition: util-rohash.c:64
SC_ERR_NO_SHA256_SUPPORT
@ SC_ERR_NO_SHA256_SUPPORT
Definition: util-error.h:321
DetectFileHashFree
void DetectFileHashFree(DetectEngineCtx *, void *)
str
#define str(s)
Definition: suricata-common.h:273
ROHashFree
void ROHashFree(ROHashTable *table)
Definition: util-rohash.c:92
Signature_
Signature container.
Definition: detect.h:528
DETECT_FILESHA256
@ DETECT_FILESHA256
Definition: detect-engine-register.h:206
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203
detect-filesha256.h