suricata
detect-tls-cert-serial.c
Go to the documentation of this file.
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"
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(const DetectEngineCtx *de_ctx,
65  Signature *s);
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  */
74 {
75  sigmatch_table[DETECT_TLS_CERT_SERIAL].name = "tls.cert_serial";
76  sigmatch_table[DETECT_TLS_CERT_SERIAL].alias = "tls_cert_serial";
78  "sticky buffer to match the TLS cert serial buffer";
79  sigmatch_table[DETECT_TLS_CERT_SERIAL].url = "/rules/tls-keywords.html#tls-cert-serial";
80  sigmatch_table[DETECT_TLS_CERT_SERIAL].Setup = DetectTlsSerialSetup;
81 #ifdef UNITTESTS
82  sigmatch_table[DETECT_TLS_CERT_SERIAL].RegisterTests = DetectTlsSerialRegisterTests;
83 #endif
86 
89 
92 
95 
98 
99  DetectBufferTypeSetDescriptionByName("tls.cert_serial",
100  "TLS certificate serial number");
101 
102  DetectBufferTypeRegisterSetupCallback("tls.cert_serial",
103  DetectTlsSerialSetupCallback);
104 
106  DetectTlsSerialValidateCallback);
107 
108  g_tls_cert_serial_buffer_id = DetectBufferTypeGetByName("tls.cert_serial");
109 }
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 {
123  if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_cert_serial_buffer_id) < 0)
124  return -1;
125 
127  return -1;
128 
129  return 0;
130 }
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 {
136  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
137  if (buffer->inspect == NULL) {
138  const SSLState *ssl_state = (SSLState *)f->alstate;
139  const SSLStateConnp *connp;
140 
141  if (flow_flags & STREAM_TOSERVER) {
142  connp = &ssl_state->client_connp;
143  } else {
144  connp = &ssl_state->server_connp;
145  }
146 
147  if (connp->cert0_serial == NULL) {
148  return NULL;
149  }
150 
151  const uint32_t data_len = (uint32_t)strlen(connp->cert0_serial);
152  const uint8_t *data = (uint8_t *)connp->cert0_serial;
153 
155  det_ctx, list_id, buffer, data, data_len, transforms);
156  }
157 
158  return buffer;
159 }
160 
161 static bool DetectTlsSerialValidateCallback(
162  const Signature *s, const char **sigerror, const DetectBufferType *dbt)
163 {
164  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
165  if (s->init_data->buffers[x].id != (uint32_t)dbt->id)
166  continue;
167  const SigMatch *sm = s->init_data->buffers[x].head;
168  for (; sm != NULL; sm = sm->next) {
169  if (sm->type != DETECT_CONTENT)
170  continue;
171 
172  const DetectContentData *cd = (DetectContentData *)sm->ctx;
173 
174  if (cd->flags & DETECT_CONTENT_NOCASE) {
175  *sigerror = "tls.cert_serial should not be used together "
176  "with nocase, since the rule is automatically "
177  "uppercased anyway which makes nocase redundant.";
178  SCLogWarning("rule %u: %s", s->id, *sigerror);
179  }
180 
181  /* no need to worry about this if the content is short enough */
182  if (cd->content_len <= 2)
183  return true;
184 
185  uint32_t u;
186  for (u = 0; u < cd->content_len; u++)
187  if (cd->content[u] == ':')
188  return true;
189 
190  *sigerror = "No colon delimiters ':' detected in content after "
191  "tls.cert_serial. This rule will therefore never "
192  "match.";
193  SCLogWarning("rule %u: %s", s->id, *sigerror);
194 
195  return false;
196  }
197  }
198  return true;
199 }
200 
201 static void DetectTlsSerialSetupCallback(const DetectEngineCtx *de_ctx,
202  Signature *s)
203 {
204  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
205  if (s->init_data->buffers[x].id != (uint32_t)g_tls_cert_serial_buffer_id)
206  continue;
207  SigMatch *sm = s->init_data->buffers[x].head;
208  for (; sm != NULL; sm = sm->next) {
209  if (sm->type != DETECT_CONTENT)
210  continue;
211 
213 
214  bool changed = false;
215  uint32_t u;
216  for (u = 0; u < cd->content_len; u++) {
217  if (islower(cd->content[u])) {
218  cd->content[u] = u8_toupper(cd->content[u]);
219  changed = true;
220  }
221  }
222 
223  /* recreate the context if changes were made */
224  if (changed) {
225  SpmDestroyCtx(cd->spm_ctx);
226  cd->spm_ctx =
228  }
229  }
230  }
231 }
232 
233 #ifdef UNITTESTS
235 #endif
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
SigTableElmt_::url
const char * url
Definition: detect.h:1431
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2313
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:548
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:301
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1645
DETECT_TLS_CERT_SERIAL
@ DETECT_TLS_CERT_SERIAL
Definition: detect-engine-register.h:251
SigTableElmt_::desc
const char * desc
Definition: detect.h:1430
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2103
flow-util.h
DetectTlsSerialRegister
void DetectTlsSerialRegister(void)
Registration function for keyword: tls.cert_serial.
Definition: detect-tls-cert-serial.c:73
SigTableElmt_::name
const char * name
Definition: detect.h:1428
InspectionBufferSetupAndApplyTransforms
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
Definition: detect-engine.c:1570
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:415
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:322
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:69
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
detect-tls-cert-serial.c
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:323
u8_toupper
#define u8_toupper(c)
Definition: suricata-common.h:454
SSLStateConnp_
Definition: app-layer-ssl.h:240
InspectionBuffer
Definition: detect.h:380
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *))
Definition: detect-engine.c:1351
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1422
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1369
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectBufferType_
Definition: detect.h:464
DetectContentData_
Definition: detect-content.h:93
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1429
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:270
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1223
util-print.h
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:360
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1547
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:359
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:749
TLS_STATE_SERVER_CERT_DONE
@ TLS_STATE_SERVER_CERT_DONE
Definition: app-layer-ssl.h:88
DetectBufferType_::id
int id
Definition: detect.h:467
SigTableElmt_::alias
const char * alias
Definition: detect.h:1429
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:357
util-spm.h
DetectContentData_::content
uint8_t * content
Definition: detect-content.h:94
detect-engine-buffer.h
TLS_STATE_CLIENT_CERT_DONE
@ TLS_STATE_CLIENT_CERT_DONE
Definition: app-layer-ssl.h:80
DetectContentData_::spm_ctx
SpmCtx * spm_ctx
Definition: detect-content.h:111
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:649
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:381
str
#define str(s)
Definition: suricata-common.h:308
Flow_::alstate
void * alstate
Definition: flow.h:479
Signature_::id
uint32_t id
Definition: detect.h:715
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:539
Signature_
Signature container.
Definition: detect.h:670
SigMatch_
a single match condition for a signature
Definition: detect.h:356
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1620
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:245
detect-tls-cert-serial.h
DetectEngineCtx_::spm_global_thread_ctx
SpmGlobalThreadCtx * spm_global_thread_ctx
Definition: detect.h:985
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1254
flow.h
SpmDestroyCtx
void SpmDestroyCtx(SpmCtx *ctx)
Definition: util-spm.c:183
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:650
flow-var.h
SSLStateConnp_::cert0_serial
char * cert0_serial
Definition: app-layer-ssl.h:262
SpmInitCtx
SpmCtx * SpmInitCtx(const uint8_t *needle, uint16_t needle_len, int nocase, SpmGlobalThreadCtx *global_thread_ctx)
Definition: util-spm.c:173
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1420
app-layer.h