suricata
output-json-smtp.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 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 "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-print.h"
37 #include "util-unittest.h"
38 
39 #include "util-debug.h"
40 
41 #include "output.h"
42 #include "app-layer-smtp.h"
43 #include "app-layer.h"
44 #include "app-layer-parser.h"
45 #include "util-privs.h"
46 #include "util-buffer.h"
47 #include "util-proto-name.h"
48 #include "util-logopenfile.h"
49 #include "util-time.h"
50 
51 #include "output-json.h"
52 #include "output-json-smtp.h"
54 
55 static void EveSmtpDataLogger(const Flow *f, void *state, void *vtx, uint64_t tx_id, JsonBuilder *js)
56 {
57  SMTPTransaction *tx = vtx;
58  SMTPString *rcptto_str;
59  if (((SMTPState *)state)->helo) {
60  jb_set_string(js, "helo", (const char *)((SMTPState *)state)->helo);
61  }
62  if (tx->mail_from) {
63  jb_set_string(js, "mail_from", (const char *)tx->mail_from);
64  }
65  if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
66  jb_open_array(js, "rcpt_to");
67  TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
68  jb_append_string(js, (char *)rcptto_str->str);
69  }
70  jb_close(js);
71  }
72 }
73 
74 static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
75 {
76  SCEnter();
77  JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
78 
79  JsonBuilder *jb = CreateEveHeaderWithTxId(p, LOG_DIR_FLOW, "smtp", NULL, tx_id);
80  if (unlikely(jb == NULL))
81  return TM_ECODE_OK;
82  EveAddCommonOptions(&jhl->emaillog_ctx->cfg, p, f, jb);
83 
84  /* reset */
85  MemBufferReset(jhl->buffer);
86 
87  jb_open_object(jb, "smtp");
88  EveSmtpDataLogger(f, state, tx, tx_id, jb);
89  jb_close(jb);
90 
91  if (EveEmailLogJson(jhl, jb, p, f, state, tx, tx_id) == TM_ECODE_OK) {
92  OutputJsonBuilderBuffer(jb, jhl->file_ctx, &jhl->buffer);
93  }
94 
95  jb_free(jb);
96 
98 
99 }
100 
101 bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
102 {
103  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
104  if (smtp_state) {
105  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
106  if (tx) {
107  EveSmtpDataLogger(f, smtp_state, tx, tx_id, js);
108  return true;
109  }
110  }
111 
112  return false;
113 }
114 
115 static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
116 {
117  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
118  OutputJsonEmailCtx *email_ctx = output_ctx->data;
119  if (email_ctx != NULL) {
120  SCFree(email_ctx);
121  }
122  SCFree(output_ctx);
123 }
124 
125 static OutputInitResult OutputSmtpLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
126 {
127  OutputInitResult result = { NULL, false };
128  OutputJsonCtx *ojc = parent_ctx->data;
129 
130  OutputJsonEmailCtx *email_ctx = SCMalloc(sizeof(OutputJsonEmailCtx));
131  if (unlikely(email_ctx == NULL))
132  return result;
133 
134  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
135  if (unlikely(output_ctx == NULL)) {
136  SCFree(email_ctx);
137  return result;
138  }
139 
140  email_ctx->file_ctx = ojc->file_ctx;
141  email_ctx->cfg = ojc->cfg;
142 
143  OutputEmailInitConf(conf, email_ctx);
144 
145  output_ctx->data = email_ctx;
146  output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
147 
148  /* enable the logger for the app layer */
150 
151  result.ctx = output_ctx;
152  result.ok = true;
153  return result;
154 }
155 
156 static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
157 {
159  if (unlikely(aft == NULL))
160  return TM_ECODE_FAILED;
161 
162  if(initdata == NULL) {
163  SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
164  goto error_exit;
165  }
166 
167  /* Use the Output Context (file pointer and mutex) */
168  aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
169 
171  if (aft->buffer == NULL) {
172  goto error_exit;
173  }
174 
176  if (!aft->file_ctx) {
177  goto error_exit;
178  }
179  *data = (void *)aft;
180  return TM_ECODE_OK;
181 
182 error_exit:
183  if (aft->buffer != NULL) {
184  MemBufferFree(aft->buffer);
185  }
186  SCFree(aft);
187  return TM_ECODE_FAILED;
188 }
189 
190 static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
191 {
192  JsonEmailLogThread *aft = (JsonEmailLogThread *)data;
193  if (aft == NULL) {
194  return TM_ECODE_OK;
195  }
196 
197  MemBufferFree(aft->buffer);
198  /* clear memory */
199  memset(aft, 0, sizeof(JsonEmailLogThread));
200 
201  SCFree(aft);
202  return TM_ECODE_OK;
203 }
204 
205 void JsonSmtpLogRegister (void) {
206  /* register as child of eve-log */
207  OutputRegisterTxSubModule(LOGGER_JSON_SMTP, "eve-log", "JsonSmtpLog",
208  "eve-log.smtp", OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger,
209  JsonSmtpLogThreadInit, JsonSmtpLogThreadDeinit, NULL);
210 }
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:107
EveSMTPAddMetadata
bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-smtp.c:101
OutputJsonEmailCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-email-common.h:31
JsonEmailLogThread_
Definition: output-json-email-common.h:34
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:104
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:364
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:63
threads.h
OutputJsonCtx_
Definition: output-json.h:101
Flow_
Flow data structure.
Definition: flow.h:347
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:347
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
util-privs.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:444
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1102
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:78
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
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:456
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
SMTPTransaction_::mail_from
uint8_t * mail_from
Definition: app-layer-smtp.h:86
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:659
output-json-email-common.h
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
OutputEmailInitConf
void OutputEmailInitConf(ConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:401
Packet_
Definition: decode.h:414
SMTPTransaction_
Definition: app-layer-smtp.h:68
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
util-proto-name.h
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1032
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
SMTPString_
Definition: app-layer-smtp.h:61
JsonEmailLogThread_::buffer
MemBuffer * buffer
Definition: output-json-email-common.h:37
JsonEmailLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-email-common.h:35
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:409
JsonSmtpLogRegister
void JsonSmtpLogRegister(void)
Definition: output-json-smtp.c:205
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
LOGGER_JSON_SMTP
@ LOGGER_JSON_SMTP
Definition: suricata-common.h:451
SCFree
#define SCFree(p)
Definition: util-mem.h:61
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id)
Definition: output-json.c:923
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:102
OutputJsonEmailCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-email-common.h:28
SMTPString_::str
uint8_t * str
Definition: app-layer-smtp.h:62
app-layer-smtp.h
JsonEmailLogThread_::emaillog_ctx
OutputJsonEmailCtx * emaillog_ctx
Definition: output-json-email-common.h:36
output-json-smtp.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h