/src/suricata8/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 | 79 | { |
56 | 79 | sigmatch_table[DETECT_TTL].name = "ttl"; |
57 | 79 | sigmatch_table[DETECT_TTL].desc = "check for a specific IP time-to-live value"; |
58 | 79 | sigmatch_table[DETECT_TTL].url = "/rules/header-keywords.html#ttl"; |
59 | 79 | sigmatch_table[DETECT_TTL].Match = DetectTtlMatch; |
60 | 79 | sigmatch_table[DETECT_TTL].Setup = DetectTtlSetup; |
61 | 79 | sigmatch_table[DETECT_TTL].Free = DetectTtlFree; |
62 | | #ifdef UNITTESTS |
63 | | sigmatch_table[DETECT_TTL].RegisterTests = DetectTtlRegisterTests; |
64 | | #endif |
65 | 79 | sigmatch_table[DETECT_TTL].SupportsPrefilter = PrefilterTtlIsPrefilterable; |
66 | 79 | sigmatch_table[DETECT_TTL].SetupPrefilter = PrefilterSetupTtl; |
67 | 79 | } |
68 | | |
69 | | /** |
70 | | * \brief This function is used to match TTL rule option on a packet with |
71 | | * those passed via ttl |
72 | | * |
73 | | * \param t pointer to thread vars |
74 | | * \param det_ctx pointer to the pattern matcher thread |
75 | | * \param p pointer to the current packet |
76 | | * \param m pointer to the sigmatch that we will cast into DetectU8Data |
77 | | * |
78 | | * \retval 0 no match |
79 | | * \retval 1 match |
80 | | */ |
81 | | static int DetectTtlMatch (DetectEngineThreadCtx *det_ctx, Packet *p, |
82 | | const Signature *s, const SigMatchCtx *ctx) |
83 | 33.0k | { |
84 | 33.0k | DEBUG_VALIDATE_BUG_ON(PKT_IS_PSEUDOPKT(p)); |
85 | | |
86 | 33.0k | uint8_t pttl; |
87 | 33.0k | if (PacketIsIPv4(p)) { |
88 | 26.8k | const IPV4Hdr *ip4h = PacketGetIPv4(p); |
89 | 26.8k | pttl = IPV4_GET_RAW_IPTTL(ip4h); |
90 | 26.8k | } else if (PacketIsIPv6(p)) { |
91 | 6.14k | const IPV6Hdr *ip6h = PacketGetIPv6(p); |
92 | 6.14k | pttl = IPV6_GET_RAW_HLIM(ip6h); |
93 | 6.14k | } else { |
94 | 0 | SCLogDebug("Packet is not IPv4 or IPv6"); |
95 | 0 | return 0; |
96 | 0 | } |
97 | | |
98 | 33.0k | const DetectU8Data *ttld = (const DetectU8Data *)ctx; |
99 | 33.0k | return DetectU8Match(pttl, ttld); |
100 | 33.0k | } |
101 | | |
102 | | /** |
103 | | * \brief this function is used to attld the parsed ttl data into the current signature |
104 | | * |
105 | | * \param de_ctx pointer to the Detection Engine Context |
106 | | * \param s pointer to the Current Signature |
107 | | * \param ttlstr pointer to the user provided ttl options |
108 | | * |
109 | | * \retval 0 on Success |
110 | | * \retval -1 on Failure |
111 | | */ |
112 | | static int DetectTtlSetup (DetectEngineCtx *de_ctx, Signature *s, const char *ttlstr) |
113 | 15.8k | { |
114 | 15.8k | DetectU8Data *ttld = DetectU8Parse(ttlstr); |
115 | 15.8k | if (ttld == NULL) |
116 | 2.35k | return -1; |
117 | | |
118 | 13.4k | if (SCSigMatchAppendSMToList( |
119 | 13.4k | de_ctx, s, DETECT_TTL, (SigMatchCtx *)ttld, DETECT_SM_LIST_MATCH) == NULL) { |
120 | 0 | DetectTtlFree(de_ctx, ttld); |
121 | 0 | return -1; |
122 | 0 | } |
123 | 13.4k | s->flags |= SIG_FLAG_REQUIRE_PACKET; |
124 | 13.4k | return 0; |
125 | 13.4k | } |
126 | | |
127 | | /** |
128 | | * \brief this function will free memory associated with DetectU8Data |
129 | | * |
130 | | * \param ptr pointer to DetectU8Data |
131 | | */ |
132 | | void DetectTtlFree(DetectEngineCtx *de_ctx, void *ptr) |
133 | 13.4k | { |
134 | 13.4k | SCDetectU8Free(ptr); |
135 | 13.4k | } |
136 | | |
137 | | /* prefilter code */ |
138 | | |
139 | | static void |
140 | | PrefilterPacketTtlMatch(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx) |
141 | 4.33k | { |
142 | 4.33k | DEBUG_VALIDATE_BUG_ON(PKT_IS_PSEUDOPKT(p)); |
143 | | |
144 | 4.33k | uint8_t pttl; |
145 | 4.33k | if (PacketIsIPv4(p)) { |
146 | 4.08k | const IPV4Hdr *ip4h = PacketGetIPv4(p); |
147 | 4.08k | pttl = IPV4_GET_RAW_IPTTL(ip4h); |
148 | 4.08k | } else if (PacketIsIPv6(p)) { |
149 | 247 | const IPV6Hdr *ip6h = PacketGetIPv6(p); |
150 | 247 | pttl = IPV6_GET_RAW_HLIM(ip6h); |
151 | 247 | } else { |
152 | 0 | SCLogDebug("Packet is not IPv4 or IPv6"); |
153 | 0 | return; |
154 | 0 | } |
155 | | |
156 | 4.33k | const PrefilterPacketHeaderCtx *ctx = pectx; |
157 | 4.33k | if (!PrefilterPacketHeaderExtraMatch(ctx, p)) |
158 | 565 | return; |
159 | | |
160 | 3.76k | DetectU8Data du8; |
161 | 3.76k | du8.mode = ctx->v1.u8[0]; |
162 | 3.76k | du8.arg1 = ctx->v1.u8[1]; |
163 | 3.76k | du8.arg2 = ctx->v1.u8[2]; |
164 | 3.76k | if (DetectU8Match(pttl, &du8)) { |
165 | 31 | SCLogDebug("packet matches ttl/hl %u", pttl); |
166 | 31 | PrefilterAddSids(&det_ctx->pmq, ctx->sigs_array, ctx->sigs_cnt); |
167 | 31 | } |
168 | 3.76k | } |
169 | | |
170 | | static int PrefilterSetupTtl(DetectEngineCtx *de_ctx, SigGroupHead *sgh) |
171 | 3.53k | { |
172 | 3.53k | return PrefilterSetupPacketHeader(de_ctx, sgh, DETECT_TTL, SIG_MASK_REQUIRE_REAL_PKT, |
173 | 3.53k | PrefilterPacketU8Set, PrefilterPacketU8Compare, PrefilterPacketTtlMatch); |
174 | 3.53k | } |
175 | | |
176 | | static bool PrefilterTtlIsPrefilterable(const Signature *s) |
177 | 0 | { |
178 | 0 | const SigMatch *sm; |
179 | 0 | for (sm = s->init_data->smlists[DETECT_SM_LIST_MATCH] ; sm != NULL; sm = sm->next) { |
180 | 0 | switch (sm->type) { |
181 | 0 | case DETECT_TTL: |
182 | 0 | return true; |
183 | 0 | } |
184 | 0 | } |
185 | 0 | return false; |
186 | 0 | } |
187 | | |
188 | | #ifdef UNITTESTS |
189 | | #include "tests/detect-ttl.c" |
190 | | #endif |