suricata
detect-filemd5.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 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  *
23  */
24 
25 #include "suricata-common.h"
26 
27 #include "detect-engine.h"
29 #include "util-unittest.h"
30 #include "util-unittest-helper.h"
31 
32 #include "detect-filemd5.h"
33 
34 static int g_file_match_list_id = 0;
35 
36 static int DetectFileMd5Setup (DetectEngineCtx *, Signature *, const char *);
37 #ifdef UNITTESTS
38 static void DetectFileMd5RegisterTests(void);
39 #endif
40 
41 /**
42  * \brief Registration function for keyword: filemd5
43  */
45 {
46  sigmatch_table[DETECT_FILEMD5].name = "filemd5";
47  sigmatch_table[DETECT_FILEMD5].desc = "match file MD5 against list of MD5 checksums";
48  sigmatch_table[DETECT_FILEMD5].url = "/rules/file-keywords.html#filemd5";
50  sigmatch_table[DETECT_FILEMD5].Setup = DetectFileMd5Setup;
52 #ifdef UNITTESTS
53  sigmatch_table[DETECT_FILEMD5].RegisterTests = DetectFileMd5RegisterTests;
54 #endif
55  g_file_match_list_id = DetectBufferTypeRegister("files");
56 
57  SCLogDebug("registering filemd5 rule option");
58  return;
59 }
60 
61 /**
62  * \brief this function is used to parse filemd5 options
63  * \brief into the current signature
64  *
65  * \param de_ctx pointer to the Detection Engine Context
66  * \param s pointer to the Current Signature
67  * \param str pointer to the user provided "filemd5" option
68  *
69  * \retval 0 on Success
70  * \retval -1 on Failure
71  */
72 static int DetectFileMd5Setup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
73 {
74  return DetectFileHashSetup(de_ctx, s, str, DETECT_FILEMD5, g_file_match_list_id);
75 }
76 
77 #ifdef UNITTESTS
78 static int MD5MatchLookupString(ROHashTable *hash, const char *string)
79 {
80  uint8_t md5[16];
81  if (ReadHashString(md5, string, "file", 88, 32) == 1) {
82  void *ptr = ROHashLookup(hash, &md5, (uint16_t)sizeof(md5));
83  if (ptr == NULL)
84  return 0;
85  else
86  return 1;
87  }
88  return 0;
89 }
90 
91 static int MD5MatchTest01(void)
92 {
93  ROHashTable *hash = ROHashInit(4, 16);
94  if (hash == NULL) {
95  return 0;
96  }
97  if (LoadHashTable(hash, "d80f93a93dc5f3ee945704754d6e0a36", "file", 1, DETECT_FILEMD5) != 1)
98  return 0;
99  if (LoadHashTable(hash, "92a49985b384f0d993a36e4c2d45e206", "file", 2, DETECT_FILEMD5) != 1)
100  return 0;
101  if (LoadHashTable(hash, "11adeaacc8c309815f7bc3e33888f281", "file", 3, DETECT_FILEMD5) != 1)
102  return 0;
103  if (LoadHashTable(hash, "22e10a8fe02344ade0bea8836a1714af", "file", 4, DETECT_FILEMD5) != 1)
104  return 0;
105  if (LoadHashTable(hash, "c3db2cbf02c68f073afcaee5634677bc", "file", 5, DETECT_FILEMD5) != 1)
106  return 0;
107  if (LoadHashTable(hash, "7ed095da259638f42402fb9e74287a17", "file", 6, DETECT_FILEMD5) != 1)
108  return 0;
109 
110  if (ROHashInitFinalize(hash) != 1) {
111  return 0;
112  }
113 
114  if (MD5MatchLookupString(hash, "d80f93a93dc5f3ee945704754d6e0a36") != 1)
115  return 0;
116  if (MD5MatchLookupString(hash, "92a49985b384f0d993a36e4c2d45e206") != 1)
117  return 0;
118  if (MD5MatchLookupString(hash, "11adeaacc8c309815f7bc3e33888f281") != 1)
119  return 0;
120  if (MD5MatchLookupString(hash, "22e10a8fe02344ade0bea8836a1714af") != 1)
121  return 0;
122  if (MD5MatchLookupString(hash, "c3db2cbf02c68f073afcaee5634677bc") != 1)
123  return 0;
124  if (MD5MatchLookupString(hash, "7ed095da259638f42402fb9e74287a17") != 1)
125  return 0;
126  /* shouldn't match */
127  if (MD5MatchLookupString(hash, "33333333333333333333333333333333") == 1)
128  return 0;
129 
130  ROHashFree(hash);
131  return 1;
132 }
133 
134 void DetectFileMd5RegisterTests(void)
135 {
136  UtRegisterTest("MD5MatchTest01", MD5MatchTest01);
137 }
138 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1270
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1257
SigTableElmt_::name
const char * name
Definition: detect.h:1267
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:1243
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
detect-filemd5.h
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
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
util-unittest.h
util-unittest-helper.h
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
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
ROHashLookup
void * ROHashLookup(ROHashTable *table, void *data, uint16_t size)
Definition: util-rohash.c:113
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-file-hash-common.h
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1023
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
ROHashInit
ROHashTable * ROHashInit(uint8_t hash_bits, uint16_t item_size)
initialize a new rohash
Definition: util-rohash.c:64
DETECT_FILEMD5
@ DETECT_FILEMD5
Definition: detect-engine-register.h:207
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
Signature_
Signature container.
Definition: detect.h:548
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
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
DetectFileMd5Register
void DetectFileMd5Register(void)
Registration function for keyword: filemd5.
Definition: detect-filemd5.c:44