]> git.ipfire.org Git - people/ms/suricata.git/blob - src/detect-ike-nonce-payload.c
ikev1: add ikev1 parser
[people/ms/suricata.git] / src / detect-ike-nonce-payload.c
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 "debug.h"
26 #include "decode.h"
27 #include "detect.h"
28
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-mpm.h"
32 #include "detect-engine-prefilter.h"
33 #include "detect-urilen.h"
34
35 #include "flow.h"
36 #include "flow-var.h"
37 #include "flow-util.h"
38
39 #include "util-debug.h"
40 #include "util-unittest.h"
41 #include "util-unittest-helper.h"
42 #include "util-spm.h"
43
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46
47 #include "detect-ike-nonce-payload.h"
48 #include "stream-tcp.h"
49
50 #include "rust.h"
51 #include "app-layer-ike.h"
52 #include "rust-bindings.h"
53
54 #define KEYWORD_NAME_NONCE "ike.nonce_payload"
55 #define KEYWORD_DOC_NONCE "ike-keywords.html#ike-nonce_payload";
56 #define BUFFER_NAME_NONCE "ike.nonce_payload"
57 #define BUFFER_DESC_NONCE "ike nonce payload"
58
59 static int g_buffer_nonce_id = 0;
60
61 static int DetectNonceSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
62 {
63 if (DetectBufferSetActiveList(s, g_buffer_nonce_id) < 0)
64 return -1;
65
66 if (DetectSignatureSetAppProto(s, ALPROTO_IKE) < 0)
67 return -1;
68
69 return 0;
70 }
71
72 static InspectionBuffer *GetNonceData(DetectEngineThreadCtx *det_ctx,
73 const DetectEngineTransforms *transforms, Flow *_f, const uint8_t _flow_flags, void *txv,
74 const int list_id)
75 {
76 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
77 if (buffer->inspect == NULL) {
78 const uint8_t *b = NULL;
79 uint32_t b_len = 0;
80
81 if (rs_ike_state_get_nonce(txv, &b, &b_len) != 1)
82 return NULL;
83 if (b == NULL || b_len == 0)
84 return NULL;
85
86 InspectionBufferSetup(det_ctx, list_id, buffer, b, b_len);
87 InspectionBufferApplyTransforms(buffer, transforms);
88 }
89
90 return buffer;
91 }
92
93 void DetectIkeNonceRegister(void)
94 {
95 // register nonce
96 sigmatch_table[DETECT_AL_IKE_NONCE].name = KEYWORD_NAME_NONCE;
97 sigmatch_table[DETECT_AL_IKE_NONCE].url =
98 "/rules/" KEYWORD_DOC_NONCE sigmatch_table[DETECT_AL_IKE_NONCE].desc =
99 "sticky buffer to match on the IKE nonce_payload";
100 sigmatch_table[DETECT_AL_IKE_NONCE].Setup = DetectNonceSetup;
101 sigmatch_table[DETECT_AL_IKE_NONCE].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
102
103 DetectAppLayerInspectEngineRegister2(BUFFER_NAME_NONCE, ALPROTO_IKE, SIG_FLAG_TOSERVER, 1,
104 DetectEngineInspectBufferGeneric, GetNonceData);
105
106 DetectAppLayerMpmRegister2(BUFFER_NAME_NONCE, SIG_FLAG_TOSERVER, 1, PrefilterGenericMpmRegister,
107 GetNonceData, ALPROTO_IKE, 1);
108
109 DetectAppLayerInspectEngineRegister2(BUFFER_NAME_NONCE, ALPROTO_IKE, SIG_FLAG_TOCLIENT, 1,
110 DetectEngineInspectBufferGeneric, GetNonceData);
111
112 DetectAppLayerMpmRegister2(BUFFER_NAME_NONCE, SIG_FLAG_TOCLIENT, 1, PrefilterGenericMpmRegister,
113 GetNonceData, ALPROTO_IKE, 1);
114
115 DetectBufferTypeSetDescriptionByName(BUFFER_NAME_NONCE, BUFFER_DESC_NONCE);
116
117 g_buffer_nonce_id = DetectBufferTypeGetByName(BUFFER_NAME_NONCE);
118 SCLogDebug("registering " BUFFER_NAME_NONCE " rule option");
119 }