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 #include "util-unittest.h"
37 
38 #include "util-debug.h"
39 
40 #include "output.h"
41 #include "app-layer-smtp.h"
42 #include "app-layer.h"
43 #include "app-layer-parser.h"
44 #include "util-privs.h"
45 #include "util-buffer.h"
46 #include "util-proto-name.h"
47 #include "util-logopenfile.h"
48 #include "util-time.h"
49 
50 #include "output-json.h"
51 #include "output-json-smtp.h"
53 
54 static void EveSmtpDataLogger(void *state, void *vtx, JsonBuilder *js)
55 {
56  SMTPTransaction *tx = vtx;
57  SMTPString *rcptto_str;
58  if (((SMTPState *)state)->helo) {
59  jb_set_string(js, "helo", (const char *)((SMTPState *)state)->helo);
60  }
61  if (tx->mail_from) {
62  jb_set_string(js, "mail_from", (const char *)tx->mail_from);
63  }
64  if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
65  jb_open_array(js, "rcpt_to");
66  TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
67  jb_append_string(js, (char *)rcptto_str->str);
68  }
69  jb_close(js);
70  }
71 }
72 
73 static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
74 {
75  SCEnter();
76  JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
77 
78  JsonBuilder *jb = CreateEveHeaderWithTxId(
79  p, LOG_DIR_FLOW, "smtp", NULL, tx_id, jhl->emaillog_ctx->eve_ctx);
80  if (unlikely(jb == NULL))
81  return TM_ECODE_OK;
82 
83  jb_open_object(jb, "smtp");
84  EveSmtpDataLogger(state, tx, jb);
85  jb_close(jb);
86 
87  EveEmailLogJson(jhl, jb, p, f, state, tx, tx_id);
88  OutputJsonBuilderBuffer(jb, jhl->ctx);
89 
90  jb_free(jb);
91 
93 
94 }
95 
96 bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
97 {
98  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
99  if (smtp_state) {
100  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
101  if (tx) {
102  EveSmtpDataLogger(smtp_state, tx, js);
103  return true;
104  }
105  }
106 
107  return false;
108 }
109 
110 static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
111 {
112  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
113  OutputJsonEmailCtx *email_ctx = output_ctx->data;
114  if (email_ctx != NULL) {
115  SCFree(email_ctx);
116  }
117  SCFree(output_ctx);
118 }
119 
120 static OutputInitResult OutputSmtpLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
121 {
122  OutputInitResult result = { NULL, false };
123  OutputJsonCtx *ojc = parent_ctx->data;
124 
125  OutputJsonEmailCtx *email_ctx = SCCalloc(1, sizeof(OutputJsonEmailCtx));
126  if (unlikely(email_ctx == NULL))
127  return result;
128 
129  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
130  if (unlikely(output_ctx == NULL)) {
131  SCFree(email_ctx);
132  return result;
133  }
134 
135  email_ctx->eve_ctx = ojc;
136 
137  OutputEmailInitConf(conf, email_ctx);
138 
139  output_ctx->data = email_ctx;
140  output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
141 
142  /* enable the logger for the app layer */
144 
145  result.ctx = output_ctx;
146  result.ok = true;
147  return result;
148 }
149 
150 static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
151 {
153  if (unlikely(aft == NULL))
154  return TM_ECODE_FAILED;
155 
156  if(initdata == NULL) {
157  SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
158  goto error_exit;
159  }
160 
161  /* Use the Output Context (file pointer and mutex) */
162  aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
163 
164  aft->ctx = CreateEveThreadCtx(t, aft->emaillog_ctx->eve_ctx);
165  if (aft->ctx == NULL) {
166  goto error_exit;
167  }
168 
169  *data = (void *)aft;
170  return TM_ECODE_OK;
171 
172 error_exit:
173  SCFree(aft);
174  return TM_ECODE_FAILED;
175 }
176 
177 static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
178 {
179  JsonEmailLogThread *aft = (JsonEmailLogThread *)data;
180  if (aft == NULL) {
181  return TM_ECODE_OK;
182  }
183  FreeEveThreadCtx(aft->ctx);
184 
185  /* clear memory */
186  memset(aft, 0, sizeof(JsonEmailLogThread));
187 
188  SCFree(aft);
189  return TM_ECODE_OK;
190 }
191 
192 void JsonSmtpLogRegister (void) {
193  /* register as child of eve-log */
194  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSmtpLog", "eve-log.smtp",
195  OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger, JsonSmtpLogThreadInit,
196  JsonSmtpLogThreadDeinit, NULL);
197 }
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:116
EveSMTPAddMetadata
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-smtp.c:96
JsonEmailLogThread_
Definition: output-json-email-common.h:33
EveEmailLogJson
TmEcode EveEmailLogJson(JsonEmailLogThread *aft, JsonBuilder *js, const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
Definition: output-json-email-common.c:335
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
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:79
Flow_
Flow data structure.
Definition: flow.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:928
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
util-privs.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:85
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:88
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:84
OutputCtx_
Definition: tm-modules.h:85
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
output-json.h
ALPROTO_SMTP
@ ALPROTO_SMTP
Definition: app-layer-protos.h:32
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:475
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
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:92
output-json-email-common.h
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:48
app-layer-parser.h
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
Definition: output-json.c:873
OutputEmailInitConf
void OutputEmailInitConf(ConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:372
Packet_
Definition: decode.h:436
SMTPTransaction_
Definition: app-layer-smtp.h:72
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:83
util-proto-name.h
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1125
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
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:91
SMTPString_
Definition: app-layer-smtp.h:65
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, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:413
JsonSmtpLogRegister
void JsonSmtpLogRegister(void)
Definition: output-json-smtp.c:192
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:38
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:467
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
SMTPString_::str
uint8_t * str
Definition: app-layer-smtp.h:66
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:275
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
output.h
app-layer.h