47 #define FTP_MPM mpm_default_matcher
49 static MpmCtx *ftp_mpm_ctx = NULL;
60 static void FTPParseMemcap(
void)
68 static void FTPIncrMemuse(uint64_t size)
73 static void FTPDecrMemuse(uint64_t size)
106 static int FTPCheckMemcap(uint64_t size)
114 static void *FTPCalloc(
size_t n,
size_t size)
116 if (FTPCheckMemcap((uint32_t)(n * size)) == 0) {
128 FTPIncrMemuse((uint64_t)(n * size));
132 static void *FTPRealloc(
void *ptr,
size_t orig_size,
size_t size)
134 if (FTPCheckMemcap((uint32_t)(size - orig_size)) == 0) {
145 if (size > orig_size) {
146 FTPIncrMemuse(size - orig_size);
148 FTPDecrMemuse(orig_size - size);
154 static void FTPFree(
void *ptr,
size_t size)
158 FTPDecrMemuse((uint64_t)size);
165 FTPIncrMemuse(response->total_size);
174 FTPDecrMemuse(wrapper->
response->total_size);
175 SCFTPFreeResponseLine(wrapper->
response);
181 static void *FTPLocalStorageAlloc(
void)
190 if (td->
pmq == NULL) {
203 static void FTPLocalStorageFree(
void *ptr)
207 if (td->
pmq != NULL) {
238 SCLogDebug(
"new transaction %p (state tx cnt %"PRIu64
")", tx, state->
tx_cnt);
246 SCAppLayerTxDataCleanup(&tx->
tx_data);
253 while ((wrapper =
TAILQ_FIRST(&tx->response_list))) {
255 FTPResponseWrapperFree(wrapper);
258 FTPFree(tx,
sizeof(*tx));
277 uint8_t *lf_idx = memchr(input->
buf + input->
consumed, 0x0a, input->
len);
279 if (lf_idx == NULL) {
281 *current_line_truncated =
true;
289 }
else if (*current_line_truncated) {
291 *current_line_truncated =
false;
301 uint32_t o_consumed = input->
consumed;
302 input->
consumed = (uint32_t)(lf_idx - input->
buf + 1);
307 line->
buf = input->
buf + o_consumed;
309 *current_line_truncated =
true;
334 static int FTPParseRequestCommand(
345 uint8_t command_code;
346 if (SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], NULL, &command_code, NULL)) {
357 const char *command_name = NULL;
358 (void)SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], &command_name, NULL, NULL);
359 SCLogDebug(
"matching FTP command is %s [code: %d, index %d]", command_name,
369 static void FtpTransferCmdFree(
void *data)
371 FtpTransferCmd *cmd = (FtpTransferCmd *)data;
374 if (cmd->file_name) {
375 FTPFree((
void *)cmd->file_name, cmd->file_len + 1);
377 SCFTPTransferCmdFree(cmd);
378 FTPDecrMemuse((uint64_t)
sizeof(FtpTransferCmd));
381 static uint32_t CopyCommandLine(uint8_t **dest,
FtpLineState *line)
384 uint8_t *where = FTPCalloc(line->
len + 1,
sizeof(
char));
388 memcpy(where, line->
buf, line->
len);
391 while (line->
len && isspace((
unsigned char)where[line->
len - 1])) {
395 where[line->
len] =
'\0';
399 return line->
len ? line->
len + 1 : 0;
422 const uint8_t *input = StreamSliceGetData(&stream_slice);
423 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
427 }
else if (input == NULL || input_len == 0) {
431 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
432 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
434 uint8_t direction = STREAM_TOSERVER;
438 if (res.status == 1) {
440 }
else if (res.status == -1) {
445 if (!FTPParseRequestCommand(thread_data, &line, &cmd_descriptor)) {
446 state->
command = FTP_COMMAND_UNKNOWN;
475 direction = STREAM_TOCLIENT;
479 case FTP_COMMAND_EPRT:
481 case FTP_COMMAND_PORT:
500 case FTP_COMMAND_RETR:
502 case FTP_COMMAND_STOR: {
509 FtpTransferCmd *data = SCFTPTransferCmdNew();
512 FTPIncrMemuse((uint64_t)(
sizeof *data));
513 data->data_free = FtpTransferCmdFree;
520 #if SC_FILENAME_MAX > UINT16_MAX
521 #error SC_FILENAME_MAX is greater than UINT16_MAX
523 data->file_name = FTPCalloc(file_name_len + 1,
sizeof(
char));
524 if (data->file_name == NULL) {
525 FtpTransferCmdFree(data);
528 data->file_name[file_name_len] = 0;
529 data->file_len = (uint16_t)file_name_len;
530 memcpy(data->file_name, line.
buf + 5, file_name_len);
532 data->flow_id = FlowGetId(f);
533 data->direction = direction;
537 FtpTransferCmdFree(data);
541 SCLogDebug(
"Expectation created [direction: %s, dynamic port %"PRIu16
"].",
542 state->
active ?
"to server" :
"to client",
563 static int FTPParsePassiveResponse(
FtpState *state,
const uint8_t *input, uint32_t input_len)
565 uint16_t dyn_port = SCFTPParsePortPasv(input, input_len);
569 SCLogDebug(
"FTP passive mode (v4): dynamic port %"PRIu16
"", dyn_port);
578 static int FTPParsePassiveResponseV6(
FtpState *state,
const uint8_t *input, uint32_t input_len)
580 uint16_t dyn_port = SCFTPParsePortEpsv(input, input_len);
584 SCLogDebug(
"FTP passive mode (v6): dynamic port %"PRIu16
"", dyn_port);
601 static inline bool FTPIsPPR(
const uint8_t *input, uint32_t input_len)
603 return input_len >= 4 && isdigit(input[0]) && input[0] ==
'1' &&
604 isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
621 const uint8_t *input = StreamSliceGetData(&stream_slice);
622 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
627 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
628 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
634 if (res.status == 1) {
636 }
else if (res.status == -1) {
641 tx = FTPTransactionCreate(state);
648 if (state->
command == FTP_COMMAND_UNKNOWN) {
656 case FTP_COMMAND_AUTH_TLS:
662 case FTP_COMMAND_EPRT:
671 SCLogDebug(
"FTP active mode (v6): dynamic port %" PRIu16
"", dyn_port);
674 case FTP_COMMAND_PORT:
683 SCLogDebug(
"FTP active mode (v4): dynamic port %" PRIu16
"", dyn_port);
686 case FTP_COMMAND_PASV:
688 FTPParsePassiveResponse(ftp_state, line.
buf, line.
len);
692 case FTP_COMMAND_EPSV:
694 FTPParsePassiveResponseV6(ftp_state, line.
buf, line.
len);
702 FTPResponseLine *response = SCFTPParseResponseLine((
const char *)line.
buf, line.
len);
707 if (response->truncated) {
709 &tx->
tx_data.events, FtpEventResponseCommandTooLong);
716 SCFTPFreeResponseLine(response);
719 SCLogDebug(
"unable to parse FTP response line \"%s\"", line.
buf);
724 if (FTPIsPPR(line.
buf, line.
len)) {
742 static uint64_t ftp_state_memuse = 0;
743 static uint64_t ftp_state_memcnt = 0;
746 static void *FTPStateAlloc(
void *orig_state,
AppProto proto_orig)
748 void *s = FTPCalloc(1,
sizeof(
FtpState));
764 static void FTPStateFree(
void *s)
775 const char *command_name = NULL;
776 (void)SCGetFtpCommandInfo(
778 SCLogDebug(
"[%s] state %p id %" PRIu64
", Freeing %d bytes at %p",
784 FTPTransactionFree(tx);
807 SCLogDebug(
"NULL state object; no transactions available");
824 SCLogDebug(
"Returning OLDEST tx %p id %"PRIu64, lasttx, lasttx->
tx_id);
828 static void *FTPGetTx(
void *state, uint64_t tx_id)
834 if (ftp_state->
curr_tx == NULL)
840 if (tx->
tx_id == tx_id)
859 static void FTPStateTransactionFree(
void *state, uint64_t tx_id)
864 if (tx_id < tx->tx_id)
866 else if (tx_id > tx->
tx_id)
872 FTPTransactionFree(tx);
877 static uint64_t FTPGetTxCnt(
void *state)
888 static int FTPGetAlstateProgress(
void *vtx, uint8_t direction)
894 if (direction == STREAM_TOSERVER) {
895 return FTP_STATE_FINISHED;
898 return FTP_STATE_IN_PROGRESS;
901 return FTP_STATE_FINISHED;
904 static AppProto FTPUserProbingParser(
905 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
914 static AppProto FTPServerProbingParser(
915 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
922 if (input[0] !=
'2' || input[1] !=
'2' || input[2] !=
'0') {
926 if (input[3] !=
' ' && input[3] !=
'-') {
932 if (memchr(input + 4,
'\n',
len - 4) != NULL) {
939 static int FTPRegisterPatternsForProtocolDetection(
void)
942 IPPROTO_TCP,
ALPROTO_FTP,
"220 (", 5, 0, STREAM_TOCLIENT) < 0) {
946 IPPROTO_TCP,
ALPROTO_FTP,
"FEAT", 4, 0, STREAM_TOSERVER) < 0) {
950 STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
954 IPPROTO_TCP,
ALPROTO_FTP,
"PASS ", 5, 0, STREAM_TOSERVER) < 0) {
958 IPPROTO_TCP,
ALPROTO_FTP,
"PORT ", 5, 0, STREAM_TOSERVER) < 0) {
964 "tcp", IPPROTO_TCP,
"ftp",
ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
968 FTPServerProbingParser);
986 const uint8_t *input = StreamSliceGetData(&stream_slice);
987 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
988 const bool eof = (direction & STREAM_TOSERVER)
992 SCTxDataUpdateFileFlags(&ftpdata_state->
tx_data, ftpdata_state->
state_data.file_flags);
993 if (ftpdata_state->
tx_data.file_tx == 0)
994 ftpdata_state->
tx_data.file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
995 if (direction & STREAM_TOSERVER) {
996 ftpdata_state->
tx_data.updated_ts =
true;
998 ftpdata_state->
tx_data.updated_tc =
true;
1004 SCLogDebug(
"FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len,
flags, direction,
1005 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient", eof ?
"true" :
"false");
1008 if (input_len && ftpdata_state->
files == NULL) {
1009 FtpTransferCmd *data =
1016 if ((direction & data->direction) == 0) {
1018 SCLogDebug(
"input %u not for our direction (%s): %s/%s", input_len,
1019 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1020 data->cmd == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1021 (data->direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1026 if (ftpdata_state->
files == NULL) {
1031 ftpdata_state->
file_name = data->file_name;
1032 ftpdata_state->
file_len = data->file_len;
1033 data->file_name = NULL;
1036 ftpdata_state->
command = data->cmd;
1037 switch (data->cmd) {
1038 case FTP_COMMAND_STOR:
1039 ftpdata_state->
direction = data->direction;
1041 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1043 case FTP_COMMAND_RETR:
1044 ftpdata_state->
direction = data->direction;
1046 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1055 0ULL, (uint8_t *) ftpdata_state->
file_name,
1057 input, input_len,
flags) != 0) {
1062 ftpdata_state->
tx_data.files_opened = 1;
1064 if (ftpdata_state->
state == FTPDATA_STATE_FINISHED) {
1069 if ((direction & ftpdata_state->
direction) == 0) {
1073 SCLogDebug(
"input %u not for us (%s): %s/%s", input_len,
1074 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1075 ftpdata_state->
command == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1076 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1079 if (input_len != 0) {
1083 SCLogDebug(
"FileAppendData() - file no longer being extracted");
1085 }
else if (ret < 0) {
1086 SCLogDebug(
"FileAppendData() failed: %d", ret);
1096 ftpdata_state->
state = FTPDATA_STATE_FINISHED;
1097 SCLogDebug(
"closed because of eof: state now FTPDATA_STATE_FINISHED");
1109 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1115 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1120 static uint64_t ftpdata_state_memuse = 0;
1121 static uint64_t ftpdata_state_memcnt = 0;
1124 static void *FTPDataStateAlloc(
void *orig_state,
AppProto proto_orig)
1131 state->
state = FTPDATA_STATE_IN_PROGRESS;
1135 ftpdata_state_memcnt++;
1142 static void FTPDataStateFree(
void *s)
1146 SCAppLayerTxDataCleanup(&fstate->
tx_data);
1157 ftpdata_state_memcnt--;
1175 static void FTPDataStateTransactionFree(
void *state, uint64_t tx_id)
1180 static void *FTPDataGetTx(
void *state, uint64_t tx_id)
1186 static uint64_t FTPDataGetTxCnt(
void *state)
1192 static int FTPDataGetAlstateProgress(
void *tx, uint8_t direction)
1195 if (direction == ftpdata_state->
direction)
1196 return ftpdata_state->
state;
1198 return FTPDATA_STATE_FINISHED;
1206 if (direction == ftpdata_state->
direction)
1207 files.fc = ftpdata_state->
files;
1212 static void FTPSetMpmState(
void)
1215 if (
unlikely(ftp_mpm_ctx == NULL)) {
1220 SCFTPSetMpmState(ftp_mpm_ctx);
1224 static void FTPFreeMpmState(
void)
1226 if (ftp_mpm_ctx != NULL) {
1244 if (state->
un.
ptr == NULL) {
1250 while (tx_ptr->
tx_id < min_tx_id) {
1256 if (tx_ptr->
tx_id >= max_tx_id) {
1263 .has_next = (state->
un.
ptr != NULL),
1273 const char *proto_name =
"ftp";
1274 const char *proto_data_name =
"ftp-data";
1279 if (FTPRegisterPatternsForProtocolDetection() < 0 )
1291 IPPROTO_TCP,
ALPROTO_FTP, STREAM_TOSERVER | STREAM_TOCLIENT);
1301 FTPLocalStorageFree);
1307 ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1311 FTPDataParseRequest);
1313 FTPDataParseResponse);
1339 sbcfg.
Calloc = FTPCalloc;
1341 sbcfg.
Free = FTPFree;
1345 SCLogInfo(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1374 if (!buffer || *buffer ==
'\0') {
1378 char *c = strchr(buffer,
'\n');
1379 return c == NULL ?
len : (uint16_t)(c - buffer + 1);
1385 SCJbOpenObject(jb,
"ftp_data");
1388 SCJbSetStringFromBytes(jb,
"filename", ftp_state->
file_name, ftp_state->
file_len);
1391 case FTP_COMMAND_STOR:
1394 case FTP_COMMAND_RETR:
1417 static int FTPParserTest01(
void)
1420 uint8_t ftpbuf[] =
"PORT 192,168,1,1,0,80\r\n";
1421 uint32_t ftplen =
sizeof(ftpbuf) - 1;
1425 memset(&f, 0,
sizeof(f));
1426 memset(&ssn, 0,
sizeof(ssn));
1429 f.
proto = IPPROTO_TCP;
1435 STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1448 static int FTPParserTest11(
void)
1451 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1452 uint8_t ftpbuf2[] =
"RETR\r\n";
1453 uint8_t ftpbuf3[] =
"227 OK\r\n";
1458 memset(&f, 0,
sizeof(f));
1459 memset(&ssn, 0,
sizeof(ssn));
1462 f.
proto = IPPROTO_TCP;
1468 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1469 sizeof(ftpbuf1) - 1);
1476 sizeof(ftpbuf3) - 1);
1480 STREAM_TOSERVER, ftpbuf2,
1481 sizeof(ftpbuf2) - 1);
1495 static int FTPParserTest12(
void)
1498 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1499 uint8_t ftpbuf2[] =
"STOR\r\n";
1500 uint8_t ftpbuf3[] =
"227 OK\r\n";
1505 memset(&f, 0,
sizeof(f));
1506 memset(&ssn, 0,
sizeof(ssn));
1509 f.
proto = IPPROTO_TCP;
1515 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1516 sizeof(ftpbuf1) - 1);
1523 sizeof(ftpbuf3) - 1);
1527 STREAM_TOSERVER, ftpbuf2,
1528 sizeof(ftpbuf2) - 1);