suricata
output-json-rdp.c
Go to the documentation of this file.
1 /* Copyright (C) 2019 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 Zach Kelly <zach.kelly@lmco.com>
22  *
23  * Application layer logger for RDP
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 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 #include "util-unittest.h"
35 #include "util-buffer.h"
36 #include "util-debug.h"
37 #include "util-byte.h"
38 #include "output.h"
39 #include "output-json.h"
40 #include "app-layer.h"
41 #include "app-layer-parser.h"
42 #include "app-layer-rdp.h"
43 #include "output-json-rdp.h"
44 #include "rust-rdp-log-gen.h"
45 
46 typedef struct LogRdpFileCtx_ {
48  uint32_t flags;
50 
51 typedef struct LogRdpLogThread_ {
53  uint32_t count;
56 
57 static int JsonRdpLogger(ThreadVars *tv, void *thread_data,
58  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
59 {
60  LogRdpLogThread *thread = thread_data;
61 
62  json_t *js = CreateJSONHeader(p, LOG_DIR_PACKET, "rdp");
63  if (unlikely(js == NULL)) {
64  return TM_ECODE_FAILED;
65  }
66 
67  json_t *rdp_js = rs_rdp_to_json(tx);
68  if (unlikely(rdp_js == NULL)) {
69  goto error;
70  }
71  json_object_set_new(js, "rdp", rdp_js);
72 
73  MemBufferReset(thread->buffer);
74  OutputJSONBuffer(js, thread->rdplog_ctx->file_ctx, &thread->buffer);
75  json_decref(js);
76 
77  return TM_ECODE_OK;
78 
79 error:
80  json_decref(js);
81  return TM_ECODE_FAILED;
82 }
83 
84 static void OutputRdpLogDeInitCtxSub(OutputCtx *output_ctx)
85 {
86  LogRdpFileCtx *rdplog_ctx = (LogRdpFileCtx *)output_ctx->data;
87  SCFree(rdplog_ctx);
88  SCFree(output_ctx);
89 }
90 
91 static OutputInitResult OutputRdpLogInitSub(ConfNode *conf,
92  OutputCtx *parent_ctx)
93 {
94  OutputInitResult result = { NULL, false };
95  OutputJsonCtx *ajt = parent_ctx->data;
96 
97  LogRdpFileCtx *rdplog_ctx = SCCalloc(1, sizeof(*rdplog_ctx));
98  if (unlikely(rdplog_ctx == NULL)) {
99  return result;
100  }
101  rdplog_ctx->file_ctx = ajt->file_ctx;
102 
103  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
104  if (unlikely(output_ctx == NULL)) {
105  SCFree(rdplog_ctx);
106  return result;
107  }
108  output_ctx->data = rdplog_ctx;
109  output_ctx->DeInit = OutputRdpLogDeInitCtxSub;
110 
112 
113  SCLogDebug("rdp log sub-module initialized.");
114 
115  result.ctx = output_ctx;
116  result.ok = true;
117  return result;
118 }
119 
120 static TmEcode JsonRdpLogThreadInit(ThreadVars *t, const void *initdata, void **data)
121 {
122  if (initdata == NULL) {
123  SCLogDebug("Error getting context for EveLogRdp. \"initdata\" is NULL.");
124  return TM_ECODE_FAILED;
125  }
126 
127  LogRdpLogThread *thread = SCCalloc(1, sizeof(*thread));
128  if (unlikely(thread == NULL)) {
129  return TM_ECODE_FAILED;
130  }
131 
132  thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE);
133  if (unlikely(thread->buffer == NULL)) {
134  SCFree(thread);
135  return TM_ECODE_FAILED;
136  }
137 
138  thread->rdplog_ctx = ((OutputCtx *)initdata)->data;
139  *data = (void *)thread;
140 
141  return TM_ECODE_OK;
142 }
143 
144 static TmEcode JsonRdpLogThreadDeinit(ThreadVars *t, void *data)
145 {
146  LogRdpLogThread *thread = (LogRdpLogThread *)data;
147  if (thread == NULL) {
148  return TM_ECODE_OK;
149  }
150  if (thread->buffer != NULL) {
151  MemBufferFree(thread->buffer);
152  }
153  SCFree(thread);
154  return TM_ECODE_OK;
155 }
156 
158 {
159  if (ConfGetNode("app-layer.protocols.rdp") == NULL) {
160  return;
161  }
162  /* Register as an eve sub-module. */
165  "eve-log",
166  "JsonRdpLog",
167  "eve-log.rdp",
168  OutputRdpLogInitSub,
169  ALPROTO_RDP,
170  JsonRdpLogger,
171  JsonRdpLogThreadInit,
172  JsonRdpLogThreadDeinit,
173  NULL
174  );
175 
176  SCLogDebug("rdp json logger registered.");
177 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define SCLogDebug(...)
Definition: util-debug.h:335
struct LogRdpFileCtx_ LogRdpFileCtx
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
#define SCCalloc(nm, a)
Definition: util-mem.h:253
struct LogRdpLogThread_ LogRdpLogThread
LogRdpFileCtx * rdplog_ctx
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
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:402
void JsonRdpLogRegister(void)
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:322
uint16_t tx_id
LogFileCtx * file_ctx
void * data
Definition: tm-modules.h:81
MemBuffer * buffer
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:176
Per thread variable structure.
Definition: threadvars.h:57
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82