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 #include "detect-engine-uint.h"
38 
39 #include "detect-tcp-ack.h"
40 
41 #include "util-byte.h"
42 #include "util-unittest.h"
43 #include "util-unittest-helper.h"
44 #include "util-debug.h"
45 
46 /* prototypes */
47 static int DetectAckSetup(DetectEngineCtx *, Signature *, const char *);
48 static int DetectAckMatch(DetectEngineThreadCtx *,
49  Packet *, const Signature *, const SigMatchCtx *);
50 #ifdef UNITTESTS
51 static void DetectAckRegisterTests(void);
52 #endif
53 static void DetectAckFree(DetectEngineCtx *, void *);
54 static int PrefilterSetupTcpAck(DetectEngineCtx *de_ctx, SigGroupHead *sgh);
55 static bool PrefilterTcpAckIsPrefilterable(const Signature *s);
56 
58 {
59  sigmatch_table[DETECT_ACK].name = "tcp.ack";
61  sigmatch_table[DETECT_ACK].desc = "check for a specific TCP acknowledgement number";
62  sigmatch_table[DETECT_ACK].url = "/rules/header-keywords.html#ack";
63  sigmatch_table[DETECT_ACK].Match = DetectAckMatch;
64  sigmatch_table[DETECT_ACK].Setup = DetectAckSetup;
65  sigmatch_table[DETECT_ACK].Free = DetectAckFree;
67 
68  sigmatch_table[DETECT_ACK].SupportsPrefilter = PrefilterTcpAckIsPrefilterable;
69  sigmatch_table[DETECT_ACK].SetupPrefilter = PrefilterSetupTcpAck;
70 #ifdef UNITTESTS
71  sigmatch_table[DETECT_ACK].RegisterTests = DetectAckRegisterTests;
72 #endif
73 }
74 
75 /**
76  * \internal
77  * \brief This function is used to match packets with a given Ack number
78  *
79  * \param t pointer to thread vars
80  * \param det_ctx pointer to the pattern matcher thread
81  * \param p pointer to the current packet
82  * \param m pointer to the sigmatch that we will cast into DetectAckData
83  *
84  * \retval 0 no match
85  * \retval 1 match
86  */
87 static int DetectAckMatch(DetectEngineThreadCtx *det_ctx,
88  Packet *p, const Signature *s, const SigMatchCtx *ctx)
89 {
91  const DetectU32Data *data = (const DetectU32Data *)ctx;
92 
93  /* This is only needed on TCP packets */
94  if (!(PacketIsTCP(p))) {
95  return 0;
96  }
97 
98  return DetectU32Match(TCP_GET_RAW_ACK(PacketGetTCP(p)), data);
99 }
100 
101 /**
102  * \internal
103  * \brief this function is used to add the ack option into the signature
104  *
105  * \param de_ctx pointer to the Detection Engine Context
106  * \param s pointer to the Current Signature
107  * \param m pointer to the Current SigMatch
108  * \param optstr pointer to the user provided options
109  *
110  * \retval 0 on Success
111  * \retval -1 on Failure
112  */
113 static int DetectAckSetup(DetectEngineCtx *de_ctx, Signature *s, const char *optstr)
114 {
115  DetectU32Data *data = SCDetectU32Parse(optstr);
116  if (data == NULL)
117  return -1;
118 
120  de_ctx, s, DETECT_ACK, (SigMatchCtx *)data, DETECT_SM_LIST_MATCH) == NULL) {
121  DetectAckFree(de_ctx, data);
122  return -1;
123  }
125  return 0;
126 }
127 
128 /**
129  * \internal
130  * \brief this function will free memory associated with ack option
131  *
132  * \param data pointer to ack configuration data
133  */
134 static void DetectAckFree(DetectEngineCtx *de_ctx, void *ptr)
135 {
136  SCDetectU32Free(ptr);
137 }
138 
139 /* prefilter code */
140 
141 static void
142 PrefilterPacketAckMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx)
143 {
145  const PrefilterPacketHeaderCtx *ctx = pectx;
146 
147  if (!PrefilterPacketHeaderExtraMatch(ctx, p))
148  return;
149 
150  if (p->proto == IPPROTO_TCP && PacketIsTCP(p)) {
151  DetectU32Data du32;
152  du32.mode = ctx->v1.u8[0];
153  du32.arg1 = ctx->v1.u32[1];
154  du32.arg2 = ctx->v1.u32[2];
155  if (DetectU32Match(TCP_GET_RAW_ACK(PacketGetTCP(p)), &du32)) {
156  SCLogDebug("packet matches TCP ack %u", ctx->v1.u32[0]);
157  PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt);
158  }
159  }
160 }
161 
162 static int PrefilterSetupTcpAck(DetectEngineCtx *de_ctx, SigGroupHead *sgh)
163 {
165  PrefilterPacketU32Set, PrefilterPacketU32Compare, PrefilterPacketAckMatch);
166 }
167 
168 static bool PrefilterTcpAckIsPrefilterable(const Signature *s)
169 {
170  return PrefilterIsPrefilterableById(s, DETECT_ACK);
171 }
172 
173 #ifdef UNITTESTS
174 #include "detect-engine-alert.h"
175 /**
176  * \internal
177  * \brief This test tests sameip success and failure.
178  */
179 static int DetectAckSigTest01(void)
180 {
181  ThreadVars th_v;
182  memset(&th_v, 0, sizeof(th_v));
183  StatsThreadInit(&th_v.stats);
184  DetectEngineThreadCtx *det_ctx = NULL;
185 
186  /* TCP w/ack=42 */
187  Packet *p1 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
188  p1->l4.hdrs.tcph->th_ack = htonl(42);
189 
190  /* TCP w/ack=100 */
191  Packet *p2 = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
192  p2->l4.hdrs.tcph->th_ack = htonl(100);
193 
194  /* ICMP */
195  Packet *p3 = UTHBuildPacket(NULL, 0, IPPROTO_ICMP);
196 
199  de_ctx->flags |= DE_QUIET;
200 
201  /* These three are crammed in here as there is no Parse */
202  Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
203  "(msg:\"Testing ack\";ack:foo;sid:1;)");
204  FAIL_IF_NOT_NULL(s);
205  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
206  "(msg:\"Testing ack\";ack:9999999999;sid:1;)");
207  FAIL_IF_NOT_NULL(s);
208  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
209  "(msg:\"Testing ack\";ack:-100;sid:1;)");
210  FAIL_IF_NOT_NULL(s);
211 
212  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
213  "(msg:\"Testing ack\";ack:41;sid:1;)");
214  FAIL_IF_NULL(s);
215  s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
216  "(msg:\"Testing ack\";ack:42;sid:2;)");
217  FAIL_IF_NULL(s);
218 
220  DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
221 
222  SigMatchSignatures(&th_v, de_ctx, det_ctx, p1);
223  FAIL_IF(PacketAlertCheck(p1, 1));
225 
226  SigMatchSignatures(&th_v, de_ctx, det_ctx, p2);
227  FAIL_IF(PacketAlertCheck(p2, 1));
228  FAIL_IF(PacketAlertCheck(p2, 2));
229 
230  SigMatchSignatures(&th_v, de_ctx, det_ctx, p3);
231  FAIL_IF(PacketAlertCheck(p3, 1));
232  FAIL_IF(PacketAlertCheck(p3, 2));
233 
234  UTHFreePacket(p1);
235  UTHFreePacket(p2);
236  UTHFreePacket(p3);
237 
238  DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
240  StatsThreadCleanup(&th_v.stats);
241  PASS;
242 }
243 
244 /**
245  * \internal
246  * \brief This function registers unit tests for DetectAck
247  */
248 static void DetectAckRegisterTests(void)
249 {
250  UtRegisterTest("DetectAckSigTest01", DetectAckSigTest01);
251 }
252 #endif /* UNITTESTS */
util-byte.h
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1461
Packet_::proto
uint8_t proto
Definition: decode.h:523
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SIG_MASK_REQUIRE_REAL_PKT
#define SIG_MASK_REQUIRE_REAL_PKT
Definition: detect.h:316
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
SigTableElmt_::desc
const char * desc
Definition: detect.h:1460
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1445
SigTableElmt_::name
const char * name
Definition: detect.h:1458
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1323
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1628
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1449
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:279
PrefilterPacketU32Set
void PrefilterPacketU32Set(PrefilterPacketHeaderValue *v, void *smctx)
Definition: detect-engine-uint.c:51
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:142
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1348
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:933
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2634
DE_QUIET
#define DE_QUIET
Definition: detect.h:330
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:365
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:2418
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:3447
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1440
detect-engine-prefilter.h
util-unittest.h
util-unittest-helper.h
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
TCPHdr_::th_ack
uint32_t th_ack
Definition: decode-tcp.h:153
SigTableElmt_::SetupPrefilter
int(* SetupPrefilter)(DetectEngineCtx *de_ctx, struct SigGroupHead_ *sgh)
Definition: detect.h:1443
SIGMATCH_INFO_UINT32
#define SIGMATCH_INFO_UINT32
Definition: detect.h:1691
PrefilterPacketHeaderCtx_
Definition: detect-engine-prefilter-common.h:35
decode.h
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:19
DetectEngineThreadCtx_
Definition: detect.h:1245
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:388
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
initialize thread specific detection engine context
Definition: detect-engine.c:3364
DETECT_SM_LIST_MATCH
@ DETECT_SM_LIST_MATCH
Definition: detect.h:117
DETECT_ACK
@ DETECT_ACK
Definition: detect-engine-register.h:36
Signature_::flags
uint32_t flags
Definition: detect.h:669
Packet_
Definition: decode.h:501
detect-engine-build.h
detect-engine-alert.h
Packet_::l4
struct PacketL4 l4
Definition: decode.h:601
PrefilterSetupPacketHeader
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))
Definition: detect-engine-prefilter-common.c:470
PrefilterPacketU32Compare
bool PrefilterPacketU32Compare(PrefilterPacketHeaderValue v, void *smctx)
Definition: detect-engine-uint.c:60
SigTableElmt_::Match
int(* Match)(DetectEngineThreadCtx *, Packet *, const Signature *, const SigMatchCtx *)
Definition: detect.h:1420
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:2194
StatsThreadInit
void StatsThreadInit(StatsThreadContext *stats)
Definition: counters.c:1258
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:351
DetectAckRegister
void DetectAckRegister(void)
Registration function for ack: keyword.
Definition: detect-tcp-ack.c:57
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
SigTableElmt_::alias
const char * alias
Definition: detect.h:1459
suricata-common.h
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
Definition: detect-engine.c:3601
PacketL4::L4Hdrs::tcph
TCPHdr * tcph
Definition: decode.h:469
detect-tcp-ack.h
SigTableElmt_::SupportsPrefilter
bool(* SupportsPrefilter)(const Signature *s)
Definition: detect.h:1442
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:473
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
PacketL4::hdrs
union PacketL4::L4Hdrs hdrs
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2595
DetectEngineCtx_::flags
uint8_t flags
Definition: detect.h:935
detect-engine-prefilter-common.h
TCP_GET_RAW_ACK
#define TCP_GET_RAW_ACK(tcph)
Definition: decode-tcp.h:81
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:121
StatsThreadCleanup
void StatsThreadCleanup(StatsThreadContext *stats)
Definition: counters.c:1354
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1447
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:254