suricata
output-json-ftp.c
Go to the documentation of this file.
1 /* Copyright (C) 2017-2021 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 static void EveFTPLogCommand(Flow *f, FTPTransaction *tx, JsonBuilder *jb)
52 {
53  /* Preallocate array objects to simplify failure case */
54  JsonBuilder *js_resplist = NULL;
55  if (!TAILQ_EMPTY(&tx->response_list)) {
56  js_resplist = jb_new_array();
57 
58  if (unlikely(js_resplist == NULL)) {
59  return;
60  }
61  }
62  jb_set_string(jb, "command", tx->command_descriptor->command_name);
63  uint32_t min_length = tx->command_descriptor->command_length + 1; /* command + space */
64  if (tx->request_length > min_length) {
65  jb_set_string_from_bytes(jb,
66  "command_data",
67  (const uint8_t *)tx->request + min_length,
68  tx->request_length - min_length - 1);
69  }
70 
71  if (!TAILQ_EMPTY(&tx->response_list)) {
72  int resp_code_cnt = 0;
73  int resp_cnt = 0;
74  FTPString *response;
75  bool is_cc_array_open = false;
76  TAILQ_FOREACH(response, &tx->response_list, next) {
77  /* handle multiple lines within the response, \r\n delimited */
78  uint8_t *where = response->str;
79  uint16_t length = response->len ? response->len -1 : 0;
80  uint16_t pos;
81  while ((pos = JsonGetNextLineFromBuffer((const char *)where, length)) != UINT16_MAX) {
82  uint16_t offset = 0;
83  /* Try to find a completion code for this line */
84  if (pos >= 3) {
85  /* Gather the completion code if present */
86  if (isdigit(where[0]) && isdigit(where[1]) && isdigit(where[2])) {
87  if (!is_cc_array_open) {
88  jb_open_array(jb, "completion_code");
89  is_cc_array_open = true;
90  }
91  jb_append_string_from_bytes(jb, (const uint8_t *)where, 3);
92  resp_code_cnt++;
93  offset = 4;
94  }
95  }
96  /* move past 3 character completion code */
97  if (pos >= offset) {
98  jb_append_string_from_bytes(js_resplist, (const uint8_t *)where + offset, pos - offset);
99  resp_cnt++;
100  }
101 
102  where += pos;
103  length -= pos;
104  }
105  }
106 
107  if (is_cc_array_open) {
108  jb_close(jb);
109  }
110  if (resp_cnt) {
111  jb_close(js_resplist);
112  jb_set_object(jb, "reply", js_resplist);
113  }
114  jb_free(js_resplist);
115  }
116 
117  if (tx->dyn_port) {
118  jb_set_uint(jb, "dynamic_port", tx->dyn_port);
119  }
120 
123  if (tx->active) {
124  JB_SET_STRING(jb, "mode", "active");
125  } else {
126  JB_SET_STRING(jb, "mode", "passive");
127  }
128  }
129 
130  if (tx->done) {
131  JB_SET_STRING(jb, "reply_received", "yes");
132  } else {
133  JB_SET_STRING(jb, "reply_received", "no");
134  }
135 }
136 
137 
138 static int JsonFTPLogger(ThreadVars *tv, void *thread_data,
139  const Packet *p, Flow *f, void *state, void *vtx, uint64_t tx_id)
140 {
141  SCEnter();
142  OutputJsonThreadCtx *thread = thread_data;
143 
144  const char *event_type;
145  if (f->alproto == ALPROTO_FTPDATA) {
146  event_type = "ftp_data";
147  } else {
148  event_type = "ftp";
149  }
150  FTPTransaction *tx = vtx;
151 
152  JsonBuilder *jb =
153  CreateEveHeaderWithTxId(p, LOG_DIR_FLOW, event_type, NULL, tx_id, thread->ctx);
154  if (likely(jb)) {
155  jb_open_object(jb, event_type);
156  if (f->alproto == ALPROTO_FTPDATA) {
157  EveFTPDataAddMetadata(f, jb);
158  } else {
159  EveFTPLogCommand(f, tx, jb);
160  }
161 
162  if (!jb_close(jb)) {
163  goto fail;
164  }
165 
166  OutputJsonBuilderBuffer(jb, thread);
167 
168  jb_free(jb);
169  }
170  return TM_ECODE_OK;
171 
172 fail:
173  jb_free(jb);
174  return TM_ECODE_FAILED;
175 }
176 
177 static OutputInitResult OutputFTPLogInitSub(ConfNode *conf,
178  OutputCtx *parent_ctx)
179 {
182  return OutputJsonLogInitSub(conf, parent_ctx);
183 }
184 
186 {
187  /* Register as an eve sub-module. */
188  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog", "eve-log.ftp",
189  OutputFTPLogInitSub, ALPROTO_FTP, JsonFTPLogger, JsonLogThreadInit, JsonLogThreadDeinit,
190  NULL);
191  OutputRegisterTxSubModule(LOGGER_JSON_FTP, "eve-log", "JsonFTPLog", "eve-log.ftp",
192  OutputFTPLogInitSub, ALPROTO_FTPDATA, JsonFTPLogger, JsonLogThreadInit,
193  JsonLogThreadDeinit, NULL);
194 
195  SCLogDebug("FTP JSON logger registered.");
196 }
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:145
output-json-ftp.h
OutputJsonLogInitSub
OutputInitResult OutputJsonLogInitSub(ConfNode *conf, OutputCtx *parent_ctx)
Definition: output-json-common.c:88
FTPTransaction_::request
uint8_t * request
Definition: app-layer-ftp.h:142
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
OutputJsonThreadCtx_::ctx
OutputJsonCtx * ctx
Definition: output-json.h:90
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
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
JsonLogThreadInit
TmEcode JsonLogThreadInit(ThreadVars *t, const void *initdata, void **data)
Definition: output-json-common.c:105
threads.h
Flow_
Flow data structure.
Definition: flow.h:353
FTPTransaction_::done
bool done
Definition: app-layer-ftp.h:148
OutputJsonBuilderBuffer
int OutputJsonBuilderBuffer(JsonBuilder *js, OutputJsonThreadCtx *ctx)
Definition: output-json.c:975
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:347
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
FtpCommand_::command
FtpRequestCommand command
Definition: app-layer-ftp.h:93
ALPROTO_FTP
@ ALPROTO_FTP
Definition: app-layer-protos.h:31
FtpCommand_::command_name
const char * command_name
Definition: app-layer-ftp.h:94
app-layer-ftp.h
TM_ECODE_FAILED
@ TM_ECODE_FAILED
Definition: tm-threads-common.h:81
LOGGER_JSON_FTP
@ LOGGER_JSON_FTP
Definition: suricata-common.h:449
util-unittest.h
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
OutputCtx_
Definition: tm-modules.h:78
OutputJsonThreadCtx_
Definition: output-json.h:89
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:33
output-json.h
AppLayerParserRegisterLogger
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
Definition: app-layer-parser.c:455
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
app-layer-parser.h
CreateEveHeaderWithTxId
JsonBuilder * CreateEveHeaderWithTxId(const Packet *p, enum OutputJsonLogDirection dir, const char *event_type, JsonAddrInfo *addr, uint64_t tx_id, OutputJsonCtx *eve_ctx)
Definition: output-json.c:920
Packet_
Definition: decode.h:414
conf.h
util-mem.h
OutputInitResult_
Definition: output.h:43
FTPTransaction_::request_length
uint32_t request_length
Definition: app-layer-ftp.h:141
suricata-common.h
ALPROTO_FTPDATA
@ ALPROTO_FTPDATA
Definition: app-layer-protos.h:47
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
LOG_DIR_FLOW
@ LOG_DIR_FLOW
Definition: output-json.h:40
ConfNode_
Definition: conf.h:32
util-buffer.h
JsonFTPLogRegister
void JsonFTPLogRegister(void)
Definition: output-json-ftp.c:185
EveFTPDataAddMetadata
void EveFTPDataAddMetadata(const Flow *f, JsonBuilder *jb)
Definition: app-layer-ftp.c:1401
FTPTransaction_::dyn_port
uint16_t dyn_port
Definition: app-layer-ftp.h:147
FTPTransaction_::active
bool active
Definition: app-layer-ftp.h:149
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
JsonLogThreadDeinit
TmEcode JsonLogThreadDeinit(ThreadVars *t, void *data)
Definition: output-json-common.c:138
JsonGetNextLineFromBuffer
uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
Definition: app-layer-ftp.c:1391
FTPTransaction_
Definition: app-layer-ftp.h:134
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:460
FTPString_::str
uint8_t * str
Definition: app-layer-ftp.h:129
debug.h
output.h
app-layer.h