Coverage Report

Created: 2026-03-31 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata/src/detect-filemd5.c
Line
Count
Source
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"
28
#include "detect-file-hash-common.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
 */
44
void DetectFileMd5Register(void)
45
39
{
46
39
    sigmatch_table[DETECT_FILEMD5].name = "filemd5";
47
39
    sigmatch_table[DETECT_FILEMD5].desc = "match file MD5 against list of MD5 checksums";
48
39
    sigmatch_table[DETECT_FILEMD5].url = "/rules/file-keywords.html#filemd5";
49
39
    sigmatch_table[DETECT_FILEMD5].FileMatch = DetectFileHashMatch;
50
39
    sigmatch_table[DETECT_FILEMD5].Setup = DetectFileMd5Setup;
51
39
    sigmatch_table[DETECT_FILEMD5].Free  = DetectFileHashFree;
52
#ifdef UNITTESTS
53
    sigmatch_table[DETECT_FILEMD5].RegisterTests = DetectFileMd5RegisterTests;
54
#endif
55
39
    g_file_match_list_id = DetectBufferTypeRegister("files");
56
57
39
    SCLogDebug("registering filemd5 rule option");
58
39
}
59
60
/**
61
 * \brief this function is used to parse filemd5 options
62
 * \brief into the current signature
63
 *
64
 * \param de_ctx pointer to the Detection Engine Context
65
 * \param s pointer to the Current Signature
66
 * \param str pointer to the user provided "filemd5" option
67
 *
68
 * \retval 0 on Success
69
 * \retval -1 on Failure
70
 */
71
static int DetectFileMd5Setup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
72
1.02k
{
73
1.02k
    return DetectFileHashSetup(de_ctx, s, str, DETECT_FILEMD5, g_file_match_list_id);
74
1.02k
}
75
76
#ifdef UNITTESTS
77
static int MD5MatchLookupString(ROHashTable *hash, const char *string)
78
{
79
    uint8_t md5[16];
80
    if (ReadHashString(md5, string, "file", 88, 32) == 1) {
81
        void *ptr = ROHashLookup(hash, &md5, (uint16_t)sizeof(md5));
82
        if (ptr == NULL)
83
            return 0;
84
        else
85
            return 1;
86
    }
87
    return 0;
88
}
89
90
static int MD5MatchTest01(void)
91
{
92
    ROHashTable *hash = ROHashInit(4, 16);
93
    FAIL_IF_NULL(hash);
94
    FAIL_IF(LoadHashTable(hash, "d80f93a93dc5f3ee945704754d6e0a36", "file", 1, DETECT_FILEMD5) !=
95
            1);
96
    FAIL_IF(LoadHashTable(hash, "92a49985b384f0d993a36e4c2d45e206", "file", 2, DETECT_FILEMD5) !=
97
            1);
98
    FAIL_IF(LoadHashTable(hash, "11adeaacc8c309815f7bc3e33888f281", "file", 3, DETECT_FILEMD5) !=
99
            1);
100
    FAIL_IF(LoadHashTable(hash, "22e10a8fe02344ade0bea8836a1714af", "file", 4, DETECT_FILEMD5) !=
101
            1);
102
    FAIL_IF(LoadHashTable(hash, "c3db2cbf02c68f073afcaee5634677bc", "file", 5, DETECT_FILEMD5) !=
103
            1);
104
    FAIL_IF(LoadHashTable(hash, "7ed095da259638f42402fb9e74287a17", "file", 6, DETECT_FILEMD5) !=
105
            1);
106
    FAIL_IF(ROHashInitFinalize(hash) != 1);
107
    FAIL_IF(MD5MatchLookupString(hash, "d80f93a93dc5f3ee945704754d6e0a36") != 1);
108
    FAIL_IF(MD5MatchLookupString(hash, "92a49985b384f0d993a36e4c2d45e206") != 1);
109
    FAIL_IF(MD5MatchLookupString(hash, "11adeaacc8c309815f7bc3e33888f281") != 1);
110
    FAIL_IF(MD5MatchLookupString(hash, "22e10a8fe02344ade0bea8836a1714af") != 1);
111
    FAIL_IF(MD5MatchLookupString(hash, "c3db2cbf02c68f073afcaee5634677bc") != 1);
112
    FAIL_IF(MD5MatchLookupString(hash, "7ed095da259638f42402fb9e74287a17") != 1);
113
    /* shouldn't match */
114
    FAIL_IF(MD5MatchLookupString(hash, "33333333333333333333333333333333") == 1);
115
    ROHashFree(hash);
116
    PASS;
117
}
118
119
void DetectFileMd5RegisterTests(void)
120
{
121
    UtRegisterTest("MD5MatchTest01", MD5MatchTest01);
122
}
123
#endif