suricata
output-json-drop.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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  * JSON Drop log module to log the dropped packet information
24  *
25  */
26 
27 #include "suricata-common.h"
28 #include "debug.h"
29 #include "detect.h"
30 #include "flow.h"
31 #include "conf.h"
32 
33 #include "threads.h"
34 #include "tm-threads.h"
35 #include "threadvars.h"
36 #include "util-debug.h"
37 
38 #include "decode-ipv4.h"
39 #include "detect-parse.h"
40 #include "detect-engine.h"
41 #include "detect-engine-mpm.h"
42 #include "detect-reference.h"
43 
44 #include "output.h"
45 #include "output-json.h"
46 #include "output-json-alert.h"
47 #include "output-json-drop.h"
48 
49 #include "util-unittest.h"
50 #include "util-unittest-helper.h"
52 #include "util-privs.h"
53 #include "util-print.h"
54 #include "util-proto-name.h"
55 #include "util-logopenfile.h"
56 #include "util-time.h"
57 #include "util-buffer.h"
58 
59 #define MODULE_NAME "JsonDropLog"
60 
61 #define LOG_DROP_ALERTS 1
62 
63 typedef struct JsonDropOutputCtx_ {
65  uint8_t flags;
68 
69 typedef struct JsonDropLogThread_ {
73 
74 /* default to true as this has been the default behavior for a long time */
75 static int g_droplog_flows_start = 1;
76 
77 /**
78  * \brief Log the dropped packets in netfilter format when engine is running
79  * in inline mode
80  *
81  * \param tv Pointer the current thread variables
82  * \param p Pointer the packet which is being logged
83  *
84  * \return return TM_EODE_OK on success
85  */
86 static int DropLogJSON (JsonDropLogThread *aft, const Packet *p)
87 {
88  JsonDropOutputCtx *drop_ctx = aft->drop_ctx;
89 
90  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "drop");
91  if (unlikely(js == NULL))
92  return TM_ECODE_OK;
93 
94  JsonAddCommonOptions(&drop_ctx->cfg, p, p->flow, js);
95 
96  json_t *djs = json_object();
97  if (unlikely(djs == NULL)) {
98  json_decref(js);
99  return TM_ECODE_OK;
100  }
101 
102  /* reset */
103  MemBufferReset(aft->buffer);
104 
105  uint16_t proto = 0;
106  if (PKT_IS_IPV4(p)) {
107  json_object_set_new(djs, "len", json_integer(IPV4_GET_IPLEN(p)));
108  json_object_set_new(djs, "tos", json_integer(IPV4_GET_IPTOS(p)));
109  json_object_set_new(djs, "ttl", json_integer(IPV4_GET_IPTTL(p)));
110  json_object_set_new(djs, "ipid", json_integer(IPV4_GET_IPID(p)));
111  proto = IPV4_GET_IPPROTO(p);
112  } else if (PKT_IS_IPV6(p)) {
113  json_object_set_new(djs, "len", json_integer(IPV6_GET_PLEN(p)));
114  json_object_set_new(djs, "tc", json_integer(IPV6_GET_CLASS(p)));
115  json_object_set_new(djs, "hoplimit", json_integer(IPV6_GET_HLIM(p)));
116  json_object_set_new(djs, "flowlbl", json_integer(IPV6_GET_FLOW(p)));
117  proto = IPV6_GET_L4PROTO(p);
118  }
119  switch (proto) {
120  case IPPROTO_TCP:
121  if (PKT_IS_TCP(p)) {
122  json_object_set_new(djs, "tcpseq", json_integer(TCP_GET_SEQ(p)));
123  json_object_set_new(djs, "tcpack", json_integer(TCP_GET_ACK(p)));
124  json_object_set_new(djs, "tcpwin", json_integer(TCP_GET_WINDOW(p)));
125  json_object_set_new(djs, "syn", TCP_ISSET_FLAG_SYN(p) ? json_true() : json_false());
126  json_object_set_new(djs, "ack", TCP_ISSET_FLAG_ACK(p) ? json_true() : json_false());
127  json_object_set_new(djs, "psh", TCP_ISSET_FLAG_PUSH(p) ? json_true() : json_false());
128  json_object_set_new(djs, "rst", TCP_ISSET_FLAG_RST(p) ? json_true() : json_false());
129  json_object_set_new(djs, "urg", TCP_ISSET_FLAG_URG(p) ? json_true() : json_false());
130  json_object_set_new(djs, "fin", TCP_ISSET_FLAG_FIN(p) ? json_true() : json_false());
131  json_object_set_new(djs, "tcpres", json_integer(TCP_GET_RAW_X2(p->tcph)));
132  json_object_set_new(djs, "tcpurgp", json_integer(TCP_GET_URG_POINTER(p)));
133  }
134  break;
135  case IPPROTO_UDP:
136  if (PKT_IS_UDP(p)) {
137  json_object_set_new(djs, "udplen", json_integer(UDP_GET_LEN(p)));
138  }
139  break;
140  case IPPROTO_ICMP:
141  if (PKT_IS_ICMPV4(p)) {
142  json_object_set_new(djs, "icmp_id", json_integer(ICMPV4_GET_ID(p)));
143  json_object_set_new(djs, "icmp_seq", json_integer(ICMPV4_GET_SEQ(p)));
144  } else if(PKT_IS_ICMPV6(p)) {
145  json_object_set_new(djs, "icmp_id", json_integer(ICMPV6_GET_ID(p)));
146  json_object_set_new(djs, "icmp_seq", json_integer(ICMPV6_GET_SEQ(p)));
147  }
148  break;
149  }
150  json_object_set_new(js, "drop", djs);
151 
152  if (aft->drop_ctx->flags & LOG_DROP_ALERTS) {
153  int logged = 0;
154  int i;
155  for (i = 0; i < p->alerts.cnt; i++) {
156  const PacketAlert *pa = &p->alerts.alerts[i];
157  if (unlikely(pa->s == NULL)) {
158  continue;
159  }
161  ((pa->action & ACTION_DROP) && EngineModeIsIPS()))
162  {
163  AlertJsonHeader(NULL, p, pa, js, 0);
164  logged = 1;
165  }
166  }
167  if (logged == 0) {
168  if (p->alerts.drop.action != 0) {
169  const PacketAlert *pa = &p->alerts.drop;
170  AlertJsonHeader(NULL, p, pa, js, 0);
171  }
172  }
173  }
174 
175  OutputJSONBuffer(js, aft->drop_ctx->file_ctx, &aft->buffer);
176  json_object_del(js, "drop");
177  json_object_clear(js);
178  json_decref(js);
179 
180  return TM_ECODE_OK;
181 }
182 
183 static TmEcode JsonDropLogThreadInit(ThreadVars *t, const void *initdata, void **data)
184 {
186  if (unlikely(aft == NULL))
187  return TM_ECODE_FAILED;
188  memset(aft, 0, sizeof(*aft));
189 
190  if(initdata == NULL)
191  {
192  SCLogDebug("Error getting context for EveLogDrop. \"initdata\" argument NULL");
193  SCFree(aft);
194  return TM_ECODE_FAILED;
195  }
196 
198  if (aft->buffer == NULL) {
199  SCFree(aft);
200  return TM_ECODE_FAILED;
201  }
202 
203  /** Use the Ouptut Context (file pointer and mutex) */
204  aft->drop_ctx = ((OutputCtx *)initdata)->data;
205 
206  *data = (void *)aft;
207  return TM_ECODE_OK;
208 }
209 
210 static TmEcode JsonDropLogThreadDeinit(ThreadVars *t, void *data)
211 {
212  JsonDropLogThread *aft = (JsonDropLogThread *)data;
213  if (aft == NULL) {
214  return TM_ECODE_OK;
215  }
216 
217  MemBufferFree(aft->buffer);
218 
219  /* clear memory */
220  memset(aft, 0, sizeof(*aft));
221 
222  SCFree(aft);
223  return TM_ECODE_OK;
224 }
225 
226 static void JsonDropOutputCtxFree(JsonDropOutputCtx *drop_ctx)
227 {
228  if (drop_ctx != NULL) {
229  if (drop_ctx->file_ctx != NULL)
230  LogFileFreeCtx(drop_ctx->file_ctx);
231  SCFree(drop_ctx);
232  }
233 }
234 
235 static void JsonDropLogDeInitCtx(OutputCtx *output_ctx)
236 {
238 
239  JsonDropOutputCtx *drop_ctx = output_ctx->data;
240  JsonDropOutputCtxFree(drop_ctx);
241  SCFree(output_ctx);
242 }
243 
244 static void JsonDropLogDeInitCtxSub(OutputCtx *output_ctx)
245 {
247 
248  JsonDropOutputCtx *drop_ctx = output_ctx->data;
249  SCFree(drop_ctx);
250  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
251  SCFree(output_ctx);
252 }
253 
254 #define DEFAULT_LOG_FILENAME "drop.json"
255 static OutputInitResult JsonDropLogInitCtx(ConfNode *conf)
256 {
257  OutputInitResult result = { NULL, false };
258  if (OutputDropLoggerEnable() != 0) {
259  SCLogError(SC_ERR_CONF_YAML_ERROR, "only one 'drop' logger "
260  "can be enabled");
261  return result;
262  }
263 
264  JsonDropOutputCtx *drop_ctx = SCCalloc(1, sizeof(*drop_ctx));
265  if (drop_ctx == NULL)
266  return result;
267 
268  drop_ctx->file_ctx = LogFileNewCtx();
269  if (drop_ctx->file_ctx == NULL) {
270  JsonDropOutputCtxFree(drop_ctx);
271  return result;
272  }
273 
274  if (SCConfLogOpenGeneric(conf, drop_ctx->file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
275  JsonDropOutputCtxFree(drop_ctx);
276  return result;
277  }
278 
279  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
280  if (unlikely(output_ctx == NULL)) {
281  JsonDropOutputCtxFree(drop_ctx);
282  return result;
283  }
284 
285  if (conf) {
286  const char *extended = ConfNodeLookupChildValue(conf, "alerts");
287  if (extended != NULL) {
288  if (ConfValIsTrue(extended)) {
289  drop_ctx->flags = LOG_DROP_ALERTS;
290  }
291  }
292  extended = ConfNodeLookupChildValue(conf, "flows");
293  if (extended != NULL) {
294  if (strcasecmp(extended, "start") == 0) {
295  g_droplog_flows_start = 1;
296  } else if (strcasecmp(extended, "all") == 0) {
297  g_droplog_flows_start = 0;
298  } else {
299  SCLogWarning(SC_ERR_CONF_YAML_ERROR, "valid options for "
300  "'flow' are 'start' and 'all'");
301  }
302  }
303  }
304 
305  output_ctx->data = drop_ctx;
306  output_ctx->DeInit = JsonDropLogDeInitCtx;
307 
308  result.ctx = output_ctx;
309  result.ok = true;
310  return result;
311 }
312 
313 static OutputInitResult JsonDropLogInitCtxSub(ConfNode *conf, OutputCtx *parent_ctx)
314 {
315  OutputInitResult result = { NULL, false };
316  if (OutputDropLoggerEnable() != 0) {
317  SCLogError(SC_ERR_CONF_YAML_ERROR, "only one 'drop' logger "
318  "can be enabled");
319  return result;
320  }
321 
322  OutputJsonCtx *ajt = parent_ctx->data;
323 
324  JsonDropOutputCtx *drop_ctx = SCCalloc(1, sizeof(*drop_ctx));
325  if (drop_ctx == NULL)
326  return result;
327 
328  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
329  if (unlikely(output_ctx == NULL)) {
330  JsonDropOutputCtxFree(drop_ctx);
331  return result;
332  }
333 
334  if (conf) {
335  const char *extended = ConfNodeLookupChildValue(conf, "alerts");
336  if (extended != NULL) {
337  if (ConfValIsTrue(extended)) {
338  drop_ctx->flags = LOG_DROP_ALERTS;
339  }
340  }
341  extended = ConfNodeLookupChildValue(conf, "flows");
342  if (extended != NULL) {
343  if (strcasecmp(extended, "start") == 0) {
344  g_droplog_flows_start = 1;
345  } else if (strcasecmp(extended, "all") == 0) {
346  g_droplog_flows_start = 0;
347  } else {
348  SCLogWarning(SC_ERR_CONF_YAML_ERROR, "valid options for "
349  "'flow' are 'start' and 'all'");
350  }
351  }
352  }
353 
354  drop_ctx->file_ctx = ajt->file_ctx;
355  drop_ctx->cfg = ajt->cfg;
356 
357  output_ctx->data = drop_ctx;
358  output_ctx->DeInit = JsonDropLogDeInitCtxSub;
359 
360  result.ctx = output_ctx;
361  result.ok = true;
362  return result;
363 }
364 
365 /**
366  * \brief Log the dropped packets when engine is running in inline mode
367  *
368  * \param tv Pointer the current thread variables
369  * \param data Pointer to the droplog struct
370  * \param p Pointer the packet which is being logged
371  *
372  * \retval 0 on succes
373  */
374 static int JsonDropLogger(ThreadVars *tv, void *thread_data, const Packet *p)
375 {
376  JsonDropLogThread *td = thread_data;
377  int r = DropLogJSON(td, p);
378  if (r < 0)
379  return -1;
380 
381  if (!g_droplog_flows_start)
382  return 0;
383 
384  if (p->flow) {
385  if (p->flow->flags & FLOW_ACTION_DROP) {
388  else if (PKT_IS_TOCLIENT(p) && !(p->flow->flags & FLOW_TOCLIENT_DROP_LOGGED))
390  }
391  }
392  return 0;
393 }
394 
395 
396 /**
397  * \brief Check if we need to drop-log this packet
398  *
399  * \param tv Pointer the current thread variables
400  * \param p Pointer the packet which is tested
401  *
402  * \retval bool TRUE or FALSE
403  */
404 static int JsonDropLogCondition(ThreadVars *tv, const Packet *p)
405 {
406  if (!EngineModeIsIPS()) {
407  SCLogDebug("engine is not running in inline mode, so returning");
408  return FALSE;
409  }
410  if (PKT_IS_PSEUDOPKT(p)) {
411  SCLogDebug("drop log doesn't log pseudo packets");
412  return FALSE;
413  }
414 
415  if (g_droplog_flows_start && p->flow != NULL) {
416  int ret = FALSE;
417 
418  /* for a flow that will be dropped fully, log just once per direction */
419  if (p->flow->flags & FLOW_ACTION_DROP) {
421  ret = TRUE;
422  else if (PKT_IS_TOCLIENT(p) && !(p->flow->flags & FLOW_TOCLIENT_DROP_LOGGED))
423  ret = TRUE;
424  }
425 
426  /* if drop is caused by signature, log anyway */
427  if (p->alerts.drop.action != 0)
428  ret = TRUE;
429 
430  return ret;
431  } else if (PACKET_TEST_ACTION(p, ACTION_DROP)) {
432  return TRUE;
433  }
434 
435  return FALSE;
436 }
437 
439 {
441  JsonDropLogInitCtx, JsonDropLogger, JsonDropLogCondition,
442  JsonDropLogThreadInit, JsonDropLogThreadDeinit, NULL);
444  "eve-log.drop", JsonDropLogInitCtxSub, JsonDropLogger,
445  JsonDropLogCondition, JsonDropLogThreadInit, JsonDropLogThreadDeinit,
446  NULL);
447 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define TCP_GET_RAW_X2(tcph)
Definition: decode-tcp.h:71
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
#define DEFAULT_LOG_FILENAME
#define SCLogDebug(...)
Definition: util-debug.h:335
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
void AlertJsonHeader(void *ctx, const Packet *p, const PacketAlert *pa, json_t *js, uint16_t flags)
struct Flow_ * flow
Definition: decode.h:446
LogFileCtx * file_ctx
#define ACTION_REJECT_DST
#define PACKET_TEST_ACTION(p, a)
Definition: decode.h:858
#define FALSE
#define PKT_IS_TOCLIENT(p)
Definition: decode.h:259
#define IPV4_GET_IPPROTO(p)
Definition: decode-ipv4.h:148
int logged
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
#define ACTION_REJECT
uint8_t action
Definition: decode.h:272
#define FLOW_ACTION_DROP
Definition: flow.h:64
#define ICMPV6_GET_ID(p)
#define IPV6_GET_HLIM(p)
Definition: decode-ipv6.h:90
struct JsonDropLogThread_ JsonDropLogThread
#define MODULE_NAME
int OutputDropLoggerEnable(void)
Definition: output.c:851
#define PKT_IS_IPV6(p)
Definition: decode.h:253
#define TCP_GET_WINDOW(p)
Definition: decode-tcp.h:115
#define ICMPV6_GET_SEQ(p)
OutputJsonCommonSettings cfg
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
TCPHdr * tcph
Definition: decode.h:523
int EngineModeIsIPS(void)
Definition: suricata.c:247
#define TRUE
#define ICMPV4_GET_SEQ(p)
#define PKT_IS_IPV4(p)
Definition: decode.h:252
JsonDropOutputCtx * drop_ctx
#define TCP_ISSET_FLAG_SYN(p)
Definition: decode-tcp.h:121
PacketAlert alerts[PACKET_ALERT_MAX]
Definition: decode.h:295
const struct Signature_ * s
Definition: decode.h:274
#define SCCalloc(nm, a)
Definition: util-mem.h:253
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:843
#define TCP_GET_SEQ(p)
Definition: decode-tcp.h:113
void OutputRegisterPacketModule(LoggerId id, const char *name, const char *conf_name, OutputInitFunc InitFunc, PacketLogger PacketLogFunc, PacketLogCondition PacketConditionFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Register a packet output module.
Definition: output.c:163
#define IPV6_GET_PLEN(p)
Definition: decode-ipv6.h:88
void OutputRegisterPacketSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, PacketLogger PacketLogFunc, PacketLogCondition PacketConditionFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Register a packet output sub-module.
Definition: output.c:204
#define ACTION_REJECT_BOTH
#define TCP_ISSET_FLAG_URG(p)
Definition: decode-tcp.h:125
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
OutputJsonCommonSettings cfg
Definition: output-json.h:81
#define PKT_IS_ICMPV6(p)
Definition: decode.h:257
LogFileCtx * file_ctx
Definition: output-json.h:79
#define IPV6_GET_FLOW(p)
Definition: decode-ipv6.h:84
LogFileCtx * LogFileNewCtx(void)
LogFileNewCtx() Get a new LogFileCtx.
#define PKT_IS_TOSERVER(p)
Definition: decode.h:258
#define IPV6_GET_CLASS(p)
Definition: decode-ipv6.h:82
#define UDP_GET_LEN(p)
Definition: decode-udp.h:35
void JsonDropLogRegister(void)
int SCConfLogOpenGeneric(ConfNode *conf, LogFileCtx *log_ctx, const char *default_filename, int rotate)
open a generic output "log file", which may be a regular file or a socket
#define FLOW_TOCLIENT_DROP_LOGGED
Definition: flow.h:74
struct JsonDropOutputCtx_ JsonDropOutputCtx
uint8_t proto
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
#define IPV6_GET_L4PROTO(p)
Definition: decode-ipv6.h:93
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:566
#define IPV4_GET_IPLEN(p)
Definition: decode-ipv4.h:127
Definition: conf.h:32
#define TCP_ISSET_FLAG_RST(p)
Definition: decode-tcp.h:122
OutputCtx * ctx
Definition: output.h:42
#define SCMalloc(a)
Definition: util-mem.h:222
int LogFileFreeCtx(LogFileCtx *lf_ctx)
LogFileFreeCtx() Destroy a LogFileCtx (Close the file and free memory)
#define ICMPV4_GET_ID(p)
#define SCFree(a)
Definition: util-mem.h:322
#define PKT_IS_TCP(p)
Definition: decode.h:254
#define TCP_ISSET_FLAG_ACK(p)
Definition: decode-tcp.h:124
PacketAlert drop
Definition: decode.h:298
void OutputDropLoggerDisable(void)
Definition: output.c:859
void * data
Definition: tm-modules.h:81
#define PKT_IS_ICMPV4(p)
Definition: decode.h:256
#define IPV4_GET_IPTOS(p)
Definition: decode-ipv4.h:125
PacketAlerts alerts
Definition: decode.h:556
#define TCP_GET_URG_POINTER(p)
Definition: decode-tcp.h:116
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1133
#define TCP_ISSET_FLAG_PUSH(p)
Definition: decode-tcp.h:123
uint16_t cnt
Definition: decode.h:294
#define PKT_IS_UDP(p)
Definition: decode.h:255
Per thread variable structure.
Definition: threadvars.h:57
#define LOG_DROP_ALERTS
#define ACTION_DROP
#define TCP_GET_ACK(p)
Definition: decode-tcp.h:114
#define TCP_ISSET_FLAG_FIN(p)
Definition: decode-tcp.h:120
#define IPV4_GET_IPID(p)
Definition: decode-ipv4.h:129
uint32_t flags
Definition: flow.h:379
#define FLOW_TOSERVER_DROP_LOGGED
Definition: flow.h:72
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
#define IPV4_GET_IPTTL(p)
Definition: decode-ipv4.h:146