Coverage Report

Created: 2026-01-16 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-tls-cert-fingerprint.c
Line
Count
Source
1
/* Copyright (C) 2017-2022 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 Mats Klepsland <mats.klepsland@gmail.com>
22
 *
23
 * Implements support for tls_cert_fingerprint keyword.
24
 */
25
26
#include "suricata-common.h"
27
#include "threads.h"
28
#include "decode.h"
29
#include "detect.h"
30
31
#include "detect-parse.h"
32
#include "detect-engine.h"
33
#include "detect-engine-mpm.h"
34
#include "detect-engine-prefilter.h"
35
#include "detect-content.h"
36
#include "detect-pcre.h"
37
#include "detect-tls-cert-fingerprint.h"
38
39
#include "flow.h"
40
#include "flow-util.h"
41
#include "flow-var.h"
42
43
#include "util-debug.h"
44
#include "util-spm.h"
45
#include "util-print.h"
46
47
#include "stream-tcp.h"
48
49
#include "app-layer.h"
50
#include "app-layer-ssl.h"
51
52
#include "util-unittest.h"
53
#include "util-unittest-helper.h"
54
55
static int DetectTlsFingerprintSetup(DetectEngineCtx *, Signature *, const char *);
56
#ifdef UNITTESTS
57
static void DetectTlsFingerprintRegisterTests(void);
58
#endif
59
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
60
        const DetectEngineTransforms *transforms,
61
        Flow *f, const uint8_t flow_flags,
62
        void *txv, const int list_id);
63
static void DetectTlsFingerprintSetupCallback(const DetectEngineCtx *de_ctx,
64
        Signature *s);
65
static bool DetectTlsFingerprintValidateCallback(const Signature *s,
66
        const char **sigerror);
67
static int g_tls_cert_fingerprint_buffer_id = 0;
68
69
/**
70
 * \brief Registration function for keyword: tls.cert_fingerprint
71
 */
72
void DetectTlsFingerprintRegister(void)
73
73
{
74
73
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].name = "tls.cert_fingerprint";
75
73
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].alias = "tls_cert_fingerprint";
76
73
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].desc =
77
73
            "sticky buffer to match the TLS cert fingerprint buffer";
78
73
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].url = "/rules/tls-keywords.html#tls-cert-fingerprint";
79
73
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].Setup = DetectTlsFingerprintSetup;
80
#ifdef UNITTESTS
81
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].RegisterTests = DetectTlsFingerprintRegisterTests;
82
#endif
83
73
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].flags |= SIGMATCH_NOOPT;
84
73
    sigmatch_table[DETECT_AL_TLS_CERT_FINGERPRINT].flags |= SIGMATCH_INFO_STICKY_BUFFER;
85
86
73
    DetectAppLayerInspectEngineRegister2("tls.cert_fingerprint", ALPROTO_TLS,
87
73
            SIG_FLAG_TOCLIENT, TLS_STATE_CERT_READY,
88
73
            DetectEngineInspectBufferGeneric, GetData);
89
90
73
    DetectAppLayerMpmRegister2("tls.cert_fingerprint", SIG_FLAG_TOCLIENT, 2,
91
73
            PrefilterGenericMpmRegister, GetData, ALPROTO_TLS,
92
73
            TLS_STATE_CERT_READY);
93
94
73
    DetectAppLayerInspectEngineRegister2("tls.cert_fingerprint", ALPROTO_TLS, SIG_FLAG_TOSERVER,
95
73
            TLS_STATE_CERT_READY, DetectEngineInspectBufferGeneric, GetData);
96
97
73
    DetectAppLayerMpmRegister2("tls.cert_fingerprint", SIG_FLAG_TOSERVER, 2,
98
73
            PrefilterGenericMpmRegister, GetData, ALPROTO_TLS, TLS_STATE_CERT_READY);
99
100
73
    DetectBufferTypeSetDescriptionByName("tls.cert_fingerprint",
101
73
            "TLS certificate fingerprint");
102
103
73
    DetectBufferTypeRegisterSetupCallback("tls.cert_fingerprint",
104
73
            DetectTlsFingerprintSetupCallback);
105
106
73
    DetectBufferTypeRegisterValidateCallback("tls.cert_fingerprint",
107
73
            DetectTlsFingerprintValidateCallback);
108
109
73
    g_tls_cert_fingerprint_buffer_id = DetectBufferTypeGetByName("tls.cert_fingerprint");
110
73
}
111
112
/**
113
 * \brief this function setup the tls_cert_fingerprint modifier keyword used in the rule
114
 *
115
 * \param de_ctx   Pointer to the Detection Engine Context
116
 * \param s        Pointer to the Signature to which the current keyword belongs
117
 * \param str      Should hold an empty string always
118
 *
119
 * \retval 0  On success
120
 * \retval -1 On failure
121
 */
122
static int DetectTlsFingerprintSetup(DetectEngineCtx *de_ctx, Signature *s,
123
                                     const char *str)
