Coverage Report

Created: 2025-07-23 07:29

/src/suricata7/src/detect-transform-sha256.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2007-2020 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
 * Implements the nocase keyword
24
 */
25
26
#include "suricata-common.h"
27
28
#include "detect.h"
29
#include "detect-engine.h"
30
#include "detect-engine-prefilter.h"
31
#include "detect-parse.h"
32
#include "detect-transform-sha256.h"
33
34
#include "util-unittest.h"
35
#include "util-print.h"
36
37
#include "rust.h"
38
39
static int DetectTransformToSha256Setup (DetectEngineCtx *, Signature *, const char *);
40
#ifdef UNITTESTS
41
static void DetectTransformToSha256RegisterTests(void);
42
#endif
43
static void TransformToSha256(InspectionBuffer *buffer, void *options);
44
45
void DetectTransformSha256Register(void)
46
34
{
47
34
    sigmatch_table[DETECT_TRANSFORM_SHA256].name = "to_sha256";
48
34
    sigmatch_table[DETECT_TRANSFORM_SHA256].desc =
49
34
        "convert to sha256 hash of the buffer";
50
34
    sigmatch_table[DETECT_TRANSFORM_SHA256].url =
51
34
        "/rules/transforms.html#to-sha256";
52
34
    sigmatch_table[DETECT_TRANSFORM_SHA256].Setup =
53
34
        DetectTransformToSha256Setup;
54
34
    sigmatch_table[DETECT_TRANSFORM_SHA256].Transform =
55
34
        TransformToSha256;
56
#ifdef UNITTESTS
57
    sigmatch_table[DETECT_TRANSFORM_SHA256].RegisterTests =
58
        DetectTransformToSha256RegisterTests;
59
#endif
60
34
    sigmatch_table[DETECT_TRANSFORM_SHA256].flags |= SIGMATCH_NOOPT;
61
34
}
62
63
/**
64
 *  \internal
65
 *  \brief Apply the nocase keyword to the last pattern match, either content or uricontent
66
 *  \param det_ctx detection engine ctx
67
 *  \param s signature
68
 *  \param nullstr should be null
69
 *  \retval 0 ok
70
 *  \retval -1 failure
71
 */
72
static int DetectTransformToSha256Setup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
73
1.90k
{
74
1.90k
    SCEnter();
75
1.90k
    if (g_disable_hashing) {
76
0
        SCLogError("SHA256 hashing has been disabled, "
77
0
                   "needed for to_sha256 keyword");
78
0
        SCReturnInt(-1);
79
0
    }
80
1.90k
    int r = DetectSignatureAddTransform(s, DETECT_TRANSFORM_SHA256, NULL);
81
1.90k
    SCReturnInt(r);
82
1.90k
}
83
84
static void TransformToSha256(InspectionBuffer *buffer, void *options)
85
0
{
86
0
    const uint8_t *input = buffer->inspect;
87
0
    const uint32_t input_len = buffer->inspect_len;
88
0
    uint8_t output[SC_SHA256_LEN];
89
90
    //PrintRawDataFp(stdout, input, input_len);
91
0
    SCSha256HashBuffer(input, input_len, output, sizeof(output));
92
0
    InspectionBufferCopy(buffer, output, sizeof(output));
93
0
}
94
95
#ifdef UNITTESTS
96
static int DetectTransformToSha256Test01(void)
97
{
98
    const uint8_t *input = (const uint8_t *)" A B C D ";
99
    uint32_t input_len = strlen((char *)input);
100
101
    InspectionBuffer buffer;
102
    InspectionBufferInit(&buffer, 8);
103
    InspectionBufferSetup(NULL, -1, &buffer, input, input_len);
104
    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
105
    TransformToSha256(&buffer, NULL);
106
    PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
107
    InspectionBufferFree(&buffer);
108
    PASS;
109
}
110
111
static void DetectTransformToSha256RegisterTests(void)
112
{
113
    UtRegisterTest("DetectTransformToSha256Test01",
114
            DetectTransformToSha256Test01);
115
}
116
#endif