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 static int DetectFileSha256Setup (DetectEngineCtx *, Signature *, const char *);
36 #ifdef UNITTESTS
37 static void DetectFileSha256RegisterTests(void);
38 #endif
39 static int g_file_match_list_id = 0;
40 
41 /**
42  * \brief Registration function for keyword: filesha256
43  */
45 {
46  sigmatch_table[DETECT_FILESHA256].name = "filesha256";
47  sigmatch_table[DETECT_FILESHA256].desc = "match file SHA-256 against list of SHA-256 checksums";
48  sigmatch_table[DETECT_FILESHA256].url = "/rules/file-keywords.html#filesha256";
50  sigmatch_table[DETECT_FILESHA256].Setup = DetectFileSha256Setup;
52 #ifdef UNITTESTS
53  sigmatch_table[DETECT_FILESHA256].RegisterTests = DetectFileSha256RegisterTests;
54 #endif
55 
56  g_file_match_list_id = DetectBufferTypeRegister("files");
57 
58  SCLogDebug("registering filesha256 rule option");
59 }
60 
61 /**
62  * \brief this function is used to parse filesha256 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 "filesha256" option
68  *
69  * \retval 0 on Success
70  * \retval -1 on Failure
71  */
72 static int DetectFileSha256Setup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
73 {
74  return DetectFileHashSetup(de_ctx, s, str, DETECT_FILESHA256, g_file_match_list_id);
75 }
76 
77 #ifdef UNITTESTS
78 static int SHA256MatchLookupString(ROHashTable *hash, const char *string)
79 {
80  uint8_t sha256[32];
81  if (ReadHashString(sha256, string, "file", 88, 64) == 1) {
82  void *ptr = ROHashLookup(hash, &sha256, (uint16_t)sizeof(sha256));
83  if (ptr == NULL)
84  return 0;
85  else
86  return 1;
87  }
88  return 0;
89 }
90 
91 static int SHA256MatchTest01(void)
92 {
93  ROHashTable *hash = ROHashInit(4, 32);
94  FAIL_IF_NULL(hash);
95  FAIL_IF(LoadHashTable(hash, "9c891edb5da763398969b6aaa86a5d46971bd28a455b20c2067cb512c9f9a0f8",
96  "file", 1, DETECT_FILESHA256) != 1);
97  FAIL_IF(LoadHashTable(hash, "6eee51705f34b6cfc7f0c872a7949ec3e3172a908303baf5d67d03b98f70e7e3",
98  "file", 2, DETECT_FILESHA256) != 1);
99  FAIL_IF(LoadHashTable(hash, "b12c7d57507286bbbe36d7acf9b34c22c96606ffd904e3c23008399a4a50c047",
100  "file", 3, DETECT_FILESHA256) != 1);
101  FAIL_IF(LoadHashTable(hash, "ca496e1ddadc290050339dd75ce8830ad3028ce1556a5368874a4aec3aee114b",
102  "file", 4, DETECT_FILESHA256) != 1);
103  FAIL_IF(LoadHashTable(hash, "275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f",
104  "file", 5, DETECT_FILESHA256) != 1);
105  FAIL_IF(LoadHashTable(hash, "d765e722e295969c0a5c2d90f549db8b89ab617900bf4698db41c7cdad993bb9",
106  "file", 6, DETECT_FILESHA256) != 1);
107  FAIL_IF(ROHashInitFinalize(hash) != 1);
108  FAIL_IF(SHA256MatchLookupString(
109  hash, "9c891edb5da763398969b6aaa86a5d46971bd28a455b20c2067cb512c9f9a0f8") != 1);
110  FAIL_IF(SHA256MatchLookupString(
111  hash, "6eee51705f34b6cfc7f0c872a7949ec3e3172a908303baf5d67d03b98f70e7e3") != 1);
112  FAIL_IF(SHA256MatchLookupString(
113  hash, "b12c7d57507286bbbe36d7acf9b34c22c96606ffd904e3c23008399a4a50c047") != 1);
114  FAIL_IF(SHA256MatchLookupString(
115  hash, "ca496e1ddadc290050339dd75ce8830ad3028ce1556a5368874a4aec3aee114b") != 1);
116  FAIL_IF(SHA256MatchLookupString(
117  hash, "275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f") != 1);
118  FAIL_IF(SHA256MatchLookupString(
119  hash, "d765e722e295969c0a5c2d90f549db8b89ab617900bf4698db41c7cdad993bb9") != 1);
120  /* Shouldn't match */
121  FAIL_IF(SHA256MatchLookupString(
122  hash, "3333333333333333333333333333333333333333333333333333333333333333") == 1);
123  ROHashFree(hash);
124  PASS;
125 }
126 
127 void DetectFileSha256RegisterTests(void)
128 {
129  UtRegisterTest("SHA256MatchTest01", SHA256MatchTest01);
130 }
131 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1304
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1303
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1291
SigTableElmt_::name
const char * name
Definition: detect.h:1301
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
SigTableElmt_::FileMatch
int(* FileMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, File *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1277
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
ROHashTable_
Definition: util-rohash.h:27
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1286
util-unittest.h
util-unittest-helper.h
ROHashInitFinalize
int ROHashInitFinalize(ROHashTable *table)
create final hash data structure
Definition: util-rohash.c:181
DetectFileHashFree
void DetectFileHashFree(DetectEngineCtx *de_ctx, void *ptr)
this function will free memory associated with DetectFileHashData
Definition: detect-file-hash-common.c:359
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
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:318
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:144
DetectFileSha256Register
void DetectFileSha256Register(void)
Registration function for keyword: filesha256.
Definition: detect-filesha256.c:44
detect-file-hash-common.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:1027
suricata-common.h
ROHashInit
ROHashTable * ROHashInit(uint8_t hash_bits, uint16_t item_size)
initialize a new rohash
Definition: util-rohash.c:64
str
#define str(s)
Definition: suricata-common.h:291
ROHashFree
void ROHashFree(ROHashTable *table)
Definition: util-rohash.c:91
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
Signature_
Signature container.
Definition: detect.h:601
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:235
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1293
detect-filesha256.h