suricata
detect-sid.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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;
47 #ifdef UNITTESTS
48  sigmatch_table[DETECT_SID].RegisterTests = DetectSidRegisterTests;
49 #endif
50 }
51 
52 static int DetectSidSetup (DetectEngineCtx *de_ctx, Signature *s, const char *sidstr)
53 {
54  unsigned long id = 0;
55  char *endptr = NULL;
56  id = strtoul(sidstr, &endptr, 10);
57  if (endptr == NULL || *endptr != '\0') {
58  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid character as arg "
59  "to sid keyword");
60  goto error;
61  }
62  if (id >= UINT_MAX) {
63  SCLogError(SC_ERR_INVALID_NUMERIC_VALUE, "sid value to high, max %u", UINT_MAX);
64  goto error;
65  }
66  if (id == 0) {
67  SCLogError(SC_ERR_INVALID_NUMERIC_VALUE, "sid value 0 is invalid");
68  goto error;
69  }
70  if (s->id > 0) {
71  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "duplicated 'sid' keyword detected");
72  goto error;
73  }
74 
75  s->id = (uint32_t)id;
76  return 0;
77 
78  error:
79  return -1;
80 }
81 
82 #ifdef UNITTESTS
83 
84 static int SidTestParse01(void)
85 {
86  int result = 0;
87  Signature *s = NULL;
88 
90  if (de_ctx == NULL)
91  goto end;
92 
94  "alert tcp 1.2.3.4 any -> any any (sid:1; gid:1;)");
95  if (s == NULL || s->id != 1)
96  goto end;
97 
98  result = 1;
99 
100 end:
101  if (de_ctx != NULL)
103  return result;
104 }
105 
106 static int SidTestParse02(void)
107 {
108  int result = 0;
109 
111  if (de_ctx == NULL)
112  goto end;
113 
115  "alert tcp 1.2.3.4 any -> any any (sid:a; gid:1;)") != NULL)
116  goto end;
117 
118  result = 1;
119 
120 end:
121  if (de_ctx != NULL)
123  return result;
124 }
125 
126 static int SidTestParse03(void)
127 {
128  int result = 0;
129 
131  if (de_ctx == NULL)
132  goto end;
133 
135  "alert tcp any any -> any any (content:\"ABC\"; sid:\";)") != NULL)
136  goto end;
137 
138  result = 1;
139 end:
140  if (de_ctx != NULL)
142  return result;
143 }
144 
145 /**
146  * \brief Register DetectSid unit tests.
147  */
148 static void DetectSidRegisterTests(void)
149 {
150  UtRegisterTest("SidTestParse01", SidTestParse01);
151  UtRegisterTest("SidTestParse02", SidTestParse02);
152  UtRegisterTest("SidTestParse03", SidTestParse03);
153 }
154 #endif /* UNITTESTS */
SigTableElmt_::url
const char * url
Definition: detect.h:1214
detect-engine.h
SigTableElmt_::desc
const char * desc
Definition: detect.h:1213
detect-sid.h
DetectSidRegister
void DetectSidRegister(void)
Definition: detect-sid.c:40
SigTableElmt_::name
const char * name
Definition: detect.h:1211
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SC_ERR_INVALID_RULE_ARGUMENT
@ SC_ERR_INVALID_RULE_ARGUMENT
Definition: util-error.h:302
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:767
SC_ERR_INVALID_SIGNATURE
@ SC_ERR_INVALID_SIGNATURE
Definition: util-error.h:69
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2093
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1196
DETECT_SID
@ DETECT_SID
Definition: detect-engine-register.h:28
util-unittest.h
util-debug.h
util-error.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
detect.h
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1179
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2361
suricata-common.h
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
Signature_::id
uint32_t id
Definition: detect.h:561
detect-parse.h
Signature_
Signature container.
Definition: detect.h:528
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2048
SC_ERR_INVALID_NUMERIC_VALUE
@ SC_ERR_INVALID_NUMERIC_VALUE
Definition: util-error.h:90
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1203