suricata
detect-udphdr.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"
31#include "detect-engine-mpm.h"
34#include "detect-fast-pattern.h"
35#include "detect-udphdr.h"
36
37/* prototypes */
38static int DetectUdphdrSetup (DetectEngineCtx *, Signature *, const char *);
39#ifdef UNITTESTS
41#endif
42
43static int g_udphdr_buffer_id = 0;
44
45static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
46 const DetectEngineTransforms *transforms, Packet *p, const int list_id);
47/**
48 * \brief Registration function for udp.hdr: keyword
49 */
51{
53 sigmatch_table[DETECT_UDPHDR].desc = "sticky buffer to match on the UDP header";
54 sigmatch_table[DETECT_UDPHDR].url = "/rules/header-keywords.html#udphdr";
55 sigmatch_table[DETECT_UDPHDR].Setup = DetectUdphdrSetup;
57#ifdef UNITTESTS
59#endif
60
61 g_udphdr_buffer_id = DetectBufferTypeRegister("udp.hdr");
62 BUG_ON(g_udphdr_buffer_id < 0);
63
65
67
69}
70
71/**
72 * \brief set up the udp.hdr sticky buffer
73 *
74 * \param de_ctx pointer to the Detection Engine Context
75 * \param s pointer to the Current Signature
76 * \param _unused unused
77 *
78 * \retval 0 on Success
79 * \retval -1 on Failure
80 */
81static int DetectUdphdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
82{
83 if (!(DetectProtoContainsProto(&s->proto, IPPROTO_UDP)))
84 return -1;
85
87
88 if (SCDetectBufferSetActiveList(de_ctx, s, g_udphdr_buffer_id) < 0)
89 return -1;
90
91 return 0;
92}
93
94static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
95 const DetectEngineTransforms *transforms, Packet *p, const int list_id)
96{
97 SCEnter();
98
99 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
100 if (buffer->inspect == NULL) {
101 if (!PacketIsUDP(p)) {
102 return NULL;
103 }
104 const UDPHdr *udph = PacketGetUDP(p);
105 if (((uint8_t *)udph + (ptrdiff_t)UDP_HEADER_LEN) >
106 ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) {
107 SCLogDebug("data out of range: %p > %p", ((uint8_t *)udph + (ptrdiff_t)UDP_HEADER_LEN),
108 ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
109 return NULL;
110 }
111
112 const uint32_t data_len = UDP_HEADER_LEN;
113 const uint8_t *data = (const uint8_t *)udph;
114
116 det_ctx, list_id, buffer, data, data_len, transforms);
117 }
118
119 return buffer;
120}
121
122#ifdef UNITTESTS
123#include "tests/detect-udphdr.c"
124#endif
#define UDP_HEADER_LEN
Definition decode-udp.h:27
#define GET_PKT_DATA(p)
Definition decode.h:209
#define GET_PKT_LEN(p)
Definition decode.h:208
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void InspectionBufferSetupAndApplyTransforms(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len, const DetectEngineTransforms *transforms)
setup the buffer with our initial data
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
void DetectPktMpmRegister(const char *name, int priority, int(*PrefilterRegister)(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id), InspectionBufferGetPktDataPtr GetData)
register a MPM engine
int PrefilterGenericMpmPktRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
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.
int DetectBufferTypeRegister(const char *name)
void DetectPktInspectEngineRegister(const char *name, InspectionBufferGetPktDataPtr GetPktData, InspectionBufferPktInspectFunc Callback)
register inspect engine at start up time
void DetectBufferTypeSupportsPacket(const char *name)
SigTableElmt * sigmatch_table
void DetectUdphdrRegister(void)
Registration function for udp.hdr: keyword.
void DetectUdphdrRegisterTests(void)
this function registers unit tests for DetectUdphdr
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_REQUIRE_PACKET
Definition detect.h:254
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
void(* RegisterTests)(void)
Definition detect.h:1448
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
uint32_t flags
Definition detect.h:669
DetectProto proto
Definition detect.h:687
#define BUG_ON(x)
#define SCEnter(...)
Definition util-debug.h:277
#define SCLogDebug(...)
Definition util-debug.h:275