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) {
248 SCLogDebug(
"new transaction %p (state tx cnt %"PRIu64
")", tx, state->
tx_cnt);
263 while ((wrapper =
TAILQ_FIRST(&tx->response_list))) {
265 FTPResponseWrapperFree(wrapper);
268 FTPFree(tx,
sizeof(*tx));
287 const uint8_t *lf_idx = memchr(input->
buf + input->
consumed, 0x0a, input->
len);
289 if (lf_idx == NULL) {
291 *current_line_truncated =
true;
299 }
else if (*current_line_truncated) {
301 *current_line_truncated =
false;
311 uint32_t o_consumed = input->
consumed;
312 input->
consumed = (uint32_t)(lf_idx - input->
buf + 1);
317 line->
buf = input->
buf + o_consumed;
319 *current_line_truncated =
true;
344 static int FTPParseRequestCommand(
355 uint8_t command_code;
356 if (SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], NULL, &command_code, NULL)) {
367 const char *command_name = NULL;
368 (void)SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], &command_name, NULL, NULL);
369 SCLogDebug(
"matching FTP command is %s [code: %d, index %d]", command_name,
379 static void FtpTransferCmdFree(
void *data)
381 FtpTransferCmd *cmd = (FtpTransferCmd *)data;
384 if (cmd->file_name) {
385 FTPFree((
void *)cmd->file_name, cmd->file_len + 1);
387 SCFTPTransferCmdFree(cmd);
388 FTPDecrMemuse((uint64_t)
sizeof(FtpTransferCmd));
391 static uint32_t CopyCommandLine(uint8_t **dest,
FtpLineState *line)
394 uint8_t *where = FTPCalloc(line->
len + 1,
sizeof(
char));
398 memcpy(where, line->
buf, line->
len);
401 while (line->
len && isspace((
unsigned char)where[line->
len - 1])) {
405 where[line->
len] =
'\0';
409 return line->
len ? line->
len + 1 : 0;
432 const uint8_t *input = StreamSliceGetData(&stream_slice);
433 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
437 }
else if (input == NULL || input_len == 0) {
441 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
442 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
444 uint8_t direction = STREAM_TOSERVER;
450 }
else if (res.
status == -1) {
455 if (!FTPParseRequestCommand(thread_data, &line, &cmd_descriptor)) {
456 state->
command = FTP_COMMAND_UNKNOWN;
484 (state->
command == FTP_COMMAND_STOR || state->
command == FTP_COMMAND_APPE ||
485 state->
command == FTP_COMMAND_STOU)) ||
487 (state->
command == FTP_COMMAND_RETR || state->
command == FTP_COMMAND_NLST ||
488 state->
command == FTP_COMMAND_LIST ||
489 state->
command == FTP_COMMAND_MLSD))) {
490 direction = STREAM_TOCLIENT;
493 bool has_file =
false;
496 case FTP_COMMAND_EPRT:
498 case FTP_COMMAND_PORT:
517 case FTP_COMMAND_RETR:
519 case FTP_COMMAND_STOR:
521 case FTP_COMMAND_APPE:
532 case FTP_COMMAND_STOU:
537 case FTP_COMMAND_NLST:
538 case FTP_COMMAND_LIST:
539 case FTP_COMMAND_MLSD: {
546 FtpTransferCmd *data = SCFTPTransferCmdNew();
549 FTPIncrMemuse((uint64_t)(
sizeof *data));
551 data->flow_id = FlowGetId(f);
552 data->direction = direction;
553 data->data_free = FtpTransferCmdFree;
561 #if SC_FILENAME_MAX > UINT16_MAX
562 #error SC_FILENAME_MAX is greater than UINT16_MAX
564 data->file_name = FTPCalloc(file_name_len + 1,
sizeof(
char));
565 if (data->file_name == NULL) {
566 FtpTransferCmdFree(data);
569 data->file_name[file_name_len] = 0;
570 data->file_len = (uint16_t)file_name_len;
571 memcpy(data->file_name, line.
buf + 5, file_name_len);
572 }
else if (state->
command == FTP_COMMAND_STOU) {
573 const char default_file_name[] =
"<stou>";
574 uint32_t file_name_len =
sizeof(default_file_name);
575 data->file_name = FTPCalloc(file_name_len,
sizeof(
char));
576 if (data->file_name == NULL) {
577 FtpTransferCmdFree(data);
580 data->file_name[file_name_len - 1] = 0;
581 data->file_len = (uint16_t)file_name_len - 1;
582 memcpy(data->file_name, default_file_name, file_name_len);
587 FtpTransferCmdFree(data);
591 SCLogDebug(
"Expectation created [direction: %s, dynamic port %" PRIu16
"].",
615 static int FTPParsePassiveResponse(
FtpState *state,
const uint8_t *input, uint32_t input_len)
617 uint16_t dyn_port = SCFTPParsePortPasv(input, input_len);
621 SCLogDebug(
"FTP passive mode (v4): dynamic port %"PRIu16
"", dyn_port);
630 static int FTPParsePassiveResponseV6(
FtpState *state,
const uint8_t *input, uint32_t input_len)
632 uint16_t dyn_port = SCFTPParsePortEpsv(input, input_len);
636 SCLogDebug(
"FTP passive mode (v6): dynamic port %"PRIu16
"", dyn_port);
653 static inline bool FTPIsPPR(
const uint8_t *input, uint32_t input_len)
655 return input_len >= 4 && isdigit(input[0]) && input[0] ==
'1' &&
656 isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
673 const uint8_t *input = StreamSliceGetData(&stream_slice);
674 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
679 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
680 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
688 }
else if (res.
status == -1) {
693 tx = FTPTransactionCreate(state);
704 if (state->
command == FTP_COMMAND_UNKNOWN) {
712 case FTP_COMMAND_AUTH_TLS:
718 case FTP_COMMAND_EPRT:
727 SCLogDebug(
"FTP active mode (v6): dynamic port %" PRIu16
"", dyn_port);
730 case FTP_COMMAND_PORT:
739 SCLogDebug(
"FTP active mode (v4): dynamic port %" PRIu16
"", dyn_port);
742 case FTP_COMMAND_PASV:
744 FTPParsePassiveResponse(ftp_state, line.
buf, line.
len);
748 case FTP_COMMAND_EPSV:
750 FTPParsePassiveResponseV6(ftp_state, line.
buf, line.
len);
758 FTPResponseLine *response = SCFTPParseResponseLine((
const char *)line.
buf, line.
len);
763 if (response->truncated) {
772 SCFTPFreeResponseLine(response);
775 SCLogDebug(
"unable to parse FTP response line \"%s\"", line.
buf);
780 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)
971 static AppProto FTPQuitProbingParser(
972 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
979 if (
SCMemcmp(input,
"QUIT", 4) != 0) {
985 static AppProto FTPServerProbingParser(
986 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
993 if (input[0] !=
'2' || input[1] !=
'2' || input[2] !=
'0') {
997 if (input[3] !=
' ' && input[3] !=
'-') {
1003 if (memchr(input + 4,
'\n',
len - 4) != NULL) {
1010 static int FTPRegisterPatternsForProtocolDetection(
void)
1013 IPPROTO_TCP,
ALPROTO_FTP,
"220 (", 5, 0, STREAM_TOCLIENT) < 0) {
1017 IPPROTO_TCP,
ALPROTO_FTP,
"FEAT", 4, 0, STREAM_TOSERVER) < 0) {
1021 STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
1026 IPPROTO_TCP,
ALPROTO_FTP,
"PORT ", 5, 0, STREAM_TOSERVER) < 0) {
1032 "tcp", IPPROTO_TCP,
"ftp",
ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
1036 FTPQuitProbingParser, FTPServerProbingParser);
1054 const uint8_t *input = StreamSliceGetData(&stream_slice);
1055 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
1056 const bool eof = (direction & STREAM_TOSERVER)
1062 ftpdata_state->
tx_data.
file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
1063 if (direction & STREAM_TOSERVER) {
1072 SCLogDebug(
"FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len,
flags, direction,
1073 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient", eof ?
"true" :
"false");
1077 FtpTransferCmd *data =
1084 if ((direction & data->direction) == 0) {
1086 SCLogDebug(
"input %u not for our direction (%s): %s/%s", input_len,
1087 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1088 data->cmd == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1089 (data->direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1093 if (data->file_name) {
1095 if (ftpdata_state->
files == NULL) {
1100 ftpdata_state->
file_name = data->file_name;
1101 ftpdata_state->
file_len = data->file_len;
1102 data->file_name = NULL;
1106 ftpdata_state->
command = data->cmd;
1107 switch (data->cmd) {
1108 case FTP_COMMAND_STOR:
1109 ftpdata_state->
direction = data->direction;
1111 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1113 case FTP_COMMAND_APPE:
1114 ftpdata_state->
direction = data->direction;
1116 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1118 case FTP_COMMAND_STOU:
1119 ftpdata_state->
direction = data->direction;
1121 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1123 case FTP_COMMAND_RETR:
1124 ftpdata_state->
direction = data->direction;
1126 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1128 case FTP_COMMAND_NLST:
1129 ftpdata_state->
direction = data->direction;
1131 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1133 case FTP_COMMAND_LIST:
1134 ftpdata_state->
direction = data->direction;
1136 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1138 case FTP_COMMAND_MLSD:
1139 ftpdata_state->
direction = data->direction;
1141 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1152 input_len,
flags) != 0) {
1162 if ((direction & ftpdata_state->
direction) == 0) {
1166 SCLogDebug(
"input %u not for us (%s): %s/%s", input_len,
1167 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1168 ftpdata_state->
command == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1169 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1172 if (ftpdata_state->
state == FTPDATA_STATE_FINISHED) {
1177 if (ftpdata_state->
file_name && input_len != 0) {
1181 SCLogDebug(
"FileAppendData() - file no longer being extracted");
1183 }
else if (ret < 0) {
1184 SCLogDebug(
"FileAppendData() failed: %d", ret);
1196 ftpdata_state->
state = FTPDATA_STATE_FINISHED;
1197 SCLogDebug(
"closed because of eof: state now FTPDATA_STATE_FINISHED");
1209 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1215 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1220 static uint64_t ftpdata_state_memuse = 0;
1221 static uint64_t ftpdata_state_memcnt = 0;
1224 static void *FTPDataStateAlloc(
void *orig_state,
AppProto proto_orig)
1231 state->
state = FTPDATA_STATE_IN_PROGRESS;
1235 ftpdata_state_memcnt++;
1242 static void FTPDataStateFree(
void *s)
1257 ftpdata_state_memcnt--;
1275 static void FTPDataStateTransactionFree(
void *state, uint64_t tx_id)
1280 static void *FTPDataGetTx(
void *state, uint64_t tx_id)
1286 static uint64_t FTPDataGetTxCnt(
void *state)
1292 static int FTPDataGetAlstateProgress(
void *tx, uint8_t direction)
1295 if (direction == ftpdata_state->
direction)
1296 return ftpdata_state->
state;
1298 return FTPDATA_STATE_FINISHED;
1306 if (direction == ftpdata_state->
direction)
1307 files.
fc = ftpdata_state->
files;
1312 static void FTPSetMpmState(
void)
1315 if (
unlikely(ftp_mpm_ctx == NULL)) {
1320 SCFTPSetMpmState(ftp_mpm_ctx);
1324 static void FTPFreeMpmState(
void)
1326 if (ftp_mpm_ctx != NULL) {
1344 if (state->
un.
ptr == NULL) {
1350 while (tx_ptr->
tx_id < min_tx_id) {
1356 if (tx_ptr->
tx_id >= max_tx_id) {
1363 .has_next = (state->
un.
ptr != NULL),
1373 const char *proto_name =
"ftp";
1374 const char *proto_data_name =
"ftp-data";
1379 if (FTPRegisterPatternsForProtocolDetection() < 0 )
1391 IPPROTO_TCP,
ALPROTO_FTP, STREAM_TOSERVER | STREAM_TOCLIENT);
1401 FTPLocalStorageFree);
1407 ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1411 FTPDataParseRequest);
1413 FTPDataParseResponse);
1439 sbcfg.
Calloc = FTPCalloc;
1441 sbcfg.
Free = FTPFree;
1445 SCLogInfo(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1474 if (!buffer || *buffer ==
'\0') {
1478 const char *c = strchr(buffer,
'\n');
1479 return c == NULL ?
len : (uint16_t)(c - buffer + 1);
1485 SCJbOpenObject(jb,
"ftp_data");
1488 SCJbSetStringFromBytes(jb,
"filename", ftp_state->
file_name, ftp_state->
file_len);
1491 case FTP_COMMAND_STOR:
1494 case FTP_COMMAND_APPE:
1497 case FTP_COMMAND_STOU:
1500 case FTP_COMMAND_RETR:
1503 case FTP_COMMAND_NLST:
1506 case FTP_COMMAND_LIST:
1509 case FTP_COMMAND_MLSD:
1533 static int FTPParserTest01(
void)
1536 uint8_t ftpbuf[] =
"PORT 192,168,1,1,0,80\r\n";
1537 uint32_t ftplen =
sizeof(ftpbuf) - 1;
1541 memset(&f, 0,
sizeof(f));
1542 memset(&ssn, 0,
sizeof(ssn));
1545 f.
proto = IPPROTO_TCP;
1551 STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1565 static int FTPParserTest11(
void)
1568 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1569 uint8_t ftpbuf2[] =
"RETR\r\n";
1570 uint8_t ftpbuf3[] =
"227 OK\r\n";
1575 memset(&f, 0,
sizeof(f));
1576 memset(&ssn, 0,
sizeof(ssn));
1579 f.
proto = IPPROTO_TCP;
1585 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1586 sizeof(ftpbuf1) - 1);
1593 sizeof(ftpbuf3) - 1);
1597 STREAM_TOSERVER, ftpbuf2,
1598 sizeof(ftpbuf2) - 1);
1613 static int FTPParserTest12(
void)
1616 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1617 uint8_t ftpbuf2[] =
"STOR\r\n";
1618 uint8_t ftpbuf3[] =
"227 OK\r\n";
1623 memset(&f, 0,
sizeof(f));
1624 memset(&ssn, 0,
sizeof(ssn));
1627 f.
proto = IPPROTO_TCP;
1633 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1634 sizeof(ftpbuf1) - 1);
1641 sizeof(ftpbuf3) - 1);
1645 STREAM_TOSERVER, ftpbuf2,
1646 sizeof(ftpbuf2) - 1);