suricata
output-json-smtp.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2021 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 Tom DeCanio <td@npulsetech.com>
22  *
23  * Implements SMTP JSON logging portion of the engine.
24  */
25 
26 #include "suricata-common.h"
27 #include "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-print.h"
36 
37 #include "util-debug.h"
38 
39 #include "output.h"
40 #include "app-layer-smtp.h"
41 #include "app-layer.h"
42 #include "app-layer-parser.h"
43 #include "util-privs.h"
44 #include "util-buffer.h"
45 #include "util-proto-name.h"
46 #include "util-logopenfile.h"
47 
48 #include "output-json.h"
49 #include "output-json-smtp.h"
51 
52 static void EveSmtpDataLogger(void *state, void *vtx, SCJsonBuilder *js)
53 {
54  if (state == NULL || vtx == NULL) {
55  return;
56  }
57 
58  SMTPTransaction *tx = vtx;
59  SMTPString *rcptto_str;
60  if (((SMTPState *)state)->helo) {
61  SCJbSetString(js, "helo", (const char *)((SMTPState *)state)->helo);
62  }
63  if (tx->mail_from) {
64  SCJbSetString(js, "mail_from", (const char *)tx->mail_from);
65  }
66  if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
67  SCJbOpenArray(js, "rcpt_to");
68  TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
69  SCJbAppendString(js, (char *)rcptto_str->str);
70  }
71  SCJbClose(js);
72  }
73 }
74 
75 static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
76 {
77  SCEnter();
78  JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
79 
81  p, LOG_DIR_FLOW, "smtp", NULL, tx_id, jhl->emaillog_ctx->eve_ctx);
82  if (unlikely(jb == NULL))
83  return TM_ECODE_OK;
84 
85  SCJbOpenObject(jb, "smtp");
86  EveSmtpDataLogger(state, tx, jb);
87  SCJbClose(jb);
88 
89  EveEmailLogJson(jhl, jb, p, f, state, tx, tx_id);
90  OutputJsonBuilderBuffer(tv, p, p->flow, jb, jhl->ctx);
91 
92  SCJbFree(jb);
93 
95 
96 }
97 
98 bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
99 {
100  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
101  if (smtp_state) {
102  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
103  if (tx) {
104  EveSmtpDataLogger(smtp_state, tx, js);
105  return true;
106  }
107  }
108 
109  return false;
110 }
111 
112 static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
113 {
114  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
115  OutputJsonEmailCtx *email_ctx = output_ctx->data;
116  if (email_ctx != NULL) {
117  SCFree(email_ctx);
118  }
119  SCFree(output_ctx);
120 }
121 
122 static OutputInitResult OutputSmtpLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
123 {
124  OutputInitResult result = { NULL, false };
125  OutputJsonCtx *ojc = parent_ctx->data;
126 
127  OutputJsonEmailCtx *email_ctx = SCCalloc(1, sizeof(OutputJsonEmailCtx));
128  if (unlikely(email_ctx == NULL))
129  return result;
130 
131  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
132  if (unlikely(output_ctx == NULL)) {
133  SCFree(email_ctx);
134  return result;
135  }
136 
137  email_ctx->eve_ctx = ojc;
138 
139  OutputEmailInitConf(conf, email_ctx);
140 
141  output_ctx->data = email_ctx;
142  output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
143 
144  /* enable the logger for the app layer */
146 
147  result.ctx = output_ctx;
148  result.ok = true;
149  return result;
150 }
151 
152 static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
153 {
155  if (unlikely(aft == NULL))
156  return TM_ECODE_FAILED;
157 
158  if(initdata == NULL) {
159  SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
160  goto error_exit;
161  }
162 
163  /* Use the Output Context (file pointer and mutex) */
164  aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
165 
166  aft->ctx = CreateEveThreadCtx(t, aft->emaillog_ctx->eve_ctx);
167  if (aft->ctx == NULL) {
168  goto error_exit;
169  }
170 
171  *data = (void *)aft;
172  return TM_ECODE_OK;
173 
174 error_exit:
175  SCFree(aft);
176  return TM_ECODE_FAILED;
177 }
178 
179 static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
180 {
181  JsonEmailLogThread *aft = (JsonEmailLogThread *)data;
182  if (aft == NULL) {
183  return TM_ECODE_OK;
184  }
185  FreeEveThreadCtx(aft->ctx);
186 
187  /* clear memory */
188  memset(aft, 0, sizeof(JsonEmailLogThread));
189 
190  SCFree(aft);
191  return TM_ECODE_OK;
192 }
193 
194 void JsonSmtpLogRegister (void) {
195  /* register as child of eve-log */
196  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSmtpLog", "eve-log.smtp",
197  OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger, JsonSmtpLogThreadInit,
198  JsonSmtpLogThreadDeinit);
199 }
SCJsonBuilder
struct SCJsonBuilder SCJsonBuilder
Definition: detect-engine-helper.h:83
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:131
JsonEmailLogThread_
Definition: output-json-email-common.h:33
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:58
threads.h
OutputJsonCtx_
Definition: output-json.h:77
Flow_
Flow data structure.
Definition: flow.h:359
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:29
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit)
Definition: output.c:411
EveEmailLogJson
TmEcode EveEmailLogJson(JsonEmailLogThread *aft, SCJsonBuilder *js, const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
Definition: output-json-email-common.c:168
util-privs.h
p
Packet * p
Definition: fuzz_dataset.c:30
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:1018
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
OutputCtx_::data
void * data
Definition: tm-modules.h:91
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
OutputCtx_
Definition: tm-modules.h:88
EveSMTPAddMetadata
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
Definition: output-json-smtp.c:98
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
SCAppLayerParserRegisterLogger
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:535
output-json.h
ALPROTO_SMTP
@ ALPROTO_SMTP
Definition: app-layer-protos.h:38
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
pkt-var.h
SMTPTransaction_::mail_from
uint8_t * mail_from
Definition: app-layer-smtp.h:103
output-json-email-common.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-eve-bindgen.h:35
app-layer-parser.h
Packet_
Definition: decode.h:516
SMTPTransaction_
Definition: app-layer-smtp.h:85
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
util-proto-name.h
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1219
JsonEmailLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-email-common.h:35
OutputJsonEmailCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-email-common.h:30
OutputInitResult_
Definition: output.h:46
Packet_::flow
struct Flow_ * flow
Definition: decode.h:564
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:94
SMTPString_
Definition: app-layer-smtp.h:66
CreateEveHeaderWithTxId
SCJsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum SCOutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
Definition: output-json.c:962
JsonSmtpLogRegister
void JsonSmtpLogRegister(void)
Definition: output-json-smtp.c:194
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:489
SCFree
#define SCFree(p)
Definition: util-mem.h:61
util-logopenfile.h
util-buffer.h
OutputEmailInitConf
void OutputEmailInitConf(SCConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:206
SMTPString_::str
uint8_t * str
Definition: app-layer-smtp.h:67
app-layer-smtp.h
JsonEmailLogThread_::emaillog_ctx
OutputJsonEmailCtx * emaillog_ctx
Definition: output-json-email-common.h:34
output-json-smtp.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
SCConfNode_
Definition: conf.h:37
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
output.h
app-layer.h
f
Flow f
Definition: fuzz_dataset.c:32