Skip to content

Commit

Permalink
vlan: add vlan.id keyword
Browse files Browse the repository at this point in the history
Ticket: OISF#1065
  • Loading branch information
AkakiAlice committed Oct 31, 2024
1 parent 3a7eef8 commit 619b515
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/userguide/rules/vlan-id.rst
Original file line number Diff line number Diff line change
@@ -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;)
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
3 changes: 3 additions & 0 deletions src/detect-engine-register.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -686,6 +687,8 @@ void SigTableSetup(void)

DetectFileHandlerRegister();

DetectVlanIdRegister();

ScDetectSNMPRegister();
ScDetectDHCPRegister();
ScDetectWebsocketRegister();
Expand Down
2 changes: 2 additions & 0 deletions src/detect-engine-register.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ enum DetectKeywordId {

DETECT_AL_JA4_HASH,

DETECT_VLAN_ID,

/* make sure this stays last */
DETECT_TBLSIZE_STATIC,
};
Expand Down
99 changes: 99 additions & 0 deletions src/detect-vlan-id.c
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions src/detect-vlan-id.h
Original file line number Diff line number Diff line change
@@ -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 */

0 comments on commit 619b515

Please sign in to comment.