Coverage Report

Created: 2026-05-16 07:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-filesha256.c
Line
Count
Source
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"
29
#include "detect-file-hash-common.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
 */
44
void DetectFileSha256Register(void)
45
34
{
46
34
    sigmatch_table[DETECT_FILESHA256].name = "filesha256";
47
34
    sigmatch_table[DETECT_FILESHA256].desc = "match file SHA-256 against list of SHA-256 checksums";
48
34
    sigmatch_table[DETECT_FILESHA256].url = "/rules/file-keywords.html#filesha256";
49
34
    sigmatch_table[DETECT_FILESHA256].FileMatch = DetectFileHashMatch;
50
34
    sigmatch_table[DETECT_FILESHA256].Setup = DetectFileSha256Setup;
51
34
    sigmatch_table[DETECT_FILESHA256].Free  = DetectFileHashFree;
52
#ifdef UNITTESTS
53
    sigmatch_table[DETECT_FILESHA256].RegisterTests = DetectFileSha256RegisterTests;
54
#endif
55
56
34
    g_file_match_list_id = DetectBufferTypeRegister("files");
57
58
34
    SCLogDebug("registering filesha256 rule option");
59
34
    return;
60
34
}
61
62
/**
63
 * \brief this function is used to parse filesha256 options
64
 * \brief into the current signature
65
 *
66
 * \param de_ctx pointer to the Detection Engine Context
67
 * \param s pointer to the Current Signature
68
 * \param str pointer to the user provided "filesha256" option
69
 *
70
 * \retval 0 on Success
71
 * \retval -1 on Failure
72
 */
73
static int DetectFileSha256Setup (DetectEngineCtx *de_ctx, Signature *s, const char *str)
74
612
{
75
612
    return DetectFileHashSetup(de_ctx, s, str, DETECT_FILESHA256, g_file_match_list_id);
76
612
}
77
78
#ifdef UNITTESTS
79
static int SHA256MatchLookupString(ROHashTable *hash, const char *string)
80
{
81
    uint8_t sha256[32];
82
    if (ReadHashString(sha256, string, "file", 88, 64) == 1) {
83
        void *ptr = ROHashLookup(hash, &sha256, (uint16_t)sizeof(sha256));
84
        if (ptr == NULL)
85
            return 0;
86
        else
87
            return 1;
88
    }
89
    return 0;
90
}
91
92
static int SHA256MatchTest01(void)
93
{
94
    ROHashTable *hash = ROHashInit(4, 32);
95
    FAIL_IF_NULL(hash);
96
    FAIL_IF(LoadHashTable(hash, "9c891edb5da763398969b6aaa86a5d46971bd28a455b20c2067cb512c9f9a0f8",
97
                    "file", 1, DETECT_FILESHA256) != 1);
98
    FAIL_IF(LoadHashTable(hash, "6eee51705f34b6cfc7f0c872a7949ec3e3172a908303baf5d67d03b98f70e7e3",
99
                    "file", 2, DETECT_FILESHA256) != 1);
100
    FAIL_IF(LoadHashTable(hash, "b12c7d57507286bbbe36d7acf9b34c22c96606ffd904e3c23008399a4a50c047",
101
                    "file", 3, DETECT_FILESHA256) != 1);
102
    FAIL_IF(LoadHashTable(hash, "ca496e1ddadc290050339dd75ce8830ad3028ce1556a5368874a4aec3aee114b",
103
                    "file", 4, DETECT_FILESHA256) != 1);
104
    FAIL_IF(LoadHashTable(hash, "275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f",
105
                    "file", 5, DETECT_FILESHA256) != 1);
106
    FAIL_IF(LoadHashTable(hash, "d765e722e295969c0a5c2d90f549db8b89ab617900bf4698db41c7cdad993bb9",
107
                    "file", 6, DETECT_FILESHA256) != 1);
108
    FAIL_IF(ROHashInitFinalize(hash) != 1);
109
    FAIL_IF(SHA256MatchLookupString(
110
                    hash, "9c891edb5da763398969b6aaa86a5d46971bd28a455b20c2067cb512c9f9a0f8") != 1);
111
    FAIL_IF(SHA256MatchLookupString(
112
                    hash, "6eee51705f34b6cfc7f0c872a7949ec3e3172a908303baf5d67d03b98f70e7e3") != 1);
113
    FAIL_IF(SHA256MatchLookupString(
114
                    hash, "b12c7d57507286bbbe36d7acf9b34c22c96606ffd904e3c23008399a4a50c047") != 1);
115
    FAIL_IF(SHA256MatchLookupString(
116
                    hash, "ca496e1ddadc290050339dd75ce8830ad3028ce1556a5368874a4aec3aee114b") != 1);
117
    FAIL_IF(SHA256MatchLookupString(
118
                    hash, "275a021bbfb6489e54d471899f7db9d1663fc695ec2fe2a2c4538aabf651fd0f") != 1);
119
    FAIL_IF(SHA256MatchLookupString(
120
                    hash, "d765e722e295969c0a5c2d90f549db8b89ab617900bf4698db41c7cdad993bb9") != 1);
121
    /* Shouldn't match */
122
    FAIL_IF(SHA256MatchLookupString(
123
                    hash, "3333333333333333333333333333333333333333333333333333333333333333") == 1);
124
    ROHashFree(hash);
125
    PASS;
126
}
127
128
void DetectFileSha256RegisterTests(void)
129
{
130
    UtRegisterTest("SHA256MatchTest01", SHA256MatchTest01);
131
}
132
#endif