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 static void DetectSidRegisterTests(void);
37 
38 void DetectSidRegister (void)
39 {
41  sigmatch_table[DETECT_SID].desc = "set rule id";
42  sigmatch_table[DETECT_SID].url = DOC_URL DOC_VERSION "/rules/meta.html#sid-signature-id";
44  sigmatch_table[DETECT_SID].Setup = DetectSidSetup;
46  sigmatch_table[DETECT_SID].RegisterTests = DetectSidRegisterTests;
47 }
48 
49 static int DetectSidSetup (DetectEngineCtx *de_ctx, Signature *s, const char *sidstr)
50 {
51  unsigned long id = 0;
52  char *endptr = NULL;
53  id = strtoul(sidstr, &endptr, 10);
54  if (endptr == NULL || *endptr != '\0') {
55  SCLogError(SC_ERR_INVALID_SIGNATURE, "invalid character as arg "
56  "to sid keyword");
57  goto error;
58  }
59  if (id >= UINT_MAX) {
60  SCLogError(SC_ERR_INVALID_NUMERIC_VALUE, "sid value to high, max %u", UINT_MAX);
61  goto error;
62  }
63  if (id == 0) {
64  SCLogError(SC_ERR_INVALID_NUMERIC_VALUE, "sid value 0 is invalid");
65  goto error;
66  }
67  if (s->id > 0) {
68  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "duplicated 'sid' keyword detected");
69  goto error;
70  }
71 
72  s->id = (uint32_t)id;
73  return 0;
74 
75  error:
76  return -1;
77 }
78 
79 #ifdef UNITTESTS
80 
81 static int SidTestParse01(void)
82 {
83  int result = 0;
84  Signature *s = NULL;
85 
87  if (de_ctx == NULL)
88  goto end;
89 
90  s = DetectEngineAppendSig(de_ctx,
91  "alert tcp 1.2.3.4 any -> any any (sid:1; gid:1;)");
92  if (s == NULL || s->id != 1)
93  goto end;
94 
95  result = 1;
96 
97 end:
98  if (de_ctx != NULL)
99  DetectEngineCtxFree(de_ctx);
100  return result;
101 }
102 
103 static int SidTestParse02(void)
104 {
105  int result = 0;
106 
108  if (de_ctx == NULL)
109  goto end;
110 
111  if (DetectEngineAppendSig(de_ctx,
112  "alert tcp 1.2.3.4 any -> any any (sid:a; gid:1;)") != NULL)
113  goto end;
114 
115  result = 1;
116 
117 end:
118  if (de_ctx != NULL)
119  DetectEngineCtxFree(de_ctx);
120  return result;
121 }
122 
123 static int SidTestParse03(void)
124 {
125  int result = 0;
126 
128  if (de_ctx == NULL)
129  goto end;
130 
131  if (DetectEngineAppendSig(de_ctx,
132  "alert tcp any any -> any any (content:\"ABC\"; sid:\";)") != NULL)
133  goto end;
134 
135  result = 1;
136 end:
137  if (de_ctx != NULL)
138  DetectEngineCtxFree(de_ctx);
139  return result;
140 }
141 
142 #endif
143 
144 /**
145  * \brief Register DetectSid unit tests.
146  */
147 static void DetectSidRegisterTests(void)
148 {
149 #ifdef UNITTESTS
150  UtRegisterTest("SidTestParse01", SidTestParse01);
151  UtRegisterTest("SidTestParse02", SidTestParse02);
152  UtRegisterTest("SidTestParse03", SidTestParse03);
153 #endif /* UNITTESTS */
154 }
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1403
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1146
void DetectSidRegister(void)
Definition: detect-sid.c:38
uint32_t id
Definition: detect.h:525
const char * name
Definition: detect.h:1160
Signature container.
Definition: detect.h:492
main detection engine ctx
Definition: detect.h:720
void(* Free)(void *)
Definition: detect.h:1151
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
const char * desc
Definition: detect.h:1162
int(* Match)(ThreadVars *, DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1129
const char * url
Definition: detect.h:1163
#define DOC_URL
Definition: suricata.h:86
#define DOC_VERSION
Definition: suricata.h:91
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void(* RegisterTests)(void)
Definition: detect.h:1152
DetectEngineCtx * DetectEngineCtxInit(void)