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 "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(
80  p, LOG_DIR_FLOW, "smtp", NULL, tx_id, jhl->emaillog_ctx->eve_ctx);
81  if (unlikely(jb == NULL))
82  return TM_ECODE_OK;
83 
84  jb_open_object(jb, "smtp");
85  EveSmtpDataLogger(f, state, tx, tx_id, jb);
86  jb_close(jb);
87 
88  if (EveEmailLogJson(jhl, jb, p, f, state, tx, tx_id) == TM_ECODE_OK) {
89  OutputJsonBuilderBuffer(jb, jhl->ctx);
90  }
91 
92  jb_free(jb);
93 
95 
96 }
97 
98 bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *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(f, smtp_state, tx, tx_id, 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(ConfNode *conf, OutputCtx *parent_ctx)
123 {
124  OutputInitResult result = { NULL, false };
125  OutputJsonCtx *ojc = parent_ctx->data;
126 
127  OutputJsonEmailCtx *email_ctx = SCMalloc(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_SMTP, "eve-log", "JsonSmtpLog",
197  "eve-log.smtp", OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger,
198  JsonSmtpLogThreadInit, JsonSmtpLogThreadDeinit, NULL);
199 }
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:98
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:350
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
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:73
threads.h
OutputJsonCtx_
Definition: output-json.h:81
Flow_
Flow data structure.
Definition: flow.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:974
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:347
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
util-privs.h
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:455
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
output-json-email-common.h
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:45
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:919
OutputEmailInitConf
void OutputEmailInitConf(ConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:387
Packet_
Definition: decode.h:416
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:1074
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:43
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
SMTPString_
Definition: app-layer-smtp.h:61
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:410
JsonSmtpLogRegister
void JsonSmtpLogRegister(void)
Definition: output-json-smtp.c:194
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:445
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:62
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:304
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
debug.h
output.h
app-layer.h