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"
29 #include "detect-dns-response.h"
30 #include "util-profiling.h"
31 #include "rust.h"
32 
33 static int detect_buffer_id = 0;
34 typedef struct PrefilterMpm {
35  int list_id;
36  const MpmCtx *mpm_ctx;
39 
45 
46  /* always last */
48 };
49 
51  enum DnsResponseSection response_section; /**< query, answer, authority, additional */
52  uint32_t response_id; /**< index into response resource records */
53  uint32_t local_id; /**< used as index into thread inspect array */
54 };
55 
56 static int DetectSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
57 {
58  if (DetectBufferSetActiveList(de_ctx, s, detect_buffer_id) < 0) {
59  return -1;
60  }
62  return -1;
63  }
64 
65  return 0;
66 }
67 
68 static InspectionBuffer *GetBuffer(DetectEngineThreadCtx *det_ctx, uint8_t flags,
69  const DetectEngineTransforms *transforms, void *txv, struct DnsResponseGetDataArgs *cbdata,
70  int list_id, bool get_rdata)
71 {
72  InspectionBuffer *buffer =
73  InspectionBufferMultipleForListGet(det_ctx, list_id, cbdata->local_id);
74  if (buffer == NULL) {
75  return NULL;
76  }
77  if (buffer->initialized) {
78  return buffer;
79  }
80 
81  const uint8_t *data = NULL;
82  uint32_t data_len = 0;
83 
84  if (get_rdata) {
85  /* Get rdata values that are formatted as resource names. */
86  switch (cbdata->response_section) {
88  if (!SCDnsTxGetAnswerRdata(txv, cbdata->response_id, &data, &data_len)) {
90  return NULL;
91  }
92  break;
94  if (!SCDnsTxGetAuthorityRdata(txv, cbdata->response_id, &data, &data_len)) {
96  return NULL;
97  }
98  break;
100  if (!SCDnsTxGetAdditionalRdata(txv, cbdata->response_id, &data, &data_len)) {
102  return NULL;
103  }
104  break;
105  default:
107  return NULL;
108  }
109  } else {
110  /* Get name values. */
111  switch (cbdata->response_section) {
112  case DNS_RESPONSE_QUERY:
113  if (!SCDnsTxGetQueryName(txv, true, cbdata->response_id, &data, &data_len)) {
115  return NULL;
116  }
117  break;
118  case DNS_RESPONSE_ANSWER:
119  if (!SCDnsTxGetAnswerName(txv, true, cbdata->response_id, &data, &data_len)) {
121  return NULL;
122  }
123  break;
125  if (!SCDnsTxGetAuthorityName(txv, cbdata->response_id, &data, &data_len)) {
127  return NULL;
128  }
129  break;
131  if (!SCDnsTxGetAdditionalName(txv, cbdata->response_id, &data, &data_len)) {
133  return NULL;
134  }
135  break;
136  default:
138  return NULL;
139  }
140  }
141 
142  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
143  buffer->flags = DETECT_CI_FLAGS_SINGLE;
144  return buffer;
145 }
146 
147 static inline uint8_t CheckSectionRecords(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
148  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
149  uint8_t flags, void *txv, const DetectEngineTransforms *transforms, uint32_t *local_id,
150  enum DnsResponseSection section)
151 {
152  uint32_t response_id = 0;
153 
154  /* loop through each record in DNS response section inspecting "name" and "rdata" */
155  while (1) {
156  struct DnsResponseGetDataArgs cbdata = { section, response_id, *local_id };
157 
158  /* do inspection for resource record "name" */
159  InspectionBuffer *buffer =
160  GetBuffer(det_ctx, flags, transforms, txv, &cbdata, engine->sm_list, false);
161  if (buffer == NULL || buffer->inspect == NULL) {
162  (*local_id)++;
163  break;
164  }
165 
166  if (DetectEngineContentInspectionBuffer(de_ctx, det_ctx, s, engine->smd, NULL, f, buffer,
169  }
170 
171  (*local_id)++;
172  if (section == DNS_RESPONSE_QUERY) {
173  /* no rdata to inspect for query section, move on to next record */
174  response_id++;
175  continue;
176  }
177 
178  /* do inspection for resource record "rdata" */
179  cbdata.local_id = *local_id;
180  buffer = GetBuffer(det_ctx, flags, transforms, txv, &cbdata, engine->sm_list, true);
181  if (buffer == NULL || buffer->inspect == NULL) {
182  (*local_id)++;
183  response_id++;
184  continue;
185  }
186 
187  if (DetectEngineContentInspectionBuffer(de_ctx, det_ctx, s, engine->smd, NULL, f, buffer,
190  }
191  (*local_id)++;
192  response_id++;
193  }
195 }
196 
197 static inline void CheckSectionRecordsPrefilter(DetectEngineThreadCtx *det_ctx, const void *pectx,
198  void *txv, const uint8_t flags, uint32_t *local_id, enum DnsResponseSection section)
199 {
200  const PrefilterMpm *ctx = (const PrefilterMpm *)pectx;
201  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
202  const int list_id = ctx->list_id;
203  uint32_t response_id = 0;
204 
205  while (1) {
206  struct DnsResponseGetDataArgs cbdata = { section, response_id, *local_id };
207 
208  /* extract resource record "name" */
209  InspectionBuffer *buffer =
210  GetBuffer(det_ctx, flags, ctx->transforms, txv, &cbdata, list_id, false);
211  if (buffer == NULL) {
212  (*local_id)++;
213  break;
214  }
215 
216  if (buffer->inspect_len >= mpm_ctx->minlen) {
217  (void)mpm_table[mpm_ctx->mpm_type].Search(
218  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
219  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
220  }
221 
222  (*local_id)++;
223  if (section == DNS_RESPONSE_QUERY) {
224  /* no rdata to inspect for query section, move on to next name entry */
225  response_id++;
226  continue;
227  }
228 
229  /* extract resource record "rdata" */
230  cbdata.local_id = *local_id;
231  buffer = GetBuffer(det_ctx, flags, ctx->transforms, txv, &cbdata, list_id, true);
232  if (buffer == NULL) {
233  (*local_id)++;
234  response_id++;
235  continue;
236  }
237 
238  if (buffer->inspect_len >= mpm_ctx->minlen) {
239  (void)mpm_table[mpm_ctx->mpm_type].Search(
240  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
241  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
242  }
243  (*local_id)++;
244  response_id++;
245  }
246 }
247 
248 static uint8_t DetectEngineInspectCb(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
249  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
250  uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
251 {
252  const DetectEngineTransforms *transforms = NULL;
253  if (!engine->mpm) {
254  transforms = engine->v2.transforms;
255  }
256 
257  uint32_t local_id = 0;
258  uint8_t ret_match = DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
259 
260  /* loop through each possible DNS response section */
261  for (enum DnsResponseSection section = DNS_RESPONSE_QUERY;
262  section < DNS_RESPONSE_MAX && ret_match == DETECT_ENGINE_INSPECT_SIG_NO_MATCH;
263  section++) {
264 
265  /* check each record in section inspecting "name" and "rdata" */
266  ret_match = CheckSectionRecords(
267  de_ctx, det_ctx, engine, s, f, flags, txv, transforms, &local_id, section);
268  }
269  return ret_match;
270 }
271 
272 static void DetectDnsResponsePrefilterTx(DetectEngineThreadCtx *det_ctx, const void *pectx,
273  Packet *p, Flow *f, void *txv, const uint64_t idx, const AppLayerTxData *_txd,
274  const uint8_t flags)
275 {
276  SCEnter();
277 
278  uint32_t local_id = 0;
279  /* loop through each possible DNS response section */
280  for (enum DnsResponseSection section = DNS_RESPONSE_QUERY; section < DNS_RESPONSE_MAX;
281  section++) {
282  /* check each record in section inspecting "name" and "rdata" */
283  CheckSectionRecordsPrefilter(det_ctx, pectx, txv, flags, &local_id, section);
284  }
285 }
286 
287 static void DetectDnsResponsePrefilterMpmFree(void *ptr)
288 {
289  SCFree(ptr);
290 }
291 
292 static int DetectDnsResponsePrefilterMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh,
293  MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
294 {
295  PrefilterMpm *pectx = SCCalloc(1, sizeof(*pectx));
296  if (pectx == NULL) {
297  return -1;
298  }
299  pectx->list_id = list_id;
300  pectx->mpm_ctx = mpm_ctx;
301  pectx->transforms = &mpm_reg->transforms;
302 
303  return PrefilterAppendTxEngine(de_ctx, sgh, DetectDnsResponsePrefilterTx,
304  mpm_reg->app_v2.alproto, mpm_reg->app_v2.tx_min_progress, pectx,
305  DetectDnsResponsePrefilterMpmFree, mpm_reg->pname);
306 }
307 
309 {
310  static const char *keyword = "dns.response.rrname";
312  sigmatch_table[DETECT_DNS_RESPONSE].desc = "DNS response sticky buffer";
313  sigmatch_table[DETECT_DNS_RESPONSE].url = "/rules/dns-keywords.html#dns-response-rrname";
317 
318  /* Register in the TO_CLIENT direction. */
320  keyword, ALPROTO_DNS, SIG_FLAG_TOCLIENT, 1, DetectEngineInspectCb, NULL);
321  DetectAppLayerMpmRegister(keyword, SIG_FLAG_TOCLIENT, 2, DetectDnsResponsePrefilterMpmRegister,
322  NULL, ALPROTO_DNS, 1);
323 
324  DetectBufferTypeSetDescriptionByName(keyword, "dns response rrname");
326 
327  detect_buffer_id = DetectBufferTypeGetByName(keyword);
328 }
DetectEngineAppInspectionEngine_
Definition: detect.h:431
SigTableElmt_::url
const char * url
Definition: detect.h:1323
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1782
DnsResponseGetDataArgs::response_section
enum DnsResponseSection response_section
Definition: detect-dns-response.c:51
DNS_RESPONSE_MAX
@ DNS_RESPONSE_MAX
Definition: detect-dns-response.c:47
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:435
MpmCtx_::mpm_type
uint8_t mpm_type
Definition: util-mpm.h:90
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1528
SigTableElmt_::desc
const char * desc
Definition: detect.h:1322
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:153
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:47
SigTableElmt_::name
const char * name
Definition: detect.h:1320
InspectionBuffer::initialized
bool initialized
Definition: detect.h:379
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1476
DnsResponseGetDataArgs::response_id
uint32_t response_id
Definition: detect-dns-response.c:52
DetectEngineTransforms
Definition: detect.h:410
PrefilterMpm::list_id
int list_id
Definition: detect-dns-response.c:35
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1377
InspectionBuffer
Definition: detect.h:375
Flow_
Flow data structure.
Definition: flow.h:354
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1216
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1314
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:855
DnsResponseSection
DnsResponseSection
Definition: detect-dns-response.c:40
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1064
rust.h
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:698
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:380
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:270
DNS_RESPONSE_AUTHORITY
@ DNS_RESPONSE_AUTHORITY
Definition: detect-dns-response.c:43
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1305
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:711
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1114
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:439
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1609
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1110
DnsResponseGetDataArgs
Definition: detect-dns-response.c:50
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
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:99
util-profiling.h
Packet_
Definition: decode.h:476
DETECT_DNS_RESPONSE
@ DETECT_DNS_RESPONSE
Definition: detect-engine-register.h:248
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@82::@84 app_v2
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:167
DNS_RESPONSE_ANSWER
@ DNS_RESPONSE_ANSWER
Definition: detect-dns-response.c:42
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1215
PrefilterMpm::transforms
const DetectEngineTransforms * transforms
Definition: detect-dns-response.c:37
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:453
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1383
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
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:1622
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:273
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:53
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:378
PrefilterMpm::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-dns-response.c:36
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:376
str
#define str(s)
Definition: suricata-common.h:300
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:614
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:450
DNS_RESPONSE_QUERY
@ DNS_RESPONSE_QUERY
Definition: detect-dns-response.c:41
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:1541
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1504
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:747
PrefilterMpm
struct PrefilterMpm PrefilterMpm
DetectDnsResponseRegister
void DetectDnsResponseRegister(void)
Definition: detect-dns-response.c:308
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1211
MpmCtx_
Definition: util-mpm.h:88
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PrefilterMpm
Definition: detect-dns-response.c:34
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@77 v2
DNS_RESPONSE_ADDITIONAL
@ DNS_RESPONSE_ADDITIONAL
Definition: detect-dns-response.c:44
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:700