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));
288 const uint8_t *lf_idx = memchr(input->
buf + input->
consumed, 0x0a, input->
len);
290 if (lf_idx == NULL) {
292 *current_line_truncated =
true;
300 }
else if (*current_line_truncated) {
302 *current_line_truncated =
false;
312 uint32_t o_consumed = input->
consumed;
313 input->
consumed = (uint32_t)(lf_idx - input->
buf + 1);
318 line->
buf = input->
buf + o_consumed;
320 *current_line_truncated =
true;
345 static int FTPParseRequestCommand(
356 uint8_t command_code;
357 if (SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], NULL, &command_code, NULL)) {
368 const char *command_name = NULL;
369 (void)SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], &command_name, NULL, NULL);
370 SCLogDebug(
"matching FTP command is %s [code: %d, index %d]", command_name,
380 static void FtpTransferCmdFree(
void *data)
382 FtpTransferCmd *cmd = (FtpTransferCmd *)data;
385 if (cmd->file_name) {
386 FTPFree((
void *)cmd->file_name, cmd->file_len + 1);
388 SCFTPTransferCmdFree(cmd);
389 FTPDecrMemuse((uint64_t)
sizeof(FtpTransferCmd));
392 static uint32_t CopyCommandLine(uint8_t **dest,
FtpLineState *line)
395 uint8_t *where = FTPCalloc(line->
len + 1,
sizeof(
char));
399 memcpy(where, line->
buf, line->
len);
402 while (line->
len && isspace((
unsigned char)where[line->
len - 1])) {
406 where[line->
len] =
'\0';
410 return line->
len ? line->
len + 1 : 0;
433 const uint8_t *input = StreamSliceGetData(&stream_slice);
434 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
438 }
else if (input == NULL || input_len == 0) {
442 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
443 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
445 uint8_t direction = STREAM_TOSERVER;
451 }
else if (res.
status == -1) {
456 if (!FTPParseRequestCommand(thread_data, &line, &cmd_descriptor)) {
457 state->
command = FTP_COMMAND_UNKNOWN;
485 (state->
command == FTP_COMMAND_STOR || state->
command == FTP_COMMAND_APPE ||
486 state->
command == FTP_COMMAND_STOU)) ||
488 (state->
command == FTP_COMMAND_RETR || state->
command == FTP_COMMAND_NLST ||
489 state->
command == FTP_COMMAND_LIST ||
490 state->
command == FTP_COMMAND_MLSD))) {
491 direction = STREAM_TOCLIENT;
494 bool has_file =
false;
497 case FTP_COMMAND_EPRT:
499 case FTP_COMMAND_PORT:
518 case FTP_COMMAND_RETR:
520 case FTP_COMMAND_STOR:
522 case FTP_COMMAND_APPE:
533 case FTP_COMMAND_STOU:
538 case FTP_COMMAND_NLST:
539 case FTP_COMMAND_LIST:
540 case FTP_COMMAND_MLSD: {
547 FtpTransferCmd *data = SCFTPTransferCmdNew();
550 FTPIncrMemuse((uint64_t)(
sizeof *data));
552 data->flow_id = FlowGetId(f);
553 data->direction = direction;
554 data->data_free = FtpTransferCmdFree;
562 #if SC_FILENAME_MAX > UINT16_MAX
563 #error SC_FILENAME_MAX is greater than UINT16_MAX
565 data->file_name = FTPCalloc(file_name_len + 1,
sizeof(
char));
566 if (data->file_name == NULL) {
567 FtpTransferCmdFree(data);
570 data->file_name[file_name_len] = 0;
571 data->file_len = (uint16_t)file_name_len;
572 memcpy(data->file_name, line.
buf + 5, file_name_len);
573 }
else if (state->
command == FTP_COMMAND_STOU) {
574 const char default_file_name[] =
"<stou>";
575 uint32_t file_name_len =
sizeof(default_file_name);
576 data->file_name = FTPCalloc(file_name_len,
sizeof(
char));
577 if (data->file_name == NULL) {
578 FtpTransferCmdFree(data);
581 data->file_name[file_name_len - 1] = 0;
582 data->file_len = (uint16_t)file_name_len - 1;
583 memcpy(data->file_name, default_file_name, file_name_len);
588 FtpTransferCmdFree(data);
592 SCLogDebug(
"Expectation created [direction: %s, dynamic port %" PRIu16
"].",
616 static int FTPParsePassiveResponse(
FtpState *state,
const uint8_t *input, uint32_t input_len)
618 uint16_t dyn_port = SCFTPParsePortPasv(input, input_len);
622 SCLogDebug(
"FTP passive mode (v4): dynamic port %"PRIu16
"", dyn_port);
631 static int FTPParsePassiveResponseV6(
FtpState *state,
const uint8_t *input, uint32_t input_len)
633 uint16_t dyn_port = SCFTPParsePortEpsv(input, input_len);
637 SCLogDebug(
"FTP passive mode (v6): dynamic port %"PRIu16
"", dyn_port);
654 static inline bool FTPIsPPR(
const uint8_t *input, uint32_t input_len)
656 return input_len >= 4 && isdigit(input[0]) && input[0] ==
'1' &&
657 isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
674 const uint8_t *input = StreamSliceGetData(&stream_slice);
675 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
680 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
681 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
689 }
else if (res.
status == -1) {
694 tx = FTPTransactionCreate(state);
705 if (state->
command == FTP_COMMAND_UNKNOWN) {
713 case FTP_COMMAND_AUTH_TLS:
719 case FTP_COMMAND_EPRT:
728 SCLogDebug(
"FTP active mode (v6): dynamic port %" PRIu16
"", dyn_port);
731 case FTP_COMMAND_PORT:
740 SCLogDebug(
"FTP active mode (v4): dynamic port %" PRIu16
"", dyn_port);
743 case FTP_COMMAND_PASV:
745 FTPParsePassiveResponse(ftp_state, line.
buf, line.
len);
749 case FTP_COMMAND_EPSV:
751 FTPParsePassiveResponseV6(ftp_state, line.
buf, line.
len);
759 FTPResponseLine *response = SCFTPParseResponseLine((
const char *)line.
buf, line.
len);
764 if (response->truncated) {
773 SCFTPFreeResponseLine(response);
776 SCLogDebug(
"unable to parse FTP response line \"%s\"", line.
buf);
781 if (FTPIsPPR(line.
buf, line.
len)) {
800 static uint64_t ftp_state_memuse = 0;
801 static uint64_t ftp_state_memcnt = 0;
804 static void *FTPStateAlloc(
void *orig_state,
AppProto proto_orig)
806 void *s = FTPCalloc(1,
sizeof(
FtpState));
822 static void FTPStateFree(
void *s)
833 const char *command_name = NULL;
834 (void)SCGetFtpCommandInfo(
836 SCLogDebug(
"[%s] state %p id %" PRIu64
", Freeing %d bytes at %p",
842 FTPTransactionFree(tx);
865 SCLogDebug(
"NULL state object; no transactions available");
882 SCLogDebug(
"Returning OLDEST tx %p id %"PRIu64, lasttx, lasttx->
tx_id);
886 static void *FTPGetTx(
void *state, uint64_t tx_id)
892 if (ftp_state->
curr_tx == NULL)
898 if (tx->
tx_id == tx_id)
917 static void FTPStateTransactionFree(
void *state, uint64_t tx_id)
922 if (tx_id < tx->tx_id)
924 else if (tx_id > tx->
tx_id)
930 FTPTransactionFree(tx);
935 static uint64_t FTPGetTxCnt(
void *state)
946 static int FTPGetAlstateProgress(
void *vtx, uint8_t direction)
952 if (direction == STREAM_TOSERVER) {
953 return FTP_STATE_FINISHED;
956 return FTP_STATE_IN_PROGRESS;
959 return FTP_STATE_FINISHED;
962 static AppProto FTPUserProbingParser(
963 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
972 static AppProto FTPQuitProbingParser(
973 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
980 if (
SCMemcmp(input,
"QUIT", 4) != 0) {
986 static AppProto FTPServerProbingParser(
987 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
994 if (input[0] !=
'2' || input[1] !=
'2' || input[2] !=
'0') {
998 if (input[3] !=
' ' && input[3] !=
'-') {
1004 if (memchr(input + 4,
'\n',
len - 4) != NULL) {
1011 static int FTPRegisterPatternsForProtocolDetection(
void)
1014 IPPROTO_TCP,
ALPROTO_FTP,
"220 (", 5, 0, STREAM_TOCLIENT) < 0) {
1018 IPPROTO_TCP,
ALPROTO_FTP,
"FEAT", 4, 0, STREAM_TOSERVER) < 0) {
1022 STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
1027 IPPROTO_TCP,
ALPROTO_FTP,
"PORT ", 5, 0, STREAM_TOSERVER) < 0) {
1033 "tcp", IPPROTO_TCP,
"ftp",
ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
1037 FTPQuitProbingParser, FTPServerProbingParser);
1055 const uint8_t *input = StreamSliceGetData(&stream_slice);
1056 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
1057 const bool eof = (direction & STREAM_TOSERVER)
1063 ftpdata_state->
tx_data.
file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
1064 if (direction & STREAM_TOSERVER) {
1073 SCLogDebug(
"FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len,
flags, direction,
1074 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient", eof ?
"true" :
"false");
1078 FtpTransferCmd *data =
1085 if ((direction & data->direction) == 0) {
1087 SCLogDebug(
"input %u not for our direction (%s): %s/%s", input_len,
1088 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1089 data->cmd == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1090 (data->direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1094 if (data->file_name) {
1096 if (ftpdata_state->
files == NULL) {
1101 ftpdata_state->
file_name = data->file_name;
1102 ftpdata_state->
file_len = data->file_len;
1103 data->file_name = NULL;
1107 ftpdata_state->
command = data->cmd;
1108 switch (data->cmd) {
1109 case FTP_COMMAND_STOR:
1110 ftpdata_state->
direction = data->direction;
1112 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1114 case FTP_COMMAND_APPE:
1115 ftpdata_state->
direction = data->direction;
1117 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1119 case FTP_COMMAND_STOU:
1120 ftpdata_state->
direction = data->direction;
1122 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1124 case FTP_COMMAND_RETR:
1125 ftpdata_state->
direction = data->direction;
1127 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1129 case FTP_COMMAND_NLST:
1130 ftpdata_state->
direction = data->direction;
1132 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1134 case FTP_COMMAND_LIST:
1135 ftpdata_state->
direction = data->direction;
1137 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1139 case FTP_COMMAND_MLSD:
1140 ftpdata_state->
direction = data->direction;
1142 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1153 input_len,
flags) != 0) {
1163 if ((direction & ftpdata_state->
direction) == 0) {
1167 SCLogDebug(
"input %u not for us (%s): %s/%s", input_len,
1168 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1169 ftpdata_state->
command == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1170 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1173 if (ftpdata_state->
state == FTPDATA_STATE_FINISHED) {
1178 if (ftpdata_state->
file_name && input_len != 0) {
1182 SCLogDebug(
"FileAppendData() - file no longer being extracted");
1184 }
else if (ret < 0) {
1185 SCLogDebug(
"FileAppendData() failed: %d", ret);
1197 ftpdata_state->
state = FTPDATA_STATE_FINISHED;
1198 SCLogDebug(
"closed because of eof: state now FTPDATA_STATE_FINISHED");
1210 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1216 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1221 static uint64_t ftpdata_state_memuse = 0;
1222 static uint64_t ftpdata_state_memcnt = 0;
1225 static void *FTPDataStateAlloc(
void *orig_state,
AppProto proto_orig)
1232 state->
state = FTPDATA_STATE_IN_PROGRESS;
1236 ftpdata_state_memcnt++;
1243 static void FTPDataStateFree(
void *s)
1258 ftpdata_state_memcnt--;
1276 static void FTPDataStateTransactionFree(
void *state, uint64_t tx_id)
1281 static void *FTPDataGetTx(
void *state, uint64_t tx_id)
1287 static uint64_t FTPDataGetTxCnt(
void *state)
1293 static int FTPDataGetAlstateProgress(
void *tx, uint8_t direction)
1296 if (direction == ftpdata_state->
direction)
1297 return ftpdata_state->
state;
1299 return FTPDATA_STATE_FINISHED;
1307 if (direction == ftpdata_state->
direction)
1308 files.
fc = ftpdata_state->
files;
1313 static void FTPSetMpmState(
void)
1316 if (
unlikely(ftp_mpm_ctx == NULL)) {
1321 SCFTPSetMpmState(ftp_mpm_ctx);
1325 static void FTPFreeMpmState(
void)
1327 if (ftp_mpm_ctx != NULL) {
1345 if (state->
un.
ptr == NULL) {
1351 while (tx_ptr->
tx_id < min_tx_id) {
1357 if (tx_ptr->
tx_id >= max_tx_id) {
1364 .has_next = (state->
un.
ptr != NULL),
1374 const char *proto_name =
"ftp";
1375 const char *proto_data_name =
"ftp-data";
1380 if (FTPRegisterPatternsForProtocolDetection() < 0 )
1392 IPPROTO_TCP,
ALPROTO_FTP, STREAM_TOSERVER | STREAM_TOCLIENT);
1402 FTPLocalStorageFree);
1408 ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1412 FTPDataParseRequest);
1414 FTPDataParseResponse);
1440 sbcfg.
Calloc = FTPCalloc;
1442 sbcfg.
Free = FTPFree;
1446 SCLogInfo(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1475 if (!buffer || *buffer ==
'\0') {
1479 const char *c = strchr(buffer,
'\n');
1480 return c == NULL ?
len : (uint16_t)(c - buffer + 1);
1486 SCJbOpenObject(jb,
"ftp_data");
1489 SCJbSetStringFromBytes(jb,
"filename", ftp_state->
file_name, ftp_state->
file_len);
1492 case FTP_COMMAND_STOR:
1495 case FTP_COMMAND_APPE:
1498 case FTP_COMMAND_STOU:
1501 case FTP_COMMAND_RETR:
1504 case FTP_COMMAND_NLST:
1507 case FTP_COMMAND_LIST:
1510 case FTP_COMMAND_MLSD:
1534 static int FTPParserTest01(
void)
1537 uint8_t ftpbuf[] =
"PORT 192,168,1,1,0,80\r\n";
1538 uint32_t ftplen =
sizeof(ftpbuf) - 1;
1542 memset(&f, 0,
sizeof(f));
1543 memset(&ssn, 0,
sizeof(ssn));
1546 f.
proto = IPPROTO_TCP;
1552 STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1566 static int FTPParserTest11(
void)
1569 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1570 uint8_t ftpbuf2[] =
"RETR\r\n";
1571 uint8_t ftpbuf3[] =
"227 OK\r\n";
1576 memset(&f, 0,
sizeof(f));
1577 memset(&ssn, 0,
sizeof(ssn));
1580 f.
proto = IPPROTO_TCP;
1586 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1587 sizeof(ftpbuf1) - 1);
1594 sizeof(ftpbuf3) - 1);
1598 STREAM_TOSERVER, ftpbuf2,
1599 sizeof(ftpbuf2) - 1);
1614 static int FTPParserTest12(
void)
1617 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1618 uint8_t ftpbuf2[] =
"STOR\r\n";
1619 uint8_t ftpbuf3[] =
"227 OK\r\n";
1624 memset(&f, 0,
sizeof(f));
1625 memset(&ssn, 0,
sizeof(ssn));
1628 f.
proto = IPPROTO_TCP;
1634 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1635 sizeof(ftpbuf1) - 1);
1642 sizeof(ftpbuf3) - 1);
1646 STREAM_TOSERVER, ftpbuf2,
1647 sizeof(ftpbuf2) - 1);