suricata
detect-dns-answer-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 answer name: dns.answer.name
22  */
23 
24 #include "detect.h"
25 #include "detect-parse.h"
26 #include "detect-engine.h"
29 #include "detect-dns-answer-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, uint8_t flags, void *txv, int list_id,
49  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 (!SCDnsTxGetAnswerName(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.answer.name";
76  sigmatch_table[DETECT_AL_DNS_ANSWER_NAME].desc = "DNS answer name sticky buffer";
77  sigmatch_table[DETECT_AL_DNS_ANSWER_NAME].url = "/rules/dns-keywords.html#dns-answer-name";
81 
82  /* Register in the TO_SERVER direction, even though this is not
83  normal, it could be provided as part of a request. */
84  DetectAppLayerMultiRegister(keyword, ALPROTO_DNS, SIG_FLAG_TOSERVER, 0, GetBuffer, 2, 1);
85  /* Register in the TO_CLIENT direction. */
86  DetectAppLayerMultiRegister(keyword, ALPROTO_DNS, SIG_FLAG_TOCLIENT, 0, GetBuffer, 2, 1);
87 
88  DetectBufferTypeSetDescriptionByName(keyword, "dns answer name");
90 
91  detect_buffer_id = DetectBufferTypeGetByName(keyword);
92 }
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
DetectDnsAnswerNameRegister
void DetectDnsAnswerNameRegister(void)
Definition: detect-dns-answer-name.c:72
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
DETECT_AL_DNS_ANSWER_NAME
@ DETECT_AL_DNS_ANSWER_NAME
Definition: detect-engine-register.h:245
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
detect-dns-answer-name.h
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
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190