suricata
detect-transform-strip-whitespace.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 nocase keyword
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "detect.h"
29 #include "detect-engine.h"
31 #include "detect-parse.h"
33 
34 #include "util-unittest.h"
35 #include "util-print.h"
36 
37 static int DetectTransformStripWhitespaceSetup (DetectEngineCtx *, Signature *, const char *);
38 static void DetectTransformStripWhitespaceRegisterTests(void);
39 
40 static void TransformStripWhitespace(InspectionBuffer *buffer);
41 
43 {
46  "modify buffer to strip whitespace before inspection";
48  DOC_URL DOC_VERSION "/rules/transforms.html#strip-whitespace";
50  TransformStripWhitespace;
52  DetectTransformStripWhitespaceSetup;
54  DetectTransformStripWhitespaceRegisterTests;
55 
57 }
58 
59 /**
60  * \internal
61  * \brief Apply the nocase keyword to the last pattern match, either content or uricontent
62  * \param det_ctx detection engine ctx
63  * \param s signature
64  * \param nullstr should be null
65  * \retval 0 ok
66  * \retval -1 failure
67  */
68 static int DetectTransformStripWhitespaceSetup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
69 {
70  SCEnter();
72  SCReturnInt(r);
73 }
74 
75 static void TransformStripWhitespace(InspectionBuffer *buffer)
76 {
77  const uint8_t *input = buffer->inspect;
78  const uint32_t input_len = buffer->inspect_len;
79  uint8_t output[input_len]; // we can only shrink
80  uint8_t *oi = output, *os = output;
81 
82  //PrintRawDataFp(stdout, input, input_len);
83  for (uint32_t i = 0; i < input_len; i++) {
84  if (!isspace(*input)) {
85  *oi++ = *input;
86  }
87  input++;
88  }
89  uint32_t output_size = oi - os;
90  //PrintRawDataFp(stdout, output, output_size);
91 
92  InspectionBufferCopy(buffer, os, output_size);
93 }
94 
95 #ifdef UNITTESTS
96 static int TransformDoubleWhitespace(InspectionBuffer *buffer)
97 {
98  const uint8_t *input = buffer->inspect;
99  const uint32_t input_len = buffer->inspect_len;
100  uint8_t output[input_len * 2]; // if all chars are whitespace this fits
101  uint8_t *oi = output, *os = output;
102 
103  PrintRawDataFp(stdout, input, input_len);
104  for (uint32_t i = 0; i < input_len; i++) {
105  if (isspace(*input)) {
106  *oi++ = *input;
107  }
108  *oi++ = *input;
109  input++;
110  }
111  uint32_t output_size = oi - os;
112  PrintRawDataFp(stdout, output, output_size);
113 
114  InspectionBufferCopy(buffer, os, output_size);
115  return 0;
116 }
117 
118 static int DetectTransformStripWhitespaceTest01(void)
119 {
120  const uint8_t *input = (const uint8_t *)" A B C D ";
121  uint32_t input_len = strlen((char *)input);
122 
123  InspectionBuffer buffer;
124  InspectionBufferInit(&buffer, 8);
125  InspectionBufferSetup(&buffer, input, input_len);
126  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
127  TransformStripWhitespace(&buffer);
128  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
129  InspectionBufferFree(&buffer);
130  PASS;
131 }
132 
133 static int DetectTransformStripWhitespaceTest02(void)
134 {
135  const uint8_t *input = (const uint8_t *)" A B C D ";
136  uint32_t input_len = strlen((char *)input);
137 
138  InspectionBuffer buffer;
139  InspectionBufferInit(&buffer, 8);
140  InspectionBufferSetup(&buffer, input, input_len);
141  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
142  TransformDoubleWhitespace(&buffer);
143  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
144  TransformDoubleWhitespace(&buffer);
145  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
146  TransformStripWhitespace(&buffer);
147  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
148  InspectionBufferFree(&buffer);
149  PASS;
150 }
151 
152 static int DetectTransformStripWhitespaceTest03(void)
153 {
154  const char rule[] = "alert http any any -> any any (http_request_line; strip_whitespace; content:\"GET/HTTP\"; sid:1;)";
155  ThreadVars th_v;
156  DetectEngineThreadCtx *det_ctx = NULL;
157  memset(&th_v, 0, sizeof(th_v));
158 
160  FAIL_IF_NULL(de_ctx);
161  Signature *s = DetectEngineAppendSig(de_ctx, rule);
162  FAIL_IF_NULL(s);
163  SigGroupBuild(de_ctx);
164  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
165  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
166  DetectEngineCtxFree(de_ctx);
167  PASS;
168 }
169 
170 #endif
171 
172 static void DetectTransformStripWhitespaceRegisterTests(void)
173 {
174 #ifdef UNITTESTS
175  UtRegisterTest("DetectTransformStripWhitespaceTest01",
176  DetectTransformStripWhitespaceTest01);
177  UtRegisterTest("DetectTransformStripWhitespaceTest02",
178  DetectTransformStripWhitespaceTest02);
179  UtRegisterTest("DetectTransformStripWhitespaceTest03",
180  DetectTransformStripWhitespaceTest03);
181 #endif
182 }
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:1439
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
#define PASS
Pass the test.
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
const char * name
Definition: detect.h:1193
Signature container.
Definition: detect.h:517
int DetectSignatureAddTransform(Signature *s, int transform)
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
void(* Transform)(InspectionBuffer *)
Definition: detect.h:1176
main detection engine ctx
Definition: detect.h:756
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define SCEnter(...)
Definition: util-debug.h:337
#define SCReturnInt(x)
Definition: util-debug.h:341
const char * desc
Definition: detect.h:1195
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:141
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
#define SIGMATCH_NOOPT
Definition: detect.h:1362
const char * url
Definition: detect.h:1196
uint32_t inspect_len
Definition: detect.h:340
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
const uint8_t * inspect
Definition: detect.h:338
#define DOC_URL
Definition: suricata.h:86
void DetectTransformStripWhitespaceRegister(void)
Per thread variable structure.
Definition: threadvars.h:57
void InspectionBufferFree(InspectionBuffer *buffer)
#define DOC_VERSION
Definition: suricata.h:91
uint16_t flags
Definition: detect.h:1187
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void(* RegisterTests)(void)
Definition: detect.h:1185
DetectEngineCtx * DetectEngineCtxInit(void)