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