]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
Add logger for Kerberos 5 metadata
authorPierre Chifflier <chifflier@wzdftpd.net>
Tue, 17 Apr 2018 05:50:43 +0000 (07:50 +0200)
committerPierre Chifflier <chifflier@wzdftpd.net>
Wed, 13 Jun 2018 08:25:40 +0000 (10:25 +0200)
rust/src/krb/log.rs [new file with mode: 0644]
rust/src/krb/mod.rs
src/Makefile.am
src/output-json-krb5.c [new file with mode: 0644]
src/output-json-krb5.h [new file with mode: 0644]
src/output.c
src/suricata-common.h
suricata.yaml.in

diff --git a/rust/src/krb/log.rs b/rust/src/krb/log.rs
new file mode 100644 (file)
index 0000000..0946239
--- /dev/null
@@ -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  <chifflier@wzdftpd.net>
+
+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        => "<empty>".to_owned(),
+    };
+    let realm = match tx.realm {
+        Some(ref x) => format!("{}", x.0),
+        None        => "<empty>".to_owned(),
+    };
+    // XXX PrincipalName object should be pretty-printed
+    let sname = match tx.sname {
+        Some(ref x) => format!("{}", x),
+        None        => "<empty>".to_owned(),
+    };
+    let encryption = match tx.etype {
+        Some(ref x) => format!("{:?}", x),
+        None        => "<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();
+}
+
index 735c71c1d2b84c71219066a14d2c6b5ba90b8b59..fea4d5590696449f41c02aa823be563d159c4a97 100644 (file)
@@ -18,3 +18,4 @@
 // written by Pierre Chifflier  <chifflier@wzdftpd.net>
 
 pub mod krb5;
+pub mod log;
index 5cb872a0344f08547b5dc9db9ced9e6236c5aaac..38aa0f82e82cfeee1eb18acd1812095907011297 100644 (file)
@@ -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 (file)
index 0000000..84b592c
--- /dev/null
@@ -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 <chifflier@wzdftpd.net>
+ *
+ * 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 (file)
index 0000000..2f86088
--- /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 FirstName LastName <name@domain>
+ */
+
+#ifndef __OUTPUT_JSON_KRB5_H__
+#define __OUTPUT_JSON_KRB5_H__
+
+void JsonKRB5LogRegister(void);
+
+#endif /* __OUTPUT_JSON_KRB5_H__ */
index debbc92b6cd407ec0038ab9b11ba91feffb0c9dc..dd4fa54ac9615b12fa51baeb91ded420565e720c 100644 (file)
@@ -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();
 }
index 03f769dd5c967f2f99bf6fa3d72965c24c0b571e..d57b3ba52233540ef6c1b5cd65ee5e256179327f 100644 (file)
@@ -412,6 +412,7 @@ typedef enum {
     LOGGER_JSON_SSH,
     LOGGER_JSON_SMB,
     LOGGER_JSON_IKEV2,
+    LOGGER_JSON_KRB5,
     LOGGER_JSON_TEMPLATE,
 
     LOGGER_ALERT_DEBUG,
index ec951f290f4b5e48d2fcfa13891fd4a565424441..98449968f3ecdc05dccaff5205f8a14794cc5172 100644 (file)
@@ -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