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, SCJsonBuilder *js)
55 {
56  if (state == NULL || vtx == NULL) {
57  return;
58  }
59 
60  SMTPTransaction *tx = vtx;
61  SMTPString *rcptto_str;
62  if (((SMTPState *)state)->helo) {
63  SCJbSetString(js, "helo", (const char *)((SMTPState *)state)->helo);
64  }
65  if (tx->mail_from) {
66  SCJbSetString(js, "mail_from", (const char *)tx->mail_from);
67  }
68  if (!TAILQ_EMPTY(&tx->rcpt_to_list)) {
69  SCJbOpenArray(js, "rcpt_to");
70  TAILQ_FOREACH(rcptto_str, &tx->rcpt_to_list, next) {
71  SCJbAppendString(js, (char *)rcptto_str->str);
72  }
73  SCJbClose(js);
74  }
75 }
76 
77 static int JsonSmtpLogger(ThreadVars *tv, void *thread_data, const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
78 {
79  SCEnter();
80  JsonEmailLogThread *jhl = (JsonEmailLogThread *)thread_data;
81 
82  SCJsonBuilder *jb = CreateEveHeaderWithTxId(
83  p, LOG_DIR_FLOW, "smtp", NULL, tx_id, jhl->emaillog_ctx->eve_ctx);
84  if (unlikely(jb == NULL))
85  return TM_ECODE_OK;
86 
87  SCJbOpenObject(jb, "smtp");
88  EveSmtpDataLogger(state, tx, jb);
89  SCJbClose(jb);
90 
91  EveEmailLogJson(jhl, jb, p, f, state, tx, tx_id);
92  OutputJsonBuilderBuffer(tv, p, p->flow, jb, jhl->ctx);
93 
94  SCJbFree(jb);
95 
97 
98 }
99 
100 bool EveSMTPAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
101 {
102  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
103  if (smtp_state) {
104  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
105  if (tx) {
106  EveSmtpDataLogger(smtp_state, tx, js);
107  return true;
108  }
109  }
110 
111  return false;
112 }
113 
114 static void OutputSmtpLogDeInitCtxSub(OutputCtx *output_ctx)
115 {
116  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
117  OutputJsonEmailCtx *email_ctx = output_ctx->data;
118  if (email_ctx != NULL) {
119  SCFree(email_ctx);
120  }
121  SCFree(output_ctx);
122 }
123 
124 static OutputInitResult OutputSmtpLogInitSub(SCConfNode *conf, OutputCtx *parent_ctx)
125 {
126  OutputInitResult result = { NULL, false };
127  OutputJsonCtx *ojc = parent_ctx->data;
128 
129  OutputJsonEmailCtx *email_ctx = SCCalloc(1, sizeof(OutputJsonEmailCtx));
130  if (unlikely(email_ctx == NULL))
131  return result;
132 
133  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
134  if (unlikely(output_ctx == NULL)) {
135  SCFree(email_ctx);
136  return result;
137  }
138 
139  email_ctx->eve_ctx = ojc;
140 
141  OutputEmailInitConf(conf, email_ctx);
142 
143  output_ctx->data = email_ctx;
144  output_ctx->DeInit = OutputSmtpLogDeInitCtxSub;
145 
146  /* enable the logger for the app layer */
148 
149  result.ctx = output_ctx;
150  result.ok = true;
151  return result;
152 }
153 
154 static TmEcode JsonSmtpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
155 {
157  if (unlikely(aft == NULL))
158  return TM_ECODE_FAILED;
159 
160  if(initdata == NULL) {
161  SCLogDebug("Error getting context for EveLogSMTP. \"initdata\" argument NULL");
162  goto error_exit;
163  }
164 
165  /* Use the Output Context (file pointer and mutex) */
166  aft->emaillog_ctx = ((OutputCtx *)initdata)->data;
167 
168  aft->ctx = CreateEveThreadCtx(t, aft->emaillog_ctx->eve_ctx);
169  if (aft->ctx == NULL) {
170  goto error_exit;
171  }
172 
173  *data = (void *)aft;
174  return TM_ECODE_OK;
175 
176 error_exit:
177  SCFree(aft);
178  return TM_ECODE_FAILED;
179 }
180 
181 static TmEcode JsonSmtpLogThreadDeinit(ThreadVars *t, void *data)
182 {
183  JsonEmailLogThread *aft = (JsonEmailLogThread *)data;
184  if (aft == NULL) {
185  return TM_ECODE_OK;
186  }
187  FreeEveThreadCtx(aft->ctx);
188 
189  /* clear memory */
190  memset(aft, 0, sizeof(JsonEmailLogThread));
191 
192  SCFree(aft);
193  return TM_ECODE_OK;
194 }
195 
196 void JsonSmtpLogRegister (void) {
197  /* register as child of eve-log */
198  OutputRegisterTxSubModule(LOGGER_JSON_TX, "eve-log", "JsonSmtpLog", "eve-log.smtp",
199  OutputSmtpLogInitSub, ALPROTO_SMTP, JsonSmtpLogger, JsonSmtpLogThreadInit,
200  JsonSmtpLogThreadDeinit);
201 }
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:117
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:354
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:401
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:170
util-privs.h
OutputJsonBuilderBuffer
void OutputJsonBuilderBuffer(ThreadVars *tv, const Packet *p, Flow *f, SCJsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:1021
p
Packet * p
Definition: fuzz_iprep.c:21
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
util-unittest.h
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:100
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:47
SCAppLayerParserRegisterLogger
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:495
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:58
pkt-var.h
SMTPTransaction_::mail_from
uint8_t * mail_from
Definition: app-layer-smtp.h:89
output-json-email-common.h
util-time.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:515
SMTPTransaction_
Definition: app-layer-smtp.h:73
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:1134
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:563
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:965
JsonSmtpLogRegister
void JsonSmtpLogRegister(void)
Definition: output-json-smtp.c:196
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:33
threadvars.h
LOGGER_JSON_TX
@ LOGGER_JSON_TX
Definition: suricata-common.h:492
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:208
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