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