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 
37 #include "util-print.h"
38 
39 #include "util-debug.h"
40 #include "app-layer-parser.h"
41 #include "output.h"
42 #include "app-layer-smtp.h"
43 #include "app-layer.h"
44 #include "util-privs.h"
45 #include "util-buffer.h"
46 #include "util-byte.h"
47 
48 #include "util-logopenfile.h"
49 
50 #include "output-json.h"
52 #include "flow.h"
53 
54 #define LOG_EMAIL_DEFAULT 0
55 #define LOG_EMAIL_EXTENDED (1<<0)
56 #define LOG_EMAIL_ARRAY (1<<1) /* require array handling */
57 #define LOG_EMAIL_COMMA (1<<2) /* require array handling */
58 #define LOG_EMAIL_BODY_MD5 (1<<3)
59 #define LOG_EMAIL_SUBJECT_MD5 (1<<4)
60 
61 struct {
62  const char *config_field;
63  const char *email_field;
64  uint32_t flags;
65 } email_fields[] = {
66  { "reply_to", "reply-to", LOG_EMAIL_DEFAULT },
67  { "bcc", "bcc", LOG_EMAIL_COMMA|LOG_EMAIL_EXTENDED },
68  { "message_id", "message-id", LOG_EMAIL_EXTENDED },
69  { "subject", "subject", LOG_EMAIL_EXTENDED },
70  { "x_mailer", "x-mailer", LOG_EMAIL_EXTENDED },
71  { "user_agent", "user-agent", LOG_EMAIL_EXTENDED },
72  { "received", "received", LOG_EMAIL_ARRAY },
73  { "x_originating_ip", "x-originating-ip", LOG_EMAIL_DEFAULT },
74  { "in_reply_to", "in-reply-to", LOG_EMAIL_DEFAULT },
75  { "references", "references", LOG_EMAIL_DEFAULT },
76  { "importance", "importance", LOG_EMAIL_DEFAULT },
77  { "priority", "priority", LOG_EMAIL_DEFAULT },
78  { "sensitivity", "sensitivity", LOG_EMAIL_DEFAULT },
79  { "organization", "organization", LOG_EMAIL_DEFAULT },
80  { "content_md5", "content-md5", LOG_EMAIL_DEFAULT },
81  { "date", "date", LOG_EMAIL_DEFAULT },
82  { NULL, NULL, LOG_EMAIL_DEFAULT},
83 };
84 
85 static void EveEmailLogJSONMd5(
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(
106  OutputJsonEmailCtx *email_ctx, SCJsonBuilder *js, SMTPTransaction *tx)
107 {
108  int f = 0;
109  MimeStateSMTP *entity = tx->mime_state;
110  if (entity == NULL) {
111  return;
112  }
113 
114  while(email_fields[f].config_field) {
115  if (((email_ctx->fields & (1ULL<<f)) != 0)
116  ||
117  ((email_ctx->flags & LOG_EMAIL_EXTENDED) && (email_fields[f].flags & LOG_EMAIL_EXTENDED))
118  ) {
120  SCMimeSmtpLogFieldArray(
122  } else if (email_fields[f].flags & LOG_EMAIL_COMMA) {
123  SCMimeSmtpLogFieldComma(
125  } else {
126  SCMimeSmtpLogFieldString(
128  }
129 
130  }
131  f++;
132  }
133 }
134 
135 /* JSON format logging */
136 static bool EveEmailLogJsonData(
137  const Flow *f, void *state, void *vtx, uint64_t tx_id, SCJsonBuilder *sjs)
138 {
139  SMTPState *smtp_state;
140  MimeStateSMTP *mime_state;
141 
142  /* check if we have SMTP state or not */
144  switch (proto) {
145  case ALPROTO_SMTP:
146  smtp_state = (SMTPState *)state;
147  if (smtp_state == NULL) {
148  SCLogDebug("no smtp state, so no request logging");
149  SCReturnBool(false);
150  }
151  SMTPTransaction *tx = vtx;
152  mime_state = tx->mime_state;
153  SCLogDebug("lets go mime_state %p", mime_state);
154  break;
155  default:
156  /* don't know how we got here */
157  SCReturnBool(false);
158  }
159  if ((mime_state != NULL)) {
160  SCMimeSmtpLogData(sjs, mime_state);
161  SCReturnBool(true);
162  }
163 
164  SCReturnBool(false);
165 }
166 
167 /* JSON format logging */
169  void *state, void *vtx, uint64_t tx_id)
170 {
171  OutputJsonEmailCtx *email_ctx = aft->emaillog_ctx;
172  SMTPTransaction *tx = (SMTPTransaction *) vtx;
173  SCJsonBuilderMark mark = { 0, 0, 0 };
174 
175  SCJbGetMark(js, &mark);
176  SCJbOpenObject(js, "email");
177  if (!EveEmailLogJsonData(f, state, vtx, tx_id, js)) {
178  SCJbRestoreMark(js, &mark);
180  }
181 
182  if ((email_ctx->flags & LOG_EMAIL_EXTENDED) || (email_ctx->fields != 0))
183  EveEmailLogJSONCustom(email_ctx, js, tx);
184 
185  if (!g_disable_hashing) {
186  EveEmailLogJSONMd5(email_ctx, js, tx);
187  }
188 
189  SCJbClose(js);
191 }
192 
193 bool EveEmailAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
194 {
195  SMTPState *smtp_state = (SMTPState *)FlowGetAppState(f);
196  if (smtp_state) {
197  SMTPTransaction *tx = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_SMTP, smtp_state, tx_id);
198  if (tx) {
199  return EveEmailLogJsonData(f, smtp_state, tx, tx_id, js);
200  }
201  }
202 
203  return false;
204 }
205 
207 {
208  if (conf) {
209  const char *extended = SCConfNodeLookupChildValue(conf, "extended");
210 
211  if (extended != NULL) {
212  if (SCConfValIsTrue(extended)) {
213  email_ctx->flags = LOG_EMAIL_EXTENDED;
214  }
215  }
216 
217  email_ctx->fields = 0;
218  SCConfNode *custom;
219  if ((custom = SCConfNodeLookupChild(conf, "custom")) != NULL) {
220  SCConfNode *field;
221  TAILQ_FOREACH (field, &custom->head, next) {
222  int f = 0;
223  while (email_fields[f].config_field) {
224  if ((strcmp(email_fields[f].config_field, field->val) == 0) ||
225  (strcasecmp(email_fields[f].email_field, field->val) == 0)) {
226  email_ctx->fields |= (1ULL << f);
227  break;
228  }
229  f++;
230  }
231  }
232  }
233 
234  email_ctx->flags = 0;
235  SCConfNode *md5_conf;
236  if ((md5_conf = SCConfNodeLookupChild(conf, "md5")) != NULL) {
237  SCConfNode *field;
238  TAILQ_FOREACH (field, &md5_conf->head, next) {
239  if (strcmp("body", field->val) == 0) {
240  SCLogInfo("Going to log the md5 sum of email body");
241  email_ctx->flags |= LOG_EMAIL_BODY_MD5;
242  }
243  if (strcmp("subject", field->val) == 0) {
244  SCLogInfo("Going to log the md5 sum of email subject");
245  email_ctx->flags |= LOG_EMAIL_SUBJECT_MD5;
246  }
247  }
248  }
249  }
250 }
SCJsonBuilder
struct SCJsonBuilder SCJsonBuilder
Definition: detect-engine-helper.h:83
util-byte.h
tm-threads.h
SMTPState_
Definition: app-layer-smtp.h:131
SCConfValIsTrue
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:577
LOG_EMAIL_COMMA
#define LOG_EMAIL_COMMA
Definition: output-json-email-common.c:57
JsonEmailLogThread_
Definition: output-json-email-common.h:33
LOG_EMAIL_EXTENDED
#define LOG_EMAIL_EXTENDED
Definition: output-json-email-common.c:55
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
SCFlowGetAppProtocol
AppProto SCFlowGetAppProtocol(const Flow *f)
Definition: flow.c:1288
threads.h
Flow_
Flow data structure.
Definition: flow.h:359
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
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:168
util-privs.h
email_field
const char * email_field
Definition: output-json-email-common.c:63
p
Packet * p
Definition: fuzz_dataset.c:30
flags
uint32_t flags
Definition: output-json-email-common.c:64
proto
uint8_t proto
Definition: decode-template.h:0
SCConfNodeLookupChildValue
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:877
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:82
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:81
SCReturnBool
#define SCReturnBool(x)
Definition: util-debug.h:302
email_fields
struct @140 email_fields[]
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:38
util-print.h
detect.h
pkt-var.h
output-json-email-common.h
app-layer-parser.h
Packet_
Definition: decode.h:516
SMTPTransaction_
Definition: app-layer-smtp.h:85
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:80
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1219
LOG_EMAIL_DEFAULT
#define LOG_EMAIL_DEFAULT
Definition: output-json-email-common.c:54
SCConfNodeLookupChild
SCConfNode * SCConfNodeLookupChild(const SCConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:849
EveEmailAddMetadata
bool EveEmailAddMetadata(const Flow *f, uint64_t tx_id, SCJsonBuilder *js)
Definition: output-json-email-common.c:193
config_field
const char * config_field
Definition: output-json-email-common.c:62
suricata-common.h
LOG_EMAIL_ARRAY
#define LOG_EMAIL_ARRAY
Definition: output-json-email-common.c:56
threadvars.h
util-logopenfile.h
util-buffer.h
suricata.h
LOG_EMAIL_SUBJECT_MD5
#define LOG_EMAIL_SUBJECT_MD5
Definition: output-json-email-common.c:59
OutputEmailInitConf
void OutputEmailInitConf(SCConfNode *conf, OutputJsonEmailCtx *email_ctx)
Definition: output-json-email-common.c:206
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
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
SCConfNode_
Definition: conf.h:37
OutputJsonEmailCtx_
Definition: output-json-email-common.h:27
SCConfNode_::val
char * val
Definition: conf.h:39
LOG_EMAIL_BODY_MD5
#define LOG_EMAIL_BODY_MD5
Definition: output-json-email-common.c:58
output.h
app-layer.h
f
Flow f
Definition: fuzz_dataset.c:32
SMTPTransaction_::mime_state
MimeStateSMTP * mime_state
Definition: app-layer-smtp.h:100
g_disable_hashing
bool g_disable_hashing
Definition: suricata.c:219