suricata
output-json-email-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2015 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  * \author Eric Leblond <eric@regit.org>
23  *
24  * Implements json common email logging portion of the engine.
25  */
26 
27 #include "suricata-common.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 #include "suricata.h"
32 
33 #include "threads.h"
34 #include "threadvars.h"
35 #include "tm-threads.h"
36 #include "tm-threads-common.h"
37 
38 #include "util-print.h"
39 #include "util-unittest.h"
40 
41 #include "util-debug.h"
42 #include "app-layer-parser.h"
43 #include "output.h"
44 #include "app-layer-smtp.h"
45 #include "app-layer.h"
46 #include "util-privs.h"
47 #include "util-buffer.h"
48 #include "util-byte.h"
49 
50 #include "util-logopenfile.h"
51 
52 #include "output-json.h"
54 
55 #define LOG_EMAIL_DEFAULT 0
56 #define LOG_EMAIL_EXTENDED (1<<0)
57 #define LOG_EMAIL_ARRAY (1<<1) /* require array handling */
58 #define LOG_EMAIL_COMMA (1<<2) /* require array handling */
59 #define LOG_EMAIL_BODY_MD5 (1<<3)
60 #define LOG_EMAIL_SUBJECT_MD5 (1<<4)
61 
62 struct {
63  const char *config_field;
64  const char *email_field;
65  uint32_t flags;
66 } email_fields[] = {
67  { "reply_to", "reply-to", LOG_EMAIL_DEFAULT },
68  { "bcc", "bcc", LOG_EMAIL_COMMA|LOG_EMAIL_EXTENDED },
69  { "message_id", "message-id", LOG_EMAIL_EXTENDED },
70  { "subject", "subject", LOG_EMAIL_EXTENDED },
71  { "x_mailer", "x-mailer", LOG_EMAIL_EXTENDED },
72  { "user_agent", "user-agent", LOG_EMAIL_EXTENDED },
73  { "received", "received", LOG_EMAIL_ARRAY },
74  { "x_originating_ip", "x-originating-ip", LOG_EMAIL_DEFAULT },
75  { "in_reply_to", "in-reply-to", LOG_EMAIL_DEFAULT },
76  { "references", "references", LOG_EMAIL_DEFAULT },
77  { "importance", "importance", LOG_EMAIL_DEFAULT },
78  { "priority", "priority", LOG_EMAIL_DEFAULT },
79  { "sensitivity", "sensitivity", LOG_EMAIL_DEFAULT },
80  { "organization", "organization", LOG_EMAIL_DEFAULT },
81  { "content_md5", "content-md5", LOG_EMAIL_DEFAULT },
82  { "date", "date", LOG_EMAIL_DEFAULT },
83  { NULL, NULL, LOG_EMAIL_DEFAULT},
84 };
85 
86 static void EveEmailLogJSONMd5(OutputJsonEmailCtx *email_ctx, JsonBuilder *js, SMTPTransaction *tx)
87 {
88  if (email_ctx->flags & LOG_EMAIL_SUBJECT_MD5) {
89  MimeStateSMTP *entity = tx->mime_state;
90  if (entity == NULL) {
91  return;
92  }
93  SCMimeSmtpLogSubjectMd5(js, entity);
94  }
95 
96  if (email_ctx->flags & LOG_EMAIL_BODY_MD5) {
97  MimeStateSMTP *entity = tx->mime_state;
98  if (entity == NULL) {
99  return;
100  }
101  SCMimeSmtpLogBodyMd5(js, entity);
102  }
103 }
104 
105 static void EveEmailLogJSONCustom(OutputJsonEmailCtx *email_ctx, JsonBuilder *js, SMTPTransaction *tx)
106 {
107  int f = 0;
108  MimeStateSMTP *entity = tx->mime_state;
109  if (entity == NULL) {
110  return;
111  }
112 
113  while(email_fields[f].config_field) {
114  if (((email_ctx->fields & (1ULL<<f)) != 0)
115  ||
116  ((email_ctx->flags & LOG_EMAIL_EXTENDED) && (email_fields[f].flags & LOG_EMAIL_EXTENDED))
117  ) {
119  SCMimeSmtpLogFieldArray(
121  } else if (email_fields[f].flags & LOG_EMAIL_COMMA) {
122  SCMimeSmtpLogFieldComma(
124  } else {
125  SCMimeSmtpLogFieldString(
127  }
128 
129  }
130  f++;
131  }
132 }
133 
134 /* JSON format logging */
135 static bool EveEmailLogJsonData(const Flow *f, void *state, void *vtx, uint64_t tx_id, JsonBuilder *sjs)
136 {
137  SMTPState *smtp_state;
138  MimeStateSMTP *mime_state;
139 
140  /* check if we have SMTP state or not */
141  AppProto proto = FlowGetAppProtocol(f);
142  switch (proto) {
143  case ALPROTO_SMTP:
144  smtp_state = (SMTPState *)state;
145  if (smtp_state == NULL) {
146  SCLogDebug("no smtp state, so no request logging");
147  jb_free(sjs);
148  SCReturnPtr(NULL, "JsonBuilder");
149  }
150  SMTPTransaction *tx = vtx;
151  mime_state = tx->mime_state;
152  SCLogDebug("lets go mime_state %p", mime_state);
153  break;
154  default:
155  /* don't know how we got here */
156  SCReturnBool(false);
157  }
158  if ((mime_state != NULL)) {
159  SCMimeSmtpLogData(sjs, mime_state);
160  SCReturnBool(true);
161  }
162 
163  SCReturnBool(false);
164 }
165 
166 /* JSON format logging */
167 TmEcode EveEmailLogJson(JsonEmailLogThread *aft, JsonBuilder *js, const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
168 {
169  OutputJsonEmailCtx *email_ctx = aft->emaillog_ctx;
170  SMTPTransaction *tx = (SMTPTransaction *) vtx;
171  JsonBuilderMark mark = { 0, 0, 0 };
172 
173  jb_get_mark(js, &mark);
174  jb_open_object(js, "email");
175  if (!EveEmailLogJsonData(f, state, vtx, tx_id, js)) {
176  jb_restore_mark(js, &mark);
178  }
179 
180  if ((email_ctx->flags & LOG_EMAIL_EXTENDED) || (email_ctx->fields != 0))
181  EveEmailLogJSONCustom(email_ctx, js, tx);
182 
183  if (!g_disable_hashing) {
184  EveEmailLogJSONMd5(email_ctx, js, tx);
185  }
186 
187  jb_close(js);
189 }
190 
191 bool EveEmailAddMetadata(const Flow *f, uint32_t tx_id, JsonBuilder *js)
192 {
193  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
194  if (smtp_state) {
195  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
196  if (tx) {
197  return EveEmailLogJsonData(f, smtp_state, tx, tx_id, js);
198  }
199  }
200 
201  return false;
202 }
203 
205 {
206  if (conf) {
207  const char *extended = ConfNodeLookupChildValue(conf, "extended");
208 
209  if (extended != NULL) {
210  if (ConfValIsTrue(extended)) {
211  email_ctx->flags = LOG_EMAIL_EXTENDED;
212  }
213  }
214 
215  email_ctx->fields = 0;
216  ConfNode *custom;
217  if ((custom = ConfNodeLookupChild(conf, "custom")) != NULL) {
218  ConfNode *field;
219  TAILQ_FOREACH (field, &custom->head, next) {
220  int f = 0;
221  while (email_fields[f].config_field) {
222  if ((strcmp(email_fields[f].config_field, field->val) == 0) ||
223  (strcasecmp(email_fields[f].email_field, field->val) == 0)) {
224  email_ctx->fields |= (1ULL << f);
225  break;
226  }
227  f++;
228  }
229  }
230  }
231 
232  email_ctx->flags = 0;
233  ConfNode *md5_conf;
234  if ((md5_conf = ConfNodeLookupChild(conf, "md5")) != NULL) {
235  ConfNode *field;
236  TAILQ_FOREACH (field, &md5_conf->head, next) {
237  if (strcmp("body", field->val) == 0) {
238  SCLogInfo("Going to log the md5 sum of email body");
239  email_ctx->flags |= LOG_EMAIL_BODY_MD5;
240  }
241  if (strcmp("subject", field->val) == 0) {
242  SCLogInfo("Going to log the md5 sum of email subject");
243  email_ctx->flags |= LOG_EMAIL_SUBJECT_MD5;
244  }
245  }
246  }
247  }
248 }
util-byte.h
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:116
LOG_EMAIL_COMMA
#define LOG_EMAIL_COMMA
Definition: output-json-email-common.c:58
ConfNode_::val
char * val
Definition: conf.h:34
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:167
LOG_EMAIL_EXTENDED
#define LOG_EMAIL_EXTENDED
Definition: output-json-email-common.c:56
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:84
threads.h
Flow_
Flow data structure.
Definition: flow.h:360
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
tm-threads-common.h
util-privs.h
email_field
const char * email_field
Definition: output-json-email-common.c:64
flags
uint32_t flags
Definition: output-json-email-common.c:65
proto
uint8_t proto
Definition: decode-template.h:0
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:83
util-unittest.h
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:536
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:82
SCReturnBool
#define SCReturnBool(x)
Definition: util-debug.h:289
util-debug.h
OutputJsonEmailCtx_::fields
uint64_t fields
Definition: output-json-email-common.h:29
output-json.h
ALPROTO_SMTP
@ ALPROTO_SMTP
Definition: app-layer-protos.h:32
util-print.h
detect.h
pkt-var.h
output-json-email-common.h
app-layer-parser.h
OutputEmailInitConf
void OutputEmailInitConf(ConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:204
Packet_
Definition: decode.h:479
SMTPTransaction_
Definition: app-layer-smtp.h:72
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:81
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1089
EveEmailAddMetadata
bool EveEmailAddMetadata(const Flow *f, uint32_t tx_id, JsonBuilder *js)
Definition: output-json-email-common.c:191
ConfNodeLookupChild
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:781
LOG_EMAIL_DEFAULT
#define LOG_EMAIL_DEFAULT
Definition: output-json-email-common.c:55
config_field
const char * config_field
Definition: output-json-email-common.c:63
suricata-common.h
LOG_EMAIL_ARRAY
#define LOG_EMAIL_ARRAY
Definition: output-json-email-common.c:57
threadvars.h
ConfNode_
Definition: conf.h:32
util-logopenfile.h
util-buffer.h
email_fields
struct @129 email_fields[]
suricata.h
LOG_EMAIL_SUBJECT_MD5
#define LOG_EMAIL_SUBJECT_MD5
Definition: output-json-email-common.c:60
app-layer-smtp.h
JsonEmailLogThread_::emaillog_ctx
OutputJsonEmailCtx * emaillog_ctx
Definition: output-json-email-common.h:34
OutputJsonEmailCtx_::flags
uint32_t flags
Definition: output-json-email-common.h:28
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
LOG_EMAIL_BODY_MD5
#define LOG_EMAIL_BODY_MD5
Definition: output-json-email-common.c:59
output.h
app-layer.h
SMTPTransaction_::mime_state
MimeStateSMTP * mime_state
Definition: app-layer-smtp.h:85
g_disable_hashing
bool g_disable_hashing
Definition: suricata.c:208
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:809