Coverage Report

Created: 2026-06-07 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-ike-key-exchange-payload.c
Line
Count
Source
1
/* Copyright (C) 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
 *
20
 * \author Frank Honza <frank.honza@dcso.de>
21
 */
22
23
#include "suricata-common.h"
24
#include "threads.h"
25
#include "decode.h"
26
#include "detect.h"
27
28
#include "detect-parse.h"
29
#include "detect-engine.h"
30
#include "detect-engine-mpm.h"
31
#include "detect-engine-prefilter.h"
32
#include "detect-urilen.h"
33
34
#include "flow.h"
35
#include "flow-var.h"
36
#include "flow-util.h"
37
38
#include "util-debug.h"
39
#include "util-unittest.h"
40
#include "util-unittest-helper.h"
41
#include "util-spm.h"
42
43
#include "app-layer.h"
44
#include "app-layer-parser.h"
45
46
#include "detect-ike-key-exchange-payload.h"
47
#include "stream-tcp.h"
48
49
#include "rust.h"
50
#include "app-layer-ike.h"
51
#include "rust-bindings.h"
52
53
34
#define KEYWORD_NAME_KEY_EXCHANGE "ike.key_exchange_payload"
54
34
#define KEYWORD_DOC_KEY_EXCHANGE  "ike-keywords.html#ike-key_exchange_payload";
55
204
#define BUFFER_NAME_KEY_EXCHANGE  "ike.key_exchange_payload"
56
34
#define BUFFER_DESC_KEY_EXCHANGE  "ike key_exchange payload"
57
58
static int g_buffer_key_exchange_id = 0;
59
60
static int DetectKeyExchangeSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
61
992
{
62
992
    if (DetectBufferSetActiveList(de_ctx, s, g_buffer_key_exchange_id) < 0)
63
246
        return -1;
64
65
746
    if (DetectSignatureSetAppProto(s, ALPROTO_IKE) < 0)
66
127
        return -1;
67
68
619
    return 0;
69
746
}
70
71
static InspectionBuffer *GetKeyExchangeData(DetectEngineThreadCtx *det_ctx,
72
        const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
73
        const int list_id)
74
62
{
75
62
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
76
62
    if (buffer->inspect == NULL) {
77
55
        const uint8_t *b = NULL;
78
55
        uint32_t b_len = 0;
79
80
55
        if (rs_ike_state_get_key_exchange(txv, &b, &b_len) != 1)
81
45
            return NULL;
82
10
        if (b == NULL || b_len == 0)
83
0
            return NULL;
84
85
10
        InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
86
10
        InspectionBufferApplyTransforms(buffer, transforms);
87
10
    }
88
89
17
    return buffer;
90
62
}
91
92
void DetectIkeKeyExchangeRegister(void)
93
34
{
94
    // register key_exchange
95
34
    sigmatch_table[DETECT_AL_IKE_KEY_EXCHANGE].name = KEYWORD_NAME_KEY_EXCHANGE;
96
34
    sigmatch_table[DETECT_AL_IKE_KEY_EXCHANGE].url =
97
34
            "/rules/" KEYWORD_DOC_KEY_EXCHANGE sigmatch_table[DETECT_AL_IKE_KEY_EXCHANGE].desc =
98
34
                    "sticky buffer to match on the IKE key_exchange_payload";
99
34
    sigmatch_table[DETECT_AL_IKE_KEY_EXCHANGE].Setup = DetectKeyExchangeSetup;
100
34
    sigmatch_table[DETECT_AL_IKE_KEY_EXCHANGE].flags |=
101
34
            SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
102
103
34
    DetectAppLayerInspectEngineRegister2(BUFFER_NAME_KEY_EXCHANGE, ALPROTO_IKE, SIG_FLAG_TOSERVER,
104
34
            1, DetectEngineInspectBufferGeneric, GetKeyExchangeData);
105
106
34
    DetectAppLayerMpmRegister2(BUFFER_NAME_KEY_EXCHANGE, SIG_FLAG_TOSERVER, 1,
107
34
            PrefilterGenericMpmRegister, GetKeyExchangeData, ALPROTO_IKE, 1);
108
109
34
    DetectAppLayerInspectEngineRegister2(BUFFER_NAME_KEY_EXCHANGE, ALPROTO_IKE, SIG_FLAG_TOCLIENT,
110
34
            1, DetectEngineInspectBufferGeneric, GetKeyExchangeData);
111
112
34
    DetectAppLayerMpmRegister2(BUFFER_NAME_KEY_EXCHANGE, SIG_FLAG_TOCLIENT, 1,
113
34
            PrefilterGenericMpmRegister, GetKeyExchangeData, ALPROTO_IKE, 1);
114
115
34
    DetectBufferTypeSetDescriptionByName(BUFFER_NAME_KEY_EXCHANGE, BUFFER_DESC_KEY_EXCHANGE);
116
117
34
    g_buffer_key_exchange_id = DetectBufferTypeGetByName(BUFFER_NAME_KEY_EXCHANGE);
118
34
    SCLogDebug("registering " BUFFER_NAME_KEY_EXCHANGE " rule option");
119
34
}