suricata
detect-smtp.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 * \author Philippe Antoine <pantoine@oisf.net>
22 *
23 */
24
25#include "suricata-common.h"
26#include "detect-smtp.h"
27#include "detect-engine.h"
31#include "detect-engine-mpm.h"
33#include "detect-parse.h"
34#include "app-layer-smtp.h"
35#include "rust.h"
36
37static int g_smtp_helo_buffer_id = 0;
38static int g_smtp_mail_from_buffer_id = 0;
39static int g_smtp_rcpt_to_buffer_id = 0;
40
41static int DetectSmtpHeloSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
42{
43 if (SCDetectBufferSetActiveList(de_ctx, s, g_smtp_helo_buffer_id) < 0)
44 return -1;
45
47 return -1;
48
49 return 0;
50}
51
52static InspectionBuffer *GetSmtpHeloData(DetectEngineThreadCtx *det_ctx,
53 const DetectEngineTransforms *transforms, Flow *f, const uint8_t _flow_flags, void *txv,
54 const int list_id)
55{
56 InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
57 if (buffer->inspect == NULL) {
58 SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
59 if (smtp_state) {
60 if (smtp_state->helo == NULL || smtp_state->helo_len == 0)
61 return NULL;
62 InspectionBufferSetup(det_ctx, list_id, buffer, smtp_state->helo, smtp_state->helo_len);
63 InspectionBufferApplyTransforms(det_ctx, buffer, transforms);
64 }
65 }
66 return buffer;
67}
68
69static int DetectSmtpMailFromSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
70{
71 if (SCDetectBufferSetActiveList(de_ctx, s, g_smtp_mail_from_buffer_id) < 0)
72 return -1;
73
75 return -1;
76
77 return 0;
78}
79
80static bool GetSmtpMailFromData(
81 const void *txv, const uint8_t _flow_flags, const uint8_t **data, uint32_t *data_len)
82{
84 if (tx->mail_from == NULL)
85 return false;
86 *data = tx->mail_from;
87 *data_len = tx->mail_from_len;
88 return true;
89}
90
91static int DetectSmtpRcptToSetup(DetectEngineCtx *de_ctx, Signature *s, const char *arg)
92{
93 if (SCDetectBufferSetActiveList(de_ctx, s, g_smtp_rcpt_to_buffer_id) < 0)
94 return -1;
95
97 return -1;
98
99 return 0;
100}
101
102static bool GetSmtpRcptToData(DetectEngineThreadCtx *_det_ctx, const void *txv, uint8_t _flow_flags,
103 uint32_t idx, const uint8_t **buffer, uint32_t *buffer_len)
104{
105 SMTPTransaction *tx = (SMTPTransaction *)txv;
106 if (TAILQ_EMPTY(&tx->rcpt_to_list)) {
107 return false;
108 }
109
110 SMTPString *s;
111 if (idx == 0) {
112 s = TAILQ_FIRST(&tx->rcpt_to_list);
113 } else {
114 // TODO optimize ?
115 s = TAILQ_FIRST(&tx->rcpt_to_list);
116 for (uint32_t i = 0; i < idx; i++) {
117 s = TAILQ_NEXT(s, next);
118 }
119 }
120 if (s == NULL) {
121 return false;
122 }
123
124 *buffer = s->str;
125 *buffer_len = s->len;
126 return true;
127}
128
130{
131 SCSigTableAppLiteElmt kw = { 0 };
132 kw.name = "smtp.helo";
133 kw.desc = "SMTP helo buffer";
134 kw.url = "/rules/smtp-keywords.html#smtp-helo";
135 kw.Setup = DetectSmtpHeloSetup;
139 DetectEngineInspectBufferGeneric, GetSmtpHeloData);
141 GetSmtpHeloData, ALPROTO_SMTP, 0);
142 DetectBufferTypeSetDescriptionByName("smtp.helo", "SMTP helo");
143 g_smtp_helo_buffer_id = DetectBufferTypeGetByName("smtp.helo");
144
145 kw.name = "smtp.mail_from";
146 kw.desc = "SMTP mail from buffer";
147 kw.url = "/rules/smtp-keywords.html#smtp-mail-from";
148 kw.Setup = DetectSmtpMailFromSetup;
151 g_smtp_mail_from_buffer_id = SCDetectHelperBufferMpmRegister(
152 "smtp.mail_from", "SMTP MAIL FROM", ALPROTO_SMTP, STREAM_TOSERVER, GetSmtpMailFromData);
153
154 kw.name = "smtp.rcpt_to";
155 kw.desc = "SMTP rcpt to buffer";
156 kw.url = "/rules/smtp-keywords.html#smtp-rcpt-to";
157 kw.Setup = DetectSmtpRcptToSetup;
160 g_smtp_rcpt_to_buffer_id = SCDetectHelperMultiBufferMpmRegister(
161 "smtp.rcpt_to", "SMTP RCPT TO", ALPROTO_SMTP, STREAM_TOSERVER, GetSmtpRcptToData);
162}
struct HtpBodyChunk_ * next
@ ALPROTO_SMTP
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
int SCDetectHelperBufferMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionSingleBufferGetDataPtr GetData)
int SCDetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppProto alproto, uint8_t direction, InspectionMultiBufferGetDataPtr GetData)
uint16_t SCDetectHelperKeywordRegister(const SCSigTableAppLiteElmt *kw)
void InspectionBufferApplyTransforms(DetectEngineThreadCtx *det_ctx, InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
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
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const DetectEngineAppInspectionEngine *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
int DetectBufferTypeGetByName(const char *name)
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
void SCDetectSMTPRegister(void)
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
#define TAILQ_FIRST(head)
Definition queue.h:250
#define TAILQ_NEXT(elm, field)
Definition queue.h:307
#define TAILQ_EMPTY(head)
Definition queue.h:248
main detection engine ctx
Definition detect.h:932
Flow data structure.
Definition flow.h:356
App-layer light version of SigTableElmt.
uint16_t flags
flags SIGMATCH_*
const char * desc
keyword description
const char * name
keyword name
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
function callback to parse and setup keyword in rule
const char * url
keyword documentation url
uint16_t helo_len
uint8_t * helo
uint8_t * str
Signature container.
Definition detect.h:668