diff --git a/doc/userguide/rules/vlan-id.rst b/doc/userguide/rules/vlan-id.rst new file mode 100644 index 000000000000..fcdd05f511dd --- /dev/null +++ b/doc/userguide/rules/vlan-id.rst @@ -0,0 +1,10 @@ +Vlan.id Keyword +============== + +Suricata has a ``vlan.id`` keyword that can be used in signatures to identify +and filter network packets based on Virtual Local Area Network IDs. + + +Signature example:: + + alert ip any any -> any any (msg:"Vlan ID is equal to 300"; vlan.id:300; sid:1;) \ No newline at end of file diff --git a/src/Makefile.am b/src/Makefile.am index 6970d709f35c..e17e330d6c7c 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -324,6 +324,7 @@ noinst_HEADERS = \ detect-urilen.h \ detect-within.h \ detect-xbits.h \ + detect-vlan-id.h \ device-storage.h \ feature.h \ flow-bit.h \ @@ -893,6 +894,7 @@ libsuricata_c_a_SOURCES = \ detect-urilen.c \ detect-within.c \ detect-xbits.c \ + detect-vlan-id.c \ device-storage.c \ feature.c \ flow-bit.c \ diff --git a/src/detect-engine-register.c b/src/detect-engine-register.c index 6ea3698c2c93..9809569aea98 100644 --- a/src/detect-engine-register.c +++ b/src/detect-engine-register.c @@ -260,6 +260,7 @@ #include "detect-ike-nonce-payload-length.h" #include "detect-ike-nonce-payload.h" #include "detect-ike-key-exchange-payload.h" +#include "detect-vlan-id.h" #include "action-globals.h" #include "tm-threads.h" @@ -686,6 +687,8 @@ void SigTableSetup(void) DetectFileHandlerRegister(); + DetectVlanIdRegister(); + ScDetectSNMPRegister(); ScDetectDHCPRegister(); ScDetectWebsocketRegister(); diff --git a/src/detect-engine-register.h b/src/detect-engine-register.h index c9134c77b83a..59bf3dd1cd0a 100644 --- a/src/detect-engine-register.h +++ b/src/detect-engine-register.h @@ -328,6 +328,8 @@ enum DetectKeywordId { DETECT_AL_JA4_HASH, + DETECT_VLAN_ID, + /* make sure this stays last */ DETECT_TBLSIZE_STATIC, }; diff --git a/src/detect-vlan-id.c b/src/detect-vlan-id.c new file mode 100644 index 000000000000..78927e1e60ed --- /dev/null +++ b/src/detect-vlan-id.c @@ -0,0 +1,99 @@ +/* Copyright (C) 2022 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. + */ + +#include "suricata-common.h" +#include "rust.h" +#include "detect-vlan-id.h" +#include "detect-engine.h" +#include "detect-engine-prefilter.h" +#include "detect-engine-uint.h" +#include "detect-parse.h" + +static int DetectVlanIdMatch( + DetectEngineThreadCtx *det_ctx, Packet *p, const Signature *s, const SigMatchCtx *ctx) +{ + if (p->vlan_idx == 0) { + return 0; + } + + const DetectU16Data *du16 = (const DetectU16Data *)ctx; + for (int i = 0; i < p->vlan_idx; i++) { + if (DetectU16Match(p->vlan_id[i], du16)) + return 1; + } + + return 0; +} + +static void DetectVlanIdFree(DetectEngineCtx *de_ctx, void *ptr) +{ + rs_detect_u16_free(ptr); +} + +static int DetectVlanIdSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr) +{ + DetectU16Data *du16 = DetectU16Parse(rawstr); + if (du16 == NULL) + return -1; + + if (SigMatchAppendSMToList( + de_ctx, s, DETECT_VLAN_ID, (SigMatchCtx *)du16, DETECT_SM_LIST_MATCH) == NULL) { + DetectVlanIdFree(de_ctx, du16); + return -1; + } + s->flags |= SIG_FLAG_REQUIRE_PACKET; + + return 0; +} + +static void PrefilterPacketVlanIdMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx) +{ + const PrefilterPacketHeaderCtx *ctx = pectx; + if (!PrefilterPacketHeaderExtraMatch(ctx, p)) + return; + + DetectU16Data du16; + du16.mode = ctx->v1.u8[0]; + du16.arg1 = ctx->v1.u16[1]; + du16.arg2 = ctx->v1.u16[2]; + if (DetectVlanIdMatch(det_ctx, p, NULL, (const SigMatchCtx *)&du16)) { + PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt); + } +} + +static int PrefilterSetupVlanId(DetectEngineCtx *de_ctx, SigGroupHead *sgh) +{ + return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_VLAN_ID, SIG_MASK_REQUIRE_FLOW, + PrefilterPacketU16Set, PrefilterPacketU16Compare, PrefilterPacketVlanIdMatch); +} + +static bool PrefilterVlanIdIsPrefilterable(const Signature *s) +{ + return PrefilterIsPrefilterableById(s, DETECT_VLAN_ID); +} + +void DetectVlanIdRegister(void) +{ + sigmatch_table[DETECT_VLAN_ID].name = "vlan.id"; + sigmatch_table[DETECT_VLAN_ID].desc = "match vlan id"; + sigmatch_table[DETECT_VLAN_ID].url = "/rules/vlan-id-keyword.html"; + sigmatch_table[DETECT_VLAN_ID].Match = DetectVlanIdMatch; + sigmatch_table[DETECT_VLAN_ID].Setup = DetectVlanIdSetup; + sigmatch_table[DETECT_VLAN_ID].Free = DetectVlanIdFree; + sigmatch_table[DETECT_VLAN_ID].SupportsPrefilter = PrefilterVlanIdIsPrefilterable; + sigmatch_table[DETECT_VLAN_ID].SetupPrefilter = PrefilterSetupVlanId; +} diff --git a/src/detect-vlan-id.h b/src/detect-vlan-id.h new file mode 100644 index 000000000000..69f327de8b32 --- /dev/null +++ b/src/detect-vlan-id.h @@ -0,0 +1,23 @@ +/* Copyright (C) 2022 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. + */ + +#ifndef SURICATA_DETECT_VLAN_ID_H +#define SURICATA_DETECT_VLAN_ID_H + +void DetectVlanIdRegister(void); + +#endif /* SURICATA_DETECT_VLAN_ID_H */