diff --git a/src/Makefile.am b/src/Makefile.am index 44ff227012..d5bbb60c1c 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -228,6 +228,7 @@ noinst_HEADERS = \ detect-id.h \ detect-igmphdr.h \ detect-igmp-type.h \ + detect-sctphdr.h \ detect-ipaddr.h \ detect-ipopts.h \ detect-ipproto.h \ @@ -804,6 +805,7 @@ libsuricata_c_a_SOURCES = \ detect-id.c \ detect-igmphdr.c \ detect-igmp-type.c \ + detect-sctphdr.c \ detect-ipaddr.c \ detect-ipopts.c \ detect-ipproto.c \ diff --git a/src/detect-engine-register.c b/src/detect-engine-register.c index 1b441fc296..cf8de45b35 100644 --- a/src/detect-engine-register.c +++ b/src/detect-engine-register.c @@ -155,6 +155,7 @@ #include "detect-icmpv4hdr.h" #include "detect-igmphdr.h" #include "detect-igmp-type.h" +#include "detect-sctphdr.h" #include "detect-urilen.h" #include "detect-bsize.h" #include "detect-detection-filter.h" @@ -676,6 +677,7 @@ void SigTableSetup(void) DetectIcmpv4HdrRegister(); DetectIGMPHdrRegister(); DetectIGMPTypeRegister(); + DetectSCTPHdrRegister(); DetectTlsRegister(); DetectTlsValidityRegister(); DetectTlsVersionRegister(); diff --git a/src/detect-engine-register.h b/src/detect-engine-register.h index 0538d998a9..92a57c8900 100644 --- a/src/detect-engine-register.h +++ b/src/detect-engine-register.h @@ -51,6 +51,7 @@ enum DetectKeywordId { DETECT_ICMPV4HDR, DETECT_IGMPHDR, DETECT_IGMP_TYPE, + DETECT_SCTPHDR, DETECT_DSIZE, DETECT_FLOW, diff --git a/src/detect-sctphdr.c b/src/detect-sctphdr.c new file mode 100644 index 0000000000..72ae60dbc8 --- /dev/null +++ b/src/detect-sctphdr.c @@ -0,0 +1,111 @@ +/* Copyright (C) 2026 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 + * + * Implements sctp.hdr sticky buffer + * + * Author: Giuseppe Longo + */ + +#include "suricata-common.h" + +#include "detect.h" +#include "detect-engine.h" +#include "detect-engine-buffer.h" +#include "detect-engine-mpm.h" +#include "detect-sctphdr.h" +#include "detect-engine-prefilter.h" + +static int DetectSCTPHdrSetup(DetectEngineCtx *, Signature *, const char *); + +static int g_sctphdr_buffer_id = 0; + +static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, + const DetectEngineTransforms *transforms, Packet *p, const int list_id); + +void DetectSCTPHdrRegister(void) +{ + sigmatch_table[DETECT_SCTPHDR].name = "sctp.hdr"; + sigmatch_table[DETECT_SCTPHDR].desc = "sticky buffer to match on the SCTP header"; + sigmatch_table[DETECT_SCTPHDR].url = "/rules/sctp-keywords.html#sctp-hdr"; + sigmatch_table[DETECT_SCTPHDR].Setup = DetectSCTPHdrSetup; + sigmatch_table[DETECT_SCTPHDR].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; + + g_sctphdr_buffer_id = DetectBufferTypeRegister("sctp.hdr"); + BUG_ON(g_sctphdr_buffer_id < 0); + + DetectBufferTypeSupportsPacket("sctp.hdr"); + + DetectPktMpmRegister("sctp.hdr", 2, PrefilterGenericMpmPktRegister, GetData); + + DetectPktInspectEngineRegister("sctp.hdr", GetData, DetectEngineInspectPktBufferGeneric); +} + +/** + * \brief setup sctp.hdr sticky buffer + * + * \param de_ctx pointer to the Detection Engine Context + * \param s pointer to the current Signature + * \param _unused unused + * + * \retval 0 on Success + * \retval -1 on Failure + */ +static int DetectSCTPHdrSetup(DetectEngineCtx *de_ctx, Signature *s, const char *_unused) +{ + if (!(DetectProtoContainsProto(s->proto, IPPROTO_SCTP))) + return -1; + + s->flags |= SIG_FLAG_REQUIRE_PACKET; + + if (SCDetectBufferSetActiveList(de_ctx, s, g_sctphdr_buffer_id) < 0) + return -1; + + return 0; +} + +static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, + const DetectEngineTransforms *transforms, Packet *p, const int list_id) +{ + SCEnter(); + + if (!PacketIsSCTP(p)) { + SCReturnPtr(NULL, "InspectionBuffer"); + } + + InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id); + if (buffer->inspect == NULL) { + const SCTPHdr *sctph = PacketGetSCTP(p); + const uint16_t hlen = p->l4.vars.sctp.hlen; + if (((uint8_t *)sctph + (ptrdiff_t)hlen) > + ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) { + SCLogDebug("data out of range: %p > %p", ((uint8_t *)sctph + (ptrdiff_t)hlen), + ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))); + SCReturnPtr(NULL, "InspectionBuffer"); + } + + const uint32_t data_len = hlen; + const uint8_t *data = (const uint8_t *)sctph; + + SCInspectionBufferSetupAndApplyTransforms( + det_ctx, list_id, buffer, data, data_len, transforms); + } + + SCReturnPtr(buffer, "InspectionBuffer"); +} diff --git a/src/detect-sctphdr.h b/src/detect-sctphdr.h new file mode 100644 index 0000000000..02d4715736 --- /dev/null +++ b/src/detect-sctphdr.h @@ -0,0 +1,27 @@ +/* Copyright (C) 2026 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 + */ + +#ifndef SURICATA_DETECT_SCTPHDR_H +#define SURICATA_DETECT_SCTPHDR_H + +void DetectSCTPHdrRegister(void); + +#endif /* SURICATA_DETECT_SCTPHDR_H */