suricata
detect-tls-cert-fingerprint.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_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-buffer.h"
34 #include "detect-engine-mpm.h"
36 #include "detect-content.h"
37 #include "detect-pcre.h"
39 
40 #include "flow.h"
41 #include "flow-util.h"
42 #include "flow-var.h"
43 
44 #include "util-debug.h"
45 #include "util-spm.h"
46 #include "util-print.h"
47 
48 #include "stream-tcp.h"
49 
50 #include "app-layer.h"
51 #include "app-layer-ssl.h"
52 
53 #include "util-unittest.h"
54 #include "util-unittest-helper.h"
55 
56 static int DetectTlsFingerprintSetup(DetectEngineCtx *, Signature *, const char *);
57 #ifdef UNITTESTS
58 static void DetectTlsFingerprintRegisterTests(void);
59 #endif
60 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
61  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flow_flags, void *txv,
62  const int list_id);
63 static bool DetectTlsFingerprintValidateCallback(
64  const Signature *s, const char **sigerror, const DetectBufferType *dbt);
65 static int g_tls_cert_fingerprint_buffer_id = 0;
66 
67 /**
68  * \brief Registration function for keyword: tls.cert_fingerprint
69  */
71 {
72  sigmatch_table[DETECT_TLS_CERT_FINGERPRINT].name = "tls.cert_fingerprint";
73  sigmatch_table[DETECT_TLS_CERT_FINGERPRINT].alias = "tls_cert_fingerprint";
75  "sticky buffer to match the TLS cert fingerprint buffer";
77  "/rules/tls-keywords.html#tls-cert-fingerprint";
78  sigmatch_table[DETECT_TLS_CERT_FINGERPRINT].Setup = DetectTlsFingerprintSetup;
79 #ifdef UNITTESTS
80  sigmatch_table[DETECT_TLS_CERT_FINGERPRINT].RegisterTests = DetectTlsFingerprintRegisterTests;
81 #endif
84 
87 
88  DetectAppLayerMpmRegister("tls.cert_fingerprint", SIG_FLAG_TOCLIENT, 2,
90 
93 
94  DetectAppLayerMpmRegister("tls.cert_fingerprint", SIG_FLAG_TOSERVER, 2,
96 
97  DetectBufferTypeSetDescriptionByName("tls.cert_fingerprint",
98  "TLS certificate fingerprint");
99 
101 
102  DetectBufferTypeRegisterValidateCallback("tls.cert_fingerprint",
103  DetectTlsFingerprintValidateCallback);
104 
105  g_tls_cert_fingerprint_buffer_id = DetectBufferTypeGetByName("tls.cert_fingerprint");
106 }
107 
108 /**
109  * \brief this function setup the tls_cert_fingerprint modifier keyword used in the rule
110  *
111  * \param de_ctx Pointer to the Detection Engine Context
112  * \param s Pointer to the Signature to which the current keyword belongs
113  * \param str Should hold an empty string always
114  *
115  * \retval 0 On success
116  * \retval -1 On failure
117  */
118 static int DetectTlsFingerprintSetup(DetectEngineCtx *de_ctx, Signature *s,
119  const char *str)
120 {
121  if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_cert_fingerprint_buffer_id) < 0)
122  return -1;
123 
125  return -1;
126 
127  return 0;
128 }
129 
130 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
131  const DetectEngineTransforms *transforms, Flow *f,
132  const uint8_t flow_flags, void *txv, const int list_id)
133 {
134  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
135  if (buffer->inspect == NULL) {
136  const SSLState *ssl_state = (SSLState *)f->alstate;
137  const SSLStateConnp *connp;
138 
139  if (flow_flags & STREAM_TOSERVER) {
140  connp = &ssl_state->client_connp;
141  } else {
142  connp = &ssl_state->server_connp;
143  }
144 
145  if (connp->cert0_fingerprint == NULL) {
146  return NULL;
147  }
148 
149  const uint32_t data_len = (uint32_t)strlen(connp->cert0_fingerprint);
150  const uint8_t *data = (uint8_t *)connp->cert0_fingerprint;
151 
153  det_ctx, list_id, buffer, data, data_len, transforms);
154  }
155 
156  return buffer;
157 }
158 
159 static bool DetectTlsFingerprintValidateCallback(
160  const Signature *s, const char **sigerror, const DetectBufferType *dbt)
161 {
162  for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
163  if (s->init_data->buffers[x].id != (uint32_t)dbt->id)
164  continue;
165  const SigMatch *sm = s->init_data->buffers[x].head;
166  for (; sm != NULL; sm = sm->next) {
167  if (sm->type != DETECT_CONTENT)
168  continue;
169 
170  const DetectContentData *cd = (DetectContentData *)sm->ctx;
171 
172  if (cd->content_len != 59) {
173  *sigerror = "Invalid length of the specified fingerprint. "
174  "This rule will therefore never match.";
175  SCLogWarning("rule %u: %s", s->id, *sigerror);
176  return false;
177  }
178 
179  bool have_delimiters = false;
180  uint32_t u;
181  for (u = 0; u < cd->content_len; u++) {
182  if (cd->content[u] == ':') {
183  have_delimiters = true;
184  break;
185  }
186  }
187 
188  if (!have_delimiters) {
189  *sigerror = "No colon delimiters ':' detected in content after "
190  "tls.cert_fingerprint. This rule will therefore "
191  "never match.";
192  SCLogWarning("rule %u: %s", s->id, *sigerror);
193  return false;
194  }
195 
196  if (cd->flags & DETECT_CONTENT_NOCASE) {
197  *sigerror = "tls.cert_fingerprint should not be used together "
198  "with nocase, since the rule is automatically "
199  "lowercased anyway which makes nocase redundant.";
200  SCLogWarning("rule %u: %s", s->id, *sigerror);
201  }
202  }
203  }
204  return true;
205 }
206 
207 #ifdef UNITTESTS
208 #include "detect-engine-alert.h"
210 #endif
DETECT_CONTENT_NOCASE
#define DETECT_CONTENT_NOCASE
Definition: detect-content.h:29
SigTableElmt_::url
const char * url
Definition: detect.h:1461
SignatureInitDataBuffer_::head
SigMatch * head
Definition: detect.h:534
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:237
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1675
DetectBufferTypeRegisterSetupCallback
void DetectBufferTypeRegisterSetupCallback(const char *name, void(*SetupCallback)(const DetectEngineCtx *, Signature *, const DetectBufferType *))
Definition: detect-engine.c:1463
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
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:2049
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1458
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:390
DetectTlsFingerprintRegister
void DetectTlsFingerprintRegister(void)
Registration function for keyword: tls.cert_fingerprint.
Definition: detect-tls-cert-fingerprint.c:70
detect-tls-cert-fingerprint.h
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:258
DETECT_CONTENT
@ DETECT_CONTENT
Definition: detect-engine-register.h:69
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:259
SSLStateConnp_
Definition: app-layer-ssl.h:173
InspectionBuffer
Definition: detect-engine-inspect-buffer.h:34
threads.h
Flow_
Flow data structure.
Definition: flow.h:347
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectBufferTypeRegisterValidateCallback
void DetectBufferTypeRegisterValidateCallback(const char *name, bool(*ValidateCallback)(const Signature *, const char **sigerror, const DetectBufferType *))
Definition: detect-engine.c:1481
DETECT_TLS_CERT_FINGERPRINT
@ DETECT_TLS_CERT_FINGERPRINT
Definition: detect-engine-register.h:239
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
DetectBufferType_
Definition: detect.h:448
DetectContentData_
Definition: detect-content.h:93
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2235
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine-inspect-buffer.c:56
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
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:19
detect-tls-cert-fingerprint.c
DetectEngineThreadCtx_
Definition: detect.h:1245
util-print.h
detect-engine-mpm.h
detect.h
SigMatch_::next
struct SigMatch_ * next
Definition: detect.h:359
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:1577
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
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:152
SigMatch_::ctx
SigMatchCtx * ctx
Definition: detect.h:358
detect-engine-alert.h
DetectContentData_::flags
uint32_t flags
Definition: detect-content.h:104
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:747
TLS_STATE_SERVER_CERT_DONE
@ TLS_STATE_SERVER_CERT_DONE
Definition: app-layer-ssl.h:89
DetectBufferType_::id
int id
Definition: detect.h:451
SigTableElmt_::alias
const char * alias
Definition: detect.h:1459
suricata-common.h
SigMatch_::type
uint16_t type
Definition: detect.h:356
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:81
SignatureInitData_::buffers
SignatureInitDataBuffer * buffers
Definition: detect.h:647
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect-engine-inspect-buffer.h:35
str
#define str(s)
Definition: suricata-common.h:308
Flow_::alstate
void * alstate
Definition: flow.h:472
Signature_::id
uint32_t id
Definition: detect.h:713
detect-parse.h
SignatureInitDataBuffer_::id
uint32_t id
Definition: detect.h:525
Signature_
Signature container.
Definition: detect.h:668
SigMatch_
a single match condition for a signature
Definition: detect.h:355
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-inspect-buffer.c:197
DetectContentData_::content_len
uint16_t content_len
Definition: detect-content.h:95
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1650
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:273
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1375
SSLStateConnp_::cert0_fingerprint
char * cert0_fingerprint
Definition: app-layer-ssl.h:201
flow.h
SignatureInitData_::buffer_index
uint32_t buffer_index
Definition: detect.h:648
flow-var.h
DetectLowerSetupCallback
void DetectLowerSetupCallback(const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
Definition: detect-engine.c:5120
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447
app-layer.h