suricata
detect-template-rust-buffer.c
Go to the documentation of this file.
1 /* Copyright (C) 2015-2017 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  * TODO: Update the \author in this file and detect-template.h.
20  * TODO: Update description in the \file section below.
21  * TODO: Remove SCLogNotice statements or convert to debug.
22  */
23 
24 /**
25  * \file
26  *
27  * \author FirstName LastName <yourname@domain>
28  *
29  * Set up of the "template_rust" keyword to allow content
30  * inspections on the decoded template application layer buffers.
31  */
32 
33 #include "suricata-common.h"
34 #include "conf.h"
35 #include "detect.h"
36 #include "detect-parse.h"
37 #include "detect-engine.h"
40 #include "app-layer-parser.h"
41 #include "rust.h"
42 
43 
44 static int DetectTemplateRustBufferSetup(DetectEngineCtx *, Signature *,
45  const char *);
46 static int DetectEngineInspectTemplateRustBuffer(DetectEngineCtx *de_ctx,
47  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
48  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id);
49 #ifdef UNITTESTS
50 static void DetectTemplateRustBufferRegisterTests(void);
51 #endif
52 static int g_template_rust_id = 0;
53 
55 {
56  /* TEMPLATE_START_REMOVE */
57  if (ConfGetNode("app-layer.protocols.template-rust") == NULL) {
58  return;
59  }
60  /* TEMPLATE_END_REMOVE */
62  "template_rust_buffer";
64  "Template content modifier to match on the template buffers";
66  DetectTemplateRustBufferSetup;
67 #ifdef UNITTESTS
69  DetectTemplateRustBufferRegisterTests;
70 #endif
72 
73  /* register inspect engines */
75  SIG_FLAG_TOSERVER, 0, DetectEngineInspectTemplateRustBuffer, NULL);
77  SIG_FLAG_TOCLIENT, 0, DetectEngineInspectTemplateRustBuffer, NULL);
78 
79  g_template_rust_id = DetectBufferTypeGetByName("template_rust_buffer");
80 
81  SCLogNotice("Template application layer detect registered.");
82 }
83 
84 static int DetectTemplateRustBufferSetup(DetectEngineCtx *de_ctx, Signature *s,
85  const char *str)
86 {
87  s->init_data->list = g_template_rust_id;
88 
90  return -1;
91 
92  return 0;
93 }
94 
95 static int DetectEngineInspectTemplateRustBuffer(DetectEngineCtx *de_ctx,
96  DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine,
97  const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
98 {
99  int ret = 0;
100  const uint8_t *data = NULL;
101  uint32_t data_len = 0;
102 
103  if (flags & STREAM_TOSERVER) {
104  rs_template_get_request_buffer(txv, &data, &data_len);
105  } else if (flags & STREAM_TOCLIENT) {
106  rs_template_get_response_buffer(txv, &data, &data_len);
107  }
108 
109  if (data != NULL) {
110  ret = DetectEngineContentInspection(de_ctx, det_ctx, s, engine->smd, NULL, f,
111  (uint8_t *)data, data_len, 0, DETECT_CI_FLAGS_SINGLE,
113  }
114 
115  SCLogNotice("Returning %d.", ret);
116  return ret;
117 }
118 
119 #ifdef UNITTESTS
120 
121 #include "util-unittest.h"
122 #include "util-unittest-helper.h"
123 #include "app-layer-parser.h"
124 #include "detect-engine.h"
125 #include "detect-parse.h"
126 #include "flow-util.h"
127 #include "stream-tcp.h"
128 
129 static int DetectTemplateRustBufferTest(void)
130 {
132  DetectEngineThreadCtx *det_ctx = NULL;
133  DetectEngineCtx *de_ctx = NULL;
134  Flow f;
135  Packet *p;
136  TcpSession tcp;
137  ThreadVars tv;
138  Signature *s;
139 
140  uint8_t request[] = "12:Hello World!";
141 
142  /* Setup flow. */
143  memset(&f, 0, sizeof(Flow));
144  memset(&tcp, 0, sizeof(TcpSession));
145  memset(&tv, 0, sizeof(ThreadVars));
146  p = UTHBuildPacket(request, sizeof(request), IPPROTO_TCP);
147  FLOW_INITIALIZE(&f);
149  f.protoctx = (void *)&tcp;
150  f.proto = IPPROTO_TCP;
151  f.flags |= FLOW_IPV4;
152  p->flow = &f;
155  StreamTcpInitConfig(true);
156 
159 
160  /* This rule should match. */
162  "alert tcp any any -> any any ("
163  "msg:\"TEMPLATE Test Rule\"; "
164  "template_rust_buffer; content:\"World!\"; "
165  "sid:1; rev:1;)");
166  FAIL_IF_NULL(s);
167 
168  /* This rule should not match. */
170  "alert tcp any any -> any any ("
171  "msg:\"TEMPLATE Test Rule\"; "
172  "template_rust_buffer; content:\"W0rld!\"; "
173  "sid:2; rev:1;)");
174  FAIL_IF_NULL(s);
175 
177  DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
178 
179  FLOWLOCK_WRLOCK(&f);
181  STREAM_TOSERVER, request, sizeof(request));
182  FLOWLOCK_UNLOCK(&f);
183 
184  /* Check that we have app-layer state. */
186 
187  SigMatchSignatures(&tv, de_ctx, det_ctx, p);
188  FAIL_IF(!PacketAlertCheck(p, 1));
189  FAIL_IF(PacketAlertCheck(p, 2));
190 
191  /* Cleanup. */
192  if (alp_tctx != NULL)
194  if (det_ctx != NULL)
195  DetectEngineThreadCtxDeinit(&tv, det_ctx);
196  if (de_ctx != NULL)
198  if (de_ctx != NULL)
200  StreamTcpFreeConfig(true);
201  FLOW_DESTROY(&f);
202  UTHFreePacket(p);
203 
204  PASS;
205 }
206 
207 static void DetectTemplateRustBufferRegisterTests(void)
208 {
209  UtRegisterTest("DetectTemplateRustBufferTest",
210  DetectTemplateRustBufferTest);
211 }
212 #endif /* UNITTESTS */
DetectEngineAppInspectionEngine_
Definition: detect.h:398
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1490
detect-engine.h
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SigTableElmt_::desc
const char * desc
Definition: detect.h:1269
PKT_HAS_FLOW
#define PKT_HAS_FLOW
Definition: decode.h:1175
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1267
stream-tcp.h
detect-template-rust-buffer.h
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_::proto
uint8_t proto
Definition: flow.h:375
PacketAlertCheck
int PacketAlertCheck(Packet *p, uint32_t sid)
Check if a certain sid alerted, this is used in the test functions.
Definition: detect-engine-alert.c:137
Packet_::flags
uint32_t flags
Definition: decode.h:462
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:175
Flow_
Flow data structure.
Definition: flow.h:353
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
AppLayerParserThreadCtxFree
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
Definition: app-layer-parser.c:320
FLOW_PKT_TOSERVER
#define FLOW_PKT_TOSERVER
Definition: flow.h:225
rust.h
UTHBuildPacket
Packet * UTHBuildPacket(uint8_t *payload, uint16_t payload_len, uint8_t ipproto)
UTHBuildPacket is a wrapper that build packets with default ip and port fields.
Definition: util-unittest-helper.c:337
SigMatchSignatures
void SigMatchSignatures(ThreadVars *tv, DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, Packet *p)
wrapper for old tests
Definition: detect.c:1790
DetectEngineContentInspection
int DetectEngineContentInspection(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const uint8_t *buffer, uint32_t buffer_len, uint32_t stream_start_offset, uint8_t flags, uint8_t inspection_mode)
Run the actual payload match functions.
Definition: detect-engine-content-inspection.c:102
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:458
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:237
Flow_::protoctx
void * protoctx
Definition: flow.h:451
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1252
FLOW_IPV4
#define FLOW_IPV4
Definition: flow.h:98
util-unittest.h
util-unittest-helper.h
FLOWLOCK_UNLOCK
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:270
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1077
StreamTcpInitConfig
void StreamTcpInitConfig(bool)
To initialize the stream global configuration data.
Definition: stream-tcp.c:357
FLOW_INITIALIZE
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:236
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1060
alp_tctx
AppLayerParserThreadCtx * alp_tctx
Definition: fuzz_applayerparserparse.c:20
FLOWLOCK_WRLOCK
#define FLOWLOCK_WRLOCK(fb)
Definition: flow.h:267
SignatureInitData_::list
int list
Definition: detect.h:525
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
app-layer-parser.h
SigGroupCleanup
int SigGroupCleanup(DetectEngineCtx *de_ctx)
Definition: detect-engine-build.c:2016
Packet_
Definition: decode.h:427
DetectAppLayerInspectEngineRegister2
void DetectAppLayerInspectEngineRegister2(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr2 Callback2, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:225
conf.h
Signature_::init_data
SignatureInitData * init_data
Definition: detect.h:619
SigGroupBuild
int SigGroupBuild(DetectEngineCtx *de_ctx)
Convert the signature list into the runtime match structure.
Definition: detect-engine-build.c:1948
AppLayerParserThreadCtxAlloc
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol's parser thread context.
Definition: app-layer-parser.c:299
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:415
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
Packet_::flow
struct Flow_ * flow
Definition: decode.h:464
DetectEngineThreadCtxInit
TmEcode DetectEngineThreadCtxInit(ThreadVars *, void *, void **)
initialize thread specific detection engine context
Definition: detect-engine.c:3142
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:47
StreamTcpFreeConfig
void StreamTcpFreeConfig(bool quiet)
Definition: stream-tcp.c:662
flags
uint8_t flags
Definition: decode-gre.h:0
AppLayerParserParse
int AppLayerParserParse(ThreadVars *tv, AppLayerParserThreadCtx *alp_tctx, Flow *f, AppProto alproto, uint8_t flags, const uint8_t *input, uint32_t input_len)
Definition: app-layer-parser.c:1237
suricata-common.h
DetectEngineThreadCtxDeinit
TmEcode DetectEngineThreadCtxDeinit(ThreadVars *, void *)
Definition: detect-engine.c:3354
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:73
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
DETECT_AL_TEMPLATE_RUST_BUFFER
@ DETECT_AL_TEMPLATE_RUST_BUFFER
Definition: detect-engine-register.h:269
str
#define str(s)
Definition: suricata-common.h:272
UTHFreePacket
void UTHFreePacket(Packet *p)
UTHFreePacket: function to release the allocated data from UTHBuildPacket and the packet itself.
Definition: util-unittest-helper.c:485
Flow_::alstate
void * alstate
Definition: flow.h:486
Flow_::flags
uint32_t flags
Definition: flow.h:431
detect-parse.h
Signature_
Signature container.
Definition: detect.h:548
ALPROTO_TEMPLATE
@ ALPROTO_TEMPLATE
Definition: app-layer-protos.h:59
FLOW_PKT_ESTABLISHED
#define FLOW_PKT_ESTABLISHED
Definition: flow.h:227
DetectEngineCtxInit
DetectEngineCtx * DetectEngineCtxInit(void)
Definition: detect-engine.c:2394
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1453
AppLayerParserThreadCtx_
Definition: app-layer-parser.c:86
DetectTemplateRustBufferRegister
void DetectTemplateRustBufferRegister(void)
Definition: detect-template-rust-buffer.c:54
TcpSession_
Definition: stream-tcp-private.h:260
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:232
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:460
FLOW_DESTROY
#define FLOW_DESTROY(f)
Definition: flow-util.h:130
PKT_STREAM_EST
#define PKT_STREAM_EST
Definition: decode.h:1172
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1259
ALPROTO_TEMPLATE_RUST
@ ALPROTO_TEMPLATE_RUST
Definition: app-layer-protos.h:60