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 "decode.h"
29#include "detect.h"
30
31#include "detect-parse.h"
32#include "detect-engine.h"
33#include "detect-engine-mpm.h"
36#include "detect-engine-build.h"
37
38#include "detect-tcp-ack.h"
39
40#include "util-byte.h"
41#include "util-unittest.h"
43#include "util-debug.h"
44
45/* prototypes */
46static int DetectAckSetup(DetectEngineCtx *, Signature *, const char *);
47static int DetectAckMatch(DetectEngineThreadCtx *,
48 Packet *, const Signature *, const SigMatchCtx *);
49#ifdef UNITTESTS
50static void DetectAckRegisterTests(void);
51#endif
52static void DetectAckFree(DetectEngineCtx *, void *);
53static int PrefilterSetupTcpAck(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
54static 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 */
85static int DetectAckMatch(DetectEngineThreadCtx *det_ctx,
86 Packet *p, const Signature *s, const SigMatchCtx *ctx)
87{
89 const DetectAckData *data = (const DetectAckData *)ctx;
90
91 /* This is only needed on TCP packets */
92 if (!(PacketIsTCP(p))) {
93 return 0;
94 }
95
96 return (data->ack == TCP_GET_RAW_ACK(PacketGetTCP(p))) ? 1 : 0;
97}
98
99/**
100 * \internal
101 * \brief this function is used to add the ack option into the signature
102 *
103 * \param de_ctx pointer to the Detection Engine Context
104 * \param s pointer to the Current Signature
105 * \param m pointer to the Current SigMatch
106 * \param optstr pointer to the user provided options
107 *
108 * \retval 0 on Success
109 * \retval -1 on Failure
110 */
111static int DetectAckSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
112{
113 DetectAckData *data = NULL;
114
115 data = SCMalloc(sizeof(DetectAckData));
116 if (unlikely(data == NULL))
117 goto error;
118
119 if (StringParseUint32(&data->ack, 10, 0, optstr) < 0) {
120 goto error;
121 }
122
124 de_ctx, s, DETECT_ACK, (SigMatchCtx *)data, DETECT_SM_LIST_MATCH) == NULL) {
125 goto error;
126 }
128
129 return 0;
130
131error:
132 if (data)
133 SCFree(data);
134 return -1;
135
136}
137
138/**
139 * \internal
140 * \brief this function will free memory associated with ack option
141 *
142 * \param data pointer to ack configuration data
143 */
144static void DetectAckFree(DetectEngineCtx *de_ctx, void *ptr)
145{
146 DetectAckData *data = (DetectAckData *)ptr;
147 SCFree(data);
148}
149
150/* prefilter code */
151
152static void
153PrefilterPacketAckMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
154{
156 const PrefilterPacketHeaderCtx *ctx = pectx;
157
158 if (!PrefilterPacketHeaderExtraMatch(ctx, p))
159 return;
160
161 if (p->proto == IPPROTO_TCP && PacketIsTCP(p) &&
162 (TCP_GET_RAW_ACK(PacketGetTCP(p)) == ctx->v1.u32[0])) {
163 SCLogDebug("packet matches TCP ack %u", ctx->v1.u32[0]);
164 PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
165 }
166}
167
168static void
169PrefilterPacketAckSet(PrefilterPacketHeaderValue *v, void *smctx)
170{
171 const DetectAckData *a = smctx;
172 v->u32[0] = a->ack;
173}
174
175static bool
176PrefilterPacketAckCompare(PrefilterPacketHeaderValue v, void *smctx)
177{
178 const DetectAckData *a = smctx;
179 if (v.u32[0] == a->ack)
180 return true;
181 return false;
182}
183
184static int PrefilterSetupTcpAck(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
185{
187 PrefilterPacketAckSet, PrefilterPacketAckCompare, PrefilterPacketAckMatch);
188}
189
190static bool PrefilterTcpAckIsPrefilterable(const Signature *s)
191{
192 const SigMatch *sm;
193 for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) {
194 switch (sm->type) {
195 case DETECT_ACK:
196 return true;
197 }
198 }
199 return false;
200}
201
202#ifdef UNITTESTS
203#include "detect-engine-alert.h"
204/**
205 * \internal
206 * \brief This test tests sameip success and failure.
207 */
208static int DetectAckSigTest01(void)
209{
210 Packet *p1 = NULL;
211 Packet *p2 = NULL;
212 Packet *p3 = NULL;
213 ThreadVars th_v;
214 DetectEngineThreadCtx *det_ctx;
215 int result = 0;
216
217 memset(&th_v, 0, sizeof(th_v));
218
219 /* TCP w/ack=42 */
220 p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
221 p1->l4.hdrs.tcph->th_ack = htonl(42);
222
223 /* TCP w/ack=100 */
224 p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
225 p2->l4.hdrs.tcph->th_ack = htonl(100);
226
227 /* ICMP */
228 p3 = UTHBuildPacket(NULL, 0, IPPROTO_ICMP);
229
231 if (de_ctx == NULL) {
232 goto end;
233 }
234
236
237 /* These three are crammed in here as there is no Parse */
238 if (SigInit(de_ctx,
239 "alert tcp any any -> any any "
240 "(msg:\"Testing ack\";ack:foo;sid:1;)") != NULL)
241 {
242 printf("invalid ack accepted: ");
243 goto cleanup_engine;
244 }
245 if (SigInit(de_ctx,
246 "alert tcp any any -> any any "
247 "(msg:\"Testing ack\";ack:9999999999;sid:1;)") != NULL)
248 {
249 printf("overflowing ack accepted: ");
250 goto cleanup_engine;
251 }
252 if (SigInit(de_ctx,
253 "alert tcp any any -> any any "
254 "(msg:\"Testing ack\";ack:-100;sid:1;)") != NULL)
255 {
256 printf("negative ack accepted: ");
257 goto cleanup_engine;
258 }
259
261 "alert tcp any any -> any any "
262 "(msg:\"Testing ack\";ack:41;sid:1;)");
263 if (de_ctx->sig_list == NULL) {
264 goto cleanup_engine;
265 }
266
268 "alert tcp any any -> any any "
269 "(msg:\"Testing ack\";ack:42;sid:2;)");
270 if (de_ctx->sig_list->next == NULL) {
271 goto cleanup_engine;
272 }
273
275 DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
276
277 SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
278 if (PacketAlertCheck(p1, 1) != 0) {
279 printf("sid 1 alerted, but should not have: ");
280 goto cleanup;
281 }
282 if (PacketAlertCheck(p1, 2) == 0) {
283 printf("sid 2 did not alert, but should have: ");
284 goto cleanup;
285 }
286
287 SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
288 if (PacketAlertCheck(p2, 1) != 0) {
289 printf("sid 1 alerted, but should not have: ");
290 goto cleanup;
291 }
292 if (PacketAlertCheck(p2, 2) != 0) {
293 printf("sid 2 alerted, but should not have: ");
294 goto cleanup;
295 }
296
297 SigMatchSignatures(&th_v, de_ctx, det_ctx, p3);
298 if (PacketAlertCheck(p3, 1) != 0) {
299 printf("sid 1 alerted, but should not have: ");
300 goto cleanup;
301 }
302 if (PacketAlertCheck(p3, 2) != 0) {
303 printf("sid 2 alerted, but should not have: ");
304 goto cleanup;
305 }
306
307 result = 1;
308
309cleanup:
312
313 DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
314
315cleanup_engine:
317
318end:
319 return result;
320}
321
322/**
323 * \internal
324 * \brief This function registers unit tests for DetectAck
325 */
326static void DetectAckRegisterTests(void)
327{
328 UtRegisterTest("DetectAckSigTest01", DetectAckSigTest01);
329}
330#endif /* UNITTESTS */
#define TCP_GET_RAW_ACK(tcph)
Definition decode-tcp.h:81
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition decode.h:1321
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
void SigCleanSignatures(DetectEngineCtx *de_ctx)
int SigGroupCleanup(DetectEngineCtx *de_ctx)
int PrefilterSetupPacketHeader(DetectEngineCtx *de_ctx, SigGroupHead *sgh, int sm_type, SignatureMask mask, void(*Set)(PrefilterPacketHeaderValue *v, void *), bool(*Compare)(PrefilterPacketHeaderValue v, void *), void(*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx))
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Signature * SigInit(DetectEngineCtx *de_ctx, const char *sigstr)
Parses a signature and adds it to the Detection Engine Context.
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
SigTableElmt * sigmatch_table
void DetectAckRegister(void)
Registration function for ack: keyword.
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition detect.c:2420
#define DE_QUIET
Definition detect.h:330
#define SIG_FLAG_REQUIRE_PACKET
Definition detect.h:254
#define SIG_MASK_REQUIRE_REAL_PKT
Definition detect.h:316
@ DETECT_SM_LIST_MATCH
Definition detect.h:117
DetectEngineCtx * de_ctx
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
struct Thresholds ctx
main detection engine ctx
Definition detect.h:932
uint8_t flags
Definition detect.h:934
Signature * sig_list
Definition detect.h:941
PrefilterRuleStore pmq
Definition detect.h:1349
union PacketL4::L4Hdrs hdrs
struct PacketL4 l4
Definition decode.h:601
uint8_t proto
Definition decode.h:523
Container for matching data for a signature group.
Definition detect.h:1629
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition detect.h:351
a single match condition for a signature
Definition detect.h:356
uint16_t type
Definition detect.h:357
struct SigMatch_ * next
Definition detect.h:360
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition detect.h:1444
void(* Free)(DetectEngineCtx *, void *)
Definition detect.h:1446
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition detect.h:1421
const char * alias
Definition detect.h:1460
const char * name
Definition detect.h:1459
bool(* SupportsPrefilter)(const Signature *s)
Definition detect.h:1443
struct SigMatch_ * smlists[DETECT_SM_LIST_MAX]
Definition detect.h:642
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
SignatureInitData * init_data
Definition detect.h:747
struct Signature_ * next
Definition detect.h:750
uint32_t th_ack
Definition decode-tcp.h:153
Per thread variable structure.
Definition threadvars.h:58
TCPHdr * tcph
Definition decode.h:469
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition util-byte.c:313
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCMalloc(sz)
Definition util-mem.h:47
#define SCFree(p)
Definition util-mem.h:61
#define unlikely(expr)
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.
#define DEBUG_VALIDATE_BUG_ON(exp)