suricata
output-json-krb5.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Pierre Chifflier <chifflier@wzdftpd.net>
22  *
23  * Implement JSON/eve logging app-layer KRB5.
24  */
25 
26 #include "suricata-common.h"
27 #include "debug.h"
28 #include "detect.h"
29 #include "pkt-var.h"
30 #include "conf.h"
31 
32 #include "threads.h"
33 #include "threadvars.h"
34 #include "tm-threads.h"
35 
36 #include "util-unittest.h"
37 #include "util-buffer.h"
38 #include "util-debug.h"
39 #include "util-byte.h"
40 
41 #include "output.h"
42 #include "output-json.h"
43 
44 #include "app-layer.h"
45 #include "app-layer-parser.h"
46 
47 #include "app-layer-krb5.h"
48 #include "output-json-krb5.h"
49 
50 #include "rust.h"
51 
52 typedef struct LogKRB5FileCtx_ {
56 
57 typedef struct LogKRB5LogThread_ {
61 
62 static int JsonKRB5Logger(ThreadVars *tv, void *thread_data,
63  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
64 {
65  KRB5Transaction *krb5tx = tx;
66  LogKRB5LogThread *thread = thread_data;
67  json_t *js, *krb5js;
68 
69  js = CreateJSONHeader(p, LOG_DIR_PACKET, "krb5", NULL);
70  if (unlikely(js == NULL)) {
71  return TM_ECODE_FAILED;
72  }
73 
74  JsonAddCommonOptions(&thread->krb5log_ctx->cfg, p, f, js);
75 
76  krb5js = rs_krb5_log_json_response(state, krb5tx);
77  if (unlikely(krb5js == NULL)) {
78  goto error;
79  }
80  json_object_set_new(js, "krb5", krb5js);
81 
82  MemBufferReset(thread->buffer);
83  OutputJSONBuffer(js, thread->krb5log_ctx->file_ctx, &thread->buffer);
84 
85  json_decref(js);
86  return TM_ECODE_OK;
87 
88 error:
89  json_decref(js);
90  return TM_ECODE_FAILED;
91 }
92 
93 static void OutputKRB5LogDeInitCtxSub(OutputCtx *output_ctx)
94 {
95  LogKRB5FileCtx *krb5log_ctx = (LogKRB5FileCtx *)output_ctx->data;
96  SCFree(krb5log_ctx);
97  SCFree(output_ctx);
98 }
99 
100 static OutputInitResult OutputKRB5LogInitSub(ConfNode *conf,
101  OutputCtx *parent_ctx)
102 {
103  OutputInitResult result = { NULL, false };
104  OutputJsonCtx *ajt = parent_ctx->data;
105 
106  LogKRB5FileCtx *krb5log_ctx = SCCalloc(1, sizeof(*krb5log_ctx));
107  if (unlikely(krb5log_ctx == NULL)) {
108  return result;
109  }
110  krb5log_ctx->file_ctx = ajt->file_ctx;
111  krb5log_ctx->cfg = ajt->cfg;
112 
113  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
114  if (unlikely(output_ctx == NULL)) {
115  SCFree(krb5log_ctx);
116  return result;
117  }
118  output_ctx->data = krb5log_ctx;
119  output_ctx->DeInit = OutputKRB5LogDeInitCtxSub;
120 
121  SCLogDebug("KRB5 log sub-module initialized.");
122 
125 
126  result.ctx = output_ctx;
127  result.ok = true;
128  return result;
129 }
130 
131 static TmEcode JsonKRB5LogThreadInit(ThreadVars *t, const void *initdata, void **data)
132 {
133  LogKRB5LogThread *thread = SCCalloc(1, sizeof(*thread));
134  if (unlikely(thread == NULL)) {
135  return TM_ECODE_FAILED;
136  }
137 
138  if (initdata == NULL) {
139  SCLogDebug("Error getting context for EveLogKRB5. \"initdata\" is NULL.");
140  SCFree(thread);
141  return TM_ECODE_FAILED;
142  }
143 
145  if (unlikely(thread->buffer == NULL)) {
146  SCFree(thread);
147  return TM_ECODE_FAILED;
148  }
149 
150  thread->krb5log_ctx = ((OutputCtx *)initdata)->data;
151  *data = (void *)thread;
152 
153  return TM_ECODE_OK;
154 }
155 
156 static TmEcode JsonKRB5LogThreadDeinit(ThreadVars *t, void *data)
157 {
158  LogKRB5LogThread *thread = (LogKRB5LogThread *)data;
159  if (thread == NULL) {
160  return TM_ECODE_OK;
161  }
162  if (thread->buffer != NULL) {
163  MemBufferFree(thread->buffer);
164  }
165  SCFree(thread);
166  return TM_ECODE_OK;
167 }
168 
170 {
171  /* Register as an eve sub-module. */
172  OutputRegisterTxSubModule(LOGGER_JSON_KRB5, "eve-log", "JsonKRB5Log",
173  "eve-log.krb5", OutputKRB5LogInitSub, ALPROTO_KRB5,
174  JsonKRB5Logger, JsonKRB5LogThreadInit,
175  JsonKRB5LogThreadDeinit, NULL);
176 
177  SCLogDebug("KRB5 JSON logger registered.");
178 }
util-byte.h
tm-threads.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:111
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
LogKRB5LogThread_::buffer
MemBuffer * buffer
Definition: output-json-krb5.c:59
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:62
threads.h
OutputJsonCtx_
Definition: output-json.h:108
Flow_
Flow data structure.
Definition: flow.h:343
OutputJsonCommonSettings_
Definition: output-json.h:99
LogFileCtx_
Definition: util-logopenfile.h:52
output-json-krb5.h
LogKRB5LogThread_::krb5log_ctx
LogKRB5FileCtx * krb5log_ctx
Definition: output-json-krb5.c:58
rust.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
LogKRB5FileCtx
struct LogKRB5FileCtx_ LogKRB5FileCtx
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
ALPROTO_KRB5
@ ALPROTO_KRB5
Definition: app-layer-protos.h:50
OutputCtx_
Definition: tm-modules.h:78
LogKRB5FileCtx_
Definition: output-json-krb5.c:52
app-layer-krb5.h
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
Packet_
Definition: decode.h:411
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
MemBuffer_
Definition: util-buffer.h:27
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
LogKRB5FileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-krb5.c:53
OutputInitResult_
Definition: output.h:41
LogKRB5LogThread
struct LogKRB5LogThread_ LogKRB5LogThread
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:38
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
KRB5Transaction
struct KRB5Transaction_ KRB5Transaction
Definition: app-layer-krb5.h:32
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
JsonAddCommonOptions
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:662
OutputRegisterTxSubModule
void OutputRegisterTxSubModule(LoggerId id, const char *parent_name, const char *name, const char *conf_name, OutputInitSubFunc InitFunc, AppProto alproto, TxLogger TxLogFunc, ThreadInitFunc ThreadInit, ThreadDeinitFunc ThreadDeinit, ThreadExitPrintStatsFunc ThreadExitPrintStats)
Definition: output.c:410
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
OutputJSONBuffer
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:1402
LogKRB5FileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-krb5.c:54
LogKRB5LogThread_
Definition: output-json-krb5.c:57
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
LOGGER_JSON_KRB5
@ LOGGER_JSON_KRB5
Definition: suricata-common.h:461
JsonKRB5LogRegister
void JsonKRB5LogRegister(void)
Definition: output-json-krb5.c:169
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h
CreateJSONHeader
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:1204