suricata
output-json-drop.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  * 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_ {
64  uint8_t flags;
67 
68 typedef struct JsonDropLogThread_ {
72 
73 /* default to true as this has been the default behavior for a long time */
74 static int g_droplog_flows_start = 1;
75 
76 /**
77  * \brief Log the dropped packets in netfilter format when engine is running
78  * in inline mode
79  *
80  * \param tv Pointer the current thread variables
81  * \param p Pointer the packet which is being logged
82  *
83  * \return return TM_EODE_OK on success
84  */
85 static int DropLogJSON (JsonDropLogThread *aft, const Packet *p)
86 {
87  JsonDropOutputCtx *drop_ctx = aft->drop_ctx;
88 
91 
92  JsonBuilder *js = CreateEveHeader(p, LOG_DIR_PACKET, "drop", &addr, drop_ctx->eve_ctx);
93  if (unlikely(js == NULL))
94  return TM_ECODE_OK;
95 
96  jb_open_object(js, "drop");
97 
98  uint16_t proto = 0;
99  if (PKT_IS_IPV4(p)) {
100  jb_set_uint(js, "len", IPV4_GET_IPLEN(p));
101  jb_set_uint(js, "tos", IPV4_GET_IPTOS(p));
102  jb_set_uint(js, "ttl", IPV4_GET_IPTTL(p));
103  jb_set_uint(js, "ipid", IPV4_GET_IPID(p));
104  proto = IPV4_GET_IPPROTO(p);
105  } else if (PKT_IS_IPV6(p)) {
106  jb_set_uint(js, "len", IPV6_GET_PLEN(p));
107  jb_set_uint(js, "tc", IPV6_GET_CLASS(p));
108  jb_set_uint(js, "hoplimit", IPV6_GET_HLIM(p));
109  jb_set_uint(js, "flowlbl", IPV6_GET_FLOW(p));
110  proto = IPV6_GET_L4PROTO(p);
111  }
112  switch (proto) {
113  case IPPROTO_TCP:
114  if (PKT_IS_TCP(p)) {
115  jb_set_uint(js, "tcpseq", TCP_GET_SEQ(p));
116  jb_set_uint(js, "tcpack", TCP_GET_ACK(p));
117  jb_set_uint(js, "tcpwin", TCP_GET_WINDOW(p));
118  jb_set_bool(js, "syn", TCP_ISSET_FLAG_SYN(p) ? true : false);
119  jb_set_bool(js, "ack", TCP_ISSET_FLAG_ACK(p) ? true : false);
120  jb_set_bool(js, "psh", TCP_ISSET_FLAG_PUSH(p) ? true : false);
121  jb_set_bool(js, "rst", TCP_ISSET_FLAG_RST(p) ? true : false);
122  jb_set_bool(js, "urg", TCP_ISSET_FLAG_URG(p) ? true : false);
123  jb_set_bool(js, "fin", TCP_ISSET_FLAG_FIN(p) ? true : false);
124  jb_set_uint(js, "tcpres", TCP_GET_RAW_X2(p->tcph));
125  jb_set_uint(js, "tcpurgp", TCP_GET_URG_POINTER(p));
126  }
127  break;
128  case IPPROTO_UDP:
129  if (PKT_IS_UDP(p)) {
130  jb_set_uint(js, "udplen", UDP_GET_LEN(p));
131  }
132  break;
133  case IPPROTO_ICMP:
134  if (PKT_IS_ICMPV4(p)) {
135  jb_set_uint(js, "icmp_id", ICMPV4_GET_ID(p));
136  jb_set_uint(js, "icmp_seq", ICMPV4_GET_SEQ(p));
137  } else if(PKT_IS_ICMPV6(p)) {
138  jb_set_uint(js, "icmp_id", ICMPV6_GET_ID(p));
139  jb_set_uint(js, "icmp_seq", ICMPV6_GET_SEQ(p));
140  }
141  break;
142  }
143 
144  /* Close drop. */
145  jb_close(js);
146 
147  if (aft->drop_ctx->flags & LOG_DROP_ALERTS) {
148  int logged = 0;
149  int i;
150  for (i = 0; i < p->alerts.cnt; i++) {
151  const PacketAlert *pa = &p->alerts.alerts[i];
152  if (unlikely(pa->s == NULL)) {
153  continue;
154  }
156  ((pa->action & ACTION_DROP) && EngineModeIsIPS()))
157  {
158  AlertJsonHeader(NULL, p, pa, js, 0, &addr);
159  logged = 1;
160  break;
161  }
162  }
163  if (logged == 0) {
164  if (p->alerts.drop.action != 0) {
165  const PacketAlert *pa = &p->alerts.drop;
166  AlertJsonHeader(NULL, p, pa, js, 0, &addr);
167  }
168  }
169  }
170 
171  OutputJsonBuilderBuffer(js, aft->ctx);
172  jb_free(js);
173 
174  return TM_ECODE_OK;
175 }
176 
177 static TmEcode JsonDropLogThreadInit(ThreadVars *t, const void *initdata, void **data)
178 {
179  JsonDropLogThread *aft = SCCalloc(1, sizeof(JsonDropLogThread));
180  if (unlikely(aft == NULL))
181  return TM_ECODE_FAILED;
182 
183  if(initdata == NULL)
184  {
185  SCLogDebug("Error getting context for EveLogDrop. \"initdata\" argument NULL");
186  goto error_exit;
187  }
188 
189  /** Use the Ouptut Context (file pointer and mutex) */
190  aft->drop_ctx = ((OutputCtx *)initdata)->data;
191  aft->ctx = CreateEveThreadCtx(t, aft->drop_ctx->eve_ctx);
192  if (!aft->ctx) {
193  goto error_exit;
194  }
195 
196  *data = (void *)aft;
197  return TM_ECODE_OK;
198 
199 error_exit:
200  SCFree(aft);
201  return TM_ECODE_FAILED;
202 }
203 
204 static TmEcode JsonDropLogThreadDeinit(ThreadVars *t, void *data)
205 {
206  JsonDropLogThread *aft = (JsonDropLogThread *)data;
207  if (aft == NULL) {
208  return TM_ECODE_OK;
209  }
210 
211  FreeEveThreadCtx(aft->ctx);
212 
213  /* clear memory */
214  memset(aft, 0, sizeof(*aft));
215 
216  SCFree(aft);
217  return TM_ECODE_OK;
218 }
219 
220 static void JsonDropOutputCtxFree(JsonDropOutputCtx *drop_ctx)
221 {
222  if (drop_ctx != NULL) {
223  SCFree(drop_ctx);
224  }
225 }
226 
227 static void JsonDropLogDeInitCtxSub(OutputCtx *output_ctx)
228 {
230 
231  JsonDropOutputCtx *drop_ctx = output_ctx->data;
232  SCFree(drop_ctx);
233  SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
234  SCFree(output_ctx);
235 }
236 
237 static OutputInitResult JsonDropLogInitCtxSub(ConfNode *conf, OutputCtx *parent_ctx)
238 {
239  OutputInitResult result = { NULL, false };
240  if (OutputDropLoggerEnable() != 0) {
241  SCLogError(SC_ERR_CONF_YAML_ERROR, "only one 'drop' logger "
242  "can be enabled");
243  return result;
244  }
245 
246  OutputJsonCtx *ajt = parent_ctx->data;
247 
248  JsonDropOutputCtx *drop_ctx = SCCalloc(1, sizeof(*drop_ctx));
249  if (drop_ctx == NULL)
250  return result;
251 
252  OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
253  if (unlikely(output_ctx == NULL)) {
254  JsonDropOutputCtxFree(drop_ctx);
255  return result;
256  }
257 
258  if (conf) {
259  const char *extended = ConfNodeLookupChildValue(conf, "alerts");
260  if (extended != NULL) {
261  if (ConfValIsTrue(extended)) {
262  drop_ctx->flags = LOG_DROP_ALERTS;
263  }
264  }
265  extended = ConfNodeLookupChildValue(conf, "flows");
266  if (extended != NULL) {
267  if (strcasecmp(extended, "start") == 0) {
268  g_droplog_flows_start = 1;
269  } else if (strcasecmp(extended, "all") == 0) {
270  g_droplog_flows_start = 0;
271  } else {
272  SCLogWarning(SC_ERR_CONF_YAML_ERROR, "valid options for "
273  "'flow' are 'start' and 'all'");
274  }
275  }
276  }
277 
278  drop_ctx->eve_ctx = ajt;
279 
280  output_ctx->data = drop_ctx;
281  output_ctx->DeInit = JsonDropLogDeInitCtxSub;
282 
283  result.ctx = output_ctx;
284  result.ok = true;
285  return result;
286 }
287 
288 /**
289  * \brief Log the dropped packets when engine is running in inline mode
290  *
291  * \param tv Pointer the current thread variables
292  * \param data Pointer to the droplog struct
293  * \param p Pointer the packet which is being logged
294  *
295  * \retval 0 on succes
296  */
297 static int JsonDropLogger(ThreadVars *tv, void *thread_data, const Packet *p)
298 {
299  JsonDropLogThread *td = thread_data;
300  int r = DropLogJSON(td, p);
301  if (r < 0)
302  return -1;
303 
304  if (!g_droplog_flows_start)
305  return 0;
306 
307  if (p->flow) {
308  if (p->flow->flags & FLOW_ACTION_DROP) {
311  else if (PKT_IS_TOCLIENT(p) && !(p->flow->flags & FLOW_TOCLIENT_DROP_LOGGED))
313  }
314  }
315  return 0;
316 }
317 
318 
319 /**
320  * \brief Check if we need to drop-log this packet
321  *
322  * \param tv Pointer the current thread variables
323  * \param p Pointer the packet which is tested
324  *
325  * \retval bool TRUE or FALSE
326  */
327 static int JsonDropLogCondition(ThreadVars *tv, const Packet *p)
328 {
329  if (!EngineModeIsIPS()) {
330  SCLogDebug("engine is not running in inline mode, so returning");
331  return FALSE;
332  }
333  if (PKT_IS_PSEUDOPKT(p)) {
334  SCLogDebug("drop log doesn't log pseudo packets");
335  return FALSE;
336  }
337 
338  if (g_droplog_flows_start && p->flow != NULL) {
339  int ret = FALSE;
340 
341  /* for a flow that will be dropped fully, log just once per direction */
342  if (p->flow->flags & FLOW_ACTION_DROP) {
344  ret = TRUE;
345  else if (PKT_IS_TOCLIENT(p) && !(p->flow->flags & FLOW_TOCLIENT_DROP_LOGGED))
346  ret = TRUE;
347  }
348 
349  /* if drop is caused by signature, log anyway */
350  if (p->alerts.drop.action != 0)
351  ret = TRUE;
352 
353  return ret;
354  } else if (PacketTestAction(p, ACTION_DROP)) {
355  return TRUE;
356  }
357 
358  return FALSE;
359 }
360 
362 {
364  "eve-log.drop", JsonDropLogInitCtxSub, JsonDropLogger,
365  JsonDropLogCondition, JsonDropLogThreadInit, JsonDropLogThreadDeinit,
366  NULL);
367 }
PKT_IS_TOCLIENT
#define PKT_IS_TOCLIENT(p)
Definition: decode.h:266
PKT_IS_UDP
#define PKT_IS_UDP(p)
Definition: decode.h:262
tm-threads.h
TCP_GET_RAW_X2
#define TCP_GET_RAW_X2(tcph)
Definition: decode-tcp.h:70
detect-engine.h
OutputDropLoggerEnable
int OutputDropLoggerEnable(void)
Definition: output.c:810
PacketAlert_::s
const struct Signature_ * s
Definition: decode.h:281
JsonDropLogThread_::drop_ctx
JsonDropOutputCtx * drop_ctx
Definition: output-json-drop.c:69
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1201
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:260
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
ACTION_REJECT
#define ACTION_REJECT
Definition: action-globals.h:31
JsonDropLogThread_
Definition: output-json-drop.c:68
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
PacketAlerts_::alerts
PacketAlert alerts[PACKET_ALERT_MAX]
Definition: decode.h:300
FreeEveThreadCtx
void FreeEveThreadCtx(OutputJsonThreadCtx *ctx)
Definition: output-json-common.c:73
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:299
threads.h
OutputJsonCtx_
Definition: output-json.h:81
AlertJsonHeader
void AlertJsonHeader(void *ctx, const Packet *p, const PacketAlert *pa, JsonBuilder *js, uint16_t flags, JsonAddrInfo *addr)
Definition: output-json-alert.c:340
ICMPV6_GET_SEQ
#define ICMPV6_GET_SEQ(p)
Definition: decode-icmpv6.h:113
logged
int logged
Definition: app-layer-htp.h:1
JsonDropOutputCtx
struct JsonDropOutputCtx_ JsonDropOutputCtx
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:975
CreateEveThreadCtx
OutputJsonThreadCtx * CreateEveThreadCtx(ThreadVars *t, OutputJsonCtx *ctx)
Definition: output-json-common.c:44
json_addr_info_zero
const JsonAddrInfo json_addr_info_zero
Definition: output-json.c:86
IPV6_GET_L4PROTO
#define IPV6_GET_L4PROTO(p)
Definition: decode-ipv6.h:93
ICMPV6_GET_ID
#define ICMPV6_GET_ID(p)
Definition: decode-icmpv6.h:111
PacketAlerts_::drop
PacketAlert drop
Definition: decode.h:303
util-privs.h
FLOW_ACTION_DROP
#define FLOW_ACTION_DROP
Definition: flow.h:68
FLOW_TOSERVER_DROP_LOGGED
#define FLOW_TOSERVER_DROP_LOGGED
Definition: flow.h:76
proto
uint8_t proto
Definition: decode-template.h:0
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:566
util-unittest.h
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:565
util-unittest-helper.h
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
IPV6_GET_PLEN
#define IPV6_GET_PLEN(p)
Definition: decode-ipv6.h:88
TCP_GET_WINDOW
#define TCP_GET_WINDOW(p)
Definition: decode-tcp.h:114
PacketAlert_::action
uint8_t action
Definition: decode.h:279
OutputJsonThreadCtx_
Definition: output-json.h:89
detect-reference.h
TCP_ISSET_FLAG_SYN
#define TCP_ISSET_FLAG_SYN(p)
Definition: decode-tcp.h:120
ICMPV4_GET_SEQ
#define ICMPV4_GET_SEQ(p)
Definition: decode-icmpv4.h:247
ACTION_REJECT_DST
#define ACTION_REJECT_DST
Definition: action-globals.h:32
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:261
IPV4_GET_IPPROTO
#define IPV4_GET_IPPROTO(p)
Definition: decode-ipv4.h:148
util-debug.h
SC_ERR_CONF_YAML_ERROR
@ SC_ERR_CONF_YAML_ERROR
Definition: util-error.h:274
LOG_DROP_ALERTS
#define LOG_DROP_ALERTS
Definition: output-json-drop.c:61
PKT_IS_TOSERVER
#define PKT_IS_TOSERVER(p)
Definition: decode.h:265
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
output-json.h
JsonDropLogThread_::ctx
OutputJsonThreadCtx * ctx
Definition: output-json-drop.c:70
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, OutputJsonCtx *eve_ctx)
Definition: output-json.c:839
util-print.h
detect-engine-mpm.h
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
IPV4_GET_IPLEN
#define IPV4_GET_IPLEN(p)
Definition: decode-ipv4.h:127
TCP_ISSET_FLAG_URG
#define TCP_ISSET_FLAG_URG(p)
Definition: decode-tcp.h:124
util-time.h
OutputInitResult_::ok
bool ok
Definition: output.h:45
IPV4_GET_IPID
#define IPV4_GET_IPID(p)
Definition: decode-ipv4.h:129
TRUE
#define TRUE
Definition: suricata-common.h:33
PKT_IS_ICMPV6
#define PKT_IS_ICMPV6(p)
Definition: decode.h:264
TCP_GET_SEQ
#define TCP_GET_SEQ(p)
Definition: decode-tcp.h:112
FALSE
#define FALSE
Definition: suricata-common.h:34
JsonAddrInfo_
Definition: output-json.h:49
Packet_
Definition: decode.h:414
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
util-proto-name.h
JsonDropOutputCtx_::flags
uint8_t flags
Definition: output-json-drop.c:64
ACTION_REJECT_BOTH
#define ACTION_REJECT_BOTH
Definition: action-globals.h:33
decode-ipv4.h
ICMPV4_GET_ID
#define ICMPV4_GET_ID(p)
Definition: decode-icmpv4.h:245
IPV6_GET_FLOW
#define IPV6_GET_FLOW(p)
Definition: decode-ipv6.h:84
OutputInitResult_
Definition: output.h:43
Packet_::flow
struct Flow_ * flow
Definition: decode.h:451
UDP_GET_LEN
#define UDP_GET_LEN(p)
Definition: decode-udp.h:35
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
IPV6_GET_CLASS
#define IPV6_GET_CLASS(p)
Definition: decode-ipv6.h:82
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
OutputRegisterPacketSubModule
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:214
Packet_::tcph
TCPHdr * tcph
Definition: decode.h:531
MODULE_NAME
#define MODULE_NAME
Definition: output-json-drop.c:59
ACTION_DROP
#define ACTION_DROP
Definition: action-globals.h:30
LOGGER_JSON_DROP
@ LOGGER_JSON_DROP
Definition: suricata-common.h:478
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:257
TCP_ISSET_FLAG_ACK
#define TCP_ISSET_FLAG_ACK(p)
Definition: decode-tcp.h:123
util-classification-config.h
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
output-json-alert.h
PKT_IS_ICMPV4
#define PKT_IS_ICMPV4(p)
Definition: decode.h:263
threadvars.h
TCP_ISSET_FLAG_RST
#define TCP_ISSET_FLAG_RST(p)
Definition: decode-tcp.h:121
IPV4_GET_IPTOS
#define IPV4_GET_IPTOS(p)
Definition: decode-ipv4.h:125
JsonDropLogThread
struct JsonDropLogThread_ JsonDropLogThread
SCLogWarning
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:244
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-logopenfile.h
JsonDropOutputCtx_
Definition: output-json-drop.c:63
TCP_ISSET_FLAG_FIN
#define TCP_ISSET_FLAG_FIN(p)
Definition: decode-tcp.h:119
Flow_::flags
uint32_t flags
Definition: flow.h:431
JsonDropLogRegister
void JsonDropLogRegister(void)
Definition: output-json-drop.c:361
detect-parse.h
util-buffer.h
FLOW_TOCLIENT_DROP_LOGGED
#define FLOW_TOCLIENT_DROP_LOGGED
Definition: flow.h:78
IPV6_GET_HLIM
#define IPV6_GET_HLIM(p)
Definition: decode-ipv6.h:90
EngineModeIsIPS
int EngineModeIsIPS(void)
Definition: suricata.c:244
TCP_GET_URG_POINTER
#define TCP_GET_URG_POINTER(p)
Definition: decode-tcp.h:115
PacketAlert_
Definition: decode.h:277
JsonAddrInfoInit
void JsonAddrInfoInit(const Packet *p, enum OutputJsonLogDirection dir, JsonAddrInfo *addr)
Definition: output-json.c:492
TCP_GET_ACK
#define TCP_GET_ACK(p)
Definition: decode-tcp.h:113
flow.h
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:259
debug.h
JsonDropOutputCtx_::eve_ctx
OutputJsonCtx * eve_ctx
Definition: output-json-drop.c:65
output.h
IPV4_GET_IPTTL
#define IPV4_GET_IPTTL(p)
Definition: decode-ipv4.h:146
OutputDropLoggerDisable
void OutputDropLoggerDisable(void)
Definition: output.c:818
TCP_ISSET_FLAG_PUSH
#define TCP_ISSET_FLAG_PUSH(p)
Definition: decode-tcp.h:122
output-json-drop.h
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:842