suricata
detect-tcphdr.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 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  */
24 
25 #include "suricata-common.h"
26 
27 #include "detect.h"
28 #include "detect-parse.h"
29 #include "detect-engine.h"
30 #include "detect-engine-mpm.h"
33 #include "detect-fast-pattern.h"
34 #include "detect-tcphdr.h"
35 
36 /* prototypes */
37 static int DetectTcphdrSetup (DetectEngineCtx *, Signature *, const char *);
38 #ifdef UNITTESTS
39 void DetectTcphdrRegisterTests (void);
40 #endif
41 
42 static int g_tcphdr_buffer_id = 0;
43 
44 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
45  const DetectEngineTransforms *transforms, Packet *p, const int list_id);
46 
47 /**
48  * \brief Registration function for tcp.hdr: keyword
49  */
51 {
52  sigmatch_table[DETECT_TCPHDR].name = "tcp.hdr";
53  sigmatch_table[DETECT_TCPHDR].desc = "sticky buffer to match on the TCP header";
54  sigmatch_table[DETECT_TCPHDR].url = "/rules/header-keywords.html#tcphdr";
55  sigmatch_table[DETECT_TCPHDR].Setup = DetectTcphdrSetup;
57 #ifdef UNITTESTS
59 #endif
60 
61  g_tcphdr_buffer_id = DetectBufferTypeRegister("tcp.hdr");
62  BUG_ON(g_tcphdr_buffer_id < 0);
63 
65 
67 
68  DetectPktInspectEngineRegister("tcp.hdr", GetData,
70 
71  return;
72 }
73 
74 /**
75  * \brief setup tcp.hdr sticky buffer
76  *
77  * \param de_ctx pointer to the Detection Engine Context
78  * \param s pointer to the Current Signature
79  * \param _unused unused
80  *
81  * \retval 0 on Success
82  * \retval -1 on Failure
83  */
84 static int DetectTcphdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
85 {
86  if (!(DetectProtoContainsProto(&s->proto, IPPROTO_TCP)))
87  return -1;
88 
90 
91  if (DetectBufferSetActiveList(s, g_tcphdr_buffer_id) < 0)
92  return -1;
93 
94  return 0;
95 }
96 
97 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
98  const DetectEngineTransforms *transforms, Packet *p, const int list_id)
99 {
100  SCEnter();
101 
102  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
103  if (buffer->inspect == NULL) {
104  uint32_t hlen = TCP_GET_HLEN(p);
105  if (((uint8_t *)p->tcph + (ptrdiff_t)hlen) >
106  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)))
107  {
108  SCLogDebug("data out of range: %p > %p",
109  ((uint8_t *)p->tcph + (ptrdiff_t)hlen),
110  ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
111  return NULL;
112  }
113 
114  const uint32_t data_len = hlen;
115  const uint8_t *data = (const uint8_t *)p->tcph;
116 
117  InspectionBufferSetup(buffer, data, data_len);
118  InspectionBufferApplyTransforms(buffer, transforms);
119  }
120 
121  return buffer;
122 }
123 
124 #ifdef UNITTESTS
125 #include "tests/detect-tcphdr.c"
126 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1212
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1402
SigTableElmt_::desc
const char * desc
Definition: detect.h:1211
DetectPktMpmRegister
void DetectPktMpmRegister(const char *name, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id), InspectionBufferGetPktDataPtr GetData)
register a MPM engine
Definition: detect-engine-mpm.c:287
SigTableElmt_::name
const char * name
Definition: detect.h:1209
DetectEngineTransforms
Definition: detect.h:375
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
InspectionBuffer
Definition: detect.h:343
detect-tcphdr.c
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1203
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:766
DetectTcphdrRegisterTests
void DetectTcphdrRegisterTests(void)
this function registers unit tests for DetectTcphdr
Definition: detect-tcphdr.c:44
DetectTcphdrRegister
void DetectTcphdrRegister(void)
Registration function for tcp.hdr: keyword.
Definition: detect-tcphdr.c:50
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1195
DETECT_TCPHDR
@ DETECT_TCPHDR
Definition: detect-engine-register.h:249
detect-engine-prefilter.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1060
DetectBufferTypeSupportsPacket
void DetectBufferTypeSupportsPacket(const char *name)
Definition: detect-engine.c:849
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1009
DetectPktInspectEngineRegister
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
Definition: detect-engine.c:128
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
GET_PKT_DATA
#define GET_PKT_DATA(p)
Definition: decode.h:228
detect-engine-mpm.h
detect.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:282
Signature_::flags
uint32_t flags
Definition: detect.h:528
Packet_
Definition: decode.h:411
GET_PKT_LEN
#define GET_PKT_LEN(p)
Definition: decode.h:227
detect-engine-content-inspection.h
detect-fast-pattern.h
DetectBufferTypeRegister
int DetectBufferTypeRegister(const char *name)
Definition: detect-engine.c:835
Signature_::proto
DetectProto proto
Definition: detect.h:544
suricata-common.h
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:525
InspectionBufferSetup
void InspectionBufferSetup(InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1119
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1210
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:344
detect-tcphdr.h
detect-parse.h
Signature_
Signature container.
Definition: detect.h:527
TCP_GET_HLEN
#define TCP_GET_HLEN(p)
Definition: decode-tcp.h:109
PrefilterGenericMpmPktRegister
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistery *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:681
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1378
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:991
DetectEngineInspectPktBufferGeneric
int DetectEngineInspectPktBufferGeneric(DetectEngineThreadCtx *det_ctx, const DetectEnginePktInspectionEngine *engine, const Signature *s, Packet *p, uint8_t *_alert_flags)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:1705
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1201
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:135
SIG_FLAG_REQUIRE_PACKET
#define SIG_FLAG_REQUIRE_PACKET
Definition: detect.h:223