suricata
output-json-ftp.c
Go to the documentation of this file.
1 /* Copyright (C) 2017 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 Jeff Lucovsky <jeff@lucovsky.org>
22  *
23  * Implement JSON/eve logging app-layer FTP.
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-mem.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-ftp.h"
49 #include "output-json-ftp.h"
50 
51 typedef struct LogFTPFileCtx_ {
55 
56 typedef struct LogFTPLogThread_ {
58  uint32_t count;
61 
62 static void EveFTPLogCommand(Flow *f, FTPTransaction *tx, JsonBuilder *jb)
63 {
64  /* Preallocate array objects to simplify failure case */
65  JsonBuilder *js_resplist = NULL;
66  if (!TAILQ_EMPTY(&tx->response_list)) {
67  js_resplist = jb_new_array();
68 
69  if (unlikely(js_resplist == NULL)) {
70  goto fail;
71  }
72  }
73  jb_set_string(jb, "command", tx->command_descriptor->command_name);
74  uint32_t min_length = tx->command_descriptor->command_length + 1; /* command + space */
75  if (tx->request_length > min_length) {
76  jb_set_string_from_bytes(jb,
77  "command_data",
78  (const uint8_t *)tx->request + min_length,
79  tx->request_length - min_length - 1);
80  }
81 
82  if (!TAILQ_EMPTY(&tx->response_list)) {
83  int resp_code_cnt = 0;
84  int resp_cnt = 0;
85  FTPString *response;
86  bool is_cc_array_open = false;
87  TAILQ_FOREACH(response, &tx->response_list, next) {
88  /* handle multiple lines within the response, \r\n delimited */
89  uint8_t *where = response->str;
90  uint16_t length = response->len ? response->len -1 : 0;
91  uint16_t pos;
92  while ((pos = JsonGetNextLineFromBuffer((const char *)where, length)) != UINT16_MAX) {
93  uint16_t offset = 0;
94  /* Try to find a completion code for this line */
95  if (pos >= 3) {
96  /* Gather the completion code if present */
97  if (isdigit(where[0]) && isdigit(where[1]) && isdigit(where[2])) {
98  if (!is_cc_array_open) {
99  jb_open_array(jb, "completion_code");
100  is_cc_array_open = true;
101  }
102  jb_append_string_from_bytes(jb, (const uint8_t *)where, 3);
103  resp_code_cnt++;
104  offset = 4;
105  }
106  }
107  /* move past 3 character completion code */
108  if (pos >= offset) {
109  jb_append_string_from_bytes(js_resplist, (const uint8_t *)where + offset, pos - offset);
110  resp_cnt++;
111  }
112 
113  where += pos;
114  length -= pos;
115  }
116  }
117 
118  if (is_cc_array_open) {
119  jb_close(jb);
120  }
121  if (resp_cnt) {
122  jb_close(js_resplist);
123  jb_set_object(jb, "reply", js_resplist);
124  }
125  jb_free(js_resplist);
126  }
127 
128  if (tx->dyn_port) {
129  jb_set_uint(jb, "dynamic_port", tx->dyn_port);
130  }
131 
134  if (tx->active) {
135  JB_SET_STRING(jb, "mode", "active");
136  } else {
137  JB_SET_STRING(jb, "mode", "passive");
138  }
139  }
140 
141  if (tx->done) {
142  JB_SET_STRING(jb, "reply_received", "yes");
143  } else {
144  JB_SET_STRING(jb, "reply_received", "no");
145  }
146 
147  return;
148 
149 fail:
150  if (js_resplist) {
151  jb_free(js_resplist);
152  }
153 }
154 
155 
156 static int JsonFTPLogger(ThreadVars *tv, void *thread_data,
157  const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
158 {
159  SCEnter();
160 
161  const char *event_type;
162  if (f->alproto == ALPROTO_FTPDATA) {
163  event_type = "ftp_data";
164  } else {
165  event_type = "ftp";
166  }
167  FTPTransaction *tx = vtx;
168  LogFTPLogThread *thread = thread_data;
169  LogFTPFileCtx *ftp_ctx = thread->ftplog_ctx;
170 
171  JsonBuilder *jb = CreateEveHeaderWithTxId(p, LOG_DIR_FLOW, event_type, NULL, tx_id);
172  if (likely(jb)) {
173  EveAddCommonOptions(&ftp_ctx->cfg, p, f, jb);
174  jb_open_object(jb, event_type);
175  if (f->alproto == ALPROTO_FTPDATA) {
176  EveFTPDataAddMetadata(f, jb);
177  } else {
178  EveFTPLogCommand(f, tx, jb);
179  }
180 
181  if (!jb_close(jb)) {
182  goto fail;
183  }
184 
185  MemBufferReset(thread->buffer);
186  OutputJsonBuilderBuffer(jb, thread->ftplog_ctx->file_ctx, &thread->buffer);
187 
188  jb_free(jb);
189  }
190  return TM_ECODE_OK;
191 
192 fail:
193  jb_free(jb);
194  return TM_ECODE_FAILED;
195 }
196 
197 static void OutputFTPLogDeInitCtxSub(OutputCtx *output_ctx)
198 {
199  LogFTPFileCtx *ftplog_ctx = (LogFTPFileCtx *)output_ctx->data;
200  SCFree(ftplog_ctx);
201  SCFree(output_ctx);
202 }
203 
204 
205 static OutputInitResult OutputFTPLogInitSub(ConfNode *conf,
206  OutputCtx *parent_ctx)
207 {
208  OutputInitResult result = { NULL, false };
209  OutputJsonCtx *ajt = parent_ctx->data;
210 
211  LogFTPFileCtx *ftplog_ctx = SCCalloc(1, sizeof(*ftplog_ctx));
212  if (unlikely(ftplog_ctx == NULL)) {
213  return result;
214  }
215  ftplog_ctx->file_ctx = ajt->file_ctx;
216  ftplog_ctx->cfg = ajt->cfg;
217 
218  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
219  if (unlikely(output_ctx == NULL)) {
220  SCFree(ftplog_ctx);
221  return result;
222  }
223  output_ctx->data = ftplog_ctx;
224  output_ctx->DeInit = OutputFTPLogDeInitCtxSub;
225 
226  SCLogDebug("FTP log sub-module initialized.");
227 
230 
231  result.ctx = output_ctx;
232  result.ok = true;
233  return result;
234 }
235 
236 static TmEcode JsonFTPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
237 {
238  LogFTPLogThread *thread = SCCalloc(1, sizeof(*thread));
239  if (unlikely(thread == NULL)) {
240  return TM_ECODE_FAILED;
241  }
242 
243  if (initdata == NULL) {
244  SCLogDebug("Error getting context for EveLogFTP. \"initdata\" is NULL.");
245  SCFree(thread);
246  return TM_ECODE_FAILED;
247  }
248 
250  if (unlikely(thread->buffer == NULL)) {
251  SCFree(thread);
252  return TM_ECODE_FAILED;
253  }
254 
255  thread->ftplog_ctx = ((OutputCtx *)initdata)->data;
256  *data = (void *)thread;
257 
258  return TM_ECODE_OK;
259 }
260 
261 static TmEcode JsonFTPLogThreadDeinit(ThreadVars *t, void *data)
262 {
263  LogFTPLogThread *thread = (LogFTPLogThread *)data;
264  if (thread == NULL) {
265  return TM_ECODE_OK;
266  }
267  if (thread->buffer != NULL) {
268  MemBufferFree(thread->buffer);
269  }
270  /* clear memory */
271  memset(thread, 0, sizeof(LogFTPLogThread));
272  SCFree(thread);
273  return TM_ECODE_OK;
274 }
275 
277 {
278  /* Register as an eve sub-module. */
279  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog",
280  "eve-log.ftp", OutputFTPLogInitSub,
281  ALPROTO_FTP, JsonFTPLogger,
282  JsonFTPLogThreadInit, JsonFTPLogThreadDeinit,
283  NULL);
284  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog",
285  "eve-log.ftp", OutputFTPLogInitSub,
286  ALPROTO_FTPDATA, JsonFTPLogger,
287  JsonFTPLogThreadInit, JsonFTPLogThreadDeinit,
288  NULL);
289 
290  SCLogDebug("FTP JSON logger registered.");
291 }
FtpCommand_::command_length
const uint8_t command_length
Definition: app-layer-ftp.h:95
tm-threads.h
FTPTransaction_::command_descriptor
const FtpCommand * command_descriptor
Definition: app-layer-ftp.h:149
output-json-ftp.h
FTPTransaction_::request
uint8_t * request
Definition: app-layer-ftp.h:146
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
OutputJsonCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json.h:111
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
FTPString_::len
uint16_t len
Definition: app-layer-ftp.h:130
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:298
LogFTPLogThread
struct LogFTPLogThread_ LogFTPLogThread
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
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
LogFTPLogThread_::count
uint32_t count
Definition: output-json-ftp.c:58
OutputJsonCommonSettings_
Definition: output-json.h:99
FTPTransaction_::done
bool done
Definition: app-layer-ftp.h:152
LogFileCtx_
Definition: util-logopenfile.h:52
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:347
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:1431
FtpCommand_::command
FtpRequestCommand command
Definition: app-layer-ftp.h:93
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:31
EveAddCommonOptions
void EveAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, JsonBuilder *js)
Definition: output-json.c:673
FtpCommand_::command_name
const char * command_name
Definition: app-layer-ftp.h:94
LogFTPLogThread_::ftplog_ctx
LogFTPFileCtx * ftplog_ctx
Definition: output-json-ftp.c:57
app-layer-ftp.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:79
LogFTPLogThread_::buffer
MemBuffer * buffer
Definition: output-json-ftp.c:59
LOGGER_JSON_FTP
@ LOGGER_JSON_FTP
Definition: suricata-common.h:455
util-unittest.h
OutputCtx_::data
void * data
Definition: tm-modules.h:81
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:78
OutputCtx_
Definition: tm-modules.h:78
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:24
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
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
detect.h
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
FTP_COMMAND_PORT
@ FTP_COMMAND_PORT
Definition: app-layer-ftp.h:65
pkt-var.h
OutputInitResult_::ok
bool ok
Definition: output.h:43
LogFTPFileCtx_
Definition: output-json-ftp.c:51
LogFTPLogThread_
Definition: output-json-ftp.c:56
app-layer-parser.h
LogFTPFileCtx_::cfg
OutputJsonCommonSettings cfg
Definition: output-json-ftp.c:53
Packet_
Definition: decode.h:411
conf.h
TmEcode
TmEcode
Definition: tm-threads-common.h:77
MemBuffer_
Definition: util-buffer.h:27
util-mem.h
length
uint16_t length
Definition: app-layer-modbus.c:2
MemBufferReset
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
OutputInitResult_
Definition: output.h:41
FTPTransaction_::request_length
uint32_t request_length
Definition: app-layer-ftp.h:145
suricata-common.h
OutputCtx_::DeInit
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
MemBufferFree
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
ALPROTO_FTPDATA
@ ALPROTO_FTPDATA
Definition: app-layer-protos.h:47
LogFTPFileCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json-ftp.c:52
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
LogFTPFileCtx
struct LogFTPFileCtx_ LogFTPFileCtx
threadvars.h
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:39
SCFree
#define SCFree(p)
Definition: util-mem.h:61
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id)
Definition: output-json.c:1376
ConfNode_
Definition: conf.h:32
util-buffer.h
JsonFTPLogRegister
void JsonFTPLogRegister(void)
Definition: output-json-ftp.c:276
EveFTPDataAddMetadata
void EveFTPDataAddMetadata(const Flow *f, JsonBuilder *jb)
Definition: app-layer-ftp.c:1441
OutputJsonCtx_::file_ctx
LogFileCtx * file_ctx
Definition: output-json.h:109
FTPTransaction_::dyn_port
uint16_t dyn_port
Definition: app-layer-ftp.h:151
FTPTransaction_::active
bool active
Definition: app-layer-ftp.h:153
likely
#define likely(expr)
Definition: util-optimize.h:32
FTP_COMMAND_EPRT
@ FTP_COMMAND_EPRT
Definition: app-layer-ftp.h:85
FTPString_
Definition: app-layer-ftp.h:128
JsonGetNextLineFromBuffer
uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
Definition: app-layer-ftp.c:1431
FTPTransaction_
Definition: app-layer-ftp.h:134
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:425
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
FTPString_::str
uint8_t * str
Definition: app-layer-ftp.h:129
MemBufferCreateNew
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
debug.h
output.h
app-layer.h