From: Pierre Chifflier Date: Tue, 17 Apr 2018 05:50:43 +0000 (+0200) Subject: Add logger for Kerberos 5 metadata X-Git-Tag: suricata-4.1.0-rc1~73 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd175f2bfbbd8da06ce87e9ce489e2803f3561b6;p=thirdparty%2Fsuricata.git Add logger for Kerberos 5 metadata --- diff --git a/rust/src/krb/log.rs b/rust/src/krb/log.rs new file mode 100644 index 0000000000..09462395c8 --- /dev/null +++ b/rust/src/krb/log.rs @@ -0,0 +1,52 @@ +/* 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. + */ + +// written by Pierre Chifflier + +use json::*; +use krb::krb5::{KRB5State,KRB5Transaction}; + +#[no_mangle] +pub extern "C" fn rs_krb5_log_json_response(_state: &mut KRB5State, tx: &mut KRB5Transaction) -> *mut JsonT +{ + let js = Json::object(); + js.set_string("msg_type", &format!("{:?}", tx.msg_type)); + // XXX PrincipalName object should be pretty-printed + let cname = match tx.cname { + Some(ref x) => format!("{}", x), + None => "".to_owned(), + }; + let realm = match tx.realm { + Some(ref x) => format!("{}", x.0), + None => "".to_owned(), + }; + // XXX PrincipalName object should be pretty-printed + let sname = match tx.sname { + Some(ref x) => format!("{}", x), + None => "".to_owned(), + }; + let encryption = match tx.etype { + Some(ref x) => format!("{:?}", x), + None => "".to_owned(), + }; + js.set_string("cname", &cname); + js.set_string("realm", &realm); + js.set_string("sname", &sname); + js.set_string("encryption", &encryption); + return js.unwrap(); +} + diff --git a/rust/src/krb/mod.rs b/rust/src/krb/mod.rs index 735c71c1d2..fea4d55906 100644 --- a/rust/src/krb/mod.rs +++ b/rust/src/krb/mod.rs @@ -18,3 +18,4 @@ // written by Pierre Chifflier pub mod krb5; +pub mod log; diff --git a/src/Makefile.am b/src/Makefile.am index 5cb872a034..38aa0f82e8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -323,6 +323,7 @@ output-json-nfs.c output-json-nfs.h \ output-json-tftp.c output-json-tftp.h \ output-json-smb.c output-json-smb.h \ output-json-ikev2.c output-json-ikev2.h \ +output-json-krb5.c output-json-krb5.h \ output-json-template.c output-json-template.h \ output-json-metadata.c output-json-metadata.h \ output-lua.c output-lua.h \ diff --git a/src/output-json-krb5.c b/src/output-json-krb5.c new file mode 100644 index 0000000000..84b592c48e --- /dev/null +++ b/src/output-json-krb5.c @@ -0,0 +1,202 @@ +/* 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 Pierre Chifflier + * + * Implement JSON/eve logging app-layer KRB5. + */ + +#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-krb5.h" +#include "output-json-krb5.h" + +#ifdef HAVE_RUST +#ifdef HAVE_LIBJANSSON + +#include "rust.h" +#include "rust-krb-log-gen.h" + +typedef struct LogKRB5FileCtx_ { + LogFileCtx *file_ctx; + uint32_t flags; + bool include_metadata; +} LogKRB5FileCtx; + +typedef struct LogKRB5LogThread_ { + LogKRB5FileCtx *krb5log_ctx; + uint32_t count; + MemBuffer *buffer; +} LogKRB5LogThread; + +static int JsonKRB5Logger(ThreadVars *tv, void *thread_data, + const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id) +{ + KRB5Transaction *krb5tx = tx; + LogKRB5LogThread *thread = thread_data; + json_t *js, *krb5js; + + js = CreateJSONHeader(p, LOG_DIR_PACKET, "krb5"); + if (unlikely(js == NULL)) { + return TM_ECODE_FAILED; + } + + if (thread->krb5log_ctx->include_metadata) { + JsonAddMetadata(p, f, js); + } + + krb5js = rs_krb5_log_json_response(state, krb5tx); + if (unlikely(krb5js == NULL)) { + goto error; + } + json_object_set_new(js, "krb5", krb5js); + + MemBufferReset(thread->buffer); + OutputJSONBuffer(js, thread->krb5log_ctx->file_ctx, &thread->buffer); + + json_decref(js); + return TM_ECODE_OK; + +error: + json_decref(js); + return TM_ECODE_FAILED; +} + +static void OutputKRB5LogDeInitCtxSub(OutputCtx *output_ctx) +{ + LogKRB5FileCtx *krb5log_ctx = (LogKRB5FileCtx *)output_ctx->data; + SCFree(krb5log_ctx); + SCFree(output_ctx); +} + +static OutputInitResult OutputKRB5LogInitSub(ConfNode *conf, + OutputCtx *parent_ctx) +{ + OutputInitResult result = { NULL, false }; + OutputJsonCtx *ajt = parent_ctx->data; + + LogKRB5FileCtx *krb5log_ctx = SCCalloc(1, sizeof(*krb5log_ctx)); + if (unlikely(krb5log_ctx == NULL)) { + return result; + } + krb5log_ctx->file_ctx = ajt->file_ctx; + krb5log_ctx->include_metadata = ajt->include_metadata; + + OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx)); + if (unlikely(output_ctx == NULL)) { + SCFree(krb5log_ctx); + return result; + } + output_ctx->data = krb5log_ctx; + output_ctx->DeInit = OutputKRB5LogDeInitCtxSub; + + SCLogDebug("KRB5 log sub-module initialized."); + + AppLayerParserRegisterLogger(IPPROTO_UDP, ALPROTO_KRB5); + + result.ctx = output_ctx; + result.ok = true; + return result; +} + +#define OUTPUT_BUFFER_SIZE 65535 + +static TmEcode JsonKRB5LogThreadInit(ThreadVars *t, const void *initdata, void **data) +{ + LogKRB5LogThread *thread = SCCalloc(1, sizeof(*thread)); + if (unlikely(thread == NULL)) { + return TM_ECODE_FAILED; + } + + if (initdata == NULL) { + SCLogDebug("Error getting context for EveLogKRB5. \"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->krb5log_ctx = ((OutputCtx *)initdata)->data; + *data = (void *)thread; + + return TM_ECODE_OK; +} + +static TmEcode JsonKRB5LogThreadDeinit(ThreadVars *t, void *data) +{ + LogKRB5LogThread *thread = (LogKRB5LogThread *)data; + if (thread == NULL) { + return TM_ECODE_OK; + } + if (thread->buffer != NULL) { + MemBufferFree(thread->buffer); + } + SCFree(thread); + return TM_ECODE_OK; +} + +void JsonKRB5LogRegister(void) +{ + /* Register as an eve sub-module. */ + OutputRegisterTxSubModule(LOGGER_JSON_KRB5, "eve-log", "JsonKRB5Log", + "eve-log.krb5", OutputKRB5LogInitSub, ALPROTO_KRB5, + JsonKRB5Logger, JsonKRB5LogThreadInit, + JsonKRB5LogThreadDeinit, NULL); + + SCLogDebug("KRB5 JSON logger registered."); +} + +#else /* No JSON support. */ + +void JsonKRB5LogRegister(void) +{ +} + +#endif /* HAVE_LIBJANSSON */ +#else /* No rust support. */ + +void JsonKRB5LogRegister(void) +{ +} + +#endif /* HAVE_RUST */ diff --git a/src/output-json-krb5.h b/src/output-json-krb5.h new file mode 100644 index 0000000000..2f8608819c --- /dev/null +++ b/src/output-json-krb5.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 FirstName LastName + */ + +#ifndef __OUTPUT_JSON_KRB5_H__ +#define __OUTPUT_JSON_KRB5_H__ + +void JsonKRB5LogRegister(void); + +#endif /* __OUTPUT_JSON_KRB5_H__ */ diff --git a/src/output.c b/src/output.c index debbc92b6c..dd4fa54ac9 100644 --- a/src/output.c +++ b/src/output.c @@ -72,6 +72,7 @@ #include "output-json-tftp.h" #include "output-json-smb.h" #include "output-json-ikev2.h" +#include "output-json-krb5.h" #include "output-json-template.h" #include "output-lua.h" #include "output-json-dnp3.h" @@ -1099,6 +1100,8 @@ void OutputRegisterLoggers(void) /* IKEv2 JSON logger. */ JsonIKEv2LogRegister(); + /* KRB5 JSON logger. */ + JsonKRB5LogRegister(); /* Template JSON logger. */ JsonTemplateLogRegister(); } diff --git a/src/suricata-common.h b/src/suricata-common.h index 03f769dd5c..d57b3ba522 100644 --- a/src/suricata-common.h +++ b/src/suricata-common.h @@ -412,6 +412,7 @@ typedef enum { LOGGER_JSON_SSH, LOGGER_JSON_SMB, LOGGER_JSON_IKEV2, + LOGGER_JSON_KRB5, LOGGER_JSON_TEMPLATE, LOGGER_ALERT_DEBUG, diff --git a/suricata.yaml.in b/suricata.yaml.in index ec951f290f..98449968f3 100644 --- a/suricata.yaml.in +++ b/suricata.yaml.in @@ -273,6 +273,7 @@ outputs: @rust_config_comment@- smb @rust_config_comment@- tftp @rust_config_comment@- ikev2 + @rust_config_comment@- krb5 - ssh - stats: totals: yes # stats for all threads merged together