suricata
detect-gid.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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 Breno Silva <breno.silva@gmail.com>
22  *
23  * Implements the gid keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "suricata.h"
28 #include "decode.h"
29 #include "detect.h"
30 #include "detect-engine.h"
31 #include "detect-parse.h"
32 #include "flow-var.h"
33 #include "decode-events.h"
34 
35 #include "detect-gid.h"
36 #include "util-byte.h"
37 #include "util-debug.h"
38 
39 static int DetectGidSetup (DetectEngineCtx *, Signature *, const char *);
40 #ifdef UNITTESTS
41 static void GidRegisterTests(void);
42 #endif
43 
44 /**
45  * \brief Registration function for gid: keyword
46  */
47 
48 void DetectGidRegister (void)
49 {
51  sigmatch_table[DETECT_GID].desc = "give different groups of signatures another id value";
52  sigmatch_table[DETECT_GID].url = "/rules/meta.html#gid-group-id";
54  sigmatch_table[DETECT_GID].Setup = DetectGidSetup;
56 #ifdef UNITTESTS
57  sigmatch_table[DETECT_GID].RegisterTests = GidRegisterTests;
58 #endif
59 }
60 
61 /**
62  * \internal
63  * \brief this function is used to add the parsed gid into the current signature
64  *
65  * \param de_ctx pointer to the Detection Engine Context
66  * \param s pointer to the Current Signature
67  * \param rawstr pointer to the user provided gid options
68  *
69  * \retval 0 on Success
70  * \retval -1 on Failure
71  */
72 static int DetectGidSetup (DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
73 {
74  uint32_t gid = 0;
75  if (ByteExtractStringUint32(&gid, 10, strlen(rawstr), rawstr) <= 0) {
76  SCLogError("invalid input as arg to gid keyword");
77  goto error;
78  }
79 
80  s->gid = gid;
81 
82  return 0;
83 
84  error:
85  return -1;
86 }
87 
88 /*
89  * ONLY TESTS BELOW THIS COMMENT
90  */
91 
92 #ifdef UNITTESTS
93 /**
94  * \test GidTestParse01 is a test for a valid gid value
95  */
96 static int GidTestParse01 (void)
97 {
100 
101  Signature *s =
102  DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:1; gid:1;)");
103 
104  FAIL_IF_NULL(s);
105  FAIL_IF(s->gid != 1);
106 
108  PASS;
109 }
110 
111 /**
112  * \test GidTestParse02 is a test for an invalid gid value
113  */
114 static int GidTestParse02 (void)
115 {
118 
120  DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:1; gid:a;)"));
121 
123  PASS;
124 }
125 
126 /**
127  * \test Test a gid consisting of a single quote.
128  */
129 static int GidTestParse03 (void)
130 {
133 
135  de_ctx, "alert tcp any any -> any any (content:\"ABC\"; gid:\";)"));
136 
138  PASS;
139 }
140 
141 /**
142  * \brief this function registers unit tests for Gid
143  */
144 static void GidRegisterTests(void)
145 {
146  UtRegisterTest("GidTestParse01", GidTestParse01);
147  UtRegisterTest("GidTestParse02", GidTestParse02);
148  UtRegisterTest("GidTestParse03", GidTestParse03);
149 }
150 #endif /* UNITTESTS */
util-byte.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
detect-gid.h
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
SigTableElmt_::name
const char * name
Definition: detect.h:1542
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
DetectGidRegister
void DetectGidRegister(void)
Registration function for gid: keyword.
Definition: detect-gid.c:48
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
DETECT_GID
@ DETECT_GID
Definition: detect-engine-register.h:133
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2912
ByteExtractStringUint32
int ByteExtractStringUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:194
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3971
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
Signature_::gid
uint32_t gid
Definition: detect.h:742
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
detect.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1504
decode-events.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2873
suricata.h
flow-var.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531