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