/src/suricata7/src/detect-ttl.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2020 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 Gurvinder Singh <gurvindersighdahiya@gmail.com> |
22 | | * \author Victor Julien <victor@inliniac.net> |
23 | | * |
24 | | * Implements the ttl keyword including prefilter support. |
25 | | */ |
26 | | |
27 | | #include "suricata-common.h" |
28 | | #include "stream-tcp.h" |
29 | | |
30 | | #include "detect.h" |
31 | | #include "detect-parse.h" |
32 | | #include "detect-engine-prefilter-common.h" |
33 | | #include "detect-engine-uint.h" |
34 | | |
35 | | #include "detect-ttl.h" |
36 | | #include "util-debug.h" |
37 | | #include "util-byte.h" |
38 | | |
39 | | /* prototypes */ |
40 | | static int DetectTtlMatch (DetectEngineThreadCtx *, Packet *, |
41 | | const Signature *, const SigMatchCtx *); |
42 | | static int DetectTtlSetup (DetectEngineCtx *, Signature *, const char *); |
43 | | void DetectTtlFree (DetectEngineCtx *, void *); |
44 | | #ifdef UNITTESTS |
45 | | void DetectTtlRegisterTests (void); |
46 | | #endif |
47 | | static int PrefilterSetupTtl(DetectEngineCtx *de_ctx, SigGroupHead *sgh); |
48 | | static bool PrefilterTtlIsPrefilterable(const Signature *s); |
49 | | |
50 | | /** |
51 | | * \brief Registration function for ttl: keyword |
52 | | */ |
53 | | |
54 | | void DetectTtlRegister(void) |
55 | 34 | { |
56 | 34 | sigmatch_table[DETECT_TTL].name = "ttl"; |
57 | 34 | sigmatch_table[DETECT_TTL].desc = "check for a specific IP time-to-live value"; |
58 | 34 | sigmatch_table[DETECT_TTL].url = "/rules/header-keywords.html#ttl"; |
59 | 34 | sigmatch_table[DETECT_TTL].Match = DetectTtlMatch; |
60 | 34 | sigmatch_table[DETECT_TTL].Setup = DetectTtlSetup; |
61 | 34 | sigmatch_table[DETECT_TTL].Free = DetectTtlFree; |
62 | | #ifdef UNITTESTS |
63 | | sigmatch_table[DETECT_TTL].RegisterTests = DetectTtlRegisterTests; |
64 | | #endif |
65 | 34 | sigmatch_table[DETECT_TTL].SupportsPrefilter = PrefilterTtlIsPrefilterable; |
66 | 34 | sigmatch_table[DETECT_TTL].SetupPrefilter = PrefilterSetupTtl; |
67 | | |
68 | 34 | return; |
69 | 34 | } |
70 | | |
71 | | /** |
72 | | * \brief This function is used to match TTL rule option on a packet with |
73 | | * those passed via ttl |
74 | | * |
75 | | * \param t pointer to thread vars |
76 | | * \param det_ctx pointer to the pattern matcher thread |
77 | | * \param p pointer to the current packet |
78 | | * \param m pointer to the sigmatch that we will cast into DetectU8Data |
79 | | * |
80 | | * \retval 0 no match |
81 | | * \retval 1 match |
82 | | */ |
83 | | static int DetectTtlMatch (DetectEngineThreadCtx *det_ctx, Packet *p, |
84 | | const Signature *s, const SigMatchCtx *ctx) |
85 | 11.9k | { |
86 | 11.9k | if (PKT_IS_PSEUDOPKT(p)) |
87 | 102 | return 0; |
88 | | |
89 | 11.8k | uint8_t pttl; |
90 | 11.8k | if (PKT_IS_IPV4(p)) { |
91 | 11.3k | pttl = IPV4_GET_IPTTL(p); |
92 | 11.3k | } else if (PKT_IS_IPV6(p)) { |
93 | 480 | pttl = IPV6_GET_HLIM(p); |
94 | 480 | } else { |
95 | 0 | SCLogDebug("Packet is not IPv4 or IPv6"); |
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 11.8k | const DetectU8Data *ttld = (const DetectU8Data *)ctx; |
100 | 11.8k | return DetectU8Match(pttl, ttld); |
101 | 11.8k | } |
102 | | |
103 | | /** |
104 | | * \brief this function is used to attld the parsed ttl data into the current signature |
105 | | * |
106 | | * \param de_ctx pointer to the Detection Engine Context |
107 | | * \param s pointer to the Current Signature |
108 | | * \param ttlstr pointer to the user provided ttl options |
109 | | * |
110 | | * \retval 0 on Success |
111 | | * \retval -1 on Failure |
112 | | */ |
113 | | static int DetectTtlSetup (DetectEngineCtx *de_ctx, Signature *s, const char *ttlstr) |
114 | 19.4k | { |
115 | 19.4k | DetectU8Data *ttld = DetectU8Parse(ttlstr); |
116 | 19.4k | if (ttld == NULL) |
117 | 2.00k | return -1; |
118 | | |
119 | 17.4k | SigMatch *sm = SigMatchAlloc(); |
120 | 17.4k | if (sm == NULL) { |
121 | 0 | DetectTtlFree(de_ctx, ttld); |
122 | 0 | return -1; |
123 | 0 | } |
124 | | |
125 | 17.4k | sm->type = DETECT_TTL; |
126 | 17.4k | sm->ctx = (SigMatchCtx *)ttld; |
127 | | |
128 | 17.4k | SigMatchAppendSMToList(s, sm, DETECT_SM_LIST_MATCH); |
129 | 17.4k | s->flags |= SIG_FLAG_REQUIRE_PACKET; |
130 | 17.4k | return 0; |
131 | 17.4k | } |
132 | | |
133 | | /** |
134 | | * \brief this function will free memory associated with DetectU8Data |
135 | | * |
136 | | * \param ptr pointer to DetectU8Data |
137 | | */ |
138 | | void DetectTtlFree(DetectEngineCtx *de_ctx, void *ptr) |
139 | 17.4k | { |
140 | 17.4k | rs_detect_u8_free(ptr); |
141 | 17.4k | } |
142 | | |
143 | | /* prefilter code */ |
144 | | |
145 | | static void |
146 | | PrefilterPacketTtlMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx) |
147 | 2.04k | { |
148 | 2.04k | if (PKT_IS_PSEUDOPKT(p)) { |
149 | 99 | SCReturn; |
150 | 99 | } |
151 | | |
152 | 1.94k | uint8_t pttl; |
153 | 1.94k | if (PKT_IS_IPV4(p)) { |
154 | 1.89k | pttl = IPV4_GET_IPTTL(p); |
155 | 1.89k | } else if (PKT_IS_IPV6(p)) { |
156 | 51 | pttl = IPV6_GET_HLIM(p); |
157 | 51 | } else { |
158 | 0 | SCLogDebug("Packet is not IPv4 or IPv6"); |
159 | 0 | return; |
160 | 0 | } |
161 | | |
162 | 1.94k | const PrefilterPacketHeaderCtx *ctx = pectx; |
163 | 1.94k | if (!PrefilterPacketHeaderExtraMatch(ctx, p)) |
164 | 240 | return; |
165 | | |
166 | 1.70k | DetectU8Data du8; |
167 | 1.70k | du8.mode = ctx->v1.u8[0]; |
168 | 1.70k | du8.arg1 = ctx->v1.u8[1]; |
169 | 1.70k | du8.arg2 = ctx->v1.u8[2]; |
170 | 1.70k | if (DetectU8Match(pttl, &du8)) { |
171 | 37 | SCLogDebug("packet matches ttl/hl %u", pttl); |
172 | 37 | PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt); |
173 | 37 | } |
174 | 1.70k | } |
175 | | |
176 | | static int PrefilterSetupTtl(DetectEngineCtx *de_ctx, SigGroupHead *sgh) |
177 | 4.28k | { |
178 | 4.28k | return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_TTL, PrefilterPacketU8Set, |
179 | 4.28k | PrefilterPacketU8Compare, PrefilterPacketTtlMatch); |
180 | 4.28k | } |
181 | | |
182 | | static bool PrefilterTtlIsPrefilterable(const Signature *s) |
183 | 0 | { |
184 | 0 | const SigMatch *sm; |
185 | 0 | for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) { |
186 | 0 | switch (sm->type) { |
187 | 0 | case DETECT_TTL: |
188 | 0 | return true; |
189 | 0 | } |
190 | 0 | } |
191 | 0 | return false; |
192 | 0 | } |
193 | | |
194 | | #ifdef UNITTESTS |
195 | | #include "tests/detect-ttl.c" |
196 | | #endif |