suricata
output-json-rfb.c
Go to the documentation of this file.
1 /* Copyright (C) 2020 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 Frank Honza <frank.honza@dcso.de>
22  *
23  * Implement JSON/eve logging app-layer RFB.
24  */
25 
26 #include "suricata-common.h"
27 #include "conf.h"
28 
29 #include "threads.h"
30 #include "threadvars.h"
31 #include "tm-threads.h"
32 
33 #include "util-unittest.h"
34 #include "util-buffer.h"
35 #include "util-debug.h"
36 #include "util-byte.h"
37 
38 #include "output.h"
39 #include "output-json.h"
40 
41 #include "app-layer.h"
42 #include "app-layer-parser.h"
43 
44 #include "app-layer-rfb.h"
45 #include "output-json-rfb.h"
46 
47 #include "rust-bindings.h"
48 
49 typedef struct LogRFBFileCtx_ {
51  uint32_t flags;
53 
54 typedef struct LogRFBLogThread_ {
59 
60 bool JsonRFBAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
61 {
62  RFBState *state = FlowGetAppState(f);
63  if (state) {
64  RFBTransaction *tx = AppLayerParserGetTx(f->proto, ALPROTO_RFB, state, tx_id);
65  if (tx) {
66  return rs_rfb_logger_log(state, tx, js);
67  }
68  }
69 
70  return false;
71 }
72 
73 static int JsonRFBLogger(ThreadVars *tv, void *thread_data,
74  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
75 {
76  LogRFBLogThread *thread = thread_data;
77 
78  JsonBuilder *js = CreateEveHeader(p, LOG_DIR_FLOW, "rfb", NULL);
79  if (unlikely(js == NULL)) {
80  return TM_ECODE_FAILED;
81  }
82 
83  if (!rs_rfb_logger_log(NULL, tx, js)) {
84  goto error;
85  }
86 
87  MemBufferReset(thread->buffer);
88  OutputJsonBuilderBuffer(js, thread->file_ctx, &thread->buffer);
89  jb_free(js);
90 
91  return TM_ECODE_OK;
92 
93 error:
94  jb_free(js);
95  return TM_ECODE_FAILED;
96 }
97 
98 static void OutputRFBLogDeInitCtxSub(OutputCtx *output_ctx)
99 {
100  LogRFBFileCtx *rfblog_ctx = (LogRFBFileCtx *)output_ctx->data;
101  SCFree(rfblog_ctx);
102  SCFree(output_ctx);
103 }
104 
105 static OutputInitResult OutputRFBLogInitSub(ConfNode *conf,
106  OutputCtx *parent_ctx)
107 {
108  OutputInitResult result = { NULL, false };
109  OutputJsonCtx *ajt = parent_ctx->data;
110 
111  LogRFBFileCtx *rfblog_ctx = SCCalloc(1, sizeof(*rfblog_ctx));
112  if (unlikely(rfblog_ctx == NULL)) {
113  return result;
114  }
115  rfblog_ctx->file_ctx = ajt->file_ctx;
116 
117  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
118  if (unlikely(output_ctx == NULL)) {
119  SCFree(rfblog_ctx);
120  return result;
121  }
122  output_ctx->data = rfblog_ctx;
123  output_ctx->DeInit = OutputRFBLogDeInitCtxSub;
124 
126 
127  result.ctx = output_ctx;
128  result.ok = true;
129  return result;
130 }
131 
132 static TmEcode JsonRFBLogThreadInit(ThreadVars *t, const void *initdata, void **data)
133 {
134  if (initdata == NULL) {
135  SCLogDebug("Error getting context for EveLogRFB. \"initdata\" is NULL.");
136  return TM_ECODE_FAILED;
137  }
138 
139  LogRFBLogThread *thread = SCCalloc(1, sizeof(*thread));
140  if (unlikely(thread == NULL)) {
141  return TM_ECODE_FAILED;
142  }
143 
145  if (unlikely(thread->buffer == NULL)) {
146  goto error_exit;
147  }
148 
149  thread->rfblog_ctx = ((OutputCtx *)initdata)->data;
150  thread->file_ctx = LogFileEnsureExists(thread->rfblog_ctx->file_ctx, t->id);
151  if (!thread->file_ctx) {
152  goto error_exit;
153  }
154  *data = (void *)thread;
155 
156  return TM_ECODE_OK;
157 
158 error_exit:
159  if (thread->buffer != NULL) {
160  MemBufferFree(thread->buffer);
161  }
162  SCFree(thread);
163  return TM_ECODE_FAILED;
164 }
165 
166 static TmEcode JsonRFBLogThreadDeinit(ThreadVars *t, void *data)
167 {
168  LogRFBLogThread *thread = (LogRFBLogThread *)data;
169  if (thread == NULL) {
170  return TM_ECODE_OK;
171  }
172  if (thread->buffer != NULL) {
173  MemBufferFree(thread->buffer);
174  }
175  SCFree(thread);
176  return TM_ECODE_OK;
177 }
178 
180 {
181  /* Register as an eve sub-module. */
183  "JsonRFBLog", "eve-log.rfb",
184  OutputRFBLogInitSub, ALPROTO_RFB, JsonRFBLogger,
185  JsonRFBLogThreadInit, JsonRFBLogThreadDeinit, NULL);
186 }
util-byte.h
tm-threads.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
Flow_::proto
uint8_t proto
Definition: flow.h:365
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:63
threads.h
OutputJsonCtx_
Definition: output-json.h:101
Flow_
Flow data structure.
Definition: flow.h:347
LogFileCtx_
Definition: util-logopenfile.h:60
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1102
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
LOGGER_JSON_RFB
@ LOGGER_JSON_RFB
Definition: suricata-common.h:466
LogRFBLogThread
struct LogRFBLogThread_ LogRFBLogThread
OutputCtx_
Definition: tm-modules.h:78
LogRFBLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-rfb.c:56
LogRFBLogThread_
Definition: output-json-rfb.c:54
util-debug.h
JsonRFBAddMetadata
bool JsonRFBAddMetadata(const Flow *f, uint64_t tx_id, JsonBuilder *js)
Definition: output-json-rfb.c:60
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
output-json-rfb.h
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:846
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:456
app-layer-rfb.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
LogFileEnsureExists
LogFileCtx * LogFileEnsureExists(LogFileCtx *parent_ctx, int thread_id)
LogFileEnsureExists() Ensure a log file context for the thread exists.
Definition: util-logopenfile.c:659
OutputInitResult_::ok
bool ok
Definition: output.h:45
app-layer-parser.h
ThreadVars_::id
int id
Definition: threadvars.h:87
LogRFBFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-rfb.c:50
Packet_
Definition: decode.h:414
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
MemBuffer_
Definition: util-buffer.h:27
AppLayerParserGetTx
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
Definition: app-layer-parser.c:1032
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
LogRFBFileCtx
struct LogRFBFileCtx_ LogRFBFileCtx
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
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:409
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
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:102
JsonRFBLogRegister
void JsonRFBLogRegister(void)
Definition: output-json-rfb.c:179
LogRFBLogThread_::buffer
MemBuffer * buffer
Definition: output-json-rfb.c:57
ALPROTO_RFB
@ ALPROTO_RFB
Definition: app-layer-protos.h:54
LogRFBFileCtx_::flags
uint32_t flags
Definition: output-json-rfb.c:51
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
LogRFBFileCtx_
Definition: output-json-rfb.c:49
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
output.h
LogRFBLogThread_::rfblog_ctx
LogRFBFileCtx * rfblog_ctx
Definition: output-json-rfb.c:55
app-layer.h