suricata
detect-dns-name.c
Go to the documentation of this file.
1/* Copyright (C) 2025 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 rrnames:
22 * - dns.queries.rrname
23 * - dns.answers.rrname
24 * - dns.authorities.name
25 * - dns.additionals.name
26 */
27
28#include "detect.h"
29#include "detect-parse.h"
30#include "detect-engine.h"
34#include "detect-dns-name.h"
35#include "rust.h"
36
43
44static int query_buffer_id = 0;
45static int answer_buffer_id = 0;
46static int authority_buffer_id = 0;
47static int additional_buffer_id = 0;
48
49static int mdns_query_buffer_id = 0;
50static int mdns_answer_buffer_id = 0;
51static int mdns_authority_buffer_id = 0;
52static int mdns_additional_buffer_id = 0;
53
54static int DetectSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str, int id)
55{
56 if (SCDetectBufferSetActiveList(de_ctx, s, id) < 0) {
57 return -1;
58 }
60 return -1;
61 }
62
63 return 0;
64}
65
66static int SetupQueryBuffer(DetectEngineCtx *de_ctx, Signature *s, const char *str)
67{
68 return DetectSetup(de_ctx, s, str, query_buffer_id);
69}
70
71static int SetupAnswerBuffer(DetectEngineCtx *de_ctx, Signature *s, const char *str)
72{
73 return DetectSetup(de_ctx, s, str, answer_buffer_id);
74}
75
76static int SetupAdditionalsBuffer(DetectEngineCtx *de_ctx, Signature *s, const char *str)
77{
78 return DetectSetup(de_ctx, s, str, additional_buffer_id);
79}
80
81static int SetupAuthoritiesBuffer(DetectEngineCtx *de_ctx, Signature *s, const char *str)
82{
83 return DetectSetup(de_ctx, s, str, authority_buffer_id);
84}
85
86static int SetupQueryBufferMdns(DetectEngineCtx *de_ctx, Signature *s, const char *str)
87{
88 return DetectSetup(de_ctx, s, str, mdns_query_buffer_id);
89}
90
91static int SetupAnswerBufferMdns(DetectEngineCtx *de_ctx, Signature *s, const char *str)
92{
93 return DetectSetup(de_ctx, s, str, mdns_answer_buffer_id);
94}
95
96static int SetupAdditionalsBufferMdns(DetectEngineCtx *de_ctx, Signature *s, const char *str)
97{
98 return DetectSetup(de_ctx, s, str, mdns_additional_buffer_id);
99}
100
101static int SetupAuthoritiesBufferMdns(DetectEngineCtx *de_ctx, Signature *s, const char *str)
102{
103 return DetectSetup(de_ctx, s, str, mdns_authority_buffer_id);
104}
105
106static int Register(const char *keyword, const char *desc, const char *doc,
107 int (*Setup)(DetectEngineCtx *, Signature *, const char *),
108 InspectionMultiBufferGetDataPtr GetBufferFn, AppProto alproto)
109{
110 int keyword_id = SCDetectHelperNewKeywordId();
111 sigmatch_table[keyword_id].name = keyword;
112 sigmatch_table[keyword_id].desc = desc;
113 sigmatch_table[keyword_id].url = doc;
114 sigmatch_table[keyword_id].Setup = Setup;
115 sigmatch_table[keyword_id].flags |= SIGMATCH_NOOPT;
117
118 DetectAppLayerMultiRegister(keyword, alproto, SIG_FLAG_TOSERVER, 1, GetBufferFn, 2);
119 DetectAppLayerMultiRegister(keyword, alproto, SIG_FLAG_TOCLIENT, 1, GetBufferFn, 2);
120
121 DetectBufferTypeSetDescriptionByName(keyword, keyword);
123
124 return DetectBufferTypeGetByName(keyword);
125}
126
128{
129 query_buffer_id = Register("dns.queries.rrname", "DNS query rrname sticky buffer",
130 "/rules/dns-keywords.html#dns.queries.rrname", SetupQueryBuffer, SCDnsTxGetQueryName,
132 answer_buffer_id = Register("dns.answers.rrname", "DNS answer rrname sticky buffer",
133 "/rules/dns-keywords.html#dns.answers.rrname", SetupAnswerBuffer, SCDnsTxGetAnswerName,
135 additional_buffer_id =
136 Register("dns.additionals.rrname", "DNS additionals rrname sticky buffer",
137 "/rules/dns-keywords.html#dns-additionals-rrname", SetupAdditionalsBuffer,
138 SCDnsTxGetAdditionalName, ALPROTO_DNS);
139 authority_buffer_id = Register("dns.authorities.rrname", "DNS authorities rrname sticky buffer",
140 "/rules/dns-keywords.html#dns-authorities-rrname", SetupAuthoritiesBuffer,
141 SCDnsTxGetAuthorityName, ALPROTO_DNS);
142
143 mdns_query_buffer_id = Register("mdns.queries.rrname", "mDNS query rrname sticky buffer",
144 "/rules/mdns-keywords.html#mdns.queries.rrname", SetupQueryBufferMdns,
145 SCDnsTxGetQueryName, ALPROTO_MDNS);
146 mdns_answer_buffer_id = Register("mdns.answers.rrname", "mDNS answer rrname sticky buffer",
147 "/rules/mdns-keywords.html#mdns.answers.rrname", SetupAnswerBufferMdns,
148 SCMdnsTxGetAnswerName, ALPROTO_MDNS);
149 mdns_additional_buffer_id =
150 Register("mdns.additionals.rrname", "mDNS additionals rrname sticky buffer",
151 "/rules/mdns-keywords.html#mdns-additionals-rrname", SetupAdditionalsBufferMdns,
152 SCDnsTxGetAdditionalName, ALPROTO_MDNS);
153 mdns_authority_buffer_id =
154 Register("mdns.authorities.rrname", "mDNS authorities rrname sticky buffer",
155 "/rules/mdns-keywords.html#mdns-authorities-rrname", SetupAuthoritiesBufferMdns,
156 SCDnsTxGetAuthorityName, ALPROTO_MDNS);
157}
uint16_t AppProto
@ ALPROTO_MDNS
@ ALPROTO_DNS
DnsSection
@ DNS_QUERY
@ DNS_ADDITIONAL
@ DNS_ANSWER
@ DNS_AUTHORITY
void DetectDnsNameRegister(void)
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
int SCDetectHelperNewKeywordId(void)
bool(* InspectionMultiBufferGetDataPtr)(struct DetectEngineThreadCtx_ *det_ctx, const void *txv, const uint8_t flow_flags, uint32_t local_id, const uint8_t **buf, uint32_t *buf_len)
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectBufferTypeSupportsMultiInstance(const char *name)
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority)
int DetectBufferTypeGetByName(const char *name)
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
AppProto alproto
Definition detect.h:673
#define str(s)