/src/suricata/src/detect-tls-cert-serial.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_serial 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-buffer.h" |
34 | | #include "detect-engine-mpm.h" |
35 | | #include "detect-engine-prefilter.h" |
36 | | #include "detect-content.h" |
37 | | #include "detect-pcre.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 | | #include "detect-tls-cert-serial.h" |
52 | | |
53 | | #include "util-unittest.h" |
54 | | #include "util-unittest-helper.h" |
55 | | |
56 | | static int DetectTlsSerialSetup(DetectEngineCtx *, Signature *, const char *); |
57 | | #ifdef UNITTESTS |
58 | | static void DetectTlsSerialRegisterTests(void); |
59 | | #endif |
60 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
61 | | const DetectEngineTransforms *transforms, |
62 | | Flow *f, const uint8_t flow_flags, |
63 | | void *txv, const int list_id); |
64 | | static void DetectTlsSerialSetupCallback( |
65 | | const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map); |
66 | | static bool DetectTlsSerialValidateCallback( |
67 | | const Signature *s, const char **sigerror, const DetectBufferType *dbt); |
68 | | static int g_tls_cert_serial_buffer_id = 0; |
69 | | |
70 | | /** |
71 | | * \brief Registration function for keyword: tls.cert_serial |
72 | | */ |
73 | | void DetectTlsSerialRegister(void) |
74 | 74 | { |
75 | 74 | sigmatch_table[DETECT_TLS_CERT_SERIAL].name = "tls.cert_serial"; |
76 | 74 | sigmatch_table[DETECT_TLS_CERT_SERIAL].alias = "tls_cert_serial"; |
77 | 74 | sigmatch_table[DETECT_TLS_CERT_SERIAL].desc = |
78 | 74 | "sticky buffer to match the TLS cert serial buffer"; |
79 | 74 | sigmatch_table[DETECT_TLS_CERT_SERIAL].url = "/rules/tls-keywords.html#tls-cert-serial"; |
80 | 74 | sigmatch_table[DETECT_TLS_CERT_SERIAL].Setup = DetectTlsSerialSetup; |
81 | | #ifdef UNITTESTS |
82 | | sigmatch_table[DETECT_TLS_CERT_SERIAL].RegisterTests = DetectTlsSerialRegisterTests; |
83 | | #endif |
84 | 74 | sigmatch_table[DETECT_TLS_CERT_SERIAL].flags |= SIGMATCH_NOOPT; |
85 | 74 | sigmatch_table[DETECT_TLS_CERT_SERIAL].flags |= SIGMATCH_INFO_STICKY_BUFFER; |
86 | | |
87 | 74 | DetectAppLayerInspectEngineRegister("tls.cert_serial", ALPROTO_TLS, SIG_FLAG_TOCLIENT, |
88 | 74 | TLS_STATE_SERVER_CERT_DONE, DetectEngineInspectBufferGeneric, GetData); |
89 | | |
90 | 74 | DetectAppLayerMpmRegister("tls.cert_serial", SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister, |
91 | 74 | GetData, ALPROTO_TLS, TLS_STATE_SERVER_CERT_DONE); |
92 | | |
93 | 74 | DetectAppLayerInspectEngineRegister("tls.cert_serial", ALPROTO_TLS, SIG_FLAG_TOSERVER, |
94 | 74 | TLS_STATE_CLIENT_CERT_DONE, DetectEngineInspectBufferGeneric, GetData); |
95 | | |
96 | 74 | DetectAppLayerMpmRegister("tls.cert_serial", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, |
97 | 74 | GetData, ALPROTO_TLS, TLS_STATE_CLIENT_CERT_DONE); |
98 | | |
99 | 74 | DetectBufferTypeSetDescriptionByName("tls.cert_serial", |
100 | 74 | "TLS certificate serial number"); |
101 | | |
102 | 74 | DetectBufferTypeRegisterSetupCallback("tls.cert_serial", |
103 | 74 | DetectTlsSerialSetupCallback); |
104 | | |
105 | 74 | DetectBufferTypeRegisterValidateCallback("tls.cert_serial", |
106 | 74 | DetectTlsSerialValidateCallback); |
107 | | |
108 | 74 | g_tls_cert_serial_buffer_id = DetectBufferTypeGetByName("tls.cert_serial"); |
109 | 74 | } |
110 | | |
111 | | /** |
112 | | * \brief this function setup the tls_cert_serial modifier keyword used in the rule |
113 | | * |
114 | | * \param de_ctx Pointer to the Detection Engine Context |
115 | | * \param s Pointer to the Signature to which the current keyword belongs |
116 | | * \param str Should hold an empty string always |
117 | | * |
118 | | * \retval 0 On success |
119 | | * \retval -1 On failure |
120 | | */ |
121 | | static int DetectTlsSerialSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str) |
122 | 5.78k | { |
123 | 5.78k | if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_cert_serial_buffer_id) < 0) |
124 | 2 | return -1; |
125 | | |
126 | 5.77k | if (SCDetectSignatureSetAppProto(s, ALPROTO_TLS) < 0) |
127 | 108 | return -1; |
128 | | |
129 | 5.67k | return 0; |
130 | 5.77k | } |
131 | | |
132 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
133 | | const DetectEngineTransforms *transforms, Flow *f, |
134 | | const uint8_t flow_flags, void *txv, const int list_id) |
135 | 209 | { |
136 | 209 | InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id); |
137 | 209 | if (buffer->inspect == NULL) { |
138 | 202 | const SSLState *ssl_state = (SSLState *)f->alstate; |
139 | 202 | const SSLStateConnp *connp; |
140 | | |
141 | 202 | if (flow_flags & STREAM_TOSERVER) { |
142 | 111 | connp = &ssl_state->client_connp; |
143 | 111 | } else { |
144 | 91 | connp = &ssl_state->server_connp; |
145 | 91 | } |
146 | | |
147 | 202 | if (connp->cert0_serial == NULL) { |
148 | 123 | return NULL; |
149 | 123 | } |
150 | | |
151 | 79 | const uint32_t data_len = connp->cert0_serial_len; |
152 | 79 | const uint8_t *data = connp->cert0_serial; |
153 | | |
154 | 79 | SCInspectionBufferSetupAndApplyTransforms( |
155 | 79 | det_ctx, list_id, buffer, data, data_len, transforms); |
156 | 79 | } |
157 | | |
158 | 86 | return buffer; |
159 | 209 | } |
160 | | |
161 | | static bool DetectTlsSerialValidateCallback( |
162 | | const Signature *s, const char **sigerror, const DetectBufferType *dbt) |
163 | 2.60k | { |
164 | 7.63k | for (uint32_t x = 0; x < s->init_data->buffer_index; x++) { |
165 | 5.23k | if (s->init_data->buffers[x].id != (uint32_t)dbt->id) |
166 | 4.89k | continue; |
167 | 338 | const SigMatch *sm = s->init_data->buffers[x].head; |
168 | 2.10k | for (; sm != NULL; sm = sm->next) { |
169 | 1.97k | if (sm->type != DETECT_CONTENT) |
170 | 1.76k | continue; |
171 | | |
172 | 205 | const DetectContentData *cd = (DetectContentData *)sm->ctx; |
173 | | |
174 | 205 | if (cd->flags & DETECT_CONTENT_NOCASE) { |
175 | 3 | *sigerror = "tls.cert_serial should not be used together " |
176 | 3 | "with nocase, since the rule is automatically " |
177 | 3 | "uppercased anyway which makes nocase redundant."; |
178 | 3 | SCLogWarning("rule %u: %s", s->id, *sigerror); |
179 | 3 | } |
180 | | |
181 | | /* no need to worry about this if the content is short enough */ |
182 | 205 | if (cd->content_len <= 2) |
183 | 92 | return true; |
184 | | |
185 | 113 | uint32_t u; |
186 | 1.85k | for (u = 0; u < cd->content_len; u++) |
187 | 1.82k | if (cd->content[u] == ':') |
188 | 90 | return true; |
189 | | |
190 | 23 | *sigerror = "No colon delimiters ':' detected in content after " |
191 | 23 | "tls.cert_serial. This rule will therefore never " |
192 | 23 | "match."; |
193 | 23 | SCLogWarning("rule %u: %s", s->id, *sigerror); |
194 | | |
195 | 23 | return false; |
196 | 113 | } |
197 | 338 | } |
198 | 2.39k | return true; |
199 | 2.60k | } |
200 | | |
201 | | static void DetectTlsSerialSetupCallback( |
202 | | const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map) |
203 | 4.93k | { |
204 | 17.6k | for (uint32_t x = 0; x < s->init_data->buffer_index; x++) { |
205 | 12.7k | if (s->init_data->buffers[x].id != (uint32_t)g_tls_cert_serial_buffer_id) |
206 | 11.7k | continue; |
207 | 974 | SigMatch *sm = s->init_data->buffers[x].head; |
208 | 12.2k | for (; sm != NULL; sm = sm->next) { |
209 | 11.2k | if (sm->type != DETECT_CONTENT) |
210 | 8.20k | continue; |
211 | | |
212 | 3.06k | DetectContentData *cd = (DetectContentData *)sm->ctx; |
213 | | |
214 | 3.06k | bool changed = false; |
215 | 3.06k | uint32_t u; |
216 | 22.9k | for (u = 0; u < cd->content_len; u++) { |
217 | 19.8k | if (islower(cd->content[u])) { |
218 | 1.70k | cd->content[u] = u8_toupper(cd->content[u]); |
219 | 1.70k | changed = true; |
220 | 1.70k | } |
221 | 19.8k | } |
222 | | |
223 | | /* recreate the context if changes were made */ |
224 | 3.06k | if (changed) { |
225 | 311 | SpmDestroyCtx(cd->spm_ctx); |
226 | 311 | cd->spm_ctx = |
227 | 311 | SpmInitCtx(cd->content, cd->content_len, 1, de_ctx->spm_global_thread_ctx); |
228 | 311 | } |
229 | 3.06k | } |
230 | 974 | } |
231 | 4.93k | } |
232 | | |
233 | | #ifdef UNITTESTS |
234 | | #include "tests/detect-tls-cert-serial.c" |
235 | | #endif |