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 typedef struct PrefilterMpm {
34  int list_id;
35  const MpmCtx *mpm_ctx;
38 
39 static int detect_buffer_id = 0;
40 
41 static int DetectSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
42 {
43  if (DetectBufferSetActiveList(de_ctx, s, detect_buffer_id) < 0) {
44  return -1;
45  }
47  return -1;
48  }
49 
50  return 0;
51 }
52 
53 static InspectionBuffer *GetBuffer(DetectEngineThreadCtx *det_ctx, uint8_t flags,
54  const DetectEngineTransforms *transforms, void *txv, uint32_t index, int list_id)
55 {
56  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, index);
57  if (buffer == NULL) {
58  return NULL;
59  }
60  if (buffer->initialized) {
61  return buffer;
62  }
63 
64  bool to_client = (flags & STREAM_TOSERVER) == 0;
65  const uint8_t *data = NULL;
66  uint32_t data_len = 0;
67 
68  if (!SCDnsTxGetAnswerName(txv, to_client, index, &data, &data_len)) {
70  return NULL;
71  }
72  InspectionBufferSetupMulti(buffer, transforms, data, data_len);
73  buffer->flags = DETECT_CI_FLAGS_SINGLE;
74  return buffer;
75 }
76 
77 static uint8_t DetectEngineInspectCb(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx,
78  const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f,
79  uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
80 {
81  const DetectEngineTransforms *transforms = NULL;
82  if (!engine->mpm) {
83  transforms = engine->v2.transforms;
84  }
85 
86  for (uint32_t i = 0;; i++) {
87  InspectionBuffer *buffer = GetBuffer(det_ctx, flags, transforms, txv, i, engine->sm_list);
88  if (buffer == NULL || buffer->inspect == NULL) {
89  break;
90  }
91 
92  const bool match = DetectEngineContentInspectionBuffer(de_ctx, det_ctx, s, engine->smd,
94  if (match) {
96  }
97  }
98 
100 }
101 
102 static void PrefilterTx(DetectEngineThreadCtx *det_ctx, const void *pectx, Packet *p, Flow *f,
103  void *txv, const uint64_t idx, const AppLayerTxData *_txd, const uint8_t flags)
104 {
105  SCEnter();
106 
107  const PrefilterMpm *ctx = (const PrefilterMpm *)pectx;
108  const MpmCtx *mpm_ctx = ctx->mpm_ctx;
109  const int list_id = ctx->list_id;
110 
111  for (uint32_t i = 0;; i++) {
112  InspectionBuffer *buffer = GetBuffer(det_ctx, flags, ctx->transforms, txv, i, list_id);
113  if (buffer == NULL) {
114  break;
115  }
116 
117  if (buffer->inspect_len >= mpm_ctx->minlen) {
118  (void)mpm_table[mpm_ctx->mpm_type].Search(
119  mpm_ctx, &det_ctx->mtc, &det_ctx->pmq, buffer->inspect, buffer->inspect_len);
120  PREFILTER_PROFILING_ADD_BYTES(det_ctx, buffer->inspect_len);
121  }
122  }
123 }
124 
125 static void PrefilterMpmFree(void *ptr)
126 {
127  SCFree(ptr);
128 }
129 
130 static int PrefilterMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx,
131  const DetectBufferMpmRegistry *mpm_reg, int list_id)
132 {
133  PrefilterMpm *pectx = SCCalloc(1, sizeof(*pectx));
134  if (pectx == NULL) {
135  return -1;
136  }
137  pectx->list_id = list_id;
138  pectx->mpm_ctx = mpm_ctx;
139  pectx->transforms = &mpm_reg->transforms;
140 
141  return PrefilterAppendTxEngine(de_ctx, sgh, PrefilterTx, mpm_reg->app_v2.alproto,
142  mpm_reg->app_v2.tx_min_progress, pectx, PrefilterMpmFree, mpm_reg->pname);
143 }
144 
146 {
147  static const char *keyword = "dns.answer.name";
149  sigmatch_table[DETECT_AL_DNS_ANSWER_NAME].desc = "DNS answer name sticky buffer";
150  sigmatch_table[DETECT_AL_DNS_ANSWER_NAME].url = "/rules/dns-keywords.html#dns-answer-name";
154 
155  /* Register in the TO_SERVER direction, even though this is not
156  normal, it could be provided as part of a request. */
158  keyword, ALPROTO_DNS, SIG_FLAG_TOSERVER, 0, DetectEngineInspectCb, NULL);
160  keyword, SIG_FLAG_TOSERVER, 2, PrefilterMpmRegister, NULL, ALPROTO_DNS, 1);
161 
162  /* Register in the TO_CLIENT direction. */
164  keyword, ALPROTO_DNS, SIG_FLAG_TOCLIENT, 0, DetectEngineInspectCb, NULL);
166  keyword, SIG_FLAG_TOCLIENT, 2, PrefilterMpmRegister, NULL, ALPROTO_DNS, 1);
167 
168  DetectBufferTypeSetDescriptionByName(keyword, "dns answer name");
170 
171  detect_buffer_id = DetectBufferTypeGetByName(keyword);
172 }
DetectEngineAppInspectionEngine_
Definition: detect.h:424
SigTableElmt_::url
const char * url
Definition: detect.h:1296
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1753
DetectEngineAppInspectionEngine_::mpm
bool mpm
Definition: detect.h:428
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:1497
SigTableElmt_::desc
const char * desc
Definition: detect.h:1295
ALPROTO_DNS
@ ALPROTO_DNS
Definition: app-layer-protos.h:41
SigTableElmt_::name
const char * name
Definition: detect.h:1293
InspectionBuffer::initialized
bool initialized
Definition: detect.h:375
SigGroupHead_
Container for matching data for a signature group.
Definition: detect.h:1445
DetectEngineTransforms
Definition: detect.h:406
PrefilterMpm::list_id
int list_id
Definition: detect-dns-answer-name.c:34
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1335
InspectionBuffer
Definition: detect.h:371
Flow_
Flow data structure.
Definition: flow.h:351
DetectEngineThreadCtx_::pmq
PrefilterRuleStore pmq
Definition: detect.h:1201
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1287
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1022
rust.h
DetectBufferMpmRegistry_
one time registration of keywords at start up
Definition: detect.h:677
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:376
DetectBufferMpmRegistry_::app_v2
struct DetectBufferMpmRegistry_::@88::@90 app_v2
DetectDnsAnswerNameRegister
void DetectDnsAnswerNameRegister(void)
Definition: detect-dns-answer-name.c:145
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:264
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1278
DetectBufferMpmRegistry_::transforms
DetectEngineTransforms transforms
Definition: detect.h:690
detect-engine-prefilter.h
PrefilterMpm::mpm_ctx
const MpmCtx * mpm_ctx
Definition: detect-dns-answer-name.c:35
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1072
DetectEngineAppInspectionEngine_::sm_list
uint16_t sm_list
Definition: detect.h:430
DETECT_AL_DNS_ANSWER_NAME
@ DETECT_AL_DNS_ANSWER_NAME
Definition: detect-engine-register.h:235
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:263
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1546
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1092
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 a MPM engine
Definition: detect-engine-mpm.c:89
MpmCtx_::minlen
uint16_t minlen
Definition: util-mpm.h:99
PrefilterMpm::transforms
const DetectEngineTransforms * transforms
Definition: detect-dns-answer-name.c:36
util-profiling.h
Packet_
Definition: decode.h:437
detect-dns-answer-name.h
MpmTableElmt_::Search
uint32_t(* Search)(const struct MpmCtx_ *, struct MpmThreadCtx_ *, PrefilterRuleStore *, const uint8_t *, uint32_t)
Definition: util-mpm.h:168
DetectEngineAppInspectionEngine_::v2
struct DetectEngineAppInspectionEngine_::@85 v2
DetectEngineThreadCtx_::mtc
MpmThreadCtx mtc
Definition: detect.h:1200
detect-engine-content-inspection.h
DetectEngineAppInspectionEngine_::smd
SigMatchData * smd
Definition: detect.h:441
PrefilterMpm
struct PrefilterMpm PrefilterMpm
AppLayerTxData
struct AppLayerTxData AppLayerTxData
Definition: detect.h:1355
PREFILTER_PROFILING_ADD_BYTES
#define PREFILTER_PROFILING_ADD_BYTES(det_ctx, bytes)
Definition: util-profiling.h:287
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
sigmatch_table
SigTableElmt sigmatch_table[DETECT_TBLSIZE]
Definition: detect-parse.c:127
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:1559
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:270
DETECT_ENGINE_INSPECT_SIG_NO_MATCH
#define DETECT_ENGINE_INSPECT_SIG_NO_MATCH
Definition: detect-engine-state.h:37
InspectionBuffer::inspect_len
uint32_t inspect_len
Definition: detect.h:374
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:372
str
#define str(s)
Definition: suricata-common.h:291
SCFree
#define SCFree(p)
Definition: util-mem.h:61
detect-parse.h
Signature_
Signature container.
Definition: detect.h:593
DetectEngineAppInspectionEngine_::transforms
const DetectEngineTransforms * transforms
Definition: detect.h:438
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:1499
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1473
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
register inspect engine at start up time
Definition: detect-engine.c:169
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
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1169
MpmCtx_
Definition: util-mpm.h:88
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PrefilterMpm
Definition: detect-dns-answer-name.c:33
DetectBufferMpmRegistry_::pname
char pname[32]
Definition: detect.h:679