48 #define FTP_MPM mpm_default_matcher
50 static MpmCtx *ftp_mpm_ctx = NULL;
61 static void FTPParseMemcap(
void)
69 static void FTPIncrMemuse(uint64_t size)
74 static void FTPDecrMemuse(uint64_t size)
107 static int FTPCheckMemcap(uint64_t size)
115 static void *FTPCalloc(
size_t n,
size_t size)
117 if (FTPCheckMemcap((uint32_t)(n * size)) == 0) {
129 FTPIncrMemuse((uint64_t)(n * size));
133 static void *FTPRealloc(
void *ptr,
size_t orig_size,
size_t size)
135 if (FTPCheckMemcap((uint32_t)(size - orig_size)) == 0) {
146 if (size > orig_size) {
147 FTPIncrMemuse(size - orig_size);
149 FTPDecrMemuse(orig_size - size);
155 static void FTPFree(
void *ptr,
size_t size)
159 FTPDecrMemuse((uint64_t)size);
166 FTPIncrMemuse(response->total_size);
175 FTPDecrMemuse(wrapper->
response->total_size);
176 SCFTPFreeResponseLine(wrapper->
response);
182 static void *FTPLocalStorageAlloc(
void)
191 if (td->
pmq == NULL) {
204 static void FTPLocalStorageFree(
void *ptr)
208 if (td->
pmq != NULL) {
249 SCLogDebug(
"new transaction %p (state tx cnt %"PRIu64
")", tx, state->
tx_cnt);
264 while ((wrapper =
TAILQ_FIRST(&tx->response_list))) {
266 FTPResponseWrapperFree(wrapper);
269 FTPFree(tx,
sizeof(*tx));
287 while (input->
len > 0) {
288 const uint8_t *lf_idx = memchr(input->
buf + input->
consumed, 0x0a, input->
len);
290 if (lf_idx == NULL) {
292 *current_line_truncated =
true;
306 const uint32_t o_consumed = input->
consumed;
307 input->
consumed = (uint32_t)(lf_idx - input->
buf + 1);
308 const uint32_t line_len = (uint32_t)(input->
consumed - o_consumed);
309 input->
len -= (int32_t)line_len;
312 if (*current_line_truncated) {
316 *current_line_truncated =
false;
320 line->
buf = input->
buf + o_consumed;
321 line->
len = line_len;
357 static int FTPParseRequestCommand(
368 uint8_t command_code;
369 if (SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], NULL, &command_code, NULL)) {
380 const char *command_name = NULL;
381 (void)SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], &command_name, NULL, NULL);
382 SCLogDebug(
"matching FTP command is %s [code: %d, index %d]", command_name,
392 static void FtpTransferCmdFree(
void *data)
394 FtpTransferCmd *cmd = (FtpTransferCmd *)data;
397 if (cmd->file_name) {
398 FTPFree((
void *)cmd->file_name, cmd->file_len + 1);
400 SCFTPTransferCmdFree(cmd);
401 FTPDecrMemuse((uint64_t)
sizeof(FtpTransferCmd));
404 static uint32_t CopyCommandLine(uint8_t **dest,
FtpLineState *line)
408 while (line->
len && isspace((
unsigned char)line->
buf[line->
len - 1])) {
416 uint8_t *where = FTPCalloc(line->
len + 1,
sizeof(
char));
420 memcpy(where, line->
buf, line->
len);
421 where[line->
len] =
'\0';
424 return line->
len + 1;
447 const uint8_t *input = StreamSliceGetData(&stream_slice);
448 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
452 }
else if (input == NULL || input_len == 0) {
456 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
457 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .truncated =
false };
459 uint8_t direction = STREAM_TOSERVER;
465 }
else if (res.
status == -1) {
470 if (!FTPParseRequestCommand(thread_data, &line, &cmd_descriptor)) {
471 state->
command = FTP_COMMAND_UNKNOWN;
495 (state->
command == FTP_COMMAND_STOR || state->
command == FTP_COMMAND_APPE ||
496 state->
command == FTP_COMMAND_STOU)) ||
498 (state->
command == FTP_COMMAND_RETR || state->
command == FTP_COMMAND_NLST ||
499 state->
command == FTP_COMMAND_LIST ||
500 state->
command == FTP_COMMAND_MLSD))) {
501 direction = STREAM_TOCLIENT;
504 bool has_file =
false;
507 case FTP_COMMAND_EPRT:
509 case FTP_COMMAND_PORT:
528 case FTP_COMMAND_RETR:
530 case FTP_COMMAND_STOR:
532 case FTP_COMMAND_APPE:
543 case FTP_COMMAND_STOU:
548 case FTP_COMMAND_NLST:
549 case FTP_COMMAND_LIST:
550 case FTP_COMMAND_MLSD: {
557 FtpTransferCmd *data = SCFTPTransferCmdNew();
560 FTPIncrMemuse((uint64_t)(
sizeof *data));
562 data->flow_id = FlowGetId(f);
563 data->direction = direction;
564 data->data_free = FtpTransferCmdFree;
572 #if SC_FILENAME_MAX > UINT16_MAX
573 #error SC_FILENAME_MAX is greater than UINT16_MAX
575 data->file_name = FTPCalloc(file_name_len + 1,
sizeof(
char));
576 if (data->file_name == NULL) {
577 FtpTransferCmdFree(data);
580 data->file_name[file_name_len] = 0;
581 data->file_len = (uint16_t)file_name_len;
582 memcpy(data->file_name, line.
buf + 5, file_name_len);
583 }
else if (state->
command == FTP_COMMAND_STOU) {
584 const char default_file_name[] =
"<stou>";
585 uint32_t file_name_len =
sizeof(default_file_name);
586 data->file_name = FTPCalloc(file_name_len,
sizeof(
char));
587 if (data->file_name == NULL) {
588 FtpTransferCmdFree(data);
591 data->file_name[file_name_len - 1] = 0;
592 data->file_len = (uint16_t)file_name_len - 1;
593 memcpy(data->file_name, default_file_name, file_name_len);
598 FtpTransferCmdFree(data);
602 SCLogDebug(
"Expectation created [direction: %s, dynamic port %" PRIu16
"].",
622 static int FTPParsePassiveResponse(
FtpState *state,
const uint8_t *input, uint32_t input_len)
624 uint16_t dyn_port = SCFTPParsePortPasv(input, input_len);
628 SCLogDebug(
"FTP passive mode (v4): dynamic port %"PRIu16
"", dyn_port);
637 static int FTPParsePassiveResponseV6(
FtpState *state,
const uint8_t *input, uint32_t input_len)
639 uint16_t dyn_port = SCFTPParsePortEpsv(input, input_len);
643 SCLogDebug(
"FTP passive mode (v6): dynamic port %"PRIu16
"", dyn_port);
660 static inline bool FTPIsPPR(
const uint8_t *input, uint32_t input_len)
662 return input_len >= 4 && isdigit(input[0]) && input[0] ==
'1' &&
663 isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
680 const uint8_t *input = StreamSliceGetData(&stream_slice);
681 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
686 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
687 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .truncated =
false };
695 }
else if (res.
status == -1) {
700 tx = FTPTransactionCreate(state);
711 if (state->
command == FTP_COMMAND_UNKNOWN) {
720 case FTP_COMMAND_AUTH_TLS:
726 case FTP_COMMAND_EPRT:
735 SCLogDebug(
"FTP active mode (v6): dynamic port %" PRIu16
"", dyn_port);
738 case FTP_COMMAND_PORT:
747 SCLogDebug(
"FTP active mode (v4): dynamic port %" PRIu16
"", dyn_port);
750 case FTP_COMMAND_PASV:
752 FTPParsePassiveResponse(ftp_state, line.
buf, line.
len);
756 case FTP_COMMAND_EPSV:
758 FTPParsePassiveResponseV6(ftp_state, line.
buf, line.
len);
766 FTPResponseLine *response = SCFTPParseResponseLine((
const char *)line.
buf, line.
len);
771 if (response->truncated) {
777 SCFTPFreeResponseLine(response);
780 SCLogDebug(
"unable to parse FTP response line \"%s\"", line.
buf);
785 if (FTPIsPPR(line.
buf, line.
len)) {
799 static uint64_t ftp_state_memuse = 0;
800 static uint64_t ftp_state_memcnt = 0;
803 static void *FTPStateAlloc(
void *orig_state,
AppProto proto_orig)
805 void *s = FTPCalloc(1,
sizeof(
FtpState));
821 static void FTPStateFree(
void *s)
832 const char *command_name = NULL;
833 (void)SCGetFtpCommandInfo(
835 SCLogDebug(
"[%s] state %p id %" PRIu64
", Freeing %d bytes at %p",
841 FTPTransactionFree(tx);
864 SCLogDebug(
"NULL state object; no transactions available");
881 SCLogDebug(
"Returning OLDEST tx %p id %"PRIu64, lasttx, lasttx->
tx_id);
885 static void *FTPGetTx(
void *state, uint64_t tx_id)
891 if (ftp_state->
curr_tx == NULL)
897 if (tx->
tx_id == tx_id)
916 static void FTPStateTransactionFree(
void *state, uint64_t tx_id)
921 if (tx_id < tx->tx_id)
923 else if (tx_id > tx->
tx_id)
929 FTPTransactionFree(tx);
934 static uint64_t FTPGetTxCnt(
void *state)
945 static int FTPGetAlstateProgress(
void *vtx, uint8_t direction)
951 if (direction == STREAM_TOSERVER) {
952 return FTP_STATE_FINISHED;
955 return FTP_STATE_IN_PROGRESS;
958 return FTP_STATE_FINISHED;
961 static AppProto FTPUserProbingParser(
962 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
975 static AppProto FTPQuitProbingParser(
976 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
983 if (
SCMemcmp(input,
"QUIT", 4) != 0) {
989 static AppProto FTPServerProbingParser(
990 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
997 if (input[0] !=
'2' || input[1] !=
'2' || input[2] !=
'0') {
1001 if (input[3] !=
' ' && input[3] !=
'-') {
1007 if (memchr(input + 4,
'\n',
len - 4) != NULL) {
1014 static int FTPRegisterPatternsForProtocolDetection(
void)
1017 IPPROTO_TCP,
ALPROTO_FTP,
"220 (", 5, 0, STREAM_TOCLIENT) < 0) {
1021 IPPROTO_TCP,
ALPROTO_FTP,
"FEAT", 4, 0, STREAM_TOSERVER) < 0) {
1025 STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
1030 IPPROTO_TCP,
ALPROTO_FTP,
"PORT ", 5, 0, STREAM_TOSERVER) < 0) {
1036 "tcp", IPPROTO_TCP,
"ftp",
ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
1040 FTPQuitProbingParser, FTPServerProbingParser);
1058 const uint8_t *input = StreamSliceGetData(&stream_slice);
1059 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
1060 const bool eof = (direction & STREAM_TOSERVER)
1066 ftpdata_state->
tx_data.
file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
1067 if (direction & STREAM_TOSERVER) {
1076 SCLogDebug(
"FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len,
flags, direction,
1077 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient", eof ?
"true" :
"false");
1081 FtpTransferCmd *data =
1088 if ((direction & data->direction) == 0) {
1090 SCLogDebug(
"input %u not for our direction (%s): %s/%s", input_len,
1091 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1092 data->cmd == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1093 (data->direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1097 if (data->file_name) {
1099 if (ftpdata_state->
files == NULL) {
1104 ftpdata_state->
file_name = data->file_name;
1105 ftpdata_state->
file_len = data->file_len;
1106 data->file_name = NULL;
1110 ftpdata_state->
command = data->cmd;
1111 switch (data->cmd) {
1112 case FTP_COMMAND_STOR:
1113 ftpdata_state->
direction = data->direction;
1115 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1117 case FTP_COMMAND_APPE:
1118 ftpdata_state->
direction = data->direction;
1120 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1122 case FTP_COMMAND_STOU:
1123 ftpdata_state->
direction = data->direction;
1125 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1127 case FTP_COMMAND_RETR:
1128 ftpdata_state->
direction = data->direction;
1130 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1132 case FTP_COMMAND_NLST:
1133 ftpdata_state->
direction = data->direction;
1135 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1137 case FTP_COMMAND_LIST:
1138 ftpdata_state->
direction = data->direction;
1140 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1142 case FTP_COMMAND_MLSD:
1143 ftpdata_state->
direction = data->direction;
1145 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1156 input_len,
flags) != 0) {
1166 if ((direction & ftpdata_state->
direction) == 0) {
1170 SCLogDebug(
"input %u not for us (%s): %s/%s", input_len,
1171 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1172 ftpdata_state->
command == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1173 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1176 if (ftpdata_state->
state == FTPDATA_STATE_FINISHED) {
1181 if (ftpdata_state->
file_name && input_len != 0) {
1185 SCLogDebug(
"FileAppendData() - file no longer being extracted");
1187 }
else if (ret < 0) {
1188 SCLogDebug(
"FileAppendData() failed: %d", ret);
1200 ftpdata_state->
state = FTPDATA_STATE_FINISHED;
1201 SCLogDebug(
"closed because of eof: state now FTPDATA_STATE_FINISHED");
1213 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1219 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1224 static uint64_t ftpdata_state_memuse = 0;
1225 static uint64_t ftpdata_state_memcnt = 0;
1228 static void *FTPDataStateAlloc(
void *orig_state,
AppProto proto_orig)
1235 state->
state = FTPDATA_STATE_IN_PROGRESS;
1239 ftpdata_state_memcnt++;
1246 static void FTPDataStateFree(
void *s)
1261 ftpdata_state_memcnt--;
1279 static void FTPDataStateTransactionFree(
void *state, uint64_t tx_id)
1284 static void *FTPDataGetTx(
void *state, uint64_t tx_id)
1290 static uint64_t FTPDataGetTxCnt(
void *state)
1296 static int FTPDataGetAlstateProgress(
void *tx, uint8_t direction)
1299 if (direction == ftpdata_state->
direction)
1300 return ftpdata_state->
state;
1302 return FTPDATA_STATE_FINISHED;
1310 if (direction == ftpdata_state->
direction)
1311 files.
fc = ftpdata_state->
files;
1316 static void FTPSetMpmState(
void)
1319 if (
unlikely(ftp_mpm_ctx == NULL)) {
1324 SCFTPSetMpmState(ftp_mpm_ctx);
1328 static void FTPFreeMpmState(
void)
1330 if (ftp_mpm_ctx != NULL) {
1348 if (state->
un.
ptr == NULL) {
1354 while (tx_ptr->
tx_id < min_tx_id) {
1360 if (tx_ptr->
tx_id >= max_tx_id) {
1367 .has_next = (state->
un.
ptr != NULL),
1377 const char *proto_name =
"ftp";
1378 const char *proto_data_name =
"ftp-data";
1383 if (FTPRegisterPatternsForProtocolDetection() < 0 )
1395 IPPROTO_TCP,
ALPROTO_FTP, STREAM_TOSERVER | STREAM_TOCLIENT);
1405 FTPLocalStorageFree);
1411 ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1415 FTPDataParseRequest);
1417 FTPDataParseResponse);
1443 sbcfg.
Calloc = FTPCalloc;
1445 sbcfg.
Free = FTPFree;
1449 SCLogInfo(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1478 if (!buffer || *buffer ==
'\0') {
1482 const char *c = strchr(buffer,
'\n');
1483 return c == NULL ?
len : (uint16_t)(c - buffer + 1);
1489 SCJbOpenObject(jb,
"ftp_data");
1492 SCJbSetStringFromBytes(jb,
"filename", ftp_state->
file_name, ftp_state->
file_len);
1495 case FTP_COMMAND_STOR:
1498 case FTP_COMMAND_APPE:
1501 case FTP_COMMAND_STOU:
1504 case FTP_COMMAND_RETR:
1507 case FTP_COMMAND_NLST:
1510 case FTP_COMMAND_LIST:
1513 case FTP_COMMAND_MLSD:
1537 static int FTPParserTest01(
void)
1540 uint8_t ftpbuf[] =
"PORT 192,168,1,1,0,80\r\n";
1541 uint32_t ftplen =
sizeof(ftpbuf) - 1;
1545 memset(&f, 0,
sizeof(f));
1546 memset(&ssn, 0,
sizeof(ssn));
1549 f.
proto = IPPROTO_TCP;
1555 STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1569 static int FTPParserTest11(
void)
1572 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1573 uint8_t ftpbuf2[] =
"RETR\r\n";
1574 uint8_t ftpbuf3[] =
"227 OK\r\n";
1579 memset(&f, 0,
sizeof(f));
1580 memset(&ssn, 0,
sizeof(ssn));
1583 f.
proto = IPPROTO_TCP;
1589 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1590 sizeof(ftpbuf1) - 1);
1597 sizeof(ftpbuf3) - 1);
1601 STREAM_TOSERVER, ftpbuf2,
1602 sizeof(ftpbuf2) - 1);
1617 static int FTPParserTest12(
void)
1620 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1621 uint8_t ftpbuf2[] =
"STOR\r\n";
1622 uint8_t ftpbuf3[] =
"227 OK\r\n";
1627 memset(&f, 0,
sizeof(f));
1628 memset(&ssn, 0,
sizeof(ssn));
1631 f.
proto = IPPROTO_TCP;
1637 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1638 sizeof(ftpbuf1) - 1);
1645 sizeof(ftpbuf3) - 1);
1649 STREAM_TOSERVER, ftpbuf2,
1650 sizeof(ftpbuf2) - 1);
1666 static int FTPParserTest13(
void)
1669 uint8_t ftpbuf[] =
"USER anonymous \r\n";
1670 const char expected[] =
"USER anonymous";
1675 memset(&f, 0,
sizeof(f));
1676 memset(&ssn, 0,
sizeof(ssn));
1679 f.
proto = IPPROTO_TCP;
1687 ftpbuf,
sizeof(ftpbuf) - 1);
1713 static int FTPParserTest14(
void)
1716 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80 \r\n";
1717 uint8_t ftpbuf2[] =
"227 OK\r\n";
1718 const char expected[] =
"PORT 192,168,1,1,0,80";
1723 memset(&f, 0,
sizeof(f));
1724 memset(&ssn, 0,
sizeof(ssn));
1727 f.
proto = IPPROTO_TCP;
1733 ftpbuf1,
sizeof(ftpbuf1) - 1);
1742 FAIL_IF(memcmp(ftp_state->
port_line, expected,
sizeof(expected) - 1) != 0);