/src/suricata7/src/detect-tcphdr.c
Line | Count | Source |
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" |
31 | | #include "detect-engine-prefilter.h" |
32 | | #include "detect-engine-content-inspection.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 | | */ |
50 | | void DetectTcphdrRegister(void) |
51 | 34 | { |
52 | 34 | sigmatch_table[DETECT_TCPHDR].name = "tcp.hdr"; |
53 | 34 | sigmatch_table[DETECT_TCPHDR].desc = "sticky buffer to match on the TCP header"; |
54 | 34 | sigmatch_table[DETECT_TCPHDR].url = "/rules/header-keywords.html#tcphdr"; |
55 | 34 | sigmatch_table[DETECT_TCPHDR].Setup = DetectTcphdrSetup; |
56 | 34 | sigmatch_table[DETECT_TCPHDR].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER; |
57 | | #ifdef UNITTESTS |
58 | | sigmatch_table[DETECT_TCPHDR].RegisterTests = DetectTcphdrRegisterTests; |
59 | | #endif |
60 | | |
61 | 34 | g_tcphdr_buffer_id = DetectBufferTypeRegister("tcp.hdr"); |
62 | 34 | BUG_ON(g_tcphdr_buffer_id < 0); |
63 | | |
64 | 34 | DetectBufferTypeSupportsPacket("tcp.hdr"); |
65 | | |
66 | 34 | DetectPktMpmRegister("tcp.hdr", 2, PrefilterGenericMpmPktRegister, GetData); |
67 | | |
68 | 34 | DetectPktInspectEngineRegister("tcp.hdr", GetData, |
69 | 34 | DetectEngineInspectPktBufferGeneric); |
70 | | |
71 | 34 | return; |
72 | 34 | } |
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 | 12.1k | { |
86 | 12.1k | if (!(DetectProtoContainsProto(&s->proto, IPPROTO_TCP))) |
87 | 28 | return -1; |
88 | | |
89 | 12.1k | s->flags |= SIG_FLAG_REQUIRE_PACKET; |
90 | | |
91 | 12.1k | if (DetectBufferSetActiveList(de_ctx, s, g_tcphdr_buffer_id) < 0) |
92 | 41 | return -1; |
93 | | |
94 | 12.0k | return 0; |
95 | 12.1k | } |
96 | | |
97 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
98 | | const DetectEngineTransforms *transforms, Packet *p, const int list_id) |
99 | 20.1k | { |
100 | 20.1k | SCEnter(); |
101 | | |
102 | 20.1k | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
103 | 20.1k | if (buffer->inspect == NULL) { |
104 | 13.7k | if (p->tcph == NULL) { |
105 | | // may happen when DecodeTCPPacket fails |
106 | | // for instance with invalid header length |
107 | 498 | return NULL; |
108 | 498 | } |
109 | 13.2k | uint32_t hlen = TCP_GET_HLEN(p); |
110 | 13.2k | if (((uint8_t *)p->tcph + (ptrdiff_t)hlen) > |
111 | 13.2k | ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))) |
112 | 0 | { |
113 | 0 | SCLogDebug("data out of range: %p > %p", |
114 | 0 | ((uint8_t *)p->tcph + (ptrdiff_t)hlen), |
115 | 0 | ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p))); |
116 | 0 | return NULL; |
117 | 0 | } |
118 | | |
119 | 13.2k | const uint32_t data_len = hlen; |
120 | 13.2k | const uint8_t *data = (const uint8_t *)p->tcph; |
121 | | |
122 | 13.2k | InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len); |
123 | 13.2k | InspectionBufferApplyTransforms(buffer, transforms); |
124 | 13.2k | } |
125 | | |
126 | 19.6k | return buffer; |
127 | 20.1k | } |
128 | | |
129 | | #ifdef UNITTESTS |
130 | | #include "tests/detect-tcphdr.c" |
131 | | #endif |