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