124
4.95k
{
125
4.95k
    if (DetectBufferSetActiveList(de_ctx, s, g_tls_cert_fingerprint_buffer_id) < 0)
126
3
        return -1;
127
128
4.95k
    if (DetectSignatureSetAppProto(s, ALPROTO_TLS) < 0)
129
11
        return -1;
130
131
4.94k
    return 0;
132
4.95k
}
133
134
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
135
        const DetectEngineTransforms *transforms, Flow *f,
136
        const uint8_t flow_flags, void *txv, const int list_id)
137
22
{
138
22
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
139
22
    if (buffer->inspect == NULL) {
140
18
        const SSLState *ssl_state = (SSLState *)f->alstate;
141
18
        const SSLStateConnp *connp;
142
143
18
        if (flow_flags & STREAM_TOSERVER) {
144
17
            connp = &ssl_state->client_connp;
145
17
        } else {
146
1
            connp = &ssl_state->server_connp;
147
1
        }
148
149
18
        if (connp->cert0_fingerprint == NULL) {
150
17
            return NULL;
151
17
        }
152
153
1
        const uint32_t data_len = strlen(connp->cert0_fingerprint);
154
1
        const uint8_t *data = (uint8_t *)connp->cert0_fingerprint;
155
156
1
        InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
157
1
        InspectionBufferApplyTransforms(buffer, transforms);
158
1
    }
159
160
5
    return buffer;
161
22
}
162
163
static bool DetectTlsFingerprintValidateCallback(const Signature *s,
164
                                                  const char **sigerror)
165
3.44k
{
166
6.22k
    for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
167
5.03k
        if (s->init_data->buffers[x].id != (uint32_t)g_tls_cert_fingerprint_buffer_id)
168
1.64k
            continue;
169
3.39k
        const SigMatch *sm = s->init_data->buffers[x].head;
170
7.03k
        for (; sm != NULL; sm = sm->next) {
171
5.89k
            if (sm->type != DETECT_CONTENT)
172
2.28k
                continue;
173
174
3.61k
            const DetectContentData *cd = (DetectContentData *)sm->ctx;
175
176
3.61k
            if (cd->content_len != 59) {
177
1.36k
                *sigerror = "Invalid length of the specified fingerprint. "
178
1.36k
                            "This rule will therefore never match.";
179
1.36k
                SCLogWarning("rule %u: %s", s->id, *sigerror);
180
1.36k
                return false;
181
1.36k
            }
182
183
3.61k
            bool have_delimiters = false;
184
2.24k
            uint32_t u;
185
81.9k
            for (u = 0; u < cd->content_len; u++) {
186
81.0k
                if (cd->content[u] == ':') {
187
1.36k
                    have_delimiters = true;
188
1.36k
                    break;
189
1.36k
                }
190
81.0k
            }
191
192
2.24k
            if (!have_delimiters) {
193
884
                *sigerror = "No colon delimiters ':' detected in content after "
194
884
                            "tls.cert_fingerprint. This rule will therefore "
195
884
                            "never match.";
196
884
                SCLogWarning("rule %u: %s", s->id, *sigerror);
197
884
                return false;
198
884
            }
199
200
1.36k
            if (cd->flags & DETECT_CONTENT_NOCASE) {
201
132
                *sigerror = "tls.cert_fingerprint should not be used together "
202
132
                            "with nocase, since the rule is automatically "
203
132
                            "lowercased anyway which makes nocase redundant.";
204
132
                SCLogWarning("rule %u: %s", s->id, *sigerror);
205
132
            }
206
1.36k
        }
207
3.39k
    }
208
1.19k
    return true;
209
3.44k
}
210
211
static void DetectTlsFingerprintSetupCallback(const DetectEngineCtx *de_ctx,
212
                                              Signature *s)
213
3.96k
{
214
12.2k
    for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
215
8.28k
        if (s->init_data->buffers[x].id != (uint32_t)g_tls_cert_fingerprint_buffer_id)
216
4.54k
            continue;
217
3.73k
        SigMatch *sm = s->init_data->buffers[x].head;
218
13.0k
        for (; sm != NULL; sm = sm->next) {
219
9.31k
            if (sm->type != DETECT_CONTENT)
220
3.70k
                continue;
221
222
5.61k
            DetectContentData *cd = (DetectContentData *)sm->ctx;
223
224
5.61k
            bool changed = false;
225
5.61k
            uint32_t u;
226
222k
            for (u = 0; u < cd->content_len; u++) {
227
217k
                if (isupper(cd->content[u])) {
228
22.5k
                    cd->content[u] = u8_tolower(cd->content[u]);
229
22.5k
                    changed = true;
230
22.5k
                }
231
217k
            }
232
233
            /* recreate the context if changes were made */
234
5.61k
            if (changed) {
235
3.75k
                SpmDestroyCtx(cd->spm_ctx);
236
3.75k
                cd->spm_ctx =
237
3.75k
                        SpmInitCtx(cd->content, cd->content_len, 1, de_ctx->spm_global_thread_ctx);
238
3.75k
            }
239
5.61k
        }
240
3.73k
    }
241
3.96k
}
242
243
#ifdef UNITTESTS
244
#include "detect-engine-alert.h"
245
#include "tests/detect-tls-cert-fingerprint.c"
246
#endif