From: Giuseppe Longo Date: Sun, 17 Feb 2019 17:06:17 +0000 (+0100) Subject: rust/sip: add SIP logger X-Git-Tag: suricata-5.0.0-rc1~37 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=edc2a583a9ebe19756ac225090fe032d8e727df0;p=thirdparty%2Fsuricata.git rust/sip: add SIP logger --- diff --git a/rust/src/sip/log.rs b/rust/src/sip/log.rs new file mode 100644 index 0000000000..256a089c88 --- /dev/null +++ b/rust/src/sip/log.rs @@ -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 + +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(); +} diff --git a/rust/src/sip/mod.rs b/rust/src/sip/mod.rs old mode 100644 new mode 100755 index d9ff115112..061133de8a --- a/rust/src/sip/mod.rs +++ b/rust/src/sip/mod.rs @@ -19,3 +19,4 @@ pub mod sip; pub mod parser; +pub mod log; diff --git a/src/Makefile.am b/src/Makefile.am index b29ec4086d..491c712413 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 0000000000..796614cbe5 --- /dev/null +++ b/src/output-json-sip.c @@ -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 + * + * 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 index 0000000000..0d2c53fa50 --- /dev/null +++ b/src/output-json-sip.h @@ -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 + */ + +#ifndef __OUTPUT_JSON_SIP_H__ +#define __OUTPUT_JSON_SIP_H__ + +void JsonSIPLogRegister(void); + +#endif /* __OUTPUT_JSON_SIP_H__ */ diff --git a/src/output.c b/src/output.c index 3e109c5c19..c486999805 100644 --- a/src/output.c +++ b/src/output.c @@ -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. */ diff --git a/src/suricata-common.h b/src/suricata-common.h index b35a49ad62..c3f7e9e632 100644 --- a/src/suricata-common.h +++ b/src/suricata-common.h @@ -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, diff --git a/src/util-profiling.c b/src/util-profiling.c index 4b618ab6cb..3fbcd6b6a4 100644 --- a/src/util-profiling.c +++ b/src/util-profiling.c @@ -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); diff --git a/suricata.yaml.in b/suricata.yaml.in index 240f875288..c3e827d6a6 100644 --- a/suricata.yaml.in +++ b/suricata.yaml.in @@ -267,6 +267,7 @@ outputs: - ikev2 - krb5 - snmp + - sip - dhcp: enabled: yes # When extended mode is on, all DHCP messages are logged