]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/sip: add SIP logger
authorGiuseppe Longo <giuseppe@glongo.it>
Sun, 17 Feb 2019 17:06:17 +0000 (18:06 +0100)
committerVictor Julien <victor@inliniac.net>
Tue, 17 Sep 2019 08:42:20 +0000 (10:42 +0200)
rust/src/sip/log.rs [new file with mode: 0644]
rust/src/sip/mod.rs [changed mode: 0644->0755]
src/Makefile.am
src/output-json-sip.c [new file with mode: 0644]
src/output-json-sip.h [new file with mode: 0644]
src/output.c
src/suricata-common.h
src/util-profiling.c
suricata.yaml.in

diff --git a/rust/src/sip/log.rs b/rust/src/sip/log.rs
new file mode 100644 (file)
index 0000000..256a089
--- /dev/null
@@ -0,0 +1,58 @@
+/* Copyright (C) 2019 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+// written by Giuseppe Longo <giuseppe@glongo.it>
+
+use json::*;
+use sip::sip::{SIPState, SIPTransaction};
+
+#[no_mangle]
+pub extern "C" fn rs_sip_log_json(_state: &mut SIPState, tx: &mut SIPTransaction) -> *mut JsonT
+{
+    let js = Json::object();
+
+    match tx.request {
+        Some(ref req) => {
+            js.set_string("method", &req.method);
+            js.set_string("uri", &req.path);
+            js.set_string("version", &req.version);
+        },
+        None => {}
+    }
+    match tx.request_line {
+        Some (ref req_line) => {
+            js.set_string("request_line", &req_line);
+        },
+        None => {}
+    }
+    match tx.response {
+        Some(ref resp) => {
+            js.set_string("version", &resp.version);
+            js.set_string("code", &resp.code);
+            js.set_string("reason", &resp.reason);
+        },
+        None => {}
+    }
+    match tx.response_line {
+        Some (ref resp_line) => {
+            js.set_string("response_line", &resp_line);
+        },
+        None => {}
+    }
+
+    return js.unwrap();
+}
old mode 100644 (file)
new mode 100755 (executable)
index d9ff115..061133d
@@ -19,3 +19,4 @@
 
 pub mod sip;
 pub mod parser;
+pub mod log;
index b29ec4086df286c88cd453e1cd33bf514174ec48..491c712413283fb97b59b6967854c159e9bafd2f 100644 (file)
@@ -333,6 +333,7 @@ output-json-flow.c output-json-flow.h \
 output-json-ftp.c output-json-ftp.h \
 output-json-netflow.c output-json-netflow.h \
 output-json-http.c output-json-http.h \
+output-json-sip.c output-json-sip.h \
 output-json-smtp.c output-json-smtp.h \
 output-json-ssh.c output-json-ssh.h \
 output-json-stats.c output-json-stats.h \
diff --git a/src/output-json-sip.c b/src/output-json-sip.c
new file mode 100644 (file)
index 0000000..796614c
--- /dev/null
@@ -0,0 +1,190 @@
+/* Copyright (C) 2018 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Giuseppe Longo <giuseppe@glongo.it>
+ *
+ * Implement JSON/eve logging app-layer SIP.
+ */
+
+#include "suricata-common.h"
+#include "debug.h"
+#include "detect.h"
+#include "pkt-var.h"
+#include "conf.h"
+
+#include "threads.h"
+#include "threadvars.h"
+#include "tm-threads.h"
+
+#include "util-unittest.h"
+#include "util-buffer.h"
+#include "util-debug.h"
+#include "util-byte.h"
+
+#include "output.h"
+#include "output-json.h"
+
+#include "app-layer.h"
+#include "app-layer-parser.h"
+
+#include "app-layer-sip.h"
+#include "output-json-sip.h"
+
+#include "rust.h"
+#include "rust-sip-log-gen.h"
+
+#ifdef HAVE_LIBJANSSON
+
+typedef struct LogSIPFileCtx_ {
+    LogFileCtx *file_ctx;
+    OutputJsonCommonSettings cfg;
+} LogSIPFileCtx;
+
+typedef struct LogSIPLogThread_ {
+    LogSIPFileCtx *siplog_ctx;
+    MemBuffer          *buffer;
+} LogSIPLogThread;
+
+static int JsonSIPLogger(ThreadVars *tv, void *thread_data,
+    const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
+{
+    SIPTransaction *siptx = tx;
+    LogSIPLogThread *thread = thread_data;
+    json_t *js, *sipjs;
+
+    js = CreateJSONHeader(p, LOG_DIR_PACKET, "sip");
+    if (unlikely(js == NULL)) {
+        return TM_ECODE_FAILED;
+    }
+
+    JsonAddCommonOptions(&thread->siplog_ctx->cfg, p, f, js);
+
+    sipjs = rs_sip_log_json(state, siptx);
+    if (unlikely(sipjs == NULL)) {
+      goto error;
+    }
+    json_object_set_new(js, "sip", sipjs);
+
+    MemBufferReset(thread->buffer);
+    OutputJSONBuffer(js, thread->siplog_ctx->file_ctx, &thread->buffer);
+
+    json_decref(js);
+    return TM_ECODE_OK;
+
+error:
+    json_decref(js);
+    return TM_ECODE_FAILED;
+}
+
+static void OutputSIPLogDeInitCtxSub(OutputCtx *output_ctx)
+{
+    LogSIPFileCtx *siplog_ctx = (LogSIPFileCtx *)output_ctx->data;
+    SCFree(siplog_ctx);
+    SCFree(output_ctx);
+}
+
+static OutputInitResult OutputSIPLogInitSub(ConfNode *conf,
+    OutputCtx *parent_ctx)
+{
+    OutputInitResult result = { NULL, false };
+    OutputJsonCtx *ajt = parent_ctx->data;
+
+    LogSIPFileCtx *siplog_ctx = SCCalloc(1, sizeof(*siplog_ctx));
+    if (unlikely(siplog_ctx == NULL)) {
+        return result;
+    }
+    siplog_ctx->file_ctx = ajt->file_ctx;
+    siplog_ctx->cfg = ajt->cfg;
+
+    OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
+    if (unlikely(output_ctx == NULL)) {
+        SCFree(siplog_ctx);
+        return result;
+    }
+    output_ctx->data = siplog_ctx;
+    output_ctx->DeInit = OutputSIPLogDeInitCtxSub;
+
+    SCLogDebug("SIP log sub-module initialized.");
+
+    AppLayerParserRegisterLogger(IPPROTO_UDP, ALPROTO_SIP);
+
+    result.ctx = output_ctx;
+    result.ok = true;
+    return result;
+}
+
+#define OUTPUT_BUFFER_SIZE 65535
+
+static TmEcode JsonSIPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
+{
+    LogSIPLogThread *thread = SCCalloc(1, sizeof(*thread));
+    if (unlikely(thread == NULL)) {
+        return TM_ECODE_FAILED;
+    }
+
+    if (initdata == NULL) {
+        SCLogDebug("Error getting context for EveLogSIP.  \"initdata\" is NULL.");
+        SCFree(thread);
+        return TM_ECODE_FAILED;
+    }
+
+    thread->buffer = MemBufferCreateNew(OUTPUT_BUFFER_SIZE);
+    if (unlikely(thread->buffer == NULL)) {
+        SCFree(thread);
+        return TM_ECODE_FAILED;
+    }
+
+    thread->siplog_ctx = ((OutputCtx *)initdata)->data;
+    *data = (void *)thread;
+
+    return TM_ECODE_OK;
+}
+
+static TmEcode JsonSIPLogThreadDeinit(ThreadVars *t, void *data)
+{
+    LogSIPLogThread *thread = (LogSIPLogThread *)data;
+    if (thread == NULL) {
+        return TM_ECODE_OK;
+    }
+    if (thread->buffer != NULL) {
+        MemBufferFree(thread->buffer);
+    }
+    SCFree(thread);
+    return TM_ECODE_OK;
+}
+
+void JsonSIPLogRegister(void)
+{
+    /* Register as an eve sub-module. */
+    OutputRegisterTxSubModule(LOGGER_JSON_SIP, "eve-log", "JsonSIPLog",
+        "eve-log.sip", OutputSIPLogInitSub, ALPROTO_SIP,
+        JsonSIPLogger, JsonSIPLogThreadInit,
+        JsonSIPLogThreadDeinit, NULL);
+
+    SCLogDebug("SIP JSON logger registered.");
+}
+
+#else /* No JSON support. */
+
+void JsonSIPLogRegister(void)
+{
+}
+
+#endif /* HAVE_LIBJANSSON */
diff --git a/src/output-json-sip.h b/src/output-json-sip.h
new file mode 100644 (file)
index 0000000..0d2c53f
--- /dev/null
@@ -0,0 +1,29 @@
+/* Copyright (C) 2015 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+/**
+ * \file
+ *
+ * \author Giuseppe Longo <giuseppe@glongo.it>
+ */
+
+#ifndef __OUTPUT_JSON_SIP_H__
+#define __OUTPUT_JSON_SIP_H__
+
+void JsonSIPLogRegister(void);
+
+#endif /* __OUTPUT_JSON_SIP_H__ */
index 3e109c5c19673ce5a58c4402292f1bfdb53212b2..c486999805d4a961d822414bb5bd9f6f1710697c 100644 (file)
@@ -75,6 +75,7 @@
 #include "output-json-krb5.h"
 #include "output-json-dhcp.h"
 #include "output-json-snmp.h"
+#include "output-json-sip.h"
 #include "output-json-template.h"
 #include "output-json-template-rust.h"
 #include "output-json-rdp.h"
@@ -1110,6 +1111,8 @@ void OutputRegisterLoggers(void)
     JsonDHCPLogRegister();
     /* SNMP JSON logger. */
     JsonSNMPLogRegister();
+    /* SIP JSON logger. */
+    JsonSIPLogRegister();
     /* Template JSON logger. */
     JsonTemplateLogRegister();
     /* Template Rust JSON logger. */
index b35a49ad621c9ada2818cbb9d8a41d0fd4a4eba7..c3f7e9e632616e679330ff7d9150b9479b1c5515 100644 (file)
@@ -440,6 +440,7 @@ typedef enum {
     LOGGER_JSON_KRB5,
     LOGGER_JSON_DHCP,
     LOGGER_JSON_SNMP,
+    LOGGER_JSON_SIP,
     LOGGER_JSON_TEMPLATE_RUST,
     LOGGER_JSON_TEMPLATE,
     LOGGER_JSON_RDP,
index 4b618ab6cb294b6c384a99577d6ec0ba2a204fe3..3fbcd6b6a48c7353653faa1cb20a1a16356c1119 100644 (file)
@@ -1318,6 +1318,7 @@ const char * PacketProfileLoggertIdToString(LoggerId id)
         CASE_CODE (LOGGER_JSON_SMTP);
         CASE_CODE (LOGGER_JSON_SNMP);
         CASE_CODE (LOGGER_JSON_TLS);
+        CASE_CODE (LOGGER_JSON_SIP);
         CASE_CODE (LOGGER_JSON_TEMPLATE_RUST);
         CASE_CODE (LOGGER_JSON_TEMPLATE);
         CASE_CODE (LOGGER_JSON_RDP);
index 240f875288167978b0d9c953f8c7a931995d31b5..c3e827d6a60becd21d9b9dfad04d1fa02c01326d 100644 (file)
@@ -267,6 +267,7 @@ outputs:
         - ikev2
         - krb5
         - snmp
+        - sip
         - dhcp:
             enabled: yes
             # When extended mode is on, all DHCP messages are logged