suricata
detect-file-hash-common.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.h"
29 #include "detect-parse.h"
30 
32 
33 #include "app-layer-htp.h"
34 
35 #ifdef HAVE_NSS
36 
37 /**
38  * \brief Read the bytes of a hash from an hexadecimal string
39  *
40  * \param hash buffer to store the resulting bytes
41  * \param string hexadecimal string representing the hash
42  * \param filename file name from where the string was read
43  * \param line_no file line number from where the string was read
44  * \param expected_len the expected length of the string that was read
45  *
46  * \retval -1 the hexadecimal string is invalid
47  * \retval 1 the hexadecimal string was read successfully
48  */
49 int ReadHashString(uint8_t *hash, const char *string, const char *filename, int line_no,
50  uint16_t expected_len)
51 {
52  if (strlen(string) != expected_len) {
53  SCLogError(SC_ERR_INVALID_HASH, "%s:%d hash string not %d characters",
54  filename, line_no, expected_len);
55  return -1;
56  }
57 
58  int i, x;
59  for (x = 0, i = 0; i < expected_len; i+=2, x++) {
60  char buf[3] = { 0, 0, 0 };
61  buf[0] = string[i];
62  buf[1] = string[i+1];
63 
64  long value = strtol(buf, NULL, 16);
65  if (value >= 0 && value <= 255)
66  hash[x] = (uint8_t)value;
67  else {
68  SCLogError(SC_ERR_INVALID_HASH, "%s:%d hash byte out of range %ld",
69  filename, line_no, value);
70  return -1;
71  }
72  }
73 
74  return 1;
75 }
76 
77 /**
78  * \brief Store a hash into the hash table
79  *
80  * \param hash_table hash table that will hold the hash
81  * \param string hexadecimal string representing the hash
82  * \param filename file name from where the string was read
83  * \param line_no file line number from where the string was read
84  * \param type the hash algorithm
85  *
86  * \retval -1 failed to load the hash into the hash table
87  * \retval 1 successfully loaded the has into the hash table
88  */
89 int LoadHashTable(ROHashTable *hash_table, const char *string, const char *filename,
90  int line_no, uint32_t type)
91 {
92  /* allocate the maximum size a hash can have (in this case is SHA256, 32 bytes) */
93  uint8_t hash[32];
94  /* specify the actual size that should be read depending on the hash algorithm */
95  uint16_t size = 32;
96 
97  if (type == DETECT_FILEMD5) {
98  size = 16;
99  }
100  else if (type == DETECT_FILESHA1) {
101  size = 20;
102  }
103 
104  /* every byte represented with hexadecimal digits is two characters */
105  uint16_t expected_len = (size * 2);
106 
107  if (ReadHashString(hash, string, filename, line_no, expected_len) == 1) {
108  if (ROHashInitQueueValue(hash_table, &hash, size) != 1)
109  return -1;
110  }
111 
112  return 1;
113 }
114 
115 /**
116  * \brief Match a hash stored in a hash table
117  *
118  * \param hash_table hash table that will hold the hash
119  * \param hash buffer containing the bytes of the has
120  * \param hash_len length of the hash buffer
121  *
122  * \retval 0 didn't find the specified hash
123  * \retval 1 the hash matched a stored value
124  */
125 static int HashMatchHashTable(ROHashTable *hash_table, uint8_t *hash,
126  size_t hash_len)
127 {
128  void *ptr = ROHashLookup(hash_table, hash, (uint16_t)hash_len);
129  if (ptr == NULL)
130  return 0;
131  else
132  return 1;
133 }
134 
135 /**
136  * \brief Match the specified file hash
137  *
138  * \param det_ctx pattern matcher thread local data
139  * \param f *LOCKED* flow
140  * \param flags direction flags
141  * \param file file being inspected
142  * \param s signature being inspected
143  * \param m sigmatch that we will cast into DetectFileHashData
144  *
145  * \retval 0 no match
146  * \retval 1 match
147  */
149  Flow *f, uint8_t flags, File *file, const Signature *s, const SigMatchCtx *m)
150 {
151  SCEnter();
152  int ret = 0;
153  DetectFileHashData *filehash = (DetectFileHashData *)m;
154 
155  if (file->state != FILE_STATE_CLOSED) {
156  SCReturnInt(0);
157  }
158 
159  int match = -1;
160 
161  if (s->file_flags & FILE_SIG_NEED_MD5 && file->flags & FILE_MD5) {
162  match = HashMatchHashTable(filehash->hash, file->md5, sizeof(file->md5));
163  }
164  else if (s->file_flags & FILE_SIG_NEED_SHA1 && file->flags & FILE_SHA1) {
165  match = HashMatchHashTable(filehash->hash, file->sha1, sizeof(file->sha1));
166  }
167  else if (s->file_flags & FILE_SIG_NEED_SHA256 && file->flags & FILE_SHA256) {
168  match = HashMatchHashTable(filehash->hash, file->sha256, sizeof(file->sha256));
169  }
170 
171  if (match == 1) {
172  if (filehash->negated == 0)
173  ret = 1;
174  else
175  ret = 0;
176  }
177  else if (match == 0) {
178  if (filehash->negated == 0)
179  ret = 0;
180  else
181  ret = 1;
182  }
183 
184  SCReturnInt(ret);
185 }
186 
187 static const char *hexcodes = "ABCDEFabcdef0123456789";
188 
189 /**
190  * \brief Parse the filemd5, filesha1 or filesha256 keyword
191  *
192  * \param det_ctx pattern matcher thread local data
193  * \param str Pointer to the user provided option
194  * \param type the hash algorithm
195  *
196  * \retval hash pointer to DetectFileHashData on success
197  * \retval NULL on failure
198  */
199 static DetectFileHashData *DetectFileHashParse (const DetectEngineCtx *de_ctx,
200  const char *str, uint32_t type)
201 {
202  DetectFileHashData *filehash = NULL;
203  FILE *fp = NULL;
204  char *filename = NULL;
205  char *rule_filename = NULL;
206 
207  /* We have a correct hash algorithm option */
208  filehash = SCMalloc(sizeof(DetectFileHashData));
209  if (unlikely(filehash == NULL))
210  goto error;
211 
212  memset(filehash, 0x00, sizeof(DetectFileHashData));
213 
214  if (strlen(str) && str[0] == '!') {
215  filehash->negated = 1;
216  str++;
217  }
218 
219  if (type == DETECT_FILEMD5) {
220  filehash->hash = ROHashInit(18, 16);
221  }
222  else if (type == DETECT_FILESHA1) {
223  filehash->hash = ROHashInit(18, 20);
224  }
225  else if (type == DETECT_FILESHA256) {
226  filehash->hash = ROHashInit(18, 32);
227  }
228 
229  if (filehash->hash == NULL) {
230  goto error;
231  }
232 
233  /* get full filename */
234  filename = DetectLoadCompleteSigPath(de_ctx, str);
235  if (filename == NULL) {
236  goto error;
237  }
238 
239  rule_filename = SCStrdup(de_ctx->rule_file);
240  if (rule_filename == NULL) {
241  goto error;
242  }
243 
244  char line[8192] = "";
245  fp = fopen(filename, "r");
246  if (fp == NULL) {
247 #ifdef HAVE_LIBGEN_H
248  if (de_ctx->rule_file != NULL) {
249  char *dir = dirname(rule_filename);
250  if (dir != NULL) {
251  char path[PATH_MAX];
252  snprintf(path, sizeof(path), "%s/%s", dir, str);
253  fp = fopen(path, "r");
254  if (fp == NULL) {
256  "opening hash file %s: %s", path, strerror(errno));
257  goto error;
258  }
259  }
260  }
261  if (fp == NULL) {
262 #endif
263  SCLogError(SC_ERR_OPENING_RULE_FILE, "opening hash file %s: %s", filename, strerror(errno));
264  goto error;
265 #ifdef HAVE_LIBGEN_H
266  }
267 #endif
268  }
269 
270  int line_no = 0;
271  while(fgets(line, (int)sizeof(line), fp) != NULL) {
272  size_t valid = 0, len = strlen(line);
273  line_no++;
274 
275  while (strchr(hexcodes, line[valid]) != NULL && valid++ < len);
276 
277  /* lines that do not contain sequentially any valid character are ignored */
278  if (valid == 0)
279  continue;
280 
281  /* ignore anything after the sequence of valid characters */
282  line[valid] = '\0';
283 
284  if (LoadHashTable(filehash->hash, line, filename, line_no, type) != 1) {
285  goto error;
286  }
287  }
288  fclose(fp);
289  fp = NULL;
290 
291  if (ROHashInitFinalize(filehash->hash) != 1) {
292  goto error;
293  }
294  SCLogInfo("Hash hash table size %u bytes%s", ROHashMemorySize(filehash->hash), filehash->negated ? ", negated match" : "");
295 
296  SCFree(rule_filename);
297  SCFree(filename);
298  return filehash;
299 
300 error:
301  if (filehash != NULL)
303  if (fp != NULL)
304  fclose(fp);
305  if (filename != NULL)
306  SCFree(filename);
307  if (rule_filename != NULL) {
308  SCFree(rule_filename);
309  }
310  return NULL;
311 }
312 
313 /**
314  * \brief this function is used to parse filemd5, filesha1 and filesha256 options
315  * \brief into the current signature
316  *
317  * \param de_ctx pointer to the Detection Engine Context
318  * \param s pointer to the Current Signature
319  * \param str pointer to the user provided "filemd5", "filesha1" or "filesha256" option
320  * \param type type of file hash to setup
321  *
322  * \retval 0 on Success
323  * \retval -1 on Failure
324  */
326  uint32_t type, int list)
327 {
328  DetectFileHashData *filehash = NULL;
329  SigMatch *sm = NULL;
330 
331  filehash = DetectFileHashParse(de_ctx, str, type);
332  if (filehash == NULL)
333  goto error;
334 
335  /* Okay so far so good, lets get this into a SigMatch
336  * and put it in the Signature. */
337  sm = SigMatchAlloc();
338  if (sm == NULL)
339  goto error;
340 
341  sm->type = type;
342  sm->ctx = (void *)filehash;
343 
344  SigMatchAppendSMToList(s, sm, list);
345 
347 
348  // Setup the file flags depending on the hashing algorithm
349  if (type == DETECT_FILEMD5) {
351  }
352  if (type == DETECT_FILESHA1) {
354  }
355  if (type == DETECT_FILESHA256) {
357  }
358  return 0;
359 
360 error:
361  if (filehash != NULL)
362  DetectFileHashFree(de_ctx, filehash);
363  if (sm != NULL)
364  SCFree(sm);
365  return -1;
366 }
367 
368 /**
369  * \brief this function will free memory associated with DetectFileHashData
370  *
371  * \param filehash pointer to DetectFileHashData
372  */
373 void DetectFileHashFree(DetectEngineCtx *de_ctx, void *ptr)
374 {
375  if (ptr != NULL) {
376  DetectFileHashData *filehash = (DetectFileHashData *)ptr;
377  if (filehash->hash != NULL)
378  ROHashFree(filehash->hash);
379  SCFree(filehash);
380  }
381 }
382 
383 #endif /* HAVE_NSS */
len
uint8_t len
Definition: app-layer-dnp3.h:2
FILE_SIG_NEED_SHA1
#define FILE_SIG_NEED_SHA1
Definition: detect.h:289
ReadHashString
int ReadHashString(uint8_t *, const char *, const char *, int, uint16_t)
DetectEngineCtx_::rule_file
char * rule_file
Definition: detect.h:871
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
FILE_SHA256
#define FILE_SHA256
Definition: util-file.h:43
Flow_
Flow data structure.
Definition: flow.h:347
File_::state
FileState state
Definition: util-file.h:66
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
ROHashTable_
Definition: util-rohash.h:29
m
SCMutex m
Definition: flow-hash.h:6
DetectFileHashData_
Definition: detect-file-hash-common.h:31
DetectFileHashData_::hash
ROHashTable * hash
Definition: detect-file-hash-common.h:32
DetectFileHashMatch
int DetectFileHashMatch(DetectEngineThreadCtx *, Flow *, uint8_t, File *, const Signature *, const SigMatchCtx *)
FILE_SIG_NEED_MD5
#define FILE_SIG_NEED_MD5
Definition: detect.h:288
ROHashInitFinalize
int ROHashInitFinalize(ROHashTable *table)
create final hash data structure
Definition: util-rohash.c:183
app-layer-htp.h
type
uint8_t type
Definition: decode-icmpv4.h:0
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectFileHashSetup
int DetectFileHashSetup(DetectEngineCtx *, Signature *, const char *, uint32_t, int)
DetectEngineThreadCtx_
Definition: detect.h:1010
ROHashLookup
void * ROHashLookup(ROHashTable *table, void *data, uint16_t size)
Definition: util-rohash.c:113
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:323
FILE_SIG_NEED_SHA256
#define FILE_SIG_NEED_SHA256
Definition: detect.h:290
ROHashMemorySize
uint32_t ROHashMemorySize(ROHashTable *table)
Definition: util-rohash.c:103
LoadHashTable
int LoadHashTable(ROHashTable *, const char *, const char *, int, uint32_t)
SC_ERR_OPENING_RULE_FILE
@ SC_ERR_OPENING_RULE_FILE
Definition: util-error.h:71
SigMatchAlloc
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:235
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:217
SigMatch_::type
uint8_t type
Definition: detect.h:321
DETECT_FILESHA1
@ DETECT_FILESHA1
Definition: detect-engine-register.h:205
detect-file-hash-common.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:315
File_::flags
uint16_t flags
Definition: util-file.h:64
FILE_STATE_CLOSED
@ FILE_STATE_CLOSED
Definition: util-file.h:55
File_
Definition: util-file.h:63
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
Signature_::file_flags
uint8_t file_flags
Definition: detect.h:542
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
FILE_MD5
#define FILE_MD5
Definition: util-file.h:39
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
ROHashInitQueueValue
int ROHashInitQueueValue(ROHashTable *table, void *value, uint16_t size)
Add a new value to the hash.
Definition: util-rohash.c:152
DETECT_FILEMD5
@ DETECT_FILEMD5
Definition: detect-engine-register.h:204
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
DetectFileHashFree
void DetectFileHashFree(DetectEngineCtx *, void *)
str
#define str(s)
Definition: suricata-common.h:273
ROHashFree
void ROHashFree(ROHashTable *table)
Definition: util-rohash.c:92
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
SigMatch_
a single match condition for a signature
Definition: detect.h:320
FILE_SHA1
#define FILE_SHA1
Definition: util-file.h:41
FILE_SIG_NEED_FILE
#define FILE_SIG_NEED_FILE
Definition: detect.h:284
DetectLoadCompleteSigPath
char * DetectLoadCompleteSigPath(const DetectEngineCtx *de_ctx, const char *sig_file)
Create the path if default-rule-path was specified.
Definition: detect-engine-loader.c:60
DetectFileHashData_::negated
int negated
Definition: detect-file-hash-common.h:33
DETECT_FILESHA256
@ DETECT_FILESHA256
Definition: detect-engine-register.h:206
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
SC_ERR_INVALID_HASH
@ SC_ERR_INVALID_HASH
Definition: util-error.h:319
SigMatchAppendSMToList
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:349