suricata
output-json-tftp.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 ClĂ©ment Galland <clement.galland@epita.fr>
22  *
23  * Implement JSON/eve logging app-layer TFTP.
24  */
25 
26 
27 #include "suricata-common.h"
28 #include "debug.h"
29 #include "detect.h"
30 #include "pkt-var.h"
31 #include "conf.h"
32 
33 #include "threads.h"
34 #include "threadvars.h"
35 #include "tm-threads.h"
36 
37 #include "util-unittest.h"
38 #include "util-buffer.h"
39 #include "util-debug.h"
40 #include "util-byte.h"
41 
42 #include "output.h"
43 #include "output-json.h"
44 
45 #include "app-layer.h"
46 #include "app-layer-parser.h"
47 
48 #include "app-layer-tftp.h"
49 #include "output-json-tftp.h"
50 
51 #include "rust.h"
52 
53 typedef struct LogTFTPFileCtx_ {
55  uint32_t flags;
58 
59 typedef struct LogTFTPLogThread_ {
64 
65 static int JsonTFTPLogger(ThreadVars *tv, void *thread_data,
66  const Packet *p, Flow *f, void *state, void *tx, uint64_t tx_id)
67 {
68  LogTFTPLogThread *thread = thread_data;
69 
70  JsonBuilder *jb = CreateEveHeader(p, LOG_DIR_PACKET, "tftp", NULL);
71  if (unlikely(jb == NULL)) {
72  return TM_ECODE_FAILED;
73  }
74 
75  jb_open_object(jb, "tftp");
76  if (unlikely(!rs_tftp_log_json_request(tx, jb))) {
77  goto error;
78  }
79  jb_close(jb);
80 
81  EveAddCommonOptions(&thread->tftplog_ctx->cfg, p, f, jb);
82  MemBufferReset(thread->buffer);
83  OutputJsonBuilderBuffer(jb, thread->file_ctx, &thread->buffer);
84 
85  jb_free(jb);
86  return TM_ECODE_OK;
87 
88 error:
89  jb_free(jb);
90  return TM_ECODE_FAILED;
91 }
92 
93 static void OutputTFTPLogDeInitCtxSub(OutputCtx *output_ctx)
94 {
95  LogTFTPFileCtx *tftplog_ctx = (LogTFTPFileCtx *)output_ctx->data;
96  SCFree(tftplog_ctx);
97  SCFree(output_ctx);
98 }
99 
100 static OutputInitResult OutputTFTPLogInitSub(ConfNode *conf,
101  OutputCtx *parent_ctx)
102 {
103  OutputInitResult result = { NULL, false };
104  OutputJsonCtx *ajt = parent_ctx->data;
105 
106  LogTFTPFileCtx *tftplog_ctx = SCCalloc(1, sizeof(*tftplog_ctx));
107  if (unlikely(tftplog_ctx == NULL)) {
108  return result;
109  }
110  tftplog_ctx->file_ctx = ajt->file_ctx;
111  tftplog_ctx->cfg = ajt->cfg;
112 
113  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
114  if (unlikely(output_ctx == NULL)) {
115  SCFree(tftplog_ctx);
116  return result;
117  }
118  output_ctx->data = tftplog_ctx;
119  output_ctx->DeInit = OutputTFTPLogDeInitCtxSub;
120 
121  SCLogDebug("TFTP log sub-module initialized.");
122 
124 
125  result.ctx = output_ctx;
126  result.ok = true;
127  return result;
128 }
129 
130 static TmEcode JsonTFTPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
131 {
132  LogTFTPLogThread *thread = SCCalloc(1, sizeof(*thread));
133  if (unlikely(thread == NULL)) {
134  return TM_ECODE_FAILED;
135  }
136 
137  if (initdata == NULL) {
138  SCLogDebug("Error getting context for EveLogTFTP. \"initdata\" is NULL.");
139  goto error_exit;
140  }
141 
143  if (unlikely(thread->buffer == NULL)) {
144  goto error_exit;
145  }
146 
147  thread->tftplog_ctx = ((OutputCtx *)initdata)->data;
148  thread->file_ctx = LogFileEnsureExists(thread->tftplog_ctx->file_ctx, t->id);
149  if (!thread->file_ctx) {
150  goto error_exit;
151  }
152  *data = (void *)thread;
153 
154  return TM_ECODE_OK;
155 
156 error_exit:
157  if (thread->buffer != NULL) {
158  MemBufferFree(thread->buffer);
159  }
160  SCFree(thread);
161  return TM_ECODE_FAILED;
162 }
163 
164 static TmEcode JsonTFTPLogThreadDeinit(ThreadVars *t, void *data)
165 {
166  LogTFTPLogThread *thread = (LogTFTPLogThread *)data;
167  if (thread == NULL) {
168  return TM_ECODE_OK;
169  }
170  if (thread->buffer != NULL) {
171  MemBufferFree(thread->buffer);
172  }
173  SCFree(thread);
174  return TM_ECODE_OK;
175 }
176 
178 {
179  /* Register as an eve sub-module. */
180  OutputRegisterTxSubModule(LOGGER_JSON_TFTP, "eve-log", "JsonTFTPLog",
181  "eve-log.tftp", OutputTFTPLogInitSub,
182  ALPROTO_TFTP, JsonTFTPLogger,
183  JsonTFTPLogThreadInit, JsonTFTPLogThreadDeinit,
184  NULL);
185 
186  SCLogDebug("TFTP JSON logger registered.");
187 }
util-byte.h
tm-threads.h
app-layer-tftp.h
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:104
LogTFTPLogThread
struct LogTFTPLogThread_ LogTFTPLogThread
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
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
OutputJsonCommonSettings_
Definition: output-json.h:91
LogFileCtx_
Definition: util-logopenfile.h:60
LogTFTPFileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-tftp.c:56
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:978
LogTFTPLogThread_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-tftp.c:60
rust.h
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:444
LogTFTPFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-tftp.c:54
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
output-json-tftp.h
util-unittest.h
LogTFTPLogThread_
Definition: output-json-tftp.c:59
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:78
util-debug.h
OutputInitResult_::ctx
OutputCtx * ctx
Definition: output.h:44
JsonTFTPLogRegister
void JsonTFTPLogRegister(void)
Definition: output-json-tftp.c:177
CreateEveHeader
JsonBuilder * CreateEveHeader(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr)
Definition: output-json.c:846
output-json.h
LogTFTPFileCtx_
Definition: output-json-tftp.c:53
LogTFTPLogThread_::buffer
MemBuffer * buffer
Definition: output-json-tftp.c:62
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:456
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
pkt-var.h
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
Packet_
Definition: decode.h:414
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:79
ALPROTO_TFTP
@ ALPROTO_TFTP
Definition: app-layer-protos.h:48
MemBuffer_
Definition: util-buffer.h:27
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:43
LOG_DIR_PACKET
@ LOG_DIR_PACKET
Definition: output-json.h:39
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
LogTFTPFileCtx
struct LogTFTPFileCtx_ LogTFTPFileCtx
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
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
util-buffer.h
LogTFTPFileCtx_::flags
uint32_t flags
Definition: output-json-tftp.c:55
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:102
LOGGER_JSON_TFTP
@ LOGGER_JSON_TFTP
Definition: suricata-common.h:454
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
LogTFTPLogThread_::tftplog_ctx
LogTFTPFileCtx * tftplog_ctx
Definition: output-json-tftp.c:61
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h