Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keyword to match on 'raw' TLS certificates #3851

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions doc/userguide/rules/tls-keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ Usage::

tls_cert_valid;

tls.cert
--------

Do a "raw" match on the TLS certificate. This keyword only matches on the
first certificate in the certificate chain.

Example::

alert tls any any -> any any (msg:"match bytes in TLS cert"; tls.cert; \
content:"|06 09 2a 86|"; sid:200070;)

``tls.cert`` is a 'sticky buffer'.

``tls.cert`` can be used as ``fast_pattern``.

tls.version
-----------

Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ detect-tls-cert-issuer.c detect-tls-cert-issuer.h \
detect-tls-cert-subject.c detect-tls-cert-subject.h \
detect-tls-cert-serial.c detect-tls-cert-serial.h \
detect-tls-cert-fingerprint.c detect-tls-cert-fingerprint.h \
detect-tls-cert.c detect-tls-cert.h \
detect-dsize.c detect-dsize.h \
detect-engine.c detect-engine.h \
detect-engine-address.c detect-engine-address.h \
Expand Down
2 changes: 2 additions & 0 deletions src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "detect-engine-dcepayload.h"
#include "detect-dns-query.h"
#include "detect-tls-sni.h"
#include "detect-tls-cert.h"
#include "detect-tls-cert-fingerprint.h"
#include "detect-tls-cert-issuer.h"
#include "detect-tls-cert-subject.h"
Expand Down Expand Up @@ -432,6 +433,7 @@ void SigTableSetup(void)
DetectTlsSubjectRegister();
DetectTlsSerialRegister();
DetectTlsFingerprintRegister();
DetectTlsCertRegister();

DetectTlsJa3HashRegister();
DetectTlsJa3StringRegister();
Expand Down
1 change: 1 addition & 0 deletions src/detect-engine-register.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ enum {

DETECT_AL_DNS_QUERY,
DETECT_AL_TLS_SNI,
DETECT_AL_TLS_CERT,
DETECT_AL_TLS_CERT_ISSUER,
DETECT_AL_TLS_CERT_SUBJECT,
DETECT_AL_TLS_CERT_SERIAL,
Expand Down
142 changes: 142 additions & 0 deletions src/detect-tls-cert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/* Copyright (C) 2019 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

/**
* \file
*
* \author Mats Klepsland <[email protected]>
*
* Implements support for tls.cert keyword.
*/

#include "suricata-common.h"
#include "threads.h"
#include "debug.h"
#include "decode.h"
#include "detect.h"

#include "detect-parse.h"
#include "detect-engine.h"
#include "detect-engine-mpm.h"
#include "detect-engine-prefilter.h"
#include "detect-content.h"
#include "detect-pcre.h"
#include "detect-tls-cert.h"

#include "flow.h"
#include "flow-util.h"
#include "flow-var.h"

#include "util-debug.h"
#include "util-unittest.h"
#include "util-spm.h"
#include "util-print.h"

#include "stream-tcp.h"

#include "app-layer.h"
#include "app-layer-ssl.h"

#include "util-unittest.h"
#include "util-unittest-helper.h"

static int DetectTlsCertSetup(DetectEngineCtx *, Signature *, const char *);
#ifdef UNITTESTS
static void DetectTlsCertRegisterTests(void);
#endif
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
const DetectEngineTransforms *transforms,
Flow *_f, const uint8_t _flow_flags,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should remove the underscores from the variables here as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah if they are used then you should

void *txv, const int list_id);
static int g_tls_cert_buffer_id = 0;

/**
* \brief Registration function for keyword: tls.cert
*/
void DetectTlsCertRegister(void)
{
sigmatch_table[DETECT_AL_TLS_CERT].name = "tls.cert";
sigmatch_table[DETECT_AL_TLS_CERT].desc = "content modifier to match the TLS certificate buffer";
sigmatch_table[DETECT_AL_TLS_CERT].url = DOC_URL DOC_VERSION "/rules/tls-keywords.html#tls.cert";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this should perhaps be changed to "tls-cert" instead of "tls.cert".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

sigmatch_table[DETECT_AL_TLS_CERT].Setup = DetectTlsCertSetup;
#ifdef UNITTESTS
sigmatch_table[DETECT_AL_TLS_CERT].RegisterTests = DetectTlsCertRegisterTests;
#endif
sigmatch_table[DETECT_AL_TLS_CERT].flags |= SIGMATCH_NOOPT;
victorjulien marked this conversation as resolved.
Show resolved Hide resolved

DetectAppLayerInspectEngineRegister2("tls.cert", ALPROTO_TLS,
SIG_FLAG_TOCLIENT, TLS_STATE_CERT_READY,
DetectEngineInspectBufferGeneric, GetData);

DetectAppLayerMpmRegister2("tls.cert", SIG_FLAG_TOCLIENT, 2,
PrefilterGenericMpmRegister, GetData, ALPROTO_TLS,
TLS_STATE_CERT_READY);

DetectBufferTypeSetDescriptionByName("tls.cert", "TLS certificate");

g_tls_cert_buffer_id = DetectBufferTypeGetByName("tls.cert");
}

/**
* \brief This function setup the tls.cert modifier keyword
*
* \param de_ctx Pointer to the Detect Engine Context
* \param s Pointer to the Signature to which the keyword belongs
* \param str Should hold an empty string always
*
* \retval 0 On success
* \retval -1 On failure
*/
static int DetectTlsCertSetup(DetectEngineCtx *de_ctx, Signature *s,
const char *str)
{
if (DetectBufferSetActiveList(s, g_tls_cert_buffer_id) < 0)
return -1;

if (DetectSignatureSetAppProto(s, ALPROTO_TLS) < 0)
return -1;

return 0;
}

static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
const DetectEngineTransforms *transforms, Flow *f,
const uint8_t flow_flags, void *txv, const int list_id)
{
InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
if (buffer->inspect == NULL) {
const SSLState *ssl_state = (SSLState *)f->alstate;

if (TAILQ_EMPTY(&ssl_state->server_connp.certs)) {
return NULL;
}

const SSLCertsChain *cert = TAILQ_FIRST(&ssl_state->server_connp.certs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we inspecting only one cert?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see this is mentioned in the PR. Would it make sense to inspect all? Or would there be a case for a separate keyword for all certs or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not really a good reason to only inspect one certificate. I'll change it to inspect the entire certificate chain :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool. This will complicate the inspection code somewhat, because you can now have multiple buffers per tx. I would suggest checking the dns query code which has the same issue.

if (cert == NULL) {
return NULL;
}

InspectionBufferSetup(buffer, cert->cert_data, cert->cert_len);
InspectionBufferApplyTransforms(buffer, transforms);
}

return buffer;
}

#ifdef UNITTESTS
#include "tests/detect-tls-cert.c"
#endif
29 changes: 29 additions & 0 deletions src/detect-tls-cert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (C) 2019 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

/**
* \file
*
* \author Mats Klepsland <[email protected]>
*/

#ifndef __DETECT_TLS_CERT_H__
#define __DETECT_TLS_CERT_H__

void DetectTlsCertRegister(void);

#endif /* __DETECT_TLS_CERT_H__ */
Loading