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 #include "util-byte.h"
42 
43 #include "output.h"
44 #include "output-json.h"
45 
46 #include "app-layer.h"
47 #include "app-layer-parser.h"
48 
49 #include "app-layer-ftp.h"
50 #include "output-json-ftp.h"
51 
52 #ifdef HAVE_LIBJANSSON
53 
54 typedef struct LogFTPFileCtx_ {
55  LogFileCtx *file_ctx;
56  OutputJsonCommonSettings cfg;
57 } LogFTPFileCtx;
58 
59 typedef struct LogFTPLogThread_ {
60  LogFTPFileCtx *ftplog_ctx;
61  uint32_t count;
62  MemBuffer *buffer;
63 } LogFTPLogThread;
64 
65 /*
66  * \brief Returns the ending offset of the next line.
67  *
68  * Here, "next line" is defined as terminating on
69  * - Newline character
70  * - Null character
71  *
72  * \param buffer Contains zero or more characters.
73  * \param len Size, in bytes, of buffer.
74  *
75  * \retval Offset from the start of buffer indicating the where the
76  * next "line ends". The characters between the input buffer and this
77  * value comprise the line.
78  *
79  * NULL is found first or a newline isn't found, then
80  */
81 static uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
82 {
83  if (!buffer || *buffer == '\0')
84  return UINT16_MAX;
85 
86  char *c = strchr(buffer, '\n');
87  return c == NULL ? len : c - buffer + 1;
88 }
89 
90 static json_t *JsonFTPLogCommand(Flow *f, FTPTransaction *tx)
91 {
92  json_t *cjs = json_object();
93  if (!cjs) {
94  return cjs;
95  }
96 
97  /* Preallocate array objects to simplify failure case */
98  json_t *js_resplist;
99  json_t *js_respcode_list;
100  if (!TAILQ_EMPTY(&tx->response_list)) {
101  js_resplist = json_array();
102  js_respcode_list = json_array();
103 
104  if (unlikely(js_resplist == NULL || js_respcode_list == NULL)) {
105  if (js_resplist) {
106  json_decref(js_resplist);
107  } else {
108  json_decref(js_respcode_list);
109  }
110  return cjs;
111  }
112  }
113 
114  json_object_set_new(cjs, "command",
115  json_string(tx->command_descriptor->command_name_upper));
116  uint32_t min_length = tx->command_descriptor->command_length + 1; /* command + space */
117  if (tx->request_length > min_length) {
118  json_object_set_new(cjs, "command_data",
119  JsonAddStringN((const char *)tx->request + min_length,
120  tx->request_length - min_length));
121  } else {
122  json_object_set_new(cjs, "command_data", json_string(NULL));
123  }
124 
125  if (!TAILQ_EMPTY(&tx->response_list)) {
126  FTPString *response;
127  TAILQ_FOREACH(response, &tx->response_list, next) {
128  /* handle multiple lines within the response, \r\n delimited */
129  uint8_t *where = response->str;
130  uint16_t length = response->len;
131  uint16_t pos;
132  while ((pos = JsonGetNextLineFromBuffer((const char *)where, length)) != UINT16_MAX) {
133  uint16_t offset = 0;
134  /* Try to find a completion code for this line */
135  if (pos >= 3) {
136  /* Gather the completion code if present */
137  if (isdigit(where[0]) && isdigit(where[1]) && isdigit(where[2])) {
138  json_array_append_new(js_respcode_list,
139  JsonAddStringN((const char *)where, 3));
140  offset = 4;
141  }
142  }
143  /* move past 3 character completion code */
144  if (pos >= offset) {
145  json_array_append_new(js_resplist,
146  JsonAddStringN((const char *)where + offset, pos - offset));
147  }
148 
149  where += pos;
150  length -= pos;
151  }
152  }
153 
154  json_object_set_new(cjs, "reply", js_resplist);
155  json_object_set_new(cjs, "completion_code", js_respcode_list);
156  }
157 
158  if (tx->dyn_port) {
159  json_object_set_new(cjs, "dynamic_port", json_integer(tx->dyn_port));
160  }
161 
164  json_object_set_new(cjs, "mode",
165  json_string((char *)(tx->active ? "active" : "passive")));
166  }
167 
168  json_object_set_new(cjs, "reply_received",
169  json_string((char *)(tx->done ? "yes" : "no")));
170 
171  return cjs;
172 }
173 
174 
175 static int JsonFTPLogger(ThreadVars *tv, void *thread_data,
176  const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
177 {
178  SCEnter();
179 
180  const char *event_type;
181  if (f->alproto == ALPROTO_FTPDATA) {
182  event_type = "ftp_data";
183  } else {
184  event_type = "ftp";
185  }
186  FTPTransaction *tx = vtx;
187  LogFTPLogThread *thread = thread_data;
188  LogFTPFileCtx *ftp_ctx = thread->ftplog_ctx;
189 
190  json_t *js = CreateJSONHeaderWithTxId(p, LOG_DIR_FLOW, event_type, tx_id);
191  if (likely(js)) {
192  JsonAddCommonOptions(&ftp_ctx->cfg, p, f, js);
193  json_t *cjs = NULL;
194  if (f->alproto == ALPROTO_FTPDATA) {
195  cjs = JsonFTPDataAddMetadata(f);
196  } else {
197  cjs = JsonFTPLogCommand(f, tx);
198  }
199 
200  if (cjs) {
201  json_object_set_new(js, event_type, cjs);
202  }
203 
204  MemBufferReset(thread->buffer);
205  OutputJSONBuffer(js, thread->ftplog_ctx->file_ctx, &thread->buffer);
206 
207  json_object_clear(js);
208  json_decref(js);
209  }
210  return TM_ECODE_OK;
211 }
212 
213 static void OutputFTPLogDeInitCtxSub(OutputCtx *output_ctx)
214 {
215  LogFTPFileCtx *ftplog_ctx = (LogFTPFileCtx *)output_ctx->data;
216  SCFree(ftplog_ctx);
217  SCFree(output_ctx);
218 }
219 
220 
221 static OutputInitResult OutputFTPLogInitSub(ConfNode *conf,
222  OutputCtx *parent_ctx)
223 {
224  OutputInitResult result = { NULL, false };
225  OutputJsonCtx *ajt = parent_ctx->data;
226 
227  LogFTPFileCtx *ftplog_ctx = SCCalloc(1, sizeof(*ftplog_ctx));
228  if (unlikely(ftplog_ctx == NULL)) {
229  return result;
230  }
231  ftplog_ctx->file_ctx = ajt->file_ctx;
232  ftplog_ctx->cfg = ajt->cfg;
233 
234  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
235  if (unlikely(output_ctx == NULL)) {
236  SCFree(ftplog_ctx);
237  return result;
238  }
239  output_ctx->data = ftplog_ctx;
240  output_ctx->DeInit = OutputFTPLogDeInitCtxSub;
241 
242  SCLogDebug("FTP log sub-module initialized.");
243 
246 
247  result.ctx = output_ctx;
248  result.ok = true;
249  return result;
250 }
251 
252 static TmEcode JsonFTPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
253 {
254  LogFTPLogThread *thread = SCCalloc(1, sizeof(*thread));
255  if (unlikely(thread == NULL)) {
256  return TM_ECODE_FAILED;
257  }
258 
259  if (initdata == NULL) {
260  SCLogDebug("Error getting context for EveLogFTP. \"initdata\" is NULL.");
261  SCFree(thread);
262  return TM_ECODE_FAILED;
263  }
264 
265  thread->buffer = MemBufferCreateNew(JSON_OUTPUT_BUFFER_SIZE);
266  if (unlikely(thread->buffer == NULL)) {
267  SCFree(thread);
268  return TM_ECODE_FAILED;
269  }
270 
271  thread->ftplog_ctx = ((OutputCtx *)initdata)->data;
272  *data = (void *)thread;
273 
274  return TM_ECODE_OK;
275 }
276 
277 static TmEcode JsonFTPLogThreadDeinit(ThreadVars *t, void *data)
278 {
279  LogFTPLogThread *thread = (LogFTPLogThread *)data;
280  if (thread == NULL) {
281  return TM_ECODE_OK;
282  }
283  if (thread->buffer != NULL) {
284  MemBufferFree(thread->buffer);
285  }
286  /* clear memory */
287  memset(thread, 0, sizeof(LogFTPLogThread));
288  SCFree(thread);
289  return TM_ECODE_OK;
290 }
291 
292 void JsonFTPLogRegister(void)
293 {
294  /* Register as an eve sub-module. */
295  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog",
296  "eve-log.ftp", OutputFTPLogInitSub,
297  ALPROTO_FTP, JsonFTPLogger,
298  JsonFTPLogThreadInit, JsonFTPLogThreadDeinit,
299  NULL);
300  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog",
301  "eve-log.ftp", OutputFTPLogInitSub,
302  ALPROTO_FTPDATA, JsonFTPLogger,
303  JsonFTPLogThreadInit, JsonFTPLogThreadDeinit,
304  NULL);
305 
306  SCLogDebug("FTP JSON logger registered.");
307 }
308 #else /* HAVE_LIBJANSSON */
309 
311 {
312 }
313 
314 #endif /* HAVE_LIBJANSSON */
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define SCLogDebug(...)
Definition: util-debug.h:335
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
uint32_t event_type
const uint8_t command_length
Definition: app-layer-ftp.h:94
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
uint8_t * request
uint64_t offset
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
#define SCCalloc(nm, a)
Definition: util-mem.h:253
const char * command_name_upper
Definition: app-layer-ftp.h:92
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
uint8_t * str
#define SCEnter(...)
Definition: util-debug.h:337
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:400
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
#define SCFree(a)
Definition: util-mem.h:322
uint32_t request_length
uint16_t tx_id
void * data
Definition: tm-modules.h:81
uint16_t length
void JsonFTPLogRegister(void)
#define TAILQ_EMPTY(head)
Definition: queue.h:347
uint8_t len
Per thread variable structure.
Definition: threadvars.h:57
AppProto alproto
application level protocol
Definition: flow.h:409
const FtpCommand * command_descriptor
#define likely(expr)
Definition: util-optimize.h:32
uint16_t len
Flow data structure.
Definition: flow.h:325
void MemBufferFree(MemBuffer *buffer)
Definition: util-buffer.c:82
FtpRequestCommand command
Definition: app-layer-ftp.h:91