]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
app-layer: template for application layer tx logger
authorJason Ish <ish@unx.ca>
Tue, 1 Sep 2015 18:12:28 +0000 (12:12 -0600)
committerJason Ish <ish@unx.ca>
Fri, 11 Sep 2015 05:55:02 +0000 (23:55 -0600)
src/Makefile.am
src/output-json-template.c [new file with mode: 0644]
src/output-json-template.h [new file with mode: 0644]
src/suricata.c
src/tm-modules.c
src/tm-threads-common.h

index 97a3a993633fb0d0dc5a9d0fa1e902cea627da42..9d3809c2f4036ea54441ab2eefe31829c3817b07 100644 (file)
@@ -251,6 +251,7 @@ output-json-smtp.c output-json-smtp.h \
 output-json-ssh.c output-json-ssh.h \
 output-json-stats.c output-json-stats.h \
 output-json-tls.c output-json-tls.h \
+output-json-template.c output-json-template.h \
 output-lua.c output-lua.h \
 output-packet.c output-packet.h \
 output-stats.c output-stats.h \
diff --git a/src/output-json-template.c b/src/output-json-template.c
new file mode 100644 (file)
index 0000000..ca4a937
--- /dev/null
@@ -0,0 +1,213 @@
+/* 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.
+ */
+
+#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-template.h"
+
+#ifdef HAVE_LIBJANSSON
+#include <jansson.h>
+
+typedef struct LogTemplateFileCtx_ {
+    LogFileCtx *file_ctx;
+    uint32_t    flags;
+} LogTemplateFileCtx;
+
+typedef struct LogTemplateLogThread_ {
+    LogTemplateFileCtx *templatelog_ctx;
+    uint32_t            count;
+    MemBuffer          *buffer;
+} LogTemplateLogThread;
+
+static int JsonTemplateLogger(ThreadVars *tv, void *thread_data,
+    const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
+{
+    TemplateTransaction *templatetx = tx;
+    LogTemplateLogThread *thread = thread_data;
+    MemBuffer *buffer = thread->buffer;
+    json_t *js, *templatejs;
+
+    SCLogNotice("Logging template transaction %"PRIu64".", templatetx->tx_id);
+    
+    js = CreateJSONHeader((Packet *)p, 0, "template");
+    if (unlikely(js == NULL)) {
+        return TM_ECODE_FAILED;
+    }
+
+    templatejs = json_object();
+    if (unlikely(templatejs == NULL)) {
+        goto error;
+    }
+
+    /* Convert the request buffer to a string then log. */
+    char *request_buffer = BytesToString(templatetx->request_buffer,
+        templatetx->request_buffer_len);
+    if (request_buffer != NULL) {
+        json_object_set_new(templatejs, "request", json_string(request_buffer));
+        SCFree(request_buffer);
+    }
+
+    /* Convert the response buffer to a string then log. */
+    char *response_buffer = BytesToString(templatetx->response_buffer,
+        templatetx->response_buffer_len);
+    if (response_buffer != NULL) {
+        json_object_set_new(templatejs, "response",
+            json_string(response_buffer));
+        SCFree(response_buffer);
+    }
+
+    json_object_set_new(js, "template", templatejs);
+
+    MemBufferReset(buffer);
+    OutputJSONBuffer(js, thread->templatelog_ctx->file_ctx, buffer);
+
+    json_decref(js);
+    return TM_ECODE_OK;
+    
+error:
+    if (templatejs != NULL) {
+        json_decref(templatejs);
+    }
+    json_decref(js);
+    return TM_ECODE_FAILED;
+}
+
+static void OutputTemplateLogDeInitCtxSub(OutputCtx *output_ctx)
+{
+    LogTemplateFileCtx *templatelog_ctx = (LogTemplateFileCtx *)output_ctx->data;
+    SCFree(templatelog_ctx);
+    SCFree(output_ctx);
+}
+
+static OutputCtx *OutputTemplateLogInitSub(ConfNode *conf,
+    OutputCtx *parent_ctx)
+{
+    AlertJsonThread *ajt = parent_ctx->data;
+
+    LogTemplateFileCtx *templatelog_ctx = SCCalloc(1, sizeof(*templatelog_ctx));
+    if (unlikely(templatelog_ctx == NULL)) {
+        return NULL;
+    }
+    templatelog_ctx->file_ctx = ajt->file_ctx;
+
+    OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
+    if (unlikely(output_ctx == NULL)) {
+        SCFree(templatelog_ctx);
+        return NULL;
+    }
+    output_ctx->data = templatelog_ctx;
+    output_ctx->DeInit = OutputTemplateLogDeInitCtxSub;
+
+    SCLogNotice("Template log sub-module initialized.");
+
+    AppLayerParserRegisterLogger(IPPROTO_TCP, ALPROTO_TEMPLATE);
+
+    return output_ctx;
+}
+
+#define OUTPUT_BUFFER_SIZE 65535
+
+static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, void *initdata, void **data)
+{
+    LogTemplateLogThread *thread = SCCalloc(1, sizeof(*thread));
+    if (unlikely(thread == NULL)) {
+        return TM_ECODE_FAILED;
+    }
+
+    if (initdata == NULL) {
+        SCLogDebug("Error getting context for Template.  \"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->templatelog_ctx = ((OutputCtx *)initdata)->data;
+    *data = (void *)thread;
+
+    return TM_ECODE_OK;
+}
+
+static TmEcode JsonTemplateLogThreadDeinit(ThreadVars *t, void *data)
+{
+    LogTemplateLogThread *thread = (LogTemplateLogThread *)data;
+    if (thread == NULL) {
+        return TM_ECODE_OK;
+    }
+    if (thread->buffer != NULL) {
+        MemBufferFree(thread->buffer);
+    }
+    SCFree(thread);
+    return TM_ECODE_OK;
+}
+
+void TmModuleJsonTemplateLogRegister(void)
+{
+    tmm_modules[TMM_JSONTEMPLATELOG].name = "JsonTemplateLog";
+    tmm_modules[TMM_JSONTEMPLATELOG].ThreadInit = JsonTemplateLogThreadInit;
+    tmm_modules[TMM_JSONTEMPLATELOG].ThreadDeinit = JsonTemplateLogThreadDeinit;
+    tmm_modules[TMM_JSONTEMPLATELOG].RegisterTests = NULL;
+    tmm_modules[TMM_JSONTEMPLATELOG].cap_flags = 0;
+    tmm_modules[TMM_JSONTEMPLATELOG].flags = TM_FLAG_LOGAPI_TM;
+
+    /* Register as an eve sub-module. */
+    OutputRegisterTxSubModule("eve-log", "JsonTemplateLog", "eve-log.template",
+        OutputTemplateLogInitSub, ALPROTO_TEMPLATE, JsonTemplateLogger);
+
+    SCLogNotice("Template JSON logger registered.");
+}
+
+#else /* No JSON support. */
+
+static TmEcode JsonTemplateLogThreadInit(ThreadVars *t, void *initdata,
+    void **data)
+{
+    SCLogInfo("Cannot initialize JSON output for template. "
+        "JSON support was disabled during build.");
+    return TM_ECODE_FAILED;
+}
+
+void TmModuleJsonTemplateLogRegister(void)
+{
+    tmm_modules[TMM_JSONTEMPLATELOG].name = "JsonTemplateLog";
+    tmm_modules[TMM_JSONTEMPLATELOG].ThreadInit = JsonTemplateLogThreadInit;
+}
+
+#endif /* HAVE_LIBJANSSON */
diff --git a/src/output-json-template.h b/src/output-json-template.h
new file mode 100644 (file)
index 0000000..d071e18
--- /dev/null
@@ -0,0 +1,23 @@
+/* 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.
+ */
+
+#ifndef __OUTPUT_JSON_TEMPLATE_H__
+#define __OUTPUT_JSON_TEMPLATE_H__
+
+void TmModuleJsonTemplateLogRegister(void);
+
+#endif /* __OUTPUT_JSON_TEMPLATE_H__ */
index 7c661e1938ac64c0c30f7bb49c52c1029d4028fa..6c45c57e99310c70bc592d4e45242e4e9e74b1c3 100644 (file)
 
 #include "output-json.h"
 
+#include "output-json-template.h"
+
 #include "stream-tcp.h"
 
 #include "source-nfq.h"
@@ -897,6 +899,9 @@ void RegisterAllModules()
     /* json stats */
     TmModuleJsonStatsLogRegister();
 
+    /* Template JSON logger. */
+    TmModuleJsonTemplateLogRegister();
+
     /* log api */
     TmModulePacketLoggerRegister();
     TmModuleTxLoggerRegister();
index 73e9f235fd9d19017866b45ce44f10e07dddc5ff..06190b6549058d85d12fa3f476d73d0a623c6128 100644 (file)
@@ -272,6 +272,7 @@ const char * TmModuleTmmIdToString(TmmId id)
         CASE_CODE (TMM_DETECTLOADER);
         CASE_CODE (TMM_LUALOG);
         CASE_CODE (TMM_LOGSTATSLOG);
+        CASE_CODE (TMM_JSONTEMPLATELOG);
         CASE_CODE (TMM_RECEIVENETMAP);
         CASE_CODE (TMM_DECODENETMAP);
         CASE_CODE (TMM_TLSSTORE);
index f6629eaa82e50f0f3e66805e3f5becade66030ba..6a66b41795f9bf561fd3c8640a2d1454ed25a802 100644 (file)
@@ -101,6 +101,7 @@ typedef enum {
     TMM_JSONFLOWLOG,
     TMM_JSONNETFLOWLOG,
     TMM_LOGSTATSLOG,
+    TMM_JSONTEMPLATELOG,
 
     TMM_FLOWMANAGER,
     TMM_FLOWRECYCLER,