suricata
detect-sid.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 Victor Julien <victor@inliniac.net>
22  *
23  * Implements the sid keyword
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "detect-engine.h"
29 #include "detect-parse.h"
30 #include "detect-sid.h"
31 #include "util-debug.h"
32 #include "util-error.h"
33 #include "util-unittest.h"
34 
35 static int DetectSidSetup (DetectEngineCtx *, Signature *, const char *);
36 #ifdef UNITTESTS
37 static void DetectSidRegisterTests(void);
38 #endif
39 
40 void DetectSidRegister (void)
41 {
43  sigmatch_table[DETECT_SID].desc = "set rule ID";
44  sigmatch_table[DETECT_SID].url = "/rules/meta.html#sid-signature-id";
46  sigmatch_table[DETECT_SID].Setup = DetectSidSetup;
48 #ifdef UNITTESTS
49  sigmatch_table[DETECT_SID].RegisterTests = DetectSidRegisterTests;
50 #endif
51 }
52 
53 static int DetectSidSetup (DetectEngineCtx *de_ctx, Signature *s, const char *sidstr)
54 {
55  unsigned long id = 0;
56  char *endptr = NULL;
57  errno = 0;
58  id = strtoul(sidstr, &endptr, 10);
59  if (errno == ERANGE || endptr == NULL || *endptr != '\0') {
60  SCLogError("invalid character as arg "
61  "to sid keyword");
62  goto error;
63  }
64  if (id >= UINT_MAX) {
65  SCLogError("sid value too high, max %u", UINT_MAX);
66  goto error;
67  }
68  if (id == 0) {
69  SCLogError("sid value 0 is invalid");
70  goto error;
71  }
72  if (s->id > 0) {
73  SCLogError("duplicated 'sid' keyword detected");
74  goto error;
75  }
76 
77  s->id = (uint32_t)id;
78  return 0;
79 
80  error:
81  return -1;
82 }
83 
84 #ifdef UNITTESTS
85 
86 static int SidTestParse01(void)
87 {
90 
91  Signature *s =
92  DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:1; gid:1;)");
93  FAIL_IF_NULL(s);
94  FAIL_IF(s->id != 1);
95 
97  PASS;
98 }
99 
100 static int SidTestParse02(void)
101 {
104 
106  DetectEngineAppendSig(de_ctx, "alert tcp 1.2.3.4 any -> any any (sid:a; gid:1;)"));
107 
109  PASS;
110 }
111 
112 static int SidTestParse03(void)
113 {
116 
118  de_ctx, "alert tcp any any -> any any (content:\"ABC\"; sid:\";)"));
119 
121  PASS;
122 }
123 
124 static int SidTestParse04(void)
125 {
128 
130  de_ctx, "alert tcp any any -> any any (content:\"ABC\"; sid: 0;)"));
131 
132  /* Let's also make sure that Suricata fails a rule which doesn't have a sid at all */
134  DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any (content:\"ABC\";)"));
135 
137  PASS;
138 }
139 
140 /**
141  * \brief Register DetectSid unit tests.
142  */
143 static void DetectSidRegisterTests(void)
144 {
145  UtRegisterTest("SidTestParse01", SidTestParse01);
146  UtRegisterTest("SidTestParse02", SidTestParse02);
147  UtRegisterTest("SidTestParse03", SidTestParse03);
148  UtRegisterTest("SidTestParse04", SidTestParse04);
149 }
150 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1405
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:1404
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
detect-sid.h
DetectSidRegister
void DetectSidRegister(void)
Definition: detect-sid.c:40
SigTableElmt_::name
const char * name
Definition: detect.h:1402
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1396
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:920
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2799
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect.h:1622
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3344
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1387
DETECT_SID
@ DETECT_SID
Definition: detect-engine-register.h:28
util-unittest.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
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
detect.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1370
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:261
Signature_::id
uint32_t id
Definition: detect.h:714
detect-parse.h
Signature_
Signature container.
Definition: detect.h:669
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2760
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1394