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 typedef struct LogFTPFileCtx_ {
56 
57 typedef struct LogFTPLogThread_ {
59  uint32_t count;
62 
63 static json_t *JsonFTPLogCommand(Flow *f, FTPTransaction *tx)
64 {
65  json_t *cjs = json_object();
66  if (!cjs) {
67  return cjs;
68  }
69 
70  /* Preallocate array objects to simplify failure case */
71  json_t *js_resplist = NULL;
72  json_t *js_respcode_list = NULL;
73  if (!TAILQ_EMPTY(&tx->response_list)) {
74  js_resplist = json_array();
75  js_respcode_list = json_array();
76 
77  if (unlikely(js_resplist == NULL || js_respcode_list == NULL)) {
78  if (js_resplist) {
79  json_decref(js_resplist);
80  } else {
81  json_decref(js_respcode_list);
82  }
83  return cjs;
84  }
85  }
86 
87  json_object_set_new(cjs, "command", json_string(tx->command_descriptor->command_name));
88  uint32_t min_length = tx->command_descriptor->command_length + 1; /* command + space */
89  if (tx->request_length > min_length) {
90  json_object_set_new(cjs, "command_data",
91  JsonAddStringN((const char *)tx->request + min_length,
92  tx->request_length - min_length));
93  } else {
94  json_object_set_new(cjs, "command_data", json_string(NULL));
95  }
96 
97  if (!TAILQ_EMPTY(&tx->response_list)) {
98  FTPString *response;
99  TAILQ_FOREACH(response, &tx->response_list, next) {
100  /* handle multiple lines within the response, \r\n delimited */
101  uint8_t *where = response->str;
102  uint16_t length = response->len;
103  uint16_t pos;
104  while ((pos = JsonGetNextLineFromBuffer((const char *)where, length)) != UINT16_MAX) {
105  uint16_t offset = 0;
106  /* Try to find a completion code for this line */
107  if (pos >= 3) {
108  /* Gather the completion code if present */
109  if (isdigit(where[0]) && isdigit(where[1]) && isdigit(where[2])) {
110  json_array_append_new(js_respcode_list,
111  JsonAddStringN((const char *)where, 3));
112  offset = 4;
113  }
114  }
115  /* move past 3 character completion code */
116  if (pos >= offset) {
117  json_array_append_new(js_resplist,
118  JsonAddStringN((const char *)where + offset, pos - offset));
119  }
120 
121  where += pos;
122  length -= pos;
123  }
124  }
125 
126  json_object_set_new(cjs, "reply", js_resplist);
127  json_object_set_new(cjs, "completion_code", js_respcode_list);
128  }
129 
130  if (tx->dyn_port) {
131  json_object_set_new(cjs, "dynamic_port", json_integer(tx->dyn_port));
132  }
133 
136  json_object_set_new(cjs, "mode",
137  json_string((char *)(tx->active ? "active" : "passive")));
138  }
139 
140  json_object_set_new(cjs, "reply_received",
141  json_string((char *)(tx->done ? "yes" : "no")));
142 
143  return cjs;
144 }
145 
146 
147 static int JsonFTPLogger(ThreadVars *tv, void *thread_data,
148  const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
149 {
150  SCEnter();
151 
152  const char *event_type;
153  if (f->alproto == ALPROTO_FTPDATA) {
154  event_type = "ftp_data";
155  } else {
156  event_type = "ftp";
157  }
158  FTPTransaction *tx = vtx;
159  LogFTPLogThread *thread = thread_data;
160  LogFTPFileCtx *ftp_ctx = thread->ftplog_ctx;
161 
162  json_t *js = CreateJSONHeaderWithTxId(p, LOG_DIR_FLOW, event_type, tx_id);
163  if (likely(js)) {
164  JsonAddCommonOptions(&ftp_ctx->cfg, p, f, js);
165  json_t *cjs = NULL;
166  if (f->alproto == ALPROTO_FTPDATA) {
167  cjs = JsonFTPDataAddMetadata(f);
168  } else {
169  cjs = JsonFTPLogCommand(f, tx);
170  }
171 
172  if (cjs) {
173  json_object_set_new(js, event_type, cjs);
174  }
175 
176  MemBufferReset(thread->buffer);
177  OutputJSONBuffer(js, thread->ftplog_ctx->file_ctx, &thread->buffer);
178 
179  json_object_clear(js);
180  json_decref(js);
181  }
182  return TM_ECODE_OK;
183 }
184 
185 static void OutputFTPLogDeInitCtxSub(OutputCtx *output_ctx)
186 {
187  LogFTPFileCtx *ftplog_ctx = (LogFTPFileCtx *)output_ctx->data;
188  SCFree(ftplog_ctx);
189  SCFree(output_ctx);
190 }
191 
192 
193 static OutputInitResult OutputFTPLogInitSub(ConfNode *conf,
194  OutputCtx *parent_ctx)
195 {
196  OutputInitResult result = { NULL, false };
197  OutputJsonCtx *ajt = parent_ctx->data;
198 
199  LogFTPFileCtx *ftplog_ctx = SCCalloc(1, sizeof(*ftplog_ctx));
200  if (unlikely(ftplog_ctx == NULL)) {
201  return result;
202  }
203  ftplog_ctx->file_ctx = ajt->file_ctx;
204  ftplog_ctx->cfg = ajt->cfg;
205 
206  OutputCtx *output_ctx = SCCalloc(1, sizeof(*output_ctx));
207  if (unlikely(output_ctx == NULL)) {
208  SCFree(ftplog_ctx);
209  return result;
210  }
211  output_ctx->data = ftplog_ctx;
212  output_ctx->DeInit = OutputFTPLogDeInitCtxSub;
213 
214  SCLogDebug("FTP log sub-module initialized.");
215 
218 
219  result.ctx = output_ctx;
220  result.ok = true;
221  return result;
222 }
223 
224 static TmEcode JsonFTPLogThreadInit(ThreadVars *t, const void *initdata, void **data)
225 {
226  LogFTPLogThread *thread = SCCalloc(1, sizeof(*thread));
227  if (unlikely(thread == NULL)) {
228  return TM_ECODE_FAILED;
229  }
230 
231  if (initdata == NULL) {
232  SCLogDebug("Error getting context for EveLogFTP. \"initdata\" is NULL.");
233  SCFree(thread);
234  return TM_ECODE_FAILED;
235  }
236 
238  if (unlikely(thread->buffer == NULL)) {
239  SCFree(thread);
240  return TM_ECODE_FAILED;
241  }
242 
243  thread->ftplog_ctx = ((OutputCtx *)initdata)->data;
244  *data = (void *)thread;
245 
246  return TM_ECODE_OK;
247 }
248 
249 static TmEcode JsonFTPLogThreadDeinit(ThreadVars *t, void *data)
250 {
251  LogFTPLogThread *thread = (LogFTPLogThread *)data;
252  if (thread == NULL) {
253  return TM_ECODE_OK;
254  }
255  if (thread->buffer != NULL) {
256  MemBufferFree(thread->buffer);
257  }
258  /* clear memory */
259  memset(thread, 0, sizeof(LogFTPLogThread));
260  SCFree(thread);
261  return TM_ECODE_OK;
262 }
263 
265 {
266  /* Register as an eve sub-module. */
267  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog",
268  "eve-log.ftp", OutputFTPLogInitSub,
269  ALPROTO_FTP, JsonFTPLogger,
270  JsonFTPLogThreadInit, JsonFTPLogThreadDeinit,
271  NULL);
272  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog",
273  "eve-log.ftp", OutputFTPLogInitSub,
274  ALPROTO_FTPDATA, JsonFTPLogger,
275  JsonFTPLogThreadInit, JsonFTPLogThreadDeinit,
276  NULL);
277 
278  SCLogDebug("FTP JSON logger registered.");
279 }
MemBuffer * MemBufferCreateNew(uint32_t size)
Definition: util-buffer.c:32
#define JSON_OUTPUT_BUFFER_SIZE
Definition: output-json.h:44
MemBuffer * buffer
#define SCLogDebug(...)
Definition: util-debug.h:335
struct LogFTPLogThread_ LogFTPLogThread
int OutputJSONBuffer(json_t *js, LogFileCtx *file_ctx, MemBuffer **buffer)
Definition: output-json.c:809
uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
OutputJsonCommonSettings cfg
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
LogFileCtx * file_ctx
uint32_t event_type
const uint8_t command_length
Definition: app-layer-ftp.h:93
#define unlikely(expr)
Definition: util-optimize.h:35
#define MemBufferReset(mem_buffer)
Reset the mem buffer.
Definition: util-buffer.h:42
json_t * CreateJSONHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, uint64_t tx_id)
Definition: output-json.c:783
uint8_t * request
uint64_t offset
void(* DeInit)(struct OutputCtx_ *)
Definition: tm-modules.h:84
const char * command_name
Definition: app-layer-ftp.h:92
void JsonAddCommonOptions(const OutputJsonCommonSettings *cfg, const Packet *p, const Flow *f, json_t *js)
Definition: output-json.c:390
#define SCCalloc(nm, a)
Definition: util-mem.h:253
LogFTPFileCtx * ftplog_ctx
OutputJsonCommonSettings cfg
Definition: output-json.h:81
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
uint8_t * str
LogFileCtx * file_ctx
Definition: output-json.h:79
#define SCEnter(...)
Definition: util-debug.h:337
struct LogFTPFileCtx_ LogFTPFileCtx
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
json_t * JsonFTPDataAddMetadata(const Flow *f)
Definition: conf.h:32
OutputCtx * ctx
Definition: output.h:42
json_t * JsonAddStringN(const char *string, size_t size)
Create a JSON string from a character sequence.
Definition: output-json.c:142
#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
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