suricata
detect-frame.c
Go to the documentation of this file.
1/* Copyright (C) 2021-2022 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
22#include "suricata-common.h"
23#include "threads.h"
24#include "decode.h"
25#include "detect.h"
26
27#include "app-layer-frames.h"
28#include "app-layer-parser.h"
29
30#include "detect-parse.h"
31#include "detect-engine.h"
33#include "detect-engine-mpm.h"
35#include "detect-content.h"
37#include "detect-frame.h"
38
39#include "flow.h"
40#include "flow-util.h"
41#include "flow-var.h"
42
43#include "conf.h"
44#include "conf-yaml-loader.h"
45
46#include "util-debug.h"
47#include "util-unittest.h"
48#include "util-spm.h"
49#include "util-print.h"
50
51static int DetectFrameSetup(DetectEngineCtx *, Signature *, const char *);
52
53/**
54 * \brief this function setup the sticky buffer used in the rule
55 *
56 * \param de_ctx Pointer to the Detection Engine Context
57 * \param s Pointer to the Signature to which the current keyword belongs
58 * \param str The frame name. Can be short name 'pdu' or full name 'sip.pdu'
59 *
60 * \retval 0 On success
61 * \retval -1 On failure
62 */
63static int DetectFrameSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
64{
65 char value[256] = "";
66 strlcpy(value, str, sizeof(value));
67 char buffer_name[512] = ""; // for registering in detect API we always need <proto>.<frame>.
68
69 const bool is_tcp = DetectProtoContainsProto(&s->proto, IPPROTO_TCP);
70 const bool is_udp = DetectProtoContainsProto(&s->proto, IPPROTO_UDP);
71 if (!(is_tcp || is_udp)) {
72 SCLogError("'frame' keyword only supported for TCP and UDP");
73 return -1;
74 }
75
76 char *dot = strchr(value, '.');
77 if (dot != NULL)
78 *dot++ = '\0';
79 const char *val = dot ? dot : value;
80 const char *proto = dot ? value : NULL;
81
82 bool is_short = false;
83 AppProto keyword_alproto = ALPROTO_UNKNOWN;
84 AppProto rule_alproto = s->alproto;
85
86 if (proto != NULL) {
87 keyword_alproto = StringToAppProto(proto);
88 if (!AppProtoIsValid(keyword_alproto)) {
89 is_short = true;
90 keyword_alproto = rule_alproto;
91 }
92 } else {
93 is_short = true;
94 keyword_alproto = rule_alproto;
95 }
96
97 if (is_short && rule_alproto == ALPROTO_UNKNOWN) {
98 SCLogError("rule protocol unknown, can't use shorthand notation for frame '%s'", str);
99 return -1;
100 } else if (rule_alproto == ALPROTO_UNKNOWN) {
101 if (SCDetectSignatureSetAppProto(s, keyword_alproto) < 0)
102 return -1;
103 } else if (!AppProtoEquals(rule_alproto, keyword_alproto)) {
104 SCLogError("frame '%s' protocol '%s' mismatch with rule protocol '%s'", str,
105 AppProtoToString(keyword_alproto), AppProtoToString(s->alproto));
106 return -1;
107 }
108
109 const char *frame_str = is_short ? str : val;
110 int raw_frame_type = -1;
111 if (is_tcp) {
112 if (strcmp(frame_str, "stream") == 0) {
113 raw_frame_type = FRAME_STREAM_TYPE;
114 } else {
115 raw_frame_type =
116 AppLayerParserGetFrameIdByName(IPPROTO_TCP, keyword_alproto, frame_str);
117 }
118 }
119 if (is_udp && raw_frame_type < 0)
120 raw_frame_type = AppLayerParserGetFrameIdByName(IPPROTO_UDP, keyword_alproto, frame_str);
121 if (raw_frame_type < 0) {
122 SCLogError("unknown frame '%s' for protocol '%s'", frame_str, proto);
123 return -1;
124 }
125 BUG_ON(raw_frame_type > UINT8_MAX);
126
127 if (is_short) {
128 snprintf(buffer_name, sizeof(buffer_name), "%s.%s", AppProtoToString(s->alproto), str);
129 SCLogDebug("short name: %s", buffer_name);
130 } else {
131 strlcpy(buffer_name, str, sizeof(buffer_name));
132 SCLogDebug("long name: %s", buffer_name);
133 }
134
135 uint8_t frame_type = (uint8_t)raw_frame_type;
136 /* TODO we can have TS and TC specific frames */
137 const int buffer_id = DetectEngineBufferTypeRegisterWithFrameEngines(de_ctx, buffer_name,
138 SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT, keyword_alproto, frame_type);
139 if (buffer_id < 0)
140 return -1;
141
142 if (SCDetectBufferSetActiveList(de_ctx, s, buffer_id) < 0)
143 return -1;
144
145 FrameConfigEnable(keyword_alproto, frame_type);
146 return 0;
147}
148
149#ifdef UNITTESTS
150
151static int DetectFrameTestBadRules(void)
152{
155
156 const char *sigs[] = {
157 "alert tcp-pkt any any -> any any (frame:tls.pdu; content:\"a\"; sid:1;)",
158 "alert udp any any -> any any (frame:tls.pdu; content:\"a\"; sid:2;)",
159 "alert smb any any -> any any (frame:tls.pdu; content:\"a\"; sid:3;)",
160 "alert tcp any any -> any any (frame:tls; content:\"a\"; sid:4;)",
161 "alert tls any any -> any any (content:\"abc\"; frame:tls.pdu; content:\"a\"; sid:5;)",
162 "alert tls any any -> any any (tls.version:1.0; frame:tls.pdu; content:\"a\"; sid:6;)",
163 "alert tls any any -> any any (frame:smb1.pdu; content:\"a\"; sid:7;)",
164 NULL,
165 };
166
167 const char **sig = sigs;
168 while (*sig) {
169 SCLogDebug("sig %s", *sig);
172 sig++;
173 }
174
176 PASS;
177}
178
179static void DetectFrameRegisterTests(void)
180{
181 UtRegisterTest("DetectFrameTestBadRules", DetectFrameTestBadRules);
182}
183#endif
184
185/**
186 * \brief Registration function for keyword: ja3_hash
187 */
189{
191 sigmatch_table[DETECT_FRAME].desc = "sticky buffer for inspecting app-layer frames";
192 sigmatch_table[DETECT_FRAME].Setup = DetectFrameSetup;
194#ifdef UNITTESTS
195 sigmatch_table[DETECT_FRAME].RegisterTests = DetectFrameRegisterTests;
196#endif
197}
void FrameConfigEnable(const AppProto p, const uint8_t type)
#define FRAME_STREAM_TYPE
int AppLayerParserGetFrameIdByName(uint8_t ipproto, AppProto alproto, const char *name)
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
uint16_t AppProto
@ ALPROTO_UNKNOWN
uint8_t proto
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
DetectEngineCtx * DetectEngineCtxInit(void)
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Signature * DetectEngineAppendSig(DetectEngineCtx *, const char *)
Parse and append a Signature into the Detection Engine Context signature list.
int DetectEngineBufferTypeRegisterWithFrameEngines(DetectEngineCtx *de_ctx, const char *name, const int direction, const AppProto alproto, const uint8_t frame_type)
void DetectFrameRegister(void)
Registration function for keyword: ja3_hash.
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
#define PASS
Pass the test.
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
main detection engine ctx
Definition detect.h:932
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
AppProto alproto
Definition detect.h:673
DetectProto proto
Definition detect.h:687
#define BUG_ON(x)
#define str(s)
size_t strlcpy(char *dst, const char *src, size_t siz)
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267