1 /* Copyright (C) 2007-2013 Open Information Security Foundation
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
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.
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
21 * \author Tom DeCanio <td@npulsetech.com>
23 * JSON Drop log module to log the dropped packet information
27 #include "suricata-common.h"
34 #include "tm-threads.h"
35 #include "threadvars.h"
36 #include "util-debug.h"
38 #include "decode-ipv4.h"
40 #include "detect-parse.h"
41 #include "detect-engine.h"
42 #include "detect-engine-mpm.h"
43 #include "detect-reference.h"
46 #include "output-json.h"
47 #include "output-json-alert.h"
49 #include "util-unittest.h"
50 #include "util-unittest-helper.h"
51 #include "util-classification-config.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"
59 #define MODULE_NAME "JsonDropLog"
61 #ifdef HAVE_LIBJANSSON
64 #define LOG_DROP_ALERTS 1
66 typedef struct JsonDropOutputCtx_ {
71 typedef struct JsonDropLogThread_ {
72 JsonDropOutputCtx *drop_ctx;
77 * \brief Log the dropped packets in netfilter format when engine is running
80 * \param tv Pointer the current thread variables
81 * \param p Pointer the packet which is being logged
83 * \return return TM_EODE_OK on success
85 static int DropLogJSON (JsonDropLogThread *aft, const Packet *p)
88 MemBuffer *buffer = (MemBuffer *)aft->buffer;
89 json_t *js = CreateJSONHeader((Packet *)p, 0, "drop");//TODO const
90 if (unlikely(js == NULL))
93 json_t *djs = json_object();
94 if (unlikely(djs == NULL)) {
100 MemBufferReset(buffer);
102 if (PKT_IS_IPV4(p)) {
103 json_object_set_new(djs, "len", json_integer(IPV4_GET_IPLEN(p)));
104 json_object_set_new(djs, "tos", json_integer(IPV4_GET_IPTOS(p)));
105 json_object_set_new(djs, "ttl", json_integer(IPV4_GET_IPTTL(p)));
106 json_object_set_new(djs, "ipid", json_integer(IPV4_GET_IPID(p)));
107 proto = IPV4_GET_IPPROTO(p);
108 } else if (PKT_IS_IPV6(p)) {
109 json_object_set_new(djs, "len", json_integer(IPV6_GET_PLEN(p)));
110 json_object_set_new(djs, "tc", json_integer(IPV6_GET_CLASS(p)));
111 json_object_set_new(djs, "hoplimit", json_integer(IPV6_GET_HLIM(p)));
112 json_object_set_new(djs, "flowlbl", json_integer(IPV6_GET_FLOW(p)));
113 proto = IPV6_GET_L4PROTO(p);
117 json_object_set_new(djs, "tcpseq", json_integer(TCP_GET_SEQ(p)));
118 json_object_set_new(djs, "tcpack", json_integer(TCP_GET_ACK(p)));
119 json_object_set_new(djs, "tcpwin", json_integer(TCP_GET_WINDOW(p)));
120 json_object_set_new(djs, "syn", TCP_ISSET_FLAG_SYN(p) ? json_true() : json_false());
121 json_object_set_new(djs, "ack", TCP_ISSET_FLAG_ACK(p) ? json_true() : json_false());
122 json_object_set_new(djs, "psh", TCP_ISSET_FLAG_PUSH(p) ? json_true() : json_false());
123 json_object_set_new(djs, "rst", TCP_ISSET_FLAG_RST(p) ? json_true() : json_false());
124 json_object_set_new(djs, "urg", TCP_ISSET_FLAG_URG(p) ? json_true() : json_false());
125 json_object_set_new(djs, "fin", TCP_ISSET_FLAG_FIN(p) ? json_true() : json_false());
126 json_object_set_new(djs, "tcpres", json_integer(TCP_GET_RAW_X2(p->tcph)));
127 json_object_set_new(djs, "tcpurgp", json_integer(TCP_GET_URG_POINTER(p)));
130 json_object_set_new(djs, "udplen", json_integer(UDP_GET_LEN(p)));
133 if (PKT_IS_ICMPV4(p)) {
134 json_object_set_new(djs, "icmp_id", json_integer(ICMPV4_GET_ID(p)));
135 json_object_set_new(djs, "icmp_seq", json_integer(ICMPV4_GET_SEQ(p)));
136 } else if(PKT_IS_ICMPV6(p)) {
137 json_object_set_new(djs, "icmp_id", json_integer(ICMPV6_GET_ID(p)));
138 json_object_set_new(djs, "icmp_seq", json_integer(ICMPV6_GET_SEQ(p)));
142 json_object_set_new(js, "drop", djs);
144 if (aft->drop_ctx->flags & LOG_DROP_ALERTS) {
147 for (i = 0; i < p->alerts.cnt; i++) {
148 const PacketAlert *pa = &p->alerts.alerts[i];
149 if (unlikely(pa->s == NULL)) {
152 if ((pa->action & (ACTION_REJECT|ACTION_REJECT_DST|ACTION_REJECT_BOTH)) ||
153 ((pa->action & ACTION_DROP) && EngineModeIsIPS()))
155 AlertJsonHeader(p, pa, js);
160 if (p->alerts.drop.action != 0) {
161 const PacketAlert *pa = &p->alerts.drop;
162 AlertJsonHeader(p, pa, js);
167 OutputJSONBuffer(js, aft->drop_ctx->file_ctx, buffer);
168 json_object_del(js, "drop");
169 json_object_clear(js);
175 #define OUTPUT_BUFFER_SIZE 65535
176 static TmEcode JsonDropLogThreadInit(ThreadVars *t, void *initdata, void **data)
178 JsonDropLogThread *aft = SCMalloc(sizeof(JsonDropLogThread));
179 if (unlikely(aft == NULL))
180 return TM_ECODE_FAILED;
181 memset(aft, 0, sizeof(*aft));
185 SCLogDebug("Error getting context for AlertFastLog. \"initdata\" argument NULL");
187 return TM_ECODE_FAILED;
190 aft->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
191 if (aft->buffer == NULL) {
193 return TM_ECODE_FAILED;
196 /** Use the Ouptut Context (file pointer and mutex) */
197 aft->drop_ctx = ((OutputCtx *)initdata)->data;
203 static TmEcode JsonDropLogThreadDeinit(ThreadVars *t, void *data)
205 JsonDropLogThread *aft = (JsonDropLogThread *)data;
210 MemBufferFree(aft->buffer);
213 memset(aft, 0, sizeof(*aft));
219 static void JsonDropLogDeInitCtx(OutputCtx *output_ctx)
221 OutputDropLoggerDisable();
223 LogFileCtx *logfile_ctx = (LogFileCtx *)output_ctx->data;
224 LogFileFreeCtx(logfile_ctx);
228 static void JsonDropLogDeInitCtxSub(OutputCtx *output_ctx)
230 OutputDropLoggerDisable();
232 SCLogDebug("cleaning up sub output_ctx %p", output_ctx);
236 static void JsonDropOutputCtxFree(JsonDropOutputCtx *drop_ctx)
238 if (drop_ctx != NULL) {
239 if (drop_ctx->file_ctx != NULL)
240 LogFileFreeCtx(drop_ctx->file_ctx);
245 #define DEFAULT_LOG_FILENAME "drop.json"
246 static OutputCtx *JsonDropLogInitCtx(ConfNode *conf)
248 if (OutputDropLoggerEnable() != 0) {
249 SCLogError(SC_ERR_CONF_YAML_ERROR, "only one 'drop' logger "
254 JsonDropOutputCtx *drop_ctx = SCCalloc(1, sizeof(*drop_ctx));
255 if (drop_ctx == NULL)
258 drop_ctx->file_ctx = LogFileNewCtx();
259 if (drop_ctx->file_ctx == NULL) {
260 JsonDropOutputCtxFree(drop_ctx);
264 if (SCConfLogOpenGeneric(conf, drop_ctx->file_ctx, DEFAULT_LOG_FILENAME, 1) < 0) {
265 JsonDropOutputCtxFree(drop_ctx);
269 OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
270 if (unlikely(output_ctx == NULL)) {
271 JsonDropOutputCtxFree(drop_ctx);
276 const char *extended = ConfNodeLookupChildValue(conf, "alerts");
277 if (extended != NULL) {
278 if (ConfValIsTrue(extended)) {
279 drop_ctx->flags = LOG_DROP_ALERTS;
284 output_ctx->data = drop_ctx;
285 output_ctx->DeInit = JsonDropLogDeInitCtx;
289 static OutputCtx *JsonDropLogInitCtxSub(ConfNode *conf, OutputCtx *parent_ctx)
291 if (OutputDropLoggerEnable() != 0) {
292 SCLogError(SC_ERR_CONF_YAML_ERROR, "only one 'drop' logger "
297 AlertJsonThread *ajt = parent_ctx->data;
299 JsonDropOutputCtx *drop_ctx = SCCalloc(1, sizeof(*drop_ctx));
300 if (drop_ctx == NULL)
303 OutputCtx *output_ctx = SCCalloc(1, sizeof(OutputCtx));
304 if (unlikely(output_ctx == NULL)) {
305 JsonDropOutputCtxFree(drop_ctx);
310 const char *extended = ConfNodeLookupChildValue(conf, "alerts");
311 if (extended != NULL) {
312 if (ConfValIsTrue(extended)) {
313 drop_ctx->flags = LOG_DROP_ALERTS;
318 drop_ctx->file_ctx = ajt->file_ctx;
320 output_ctx->data = drop_ctx;
321 output_ctx->DeInit = JsonDropLogDeInitCtxSub;
326 * \brief Log the dropped packets when engine is running in inline mode
328 * \param tv Pointer the current thread variables
329 * \param data Pointer to the droplog struct
330 * \param p Pointer the packet which is being logged
332 * \retval 0 on succes
334 static int JsonDropLogger(ThreadVars *tv, void *thread_data, const Packet *p)
336 JsonDropLogThread *td = thread_data;
337 int r = DropLogJSON(td, p);
342 FLOWLOCK_RDLOCK(p->flow);
343 if (p->flow->flags & FLOW_ACTION_DROP) {
344 if (PKT_IS_TOSERVER(p) && !(p->flow->flags & FLOW_TOSERVER_DROP_LOGGED))
345 p->flow->flags |= FLOW_TOSERVER_DROP_LOGGED;
346 else if (PKT_IS_TOCLIENT(p) && !(p->flow->flags & FLOW_TOCLIENT_DROP_LOGGED))
347 p->flow->flags |= FLOW_TOCLIENT_DROP_LOGGED;
349 FLOWLOCK_UNLOCK(p->flow);
356 * \brief Check if we need to drop-log this packet
358 * \param tv Pointer the current thread variables
359 * \param p Pointer the packet which is tested
361 * \retval bool TRUE or FALSE
363 static int JsonDropLogCondition(ThreadVars *tv, const Packet *p)
365 if (!EngineModeIsIPS()) {
366 SCLogDebug("engine is not running in inline mode, so returning");
369 if (PKT_IS_PSEUDOPKT(p)) {
370 SCLogDebug("drop log doesn't log pseudo packets");
374 if (p->flow != NULL) {
377 /* for a flow that will be dropped fully, log just once per direction */
378 FLOWLOCK_RDLOCK(p->flow);
379 if (p->flow->flags & FLOW_ACTION_DROP) {
380 if (PKT_IS_TOSERVER(p) && !(p->flow->flags & FLOW_TOSERVER_DROP_LOGGED))
382 else if (PKT_IS_TOCLIENT(p) && !(p->flow->flags & FLOW_TOCLIENT_DROP_LOGGED))
385 FLOWLOCK_UNLOCK(p->flow);
387 /* if drop is caused by signature, log anyway */
388 if (p->alerts.drop.action != 0)
392 } else if (PACKET_TEST_ACTION(p, ACTION_DROP)) {
399 void TmModuleJsonDropLogRegister (void)
401 tmm_modules[TMM_JSONDROPLOG].name = MODULE_NAME;
402 tmm_modules[TMM_JSONDROPLOG].ThreadInit = JsonDropLogThreadInit;
403 tmm_modules[TMM_JSONDROPLOG].ThreadDeinit = JsonDropLogThreadDeinit;
404 tmm_modules[TMM_JSONDROPLOG].cap_flags = 0;
405 tmm_modules[TMM_JSONDROPLOG].flags = TM_FLAG_LOGAPI_TM;
407 OutputRegisterPacketModule(MODULE_NAME, "drop-json-log",
408 JsonDropLogInitCtx, JsonDropLogger, JsonDropLogCondition);
409 OutputRegisterPacketSubModule("eve-log", MODULE_NAME, "eve-log.drop",
410 JsonDropLogInitCtxSub, JsonDropLogger, JsonDropLogCondition);
415 static TmEcode OutputJsonThreadInit(ThreadVars *t, void *initdata, void **data)
417 SCLogInfo("Can't init JSON output - JSON support was disabled during build.");
418 return TM_ECODE_FAILED;
421 void TmModuleJsonDropLogRegister (void)
423 tmm_modules[TMM_JSONDROPLOG].name = MODULE_NAME;
424 tmm_modules[TMM_JSONDROPLOG].ThreadInit = OutputJsonThreadInit;