suricata
detect-dns-response.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 response: dns.response.rrname
22  */
23 
24 #include "detect.h"
25 #include "detect-parse.h"
26 #include "detect-engine.h"
27 #include "detect-engine-buffer.h"
30 #include "detect-dns-response.h"
31 #include "util-profiling.h"
32 #include "rust.h"
33 
34 static int detect_buffer_id = 0;
35 typedef struct PrefilterMpm {
36  int list_id;
37  const MpmCtx *mpm_ctx;
40 
46 
47  /* always last */
49 };
50 
52  enum DnsResponseSection response_section; /**< query, answer, authority, additional */
53  uint32_t response_id; /**< index into response resource records */
54  uint32_t local_id; /**< used as index into thread inspect array */
55 };
56 
57 static int DetectSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
58 {
59  if (SCDetectBufferSetActiveList(de_ctx, s, detect_buffer_id) < 0) {
60  return -1;
61  }
63  return -1;
64  }
65 
66  return 0;
67 }
68 
69 static InspectionBuffer *GetBuffer(DetectEngineThreadCtx *det_ctx, uint8_t flags,
70  const DetectEngineTransforms *transforms, void *txv, struct DnsResponseGetDataArgs *cbdata,
71  int list_id, bool get_rdata)
72 {
73  InspectionBuffer *buffer =
74  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
75  if (buffer == NULL) {
76  return NULL;
77  }
78  if (buffer->initialized) {
79  return buffer;
80  }
81 
82  const uint8_t *data = NULL;
83  uint32_t data_len = 0;
84 
85  if (get_rdata) {
86  /* Get rdata values that are formatted as resource names. */
87  switch (cbdata->response_section) {
89  if (!SCDnsTxGetAnswerRdata(txv, cbdata->response_id, &data, &data_len)) {
91  return NULL;
92  }
93  break;
95  if (!SCDnsTxGetAuthorityRdata(txv, cbdata->response_id, &data, &data_len)) {
97  return NULL;
98  }
99  break;
101  if (!SCDnsTxGetAdditionalRdata(txv, cbdata->response_id, &data, &data_len)) {
103  return NULL;
104  }
105  break;
106  default:
108  return NULL;
109  }
110  } else {
111  /* Get name values. */
112  switch (cbdata->response_section) {
113  case DNS_RESPONSE_QUERY:
114  if (!SCDnsTxGetQueryName(
115  det_ctx, txv, STREAM_TOCLIENT, cbdata->response_id, &data, &data_len)) {
117  return NULL;
118  }
119  break;
120  case DNS_RESPONSE_ANSWER:
121  if (!SCDnsTxGetAnswerName(
122  det_ctx, txv, STREAM_TOCLIENT, cbdata->response_id, &data, &data_len)) {
124  return NULL;
125  }
126  break;
128  if (!SCDnsTxGetAuthorityName(
129  det_ctx, txv, 0, cbdata->response_id, &data, &data_len)) {
131  return NULL;
132  }
133  break;
135  if (!SCDnsTxGetAdditionalName(
136  det_ctx, txv, 0, cbdata->response_id, &data, &data_len)) {
138  return NULL;
139  }
140  break;
141  default:
143  return NULL;
144  }
145  }
146 
147  InspectionBufferSetupMulti(det_ctx, buffer, transforms, data, data_len);
148  buffer->flags = DETECT_CI_FLAGS_SINGLE;
149  return buffer;
150 }
151 
152 static inline uint8_t CheckSectionRecords(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
153  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
154  uint8_t flags, void *txv, const DetectEngineTransforms *transforms, uint32_t *local_id,
155  enum DnsResponseSection section)
156 {
157  uint32_t response_id = 0;
158 
159  /* loop through each record in DNS response section inspecting "name" and "rdata" */
160  while (1) {
161  struct DnsResponseGetDataArgs cbdata = { section, response_id, *local_id };
162 
163  /* do inspection for resource record "name" */
164  InspectionBuffer *buffer =
165  GetBuffer(det_ctx, flags, transforms, txv, &cbdata, engine->sm_list, false);
166  if (buffer == NULL || buffer->inspect == NULL) {
167  (*local_id)++;
168  break;
169  }
170 
171  if (DetectEngineContentInspectionBuffer(de_ctx, det_ctx, s, engine->smd, NULL, f, buffer,
174  }
175 
176  (*local_id)++;
177  if (section == DNS_RESPONSE_QUERY) {
178  /* no rdata to inspect for query section, move on to next record */
179  response_id++;
180  continue;
181  }
182 
183  /* do inspection for resource record "rdata" */
184  cbdata.local_id = *local_id;
185  buffer = GetBuffer(det_ctx, flags, transforms, txv, &cbdata, engine->sm_list, true);
186  if (buffer == NULL || buffer->inspect == NULL) {
187  (*local_id)++;
188  response_id++;
189  continue;
190  }
191 
192  if (DetectEngineContentInspectionBuffer(de_ctx, det_ctx, s, engine->smd, NULL, f, buffer,
195  }
196  (*local_id)++;
197  response_id++;
198  }
200 }
201 
202 static inline void CheckSectionRecordsPrefilter(DetectEngineThreadCtx *det_ctx, const void *pectx,
203  void *txv, const uint8_t flags, uint32_t *local_id, enum DnsResponseSection section)
204 {
205  const PrefilterMpm *ctx = (const PrefilterMpm *)pectx;
206  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
207  const int list_id = ctx->list_id;
208  uint32_t response_id = 0;
209 
210  while (1) {
211  struct DnsResponseGetDataArgs cbdata = { section, response_id, *local_id };
212 
213  /* extract resource record "name" */
214  InspectionBuffer *buffer =
215  GetBuffer(det_ctx, flags, ctx->transforms, txv, &cbdata, list_id, false);
216  if (buffer == NULL) {
217  (*local_id)++;
218  break;
219  }
220 
221  if (buffer->inspect_len >= mpm_ctx->minlen) {
222  (void)mpm_table[mpm_ctx->mpm_type].Search(
223  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
224  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
225  }
226 
227  (*local_id)++;
228  if (section == DNS_RESPONSE_QUERY) {
229  /* no rdata to inspect for query section, move on to next name entry */
230  response_id++;
231  continue;
232  }
233 
234  /* extract resource record "rdata" */
235  cbdata.local_id = *local_id;
236  buffer = GetBuffer(det_ctx, flags, ctx->transforms, txv, &cbdata, list_id, true);
237  if (buffer == NULL) {
238  (*local_id)++;
239  response_id++;
240  continue;
241  }
242 
243  if (buffer->inspect_len >= mpm_ctx->minlen) {
244  (void)mpm_table[mpm_ctx->mpm_type].Search(
245  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
246  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
247  }
248  (*local_id)++;
249  response_id++;
250  }
251 }
252 
253 static uint8_t DetectEngineInspectCb(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
254  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
255  uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
256 {
257  const DetectEngineTransforms *transforms = NULL;
258  if (!engine->mpm) {
259  transforms = engine->v2.transforms;
260  }
261 
262  uint32_t local_id = 0;
263  uint8_t ret_match = DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
264 
265  /* loop through each possible DNS response section */
266  for (enum DnsResponseSection section = DNS_RESPONSE_QUERY;
267  section < DNS_RESPONSE_MAX && ret_match == DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
268  section++) {
269 
270  /* check each record in section inspecting "name" and "rdata" */
271  ret_match = CheckSectionRecords(
272  de_ctx, det_ctx, engine, s, f, flags, txv, transforms, &local_id, section);
273  }
274  return ret_match;
275 }
276 
277 static void DetectDnsResponsePrefilterTx(DetectEngineThreadCtx *det_ctx, const void *pectx,
278  Packet *p, Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd,
279  const uint8_t flags)
280 {
281  SCEnter();
282 
283  uint32_t local_id = 0;
284  /* loop through each possible DNS response section */
285  for (enum DnsResponseSection section = DNS_RESPONSE_QUERY; section < DNS_RESPONSE_MAX;
286  section++) {
287  /* check each record in section inspecting "name" and "rdata" */
288  CheckSectionRecordsPrefilter(det_ctx, pectx, txv, flags, &local_id, section);
289  }
290 }
291 
292 static void DetectDnsResponsePrefilterMpmFree(void *ptr)
293 {
294  SCFree(ptr);
295 }
296 
297 static int DetectDnsResponsePrefilterMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
298  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
299 {
300  PrefilterMpm *pectx = SCCalloc(1, sizeof(*pectx));
301  if (pectx == NULL) {
302  return -1;
303  }
304  pectx->list_id = list_id;
305  pectx->mpm_ctx = mpm_ctx;
306  pectx->transforms = &mpm_reg->transforms;
307 
308  return PrefilterAppendTxEngine(de_ctx, sgh, DetectDnsResponsePrefilterTx,
309  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress, pectx,
310  DetectDnsResponsePrefilterMpmFree, mpm_reg->pname);
311 }
312 
314 {
315  static const char *keyword = "dns.response.rrname";
317  sigmatch_table[DETECT_DNS_RESPONSE].desc = "DNS response sticky buffer";
318  sigmatch_table[DETECT_DNS_RESPONSE].url = "/rules/dns-keywords.html#dns-response-rrname";
322 
323  /* Register in the TO_CLIENT direction. */
325  keyword, ALPROTO_DNS, SIG_FLAG_TOCLIENT, 1, DetectEngineInspectCb, NULL);
326  DetectAppLayerMpmRegister(keyword, SIG_FLAG_TOCLIENT, 2, DetectDnsResponsePrefilterMpmRegister,
327  NULL, ALPROTO_DNS, 1);
328 
329  DetectBufferTypeSetDescriptionByName(keyword, "dns response rrname");
331 
332  detect_buffer_id = DetectBufferTypeGetByName(keyword);
333 }
DetectEngineAppInspectionEngine_
Definition: detect.h:437
SigTableElmt_::url
const char * url
Definition: detect.h:1431
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2313
DnsResponseGetDataArgs::response_section
enum DnsResponseSection response_section
Definition: detect-dns-response.c:52
DNS_RESPONSE_MAX
@ DNS_RESPONSE_MAX
Definition: detect-dns-response.c:48
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:441
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:95
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1645
SigTableElmt_::desc
const char * desc
Definition: detect.h:1430
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:155
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
SigTableElmt_::name
const char * name
Definition: detect.h:1428
InspectionBuffer::initialized
bool initialized
Definition: detect.h:384
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1598
DnsResponseGetDataArgs::response_id
uint32_t response_id
Definition: detect-dns-response.c:53
DetectEngineTransforms
Definition: detect.h:415
PrefilterMpm::list_id
int list_id
Definition: detect-dns-response.c:36
InspectionBuffer
Definition: detect.h:380
Flow_
Flow data structure.
Definition: flow.h:356
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1324
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1422
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:931
DnsResponseSection
DnsResponseSection
Definition: detect-dns-response.c:41
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1107
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@79 v2
rust.h
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:764
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:385
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:271
DNS_RESPONSE_AUTHORITY
@ DNS_RESPONSE_AUTHORITY
Definition: detect-dns-response.c:44
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1413
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:777
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1157
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:445
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1517
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1223
DnsResponseGetDataArgs
Definition: detect-dns-response.c:51
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect.h
DETECT_ENGINE_INSPECT_SIG_MATCH
#define DETECT_ENGINE_INSPECT_SIG_MATCH
Definition: detect-engine-state.h:38
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@87::@89 app_v2
DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
@ DETECT_ENGINE_CONTENT_INSPECTION_MODE_STATE
Definition: detect-engine-content-inspection.h:36
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:104
util-profiling.h
Packet_
Definition: decode.h:492
DETECT_DNS_RESPONSE
@ DETECT_DNS_RESPONSE
Definition: detect-engine-register.h:246
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:177
DNS_RESPONSE_ANSWER
@ DNS_RESPONSE_ANSWER
Definition: detect-dns-response.c:43
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1320
PrefilterMpm::transforms
const DetectEngineTransforms * transforms
Definition: detect-dns-response.c:38
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:459
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1494
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:286
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
detect-engine-buffer.h
PrefilterAppendTxEngine
int PrefilterAppendTxEngine(DetectEngineCtx *de_ctx, SigGroupHead *sgh, PrefilterTxFn PrefilterTxFunc, AppProto alproto, int tx_min_progress, void *pectx, void(*FreeFunc)(void *pectx), const char *name)
Definition: detect-engine-prefilter.c:352
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:37
DnsResponseGetDataArgs::local_id
uint32_t local_id
Definition: detect-dns-response.c:54
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:383
PrefilterMpm::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-dns-response.c:37
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:381
str
#define str(s)
Definition: suricata-common.h:308
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:670
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:456
DNS_RESPONSE_QUERY
@ DNS_RESPONSE_QUERY
Definition: detect-dns-response.c:42
mpm_table
MpmTableElmt mpm_table[MPM_TABLE_SIZE]
Definition: util-mpm.c:47
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:1449
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1620
detect-dns-response.h
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:245
DetectEngineContentInspectionBuffer
bool DetectEngineContentInspectionBuffer(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const Signature *s, const SigMatchData *smd, Packet *p, Flow *f, const InspectionBuffer *b, const enum DetectContentInspectionType inspection_mode)
wrapper around DetectEngineContentInspectionInternal to return true/false only
Definition: detect-engine-content-inspection.c:757
PrefilterMpm
struct PrefilterMpm PrefilterMpm
DetectDnsResponseRegister
void DetectDnsResponseRegister(void)
Definition: detect-dns-response.c:313
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1254
MpmCtx_
Definition: util-mpm.h:93
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PrefilterMpm
Definition: detect-dns-response.c:35
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(DetectEngineThreadCtx *det_ctx, 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:1530
DNS_RESPONSE_ADDITIONAL
@ DNS_RESPONSE_ADDITIONAL
Definition: detect-dns-response.c:45
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:766