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_ {
56  uint32_t count;
59 
60 json_t *JsonRFBAddMetadata(const Flow *f, uint64_t tx_id)
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);
67  }
68  }
69 
70  return NULL;
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  json_t *js = CreateJSONHeader(p, LOG_DIR_FLOW, "rfb");
79  if (unlikely(js == NULL)) {
80  return TM_ECODE_FAILED;
81  }
82 
83  json_t *rfb_js = rs_rfb_logger_log(NULL, tx);
84  if (unlikely(rfb_js == NULL)) {
85  goto error;
86  }
87  json_object_set_new(js, "rfb", rfb_js);
88 
89  MemBufferReset(thread->buffer);
90  OutputJSONBuffer(js, thread->rfblog_ctx->file_ctx, &thread->buffer);
91  json_decref(js);
92 
93  return TM_ECODE_OK;
94 
95 error:
96  json_decref(js);
97  return TM_ECODE_FAILED;
98 }
99 
100 static void OutputRFBLogDeInitCtxSub(OutputCtx *output_ctx)
101 {
102  LogRFBFileCtx *rfblog_ctx = (LogRFBFileCtx *)output_ctx->data;
103  SCFree(rfblog_ctx);
104  SCFree(output_ctx);
105 }
106 
107 static OutputInitResult OutputRFBLogInitSub(ConfNode *conf,
108  OutputCtx *parent_ctx)
109 {
110  OutputInitResult result = { NULL, false };
111  OutputJsonCtx *ajt = parent_ctx->data;
112 
113  LogRFBFileCtx *rfblog_ctx = SCCalloc(1, sizeof(*rfblog_ctx));
114  if (unlikely(rfblog_ctx == NULL)) {
115  return result;
116  }
117  rfblog_ctx->file_ctx = ajt->file_ctx;
118 
119  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
120  if (unlikely(output_ctx == NULL)) {
121  SCFree(rfblog_ctx);
122  return result;
123  }
124  output_ctx->data = rfblog_ctx;
125  output_ctx->DeInit = OutputRFBLogDeInitCtxSub;
126 
128 
129  result.ctx = output_ctx;
130  result.ok = true;
131  return result;
132 }
133 
134 static TmEcode JsonRFBLogThreadInit(ThreadVars *t, const void *initdata, void **data)
135 {
136  if (initdata == NULL) {
137  SCLogDebug("Error getting context for EveLogRFB. \"initdata\" is NULL.");
138  return TM_ECODE_FAILED;
139  }
140 
141  LogRFBLogThread *thread = SCCalloc(1, sizeof(*thread));
142  if (unlikely(thread == NULL)) {
143  return TM_ECODE_FAILED;
144  }
145 
147  if (unlikely(thread->buffer == NULL)) {
148  SCFree(thread);
149  return TM_ECODE_FAILED;
150  }
151 
152  thread->rfblog_ctx = ((OutputCtx *)initdata)->data;
153  *data = (void *)thread;
154 
155  return TM_ECODE_OK;
156 }
157 
158 static TmEcode JsonRFBLogThreadDeinit(ThreadVars *t, void *data)
159 {
160  LogRFBLogThread *thread = (LogRFBLogThread *)data;
161  if (thread == NULL) {
162  return TM_ECODE_OK;
163  }
164  if (thread->buffer != NULL) {
165  MemBufferFree(thread->buffer);
166  }
167  SCFree(thread);
168  return TM_ECODE_OK;
169 }
170 
172 {
173  /* Register as an eve sub-module. */
175  "JsonRFBLog", "eve-log.rfb",
176  OutputRFBLogInitSub, ALPROTO_RFB, JsonRFBLogger,
177  JsonRFBLogThreadInit, JsonRFBLogThreadDeinit, NULL);
178 }
util-byte.h
tm-threads.h
SCFree
#define SCFree(a)
Definition: util-mem.h:322
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
Flow_::proto
uint8_t proto
Definition: flow.h:361
JSON_OUTPUT_BUFFER_SIZE
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
threads.h
OutputJsonCtx_
Definition: output-json.h:78
Flow_
Flow data structure.
Definition: flow.h:342
LogFileCtx_
Definition: util-logopenfile.h:52
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
util-unittest.h
FlowGetAppState
void * FlowGetAppState(const Flow *f)
Definition: flow.c:1087
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
LOGGER_JSON_RFB
@ LOGGER_JSON_RFB
Definition: suricata-common.h:443
LogRFBLogThread
struct LogRFBLogThread_ LogRFBLogThread
OutputCtx_
Definition: tm-modules.h:78
LogRFBLogThread_
Definition: output-json-rfb.c:54
JsonRFBAddMetadata
json_t * JsonRFBAddMetadata(const Flow *f, uint64_t tx_id)
Definition: output-json-rfb.c:60
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:42
output-json-rfb.h
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:471
app-layer-rfb.h
LogRFBLogThread_::count
uint32_t count
Definition: output-json-rfb.c:56
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
OutputInitResult_::ok
bool ok
Definition: output.h:43
app-layer-parser.h
LogRFBFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-rfb.c:50
Packet_
Definition: decode.h:408
SCCalloc
#define SCCalloc(nm, a)
Definition: util-mem.h:253
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
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:1047
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
LogRFBFileCtx
struct LogRFBFileCtx_ LogRFBFileCtx
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
CreateJSONHeader
json_t * CreateJSONHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type)
Definition: output-json.c:710
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:411
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:38
ConfNode_
Definition: conf.h:32
util-buffer.h
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:79
JsonRFBLogRegister
void JsonRFBLogRegister(void)
Definition: output-json-rfb.c:171
OutputJSONBuffer
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
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
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