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 #include "suricata-common.h"
27 #include "detect.h"
28 #include "pkt-var.h"
29 #include "conf.h"
30 
31 #include "threads.h"
32 #include "threadvars.h"
33 #include "tm-threads.h"
34 
35 #include "util-unittest.h"
36 #include "util-buffer.h"
37 #include "util-debug.h"
38 #include "util-mem.h"
39 
40 #include "output.h"
41 #include "output-json.h"
42 
43 #include "app-layer.h"
44 #include "app-layer-parser.h"
45 
46 #include "app-layer-ftp.h"
47 #include "output-json-ftp.h"
48 
49 bool EveFTPLogCommand(void *vtx, JsonBuilder *jb)
50 {
51  FTPTransaction *tx = vtx;
52  /* Preallocate array objects to simplify failure case */
53  JsonBuilder *js_resplist = NULL;
54  if (!TAILQ_EMPTY(&tx->response_list)) {
55  js_resplist = jb_new_array();
56 
57  if (unlikely(js_resplist == NULL)) {
58  return false;
59  }
60  }
61  const char *command_name = NULL;
62  uint8_t command_name_length;
63  if (tx->command_descriptor.command_code != FTP_COMMAND_UNKNOWN) {
64  if (!SCGetFtpCommandInfo(tx->command_descriptor.command_index, &command_name, NULL,
65  &command_name_length)) {
66  SCLogDebug("Unable to fetch info for FTP command code %d [index %d]",
68  return false;
69  }
70  }
71  jb_open_object(jb, "ftp");
72  if (command_name) {
73  jb_set_string(jb, "command", command_name);
74  uint32_t min_length = command_name_length + 1; /* command + space */
75  if (tx->request_length > min_length) {
76  jb_set_string_from_bytes(jb, "command_data", (const uint8_t *)tx->request + min_length,
77  tx->request_length - min_length - 1);
78  if (tx->request_truncated) {
79  JB_SET_TRUE(jb, "command_truncated");
80  } else {
81  JB_SET_FALSE(jb, "command_truncated");
82  }
83  }
84  }
85 
86  bool reply_truncated = false;
87 
88  if (!TAILQ_EMPTY(&tx->response_list)) {
89  int resp_cnt = 0;
90  FTPString *response;
91  bool is_cc_array_open = false;
92  TAILQ_FOREACH(response, &tx->response_list, next) {
93  /* handle multiple lines within the response, \r\n delimited */
94  uint8_t *where = response->str;
95  uint16_t length = 0;
96  uint16_t pos;
97  if (response->len > 0 && response->len <= UINT16_MAX) {
98  length = (uint16_t)response->len - 1;
99  } else if (response->len > UINT16_MAX) {
100  length = UINT16_MAX;
101  }
102  if (!reply_truncated && response->truncated) {
103  reply_truncated = true;
104  }
105  while ((pos = JsonGetNextLineFromBuffer((const char *)where, length)) != UINT16_MAX) {
106  uint16_t offset = 0;
107  /* Try to find a completion code for this line */
108  if (pos >= 3) {
109  /* Gather the completion code if present */
110  if (isdigit(where[0]) && isdigit(where[1]) && isdigit(where[2])) {
111  if (!is_cc_array_open) {
112  jb_open_array(jb, "completion_code");
113  is_cc_array_open = true;
114  }
115  jb_append_string_from_bytes(jb, (const uint8_t *)where, 3);
116  offset = 4;
117  }
118  }
119  /* move past 3 character completion code */
120  if (pos >= offset) {
121  jb_append_string_from_bytes(js_resplist, (const uint8_t *)where + offset, pos - offset);
122  resp_cnt++;
123  }
124 
125  where += pos;
126  length -= pos;
127  }
128  }
129 
130  if (is_cc_array_open) {
131  jb_close(jb);
132  }
133  if (resp_cnt) {
134  jb_close(js_resplist);
135  jb_set_object(jb, "reply", js_resplist);
136  }
137  jb_free(js_resplist);
138  }
139 
140  if (tx->dyn_port) {
141  jb_set_uint(jb, "dynamic_port", tx->dyn_port);
142  }
143 
144  if (tx->command_descriptor.command_code == FTP_COMMAND_PORT ||
145  tx->command_descriptor.command_code == FTP_COMMAND_EPRT) {
146  if (tx->active) {
147  JB_SET_STRING(jb, "mode", "active");
148  } else {
149  JB_SET_STRING(jb, "mode", "passive");
150  }
151  }
152 
153  if (tx->done) {
154  JB_SET_STRING(jb, "reply_received", "yes");
155  } else {
156  JB_SET_STRING(jb, "reply_received", "no");
157  }
158 
159  if (reply_truncated) {
160  JB_SET_TRUE(jb, "reply_truncated");
161  } else {
162  JB_SET_FALSE(jb, "reply_truncated");
163  }
164  jb_close(jb);
165  return true;
166 }
FTPTransaction_::request_truncated
bool request_truncated
Definition: app-layer-ftp.h:71
tm-threads.h
output-json-ftp.h
FTPTransaction_::request
uint8_t * request
Definition: app-layer-ftp.h:70
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
threads.h
FTPTransaction_::done
bool done
Definition: app-layer-ftp.h:77
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
app-layer-ftp.h
FTPString_::len
uint32_t len
Definition: app-layer-ftp.h:46
util-unittest.h
FTPString_::truncated
bool truncated
Definition: app-layer-ftp.h:47
util-debug.h
JB_SET_STRING
#define JB_SET_STRING(jb, key, val)
Definition: rust.h:26
output-json.h
detect.h
pkt-var.h
JB_SET_TRUE
#define JB_SET_TRUE(jb, key)
Definition: rust.h:27
EveFTPLogCommand
bool EveFTPLogCommand(void *vtx, JsonBuilder *jb)
Definition: output-json-ftp.c:49
app-layer-parser.h
FTPTransaction_::command_descriptor
FtpCommandInfo command_descriptor
Definition: app-layer-ftp.h:74
FtpCommandInfo_::command_code
FtpRequestCommand command_code
Definition: app-layer-ftp.h:59
conf.h
util-mem.h
FTPTransaction_::request_length
uint32_t request_length
Definition: app-layer-ftp.h:69
suricata-common.h
JB_SET_FALSE
#define JB_SET_FALSE(jb, key)
Definition: rust.h:28
threadvars.h
util-buffer.h
FtpCommandInfo_::command_index
uint8_t command_index
Definition: app-layer-ftp.h:58
FTPTransaction_::dyn_port
uint16_t dyn_port
Definition: app-layer-ftp.h:76
FTPTransaction_::active
bool active
Definition: app-layer-ftp.h:78
FTPString_
Definition: app-layer-ftp.h:44
JsonGetNextLineFromBuffer
uint16_t JsonGetNextLineFromBuffer(const char *buffer, const uint16_t len)
Definition: app-layer-ftp.c:1358
FTPTransaction_
Definition: app-layer-ftp.h:62
FTPString_::str
uint8_t * str
Definition: app-layer-ftp.h:45
output.h
app-layer.h