suricata
detect-transform-strip-whitespace.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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-engine-build.h"
32 #include "detect-parse.h"
34 
35 #include "util-unittest.h"
36 #include "util-print.h"
37 
38 static int DetectTransformStripWhitespaceSetup (DetectEngineCtx *, Signature *, const char *);
39 #ifdef UNITTESTS
40 static void DetectTransformStripWhitespaceRegisterTests(void);
41 #endif
42 static void TransformStripWhitespace(InspectionBuffer *buffer, void *options);
43 static bool TransformStripWhitespaceValidate(const uint8_t *content, uint16_t content_len, void *options);
44 
46 {
49  "modify buffer to strip whitespace before inspection";
51  "/rules/transforms.html#strip-whitespace";
53  TransformStripWhitespace;
55  TransformStripWhitespaceValidate;
57  DetectTransformStripWhitespaceSetup;
58 #ifdef UNITTESTS
60  DetectTransformStripWhitespaceRegisterTests;
61 #endif
63 }
64 
65 /**
66  * \internal
67  * \brief Apply the nocase keyword to the last pattern match, either content or uricontent
68  * \param det_ctx detection engine ctx
69  * \param s signature
70  * \param nullstr should be null
71  * \retval 0 ok
72  * \retval -1 failure
73  */
74 static int DetectTransformStripWhitespaceSetup (DetectEngineCtx *de_ctx, Signature *s, const char *nullstr)
75 {
76  SCEnter();
78  SCReturnInt(r);
79 }
80 
81 /*
82  * \brief Validate content bytes to see if it's compatible with this transform
83  * \param content Byte array to check for compatibility
84  * \param content_len Number of bytes to check
85  * \param options Ignored
86  * \retval false If the string contains spaces
87  * \retval true Otherwise.
88  */
89 static bool TransformStripWhitespaceValidate(const uint8_t *content,
90  uint16_t content_len, void *options)
91 {
92  if (content) {
93  for (uint32_t i = 0; i < content_len; i++) {
94  if (isspace(*content++)) {
95  return false;
96  }
97  }
98  }
99  return true;
100 }
101 
102 static void TransformStripWhitespace(InspectionBuffer *buffer, void *options)
103 {
104  const uint8_t *input = buffer->inspect;
105  const uint32_t input_len = buffer->inspect_len;
106  if (input_len == 0) {
107  return;
108  }
109  uint8_t output[input_len]; // we can only shrink
110  uint8_t *oi = output, *os = output;
111 
112  //PrintRawDataFp(stdout, input, input_len);
113  for (uint32_t i = 0; i < input_len; i++) {
114  if (!isspace(*input)) {
115  *oi++ = *input;
116  }
117  input++;
118  }
119  uint32_t output_size = oi - os;
120  //PrintRawDataFp(stdout, output, output_size);
121 
122  InspectionBufferCopy(buffer, os, output_size);
123 }
124 
125 #ifdef UNITTESTS
126 static int TransformDoubleWhitespace(InspectionBuffer *buffer)
127 {
128  const uint8_t *input = buffer->inspect;
129  const uint32_t input_len = buffer->inspect_len;
130  uint8_t output[input_len * 2]; // if all chars are whitespace this fits
131  uint8_t *oi = output, *os = output;
132 
133  PrintRawDataFp(stdout, input, input_len);
134  for (uint32_t i = 0; i < input_len; i++) {
135  if (isspace(*input)) {
136  *oi++ = *input;
137  }
138  *oi++ = *input;
139  input++;
140  }
141  uint32_t output_size = oi - os;
142  PrintRawDataFp(stdout, output, output_size);
143 
144  InspectionBufferCopy(buffer, os, output_size);
145  return 0;
146 }
147 
148 static int DetectTransformStripWhitespaceTest01(void)
149 {
150  const uint8_t *input = (const uint8_t *)" A B C D ";
151  uint32_t input_len = strlen((char *)input);
152 
153  InspectionBuffer buffer;
154  InspectionBufferInit(&buffer, 8);
155  InspectionBufferSetup(NULL, -1, &buffer, input, input_len);
156  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
157  TransformStripWhitespace(&buffer, NULL);
158  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
159  InspectionBufferFree(&buffer);
160  PASS;
161 }
162 
163 static int DetectTransformStripWhitespaceTest02(void)
164 {
165  const uint8_t *input = (const uint8_t *)" A B C D ";
166  uint32_t input_len = strlen((char *)input);
167 
168  InspectionBuffer buffer;
169  InspectionBufferInit(&buffer, 8);
170  InspectionBufferSetup(NULL, -1, &buffer, input, input_len);
171  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
172  TransformDoubleWhitespace(&buffer);
173  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
174  TransformDoubleWhitespace(&buffer);
175  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
176  TransformStripWhitespace(&buffer, NULL);
177  PrintRawDataFp(stdout, buffer.inspect, buffer.inspect_len);
178  InspectionBufferFree(&buffer);
179  PASS;
180 }
181 
182 static int DetectTransformStripWhitespaceTest03(void)
183 {
184  const char rule[] = "alert http any any -> any any (http_request_line; strip_whitespace; content:\"GET/HTTP\"; sid:1;)";
185  ThreadVars th_v;
186  DetectEngineThreadCtx *det_ctx = NULL;
187  memset(&th_v, 0, sizeof(th_v));
188 
192  FAIL_IF_NULL(s);
194  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
195  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
197  PASS;
198 }
199 
200 static void DetectTransformStripWhitespaceRegisterTests(void)
201 {
202  UtRegisterTest("DetectTransformStripWhitespaceTest01",
203  DetectTransformStripWhitespaceTest01);
204  UtRegisterTest("DetectTransformStripWhitespaceTest02",
205  DetectTransformStripWhitespaceTest02);
206  UtRegisterTest("DetectTransformStripWhitespaceTest03",
207  DetectTransformStripWhitespaceTest03);
208 }
209 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1248
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:1247
SigTableElmt_::name
const char * name
Definition: detect.h:1245
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
detect-transform-strip-whitespace.h
InspectionBuffer
Definition: detect.h:338
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1239
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:785
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2445
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1230
detect-engine-prefilter.h
util-unittest.h
DetectTransformStripWhitespaceRegister
void DetectTransformStripWhitespaceRegister(void)
Definition: detect-transform-strip-whitespace.c:45
InspectionBufferInit
void InspectionBufferInit(InspectionBuffer *buffer, uint32_t initial_size)
Definition: detect-engine.c:1426
SigTableElmt_::TransformValidate
bool(* TransformValidate)(const uint8_t *content, uint16_t content_len, void *context)
Definition: detect.h:1227
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1034
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:298
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:56
PrintRawDataFp
void PrintRawDataFp(FILE *fp, const uint8_t *buf, uint32_t buflen)
Definition: util-print.c:143
detect-engine-build.h
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1953
InspectionBufferCopy
void InspectionBufferCopy(InspectionBuffer *buffer, uint8_t *buf, uint32_t buf_len)
Definition: detect-engine.c:1497
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:2421
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3154
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3368
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
SigTableElmt_::Transform
void(* Transform)(InspectionBuffer *, void *context)
Definition: detect.h:1226
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:341
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:339
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1450
detect-parse.h
Signature_
Signature container.
Definition: detect.h:540
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2406
DetectSignatureAddTransform
int DetectSignatureAddTransform(Signature *s, int transform, void *options)
Definition: detect-parse.c:1465
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1431
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:302
DETECT_TRANSFORM_STRIP_WHITESPACE
@ DETECT_TRANSFORM_STRIP_WHITESPACE
Definition: detect-engine-register.h:312
InspectionBufferFree
void InspectionBufferFree(InspectionBuffer *buffer)
Definition: detect-engine.c:1468
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1237