suricata
detect-tcp-ack.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2016 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 Brian Rectanus <brectanu@gmail.com>
22  * \author Victor Julien <victor@inliniac.net>
23  *
24  * Implements the "ack" keyword.
25  */
26 
27 #include "suricata-common.h"
28 #include "debug.h"
29 #include "decode.h"
30 #include "detect.h"
31 
32 #include "detect-parse.h"
33 #include "detect-engine.h"
34 #include "detect-engine-mpm.h"
37 
38 #include "detect-tcp-ack.h"
39 
40 #include "util-byte.h"
41 #include "util-unittest.h"
42 #include "util-unittest-helper.h"
43 #include "util-debug.h"
44 
45 /* prototypes */
46 static int DetectAckSetup(DetectEngineCtx *, Signature *, const char *);
47 static int DetectAckMatch(DetectEngineThreadCtx *,
48  Packet *, const Signature *, const SigMatchCtx *);
49 static void DetectAckRegisterTests(void);
50 static void DetectAckFree(void *);
51 static int PrefilterSetupTcpAck(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
52 static _Bool PrefilterTcpAckIsPrefilterable(const Signature *s);
53 
55 {
56  sigmatch_table[DETECT_ACK].name = "tcp.ack";
58  sigmatch_table[DETECT_ACK].desc = "check for a specific TCP acknowledgement number";
59  sigmatch_table[DETECT_ACK].url = DOC_URL DOC_VERSION "/rules/header-keywords.html#ack";
60  sigmatch_table[DETECT_ACK].Match = DetectAckMatch;
61  sigmatch_table[DETECT_ACK].Setup = DetectAckSetup;
62  sigmatch_table[DETECT_ACK].Free = DetectAckFree;
63 
64  sigmatch_table[DETECT_ACK].SupportsPrefilter = PrefilterTcpAckIsPrefilterable;
65  sigmatch_table[DETECT_ACK].SetupPrefilter = PrefilterSetupTcpAck;
66 
67  sigmatch_table[DETECT_ACK].RegisterTests = DetectAckRegisterTests;
68 }
69 
70 /**
71  * \internal
72  * \brief This function is used to match packets with a given Ack number
73  *
74  * \param t pointer to thread vars
75  * \param det_ctx pointer to the pattern matcher thread
76  * \param p pointer to the current packet
77  * \param m pointer to the sigmatch that we will cast into DetectAckData
78  *
79  * \retval 0 no match
80  * \retval 1 match
81  */
82 static int DetectAckMatch(DetectEngineThreadCtx *det_ctx,
83  Packet *p, const Signature *s, const SigMatchCtx *ctx)
84 {
85  const DetectAckData *data = (const DetectAckData *)ctx;
86 
87  /* This is only needed on TCP packets */
88  if (!(PKT_IS_TCP(p)) || PKT_IS_PSEUDOPKT(p)) {
89  return 0;
90  }
91 
92  return (data->ack == TCP_GET_ACK(p)) ? 1 : 0;
93 }
94 
95 /**
96  * \internal
97  * \brief this function is used to add the ack option into the signature
98  *
99  * \param de_ctx pointer to the Detection Engine Context
100  * \param s pointer to the Current Signature
101  * \param m pointer to the Current SigMatch
102  * \param optstr pointer to the user provided options
103  *
104  * \retval 0 on Success
105  * \retval -1 on Failure
106  */
107 static int DetectAckSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
108 {
109  DetectAckData *data = NULL;
110  SigMatch *sm = NULL;
111 
112  data = SCMalloc(sizeof(DetectAckData));
113  if (unlikely(data == NULL))
114  goto error;
115 
116  sm = SigMatchAlloc();
117  if (sm == NULL)
118  goto error;
119 
120  sm->type = DETECT_ACK;
121 
122  if (-1 == ByteExtractStringUint32(&data->ack, 10, 0, optstr)) {
123  goto error;
124  }
125  sm->ctx = (SigMatchCtx*)data;
126 
129 
130  return 0;
131 
132 error:
133  if (data)
134  SCFree(data);
135  if (sm)
136  SigMatchFree(sm);
137  return -1;
138 
139 }
140 
141 /**
142  * \internal
143  * \brief this function will free memory associated with ack option
144  *
145  * \param data pointer to ack configuration data
146  */
147 static void DetectAckFree(void *ptr)
148 {
149  DetectAckData *data = (DetectAckData *)ptr;
150  SCFree(data);
151 }
152 
153 /* prefilter code */
154 
155 static void
156 PrefilterPacketAckMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
157 {
158  const PrefilterPacketHeaderCtx *ctx = pectx;
159 
160  if (PrefilterPacketHeaderExtraMatch(ctx, p) == FALSE)
161  return;
162 
163  if ((p->proto) == IPPROTO_TCP && !(PKT_IS_PSEUDOPKT(p)) &&
164  (p->tcph != NULL) && (TCP_GET_ACK(p) == ctx->v1.u32[0]))
165  {
166  SCLogDebug("packet matches TCP ack %u", ctx->v1.u32[0]);
167  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
168  }
169 }
170 
171 static void
172 PrefilterPacketAckSet(PrefilterPacketHeaderValue *v, void *smctx)
173 {
174  const DetectAckData *a = smctx;
175  v->u32[0] = a->ack;
176 }
177 
178 static _Bool
179 PrefilterPacketAckCompare(PrefilterPacketHeaderValue v, void *smctx)
180 {
181  const DetectAckData *a = smctx;
182  if (v.u32[0] == a->ack)
183  return TRUE;
184  return FALSE;
185 }
186 
187 static int PrefilterSetupTcpAck(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
188 {
189  return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_ACK,
190  PrefilterPacketAckSet,
191  PrefilterPacketAckCompare,
192  PrefilterPacketAckMatch);
193 }
194 
195 static _Bool PrefilterTcpAckIsPrefilterable(const Signature *s)
196 {
197  const SigMatch *sm;
198  for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
199  switch (sm->type) {
200  case DETECT_ACK:
201  return TRUE;
202  }
203  }
204  return FALSE;
205 }
206 
207 #ifdef UNITTESTS
208 /**
209  * \internal
210  * \brief This test tests sameip success and failure.
211  */
212 static int DetectAckSigTest01(void)
213 {
214  Packet *p1 = NULL;
215  Packet *p2 = NULL;
216  Packet *p3 = NULL;
217  ThreadVars th_v;
218  DetectEngineThreadCtx *det_ctx;
219  int result = 0;
220 
221  memset(&th_v, 0, sizeof(th_v));
222 
223  /* TCP w/ack=42 */
224  p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
225  p1->tcph->th_ack = htonl(42);
226 
227  /* TCP w/ack=100 */
228  p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
229  p2->tcph->th_ack = htonl(100);
230 
231  /* ICMP */
232  p3 = UTHBuildPacket(NULL, 0, IPPROTO_ICMP);
233 
235  if (de_ctx == NULL) {
236  goto end;
237  }
238 
239  de_ctx->flags |= DE_QUIET;
240 
241  /* These three are crammed in here as there is no Parse */
242  if (SigInit(de_ctx,
243  "alert tcp any any -> any any "
244  "(msg:\"Testing ack\";ack:foo;sid:1;)") != NULL)
245  {
246  printf("invalid ack accepted: ");
247  goto cleanup_engine;
248  }
249  if (SigInit(de_ctx,
250  "alert tcp any any -> any any "
251  "(msg:\"Testing ack\";ack:9999999999;sid:1;)") != NULL)
252  {
253  printf("overflowing ack accepted: ");
254  goto cleanup_engine;
255  }
256  if (SigInit(de_ctx,
257  "alert tcp any any -> any any "
258  "(msg:\"Testing ack\";ack:-100;sid:1;)") != NULL)
259  {
260  printf("negative ack accepted: ");
261  goto cleanup_engine;
262  }
263 
264  de_ctx->sig_list = SigInit(de_ctx,
265  "alert tcp any any -> any any "
266  "(msg:\"Testing ack\";ack:41;sid:1;)");
267  if (de_ctx->sig_list == NULL) {
268  goto cleanup_engine;
269  }
270 
271  de_ctx->sig_list->next = SigInit(de_ctx,
272  "alert tcp any any -> any any "
273  "(msg:\"Testing ack\";ack:42;sid:2;)");
274  if (de_ctx->sig_list->next == NULL) {
275  goto cleanup_engine;
276  }
277 
278  SigGroupBuild(de_ctx);
279  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
280 
281  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
282  if (PacketAlertCheck(p1, 1) != 0) {
283  printf("sid 1 alerted, but should not have: ");
284  goto cleanup;
285  }
286  if (PacketAlertCheck(p1, 2) == 0) {
287  printf("sid 2 did not alert, but should have: ");
288  goto cleanup;
289  }
290 
291  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
292  if (PacketAlertCheck(p2, 1) != 0) {
293  printf("sid 1 alerted, but should not have: ");
294  goto cleanup;
295  }
296  if (PacketAlertCheck(p2, 2) != 0) {
297  printf("sid 2 alerted, but should not have: ");
298  goto cleanup;
299  }
300 
301  SigMatchSignatures(&th_v, de_ctx, det_ctx, p3);
302  if (PacketAlertCheck(p3, 1) != 0) {
303  printf("sid 1 alerted, but should not have: ");
304  goto cleanup;
305  }
306  if (PacketAlertCheck(p3, 2) != 0) {
307  printf("sid 2 alerted, but should not have: ");
308  goto cleanup;
309  }
310 
311  result = 1;
312 
313 cleanup:
314  SigGroupCleanup(de_ctx);
315  SigCleanSignatures(de_ctx);
316 
317  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
318 
319 cleanup_engine:
320  DetectEngineCtxFree(de_ctx);
321 
322 end:
323  return result;
324 }
325 
326 #endif /* UNITTESTS */
327 
328 /**
329  * \internal
330  * \brief This function registers unit tests for DetectAck
331  */
332 static void DetectAckRegisterTests(void)
333 {
334 #ifdef UNITTESTS
335  UtRegisterTest("DetectAckSigTest01", DetectAckSigTest01);
336 #endif /* UNITTESTS */
337 }
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect.h:1439
SignatureInitData * init_data
Definition: detect.h:586
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1179
#define SCLogDebug(...)
Definition: util-debug.h:335
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1182
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, void(*Set)(PrefilterPacketHeaderValue *v, void *), _Bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
uint32_t flags
Definition: detect.h:518
_Bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1181
#define FALSE
#define unlikely(expr)
Definition: util-optimize.h:35
Signature * SigInit(DetectEngineCtx *, const char *)
Parses a signature and adds it to the Detection Engine Context.
Signature * sig_list
Definition: detect.h:762
void SigCleanSignatures(DetectEngineCtx *de_ctx)
Container for matching data for a signature group.
Definition: detect.h:1329
int ByteExtractStringUint32(uint32_t *res, int base, uint16_t len, const char *str)
Definition: util-byte.c:244
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:219
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
const char * name
Definition: detect.h:1193
TCPHdr * tcph
Definition: decode.h:522
Signature container.
Definition: detect.h:517
#define TRUE
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:308
struct SigMatch_ * next
Definition: detect.h:317
main detection engine ctx
Definition: detect.h:756
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
#define DE_QUIET
Definition: detect.h:287
uint8_t proto
Definition: decode.h:430
uint8_t flags
Definition: detect.h:757
void(* Free)(void *)
Definition: detect.h:1184
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
void DetectAckRegister(void)
Registration function for ack: keyword.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void SigMatchSignatures(ThreadVars *th_v, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1670
PrefilterRuleStore pmq
Definition: detect.h:1095
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1163
int SigGroupCleanup(DetectEngineCtx *de_ctx)
struct Signature_ * next
Definition: detect.h:589
uint8_t type
Definition: detect.h:314
const char * desc
Definition: detect.h:1195
void SigMatchAppendSMToList(Signature *s, SigMatch *new, int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:288
struct SigMatch_ ** smlists
Definition: detect.h:511
SigMatchCtx * ctx
Definition: detect.h:316
const char * alias
Definition: detect.h:1194
#define SCMalloc(a)
Definition: util-mem.h:222
#define SCFree(a)
Definition: util-mem.h:322
#define PKT_IS_TCP(p)
Definition: decode.h:253
void SigMatchFree(SigMatch *sm)
free a SigMatch
Definition: detect-parse.c:247
const char * url
Definition: detect.h:1196
#define DOC_URL
Definition: suricata.h:86
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1132
SigMatch * SigMatchAlloc(void)
Definition: detect-parse.c:232
Per thread variable structure.
Definition: threadvars.h:57
#define TCP_GET_ACK(p)
Definition: decode-tcp.h:114
#define DOC_VERSION
Definition: suricata.h:91
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
void(* RegisterTests)(void)
Definition: detect.h:1185
a single match condition for a signature
Definition: detect.h:313
DetectEngineCtx * DetectEngineCtxInit(void)