suricata
detect-dns-query-name.c
Go to the documentation of this file.
1 /* Copyright (C) 2023 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  * Detect keyword for DNS query names: dns.query.name
22  */
23 
24 #include "detect.h"
25 #include "detect-parse.h"
26 #include "detect-engine.h"
29 #include "detect-dns-query-name.h"
30 #include "util-profiling.h"
31 #include "rust.h"
32 
33 static int detect_buffer_id = 0;
34 
35 static int DetectSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
36 {
37  if (DetectBufferSetActiveList(de_ctx, s, detect_buffer_id) < 0) {
38  return -1;
39  }
41  return -1;
42  }
43 
44  return 0;
45 }
46 
47 static InspectionBuffer *GetBuffer(DetectEngineThreadCtx *det_ctx,
48  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
49  int list_id, uint32_t index)
50 {
51  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, index);
52  if (buffer == NULL) {
53  return NULL;
54  }
55  if (buffer->initialized) {
56  return buffer;
57  }
58 
59  bool to_client = (flags & STREAM_TOSERVER) == 0;
60  const uint8_t *data = NULL;
61  uint32_t data_len = 0;
62 
63  if (!SCDnsTxGetQueryName(txv, to_client, index, &data, &data_len)) {
65  return NULL;
66  }
67  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
68  buffer->flags = DETECT_CI_FLAGS_SINGLE;
69  return buffer;
70 }
71 
73 {
74  static const char *keyword = "dns.query.name";
76  sigmatch_table[DETECT_AL_DNS_QUERY_NAME].desc = "DNS query name sticky buffer";
77  sigmatch_table[DETECT_AL_DNS_QUERY_NAME].url = "/rules/dns-keywords.html#dns-query-name";
81 
82  /* Register in both directions as the query is usually echoed back
83  in the response. */
84  DetectAppLayerMultiRegister(keyword, ALPROTO_DNS, SIG_FLAG_TOSERVER, 0, GetBuffer, 2, 1);
85  DetectAppLayerMultiRegister(keyword, ALPROTO_DNS, SIG_FLAG_TOCLIENT, 0, GetBuffer, 2, 1);
86 
87  DetectBufferTypeSetDescriptionByName(keyword, "dns query name");
89 
90  detect_buffer_id = DetectBufferTypeGetByName(keyword);
91 }
SigTableElmt_::url
const char * url
Definition: detect.h:1307
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:41
SigTableElmt_::name
const char * name
Definition: detect.h:1304
InspectionBuffer::initialized
bool initialized
Definition: detect.h:377
DetectEngineTransforms
Definition: detect.h:408
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1356
InspectionBuffer
Definition: detect.h:373
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1298
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1043
rust.h
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:378
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1289
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority, int tx_min_progress)
Definition: detect-engine.c:2201
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1567
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1093
detect.h
util-profiling.h
DetectDnsQueryNameRegister
void DetectDnsQueryNameRegister(void)
Definition: detect-dns-query-name.c:72
detect-engine-content-inspection.h
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:49
flags
uint8_t flags
Definition: decode-gre.h:0
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1580
str
#define str(s)
Definition: suricata-common.h:291
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1520
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
DETECT_AL_DNS_QUERY_NAME
@ DETECT_AL_DNS_QUERY_NAME
Definition: detect-engine-register.h:246
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190
detect-dns-query-name.h