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 "debug.h"
25 #include "decode.h"
26 #include "detect.h"
27 
28 #include "app-layer-parser.h"
29 
30 #include "detect-parse.h"
31 #include "detect-engine.h"
32 #include "detect-engine-mpm.h"
34 #include "detect-content.h"
36 #include "detect-frame.h"
37 
38 #include "flow.h"
39 #include "flow-util.h"
40 #include "flow-var.h"
41 
42 #include "conf.h"
43 #include "conf-yaml-loader.h"
44 
45 #include "util-debug.h"
46 #include "util-unittest.h"
47 #include "util-spm.h"
48 #include "util-print.h"
49 
50 static int DetectFrameSetup(DetectEngineCtx *, Signature *, const char *);
51 
52 /**
53  * \brief this function setup the sticky buffer used in the rule
54  *
55  * \param de_ctx Pointer to the Detection Engine Context
56  * \param s Pointer to the Signature to which the current keyword belongs
57  * \param str The frame name. Can be short name 'pdu' or full name 'sip.pdu'
58  *
59  * \retval 0 On success
60  * \retval -1 On failure
61  */
62 static int DetectFrameSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
63 {
64  char value[256] = "";
65  strlcpy(value, str, sizeof(value));
66  char buffer_name[512] = ""; // for registering in detect API we always need <proto>.<frame>.
67 
68  const bool is_tcp = DetectProtoContainsProto(&s->proto, IPPROTO_TCP);
69  const bool is_udp = DetectProtoContainsProto(&s->proto, IPPROTO_UDP);
70  if (!(is_tcp || is_udp)) {
71  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "'frame' keyword only supported for TCP and UDP");
72  return -1;
73  }
74 
75  char *dot = strchr(value, '.');
76  if (dot != NULL)
77  *dot++ = '\0';
78  const char *val = dot ? dot : value;
79  const char *proto = dot ? value : NULL;
80 
81  bool is_short = false;
82  AppProto keyword_alproto = ALPROTO_UNKNOWN;
83  AppProto rule_alproto = s->alproto;
84 
85  if (proto != NULL) {
86  keyword_alproto = StringToAppProto(proto);
87  if (!AppProtoIsValid(keyword_alproto)) {
88  is_short = true;
89  keyword_alproto = rule_alproto;
90  }
91  } else {
92  is_short = true;
93  keyword_alproto = rule_alproto;
94  }
95 
96  if (is_short && rule_alproto == ALPROTO_UNKNOWN) {
98  "rule protocol unknown, can't use shorthand notation for frame '%s'", str);
99  return -1;
100  } else if (rule_alproto == ALPROTO_UNKNOWN) {
101  if (DetectSignatureSetAppProto(s, keyword_alproto) < 0)
102  return -1;
103  } else if (!AppProtoEquals(rule_alproto, keyword_alproto)) {
105  "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  raw_frame_type = AppLayerParserGetFrameIdByName(IPPROTO_TCP, keyword_alproto, frame_str);
114  if (is_udp && raw_frame_type < 0)
115  raw_frame_type = AppLayerParserGetFrameIdByName(IPPROTO_UDP, keyword_alproto, frame_str);
116  if (raw_frame_type < 0) {
117  SCLogError(SC_ERR_INVALID_RULE_ARGUMENT, "unknown frame '%s' for protocol '%s'", frame_str,
118  proto);
119  return -1;
120  }
121  BUG_ON(raw_frame_type >= UINT8_MAX);
122 
123  if (is_short) {
124  snprintf(buffer_name, sizeof(buffer_name), "%s.%s", AppProtoToString(s->alproto), str);
125  SCLogDebug("short name: %s", buffer_name);
126  } else {
127  strlcpy(buffer_name, str, sizeof(buffer_name));
128  SCLogDebug("long name: %s", buffer_name);
129  }
130 
131  uint8_t frame_type = (uint8_t)raw_frame_type;
132  /* TODO we can have TS and TC specific frames */
133  const int buffer_id = DetectEngineBufferTypeRegisterWithFrameEngines(de_ctx, buffer_name,
134  SIG_FLAG_TOSERVER | SIG_FLAG_TOCLIENT, keyword_alproto, frame_type);
135  if (buffer_id < 0)
136  return -1;
137 
138  if (DetectBufferSetActiveList(s, buffer_id) < 0)
139  return -1;
140 
141  return 0;
142 }
143 
144 #ifdef UNITTESTS
145 
146 static int DetectFrameTestBadRules(void)
147 {
150 
151  const char *sigs[] = {
152  "alert tcp-pkt any any -> any any (frame:tls.pdu; content:\"a\"; sid:1;)",
153  "alert udp any any -> any any (frame:tls.pdu; content:\"a\"; sid:2;)",
154  "alert smb any any -> any any (frame:tls.pdu; content:\"a\"; sid:3;)",
155  "alert tcp any any -> any any (frame:tls; content:\"a\"; sid:4;)",
156  "alert tls any any -> any any (content:\"abc\"; frame:tls.pdu; content:\"a\"; sid:5;)",
157  "alert tls any any -> any any (tls.version:1.0; frame:tls.pdu; content:\"a\"; sid:6;)",
158  "alert tls any any -> any any (frame:smb1.pdu; content:\"a\"; sid:7;)",
159  NULL,
160  };
161 
162  const char **sig = sigs;
163  while (*sig) {
164  SCLogDebug("sig %s", *sig);
166  FAIL_IF_NOT_NULL(s);
167  sig++;
168  }
169 
171  PASS;
172 }
173 
174 static void DetectFrameRegisterTests(void)
175 {
176  UtRegisterTest("DetectFrameTestBadRules", DetectFrameTestBadRules);
177 }
178 #endif
179 
180 /**
181  * \brief Registration function for keyword: ja3_hash
182  */
184 {
185  sigmatch_table[DETECT_FRAME].name = "frame";
186  sigmatch_table[DETECT_FRAME].desc = "sticky buffer for inspecting app-layer frames";
187  sigmatch_table[DETECT_FRAME].Setup = DetectFrameSetup;
189 #ifdef UNITTESTS
190  sigmatch_table[DETECT_FRAME].RegisterTests = DetectFrameRegisterTests;
191 #endif
192 }
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
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:1477
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1267
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Signature_::alproto
AppProto alproto
Definition: detect.h:552
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
SC_ERR_INVALID_RULE_ARGUMENT
@ SC_ERR_INVALID_RULE_ARGUMENT
Definition: util-error.h:302
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:80
threads.h
AppProtoToString
const char * AppProtoToString(AppProto alproto)
Maps the ALPROTO_*, to its string equivalent.
Definition: app-layer-protos.c:30
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1261
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:811
DetectEngineCtxFree
void DetectEngineCtxFree(DetectEngineCtx *)
Free a DetectEngineCtx::
Definition: detect-engine.c:2433
DETECT_FRAME
@ DETECT_FRAME
Definition: detect-engine-register.h:108
proto
uint8_t proto
Definition: decode-template.h:0
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
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:236
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:17
StringToAppProto
AppProto StringToAppProto(const char *proto_name)
Maps a string to its ALPROTO_* equivalent.
Definition: app-layer-protos.c:149
util-print.h
detect-engine-mpm.h
detect.h
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:281
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:1130
DetectEngineAppendSig
Signature * DetectEngineAppendSig(DetectEngineCtx *de_ctx, const char *sigstr)
Parse and append a Signature into the Detection Engine Context signature list.
Definition: detect-parse.c:2420
Signature_::proto
DetectProto proto
Definition: detect.h:566
suricata-common.h
AppLayerParserGetFrameIdByName
int AppLayerParserGetFrameIdByName(uint8_t ipproto, AppProto alproto, const char *name)
Definition: app-layer-parser.c:1517
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
util-spm.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
str
#define str(s)
Definition: suricata-common.h:272
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2394
DetectBufferSetActiveList
int DetectBufferSetActiveList(Signature *s, const int list)
Definition: detect-engine.c:1291
flow.h
flow-var.h
debug.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
DetectProtoContainsProto
int DetectProtoContainsProto(const DetectProto *dp, int proto)
see if a DetectProto contains a certain proto
Definition: detect-engine-proto.c:135
DetectFrameRegister
void DetectFrameRegister(void)
Registration function for keyword: ja3_hash.
Definition: detect-frame.c:183