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) {
239 SCLogDebug(
"new transaction %p (state tx cnt %"PRIu64
")", tx, state->
tx_cnt);
254 while ((wrapper =
TAILQ_FIRST(&tx->response_list))) {
256 FTPResponseWrapperFree(wrapper);
259 FTPFree(tx,
sizeof(*tx));
278 uint8_t *lf_idx = memchr(input->
buf + input->
consumed, 0x0a, input->
len);
280 if (lf_idx == NULL) {
282 *current_line_truncated =
true;
290 }
else if (*current_line_truncated) {
292 *current_line_truncated =
false;
302 uint32_t o_consumed = input->
consumed;
303 input->
consumed = (uint32_t)(lf_idx - input->
buf + 1);
308 line->
buf = input->
buf + o_consumed;
310 *current_line_truncated =
true;
335 static int FTPParseRequestCommand(
346 uint8_t command_code;
347 if (SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], NULL, &command_code, NULL)) {
358 const char *command_name = NULL;
359 (void)SCGetFtpCommandInfo(td->
pmq->
rule_id_array[0], &command_name, NULL, NULL);
360 SCLogDebug(
"matching FTP command is %s [code: %d, index %d]", command_name,
370 static void FtpTransferCmdFree(
void *data)
372 FtpTransferCmd *cmd = (FtpTransferCmd *)data;
375 if (cmd->file_name) {
376 FTPFree((
void *)cmd->file_name, cmd->file_len + 1);
378 SCFTPTransferCmdFree(cmd);
379 FTPDecrMemuse((uint64_t)
sizeof(FtpTransferCmd));
382 static uint32_t CopyCommandLine(uint8_t **dest,
FtpLineState *line)
385 uint8_t *where = FTPCalloc(line->
len + 1,
sizeof(
char));
389 memcpy(where, line->
buf, line->
len);
392 while (line->
len && isspace((
unsigned char)where[line->
len - 1])) {
396 where[line->
len] =
'\0';
400 return line->
len ? line->
len + 1 : 0;
423 const uint8_t *input = StreamSliceGetData(&stream_slice);
424 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
428 }
else if (input == NULL || input_len == 0) {
432 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
433 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
435 uint8_t direction = STREAM_TOSERVER;
441 }
else if (res.
status == -1) {
446 if (!FTPParseRequestCommand(thread_data, &line, &cmd_descriptor)) {
447 state->
command = FTP_COMMAND_UNKNOWN;
476 direction = STREAM_TOCLIENT;
480 case FTP_COMMAND_EPRT:
482 case FTP_COMMAND_PORT:
501 case FTP_COMMAND_RETR:
503 case FTP_COMMAND_STOR: {
510 FtpTransferCmd *data = SCFTPTransferCmdNew();
513 FTPIncrMemuse((uint64_t)(
sizeof *data));
514 data->data_free = FtpTransferCmdFree;
521 #if SC_FILENAME_MAX > UINT16_MAX
522 #error SC_FILENAME_MAX is greater than UINT16_MAX
524 data->file_name = FTPCalloc(file_name_len + 1,
sizeof(
char));
525 if (data->file_name == NULL) {
526 FtpTransferCmdFree(data);
529 data->file_name[file_name_len] = 0;
530 data->file_len = (uint16_t)file_name_len;
531 memcpy(data->file_name, line.
buf + 5, file_name_len);
533 data->flow_id = FlowGetId(f);
534 data->direction = direction;
538 FtpTransferCmdFree(data);
542 SCLogDebug(
"Expectation created [direction: %s, dynamic port %"PRIu16
"].",
543 state->
active ?
"to server" :
"to client",
565 static int FTPParsePassiveResponse(
FtpState *state,
const uint8_t *input, uint32_t input_len)
567 uint16_t dyn_port = SCFTPParsePortPasv(input, input_len);
571 SCLogDebug(
"FTP passive mode (v4): dynamic port %"PRIu16
"", dyn_port);
580 static int FTPParsePassiveResponseV6(
FtpState *state,
const uint8_t *input, uint32_t input_len)
582 uint16_t dyn_port = SCFTPParsePortEpsv(input, input_len);
586 SCLogDebug(
"FTP passive mode (v6): dynamic port %"PRIu16
"", dyn_port);
603 static inline bool FTPIsPPR(
const uint8_t *input, uint32_t input_len)
605 return input_len >= 4 && isdigit(input[0]) && input[0] ==
'1' &&
606 isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
623 const uint8_t *input = StreamSliceGetData(&stream_slice);
624 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
629 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
630 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
638 }
else if (res.
status == -1) {
643 tx = FTPTransactionCreate(state);
650 if (state->
command == FTP_COMMAND_UNKNOWN) {
658 case FTP_COMMAND_AUTH_TLS:
664 case FTP_COMMAND_EPRT:
673 SCLogDebug(
"FTP active mode (v6): dynamic port %" PRIu16
"", dyn_port);
676 case FTP_COMMAND_PORT:
685 SCLogDebug(
"FTP active mode (v4): dynamic port %" PRIu16
"", dyn_port);
688 case FTP_COMMAND_PASV:
690 FTPParsePassiveResponse(ftp_state, line.
buf, line.
len);
694 case FTP_COMMAND_EPSV:
696 FTPParsePassiveResponseV6(ftp_state, line.
buf, line.
len);
704 FTPResponseLine *response = SCFTPParseResponseLine((
const char *)line.
buf, line.
len);
709 if (response->truncated) {
718 SCFTPFreeResponseLine(response);
721 SCLogDebug(
"unable to parse FTP response line \"%s\"", line.
buf);
726 if (FTPIsPPR(line.
buf, line.
len)) {
745 static uint64_t ftp_state_memuse = 0;
746 static uint64_t ftp_state_memcnt = 0;
749 static void *FTPStateAlloc(
void *orig_state,
AppProto proto_orig)
751 void *s = FTPCalloc(1,
sizeof(
FtpState));
767 static void FTPStateFree(
void *s)
778 const char *command_name = NULL;
779 (void)SCGetFtpCommandInfo(
781 SCLogDebug(
"[%s] state %p id %" PRIu64
", Freeing %d bytes at %p",
787 FTPTransactionFree(tx);
810 SCLogDebug(
"NULL state object; no transactions available");
827 SCLogDebug(
"Returning OLDEST tx %p id %"PRIu64, lasttx, lasttx->
tx_id);
831 static void *FTPGetTx(
void *state, uint64_t tx_id)
837 if (ftp_state->
curr_tx == NULL)
843 if (tx->
tx_id == tx_id)
862 static void FTPStateTransactionFree(
void *state, uint64_t tx_id)
867 if (tx_id < tx->tx_id)
869 else if (tx_id > tx->
tx_id)
875 FTPTransactionFree(tx);
880 static uint64_t FTPGetTxCnt(
void *state)
891 static int FTPGetAlstateProgress(
void *vtx, uint8_t direction)
897 if (direction == STREAM_TOSERVER) {
898 return FTP_STATE_FINISHED;
901 return FTP_STATE_IN_PROGRESS;
904 return FTP_STATE_FINISHED;
907 static AppProto FTPUserProbingParser(
908 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
917 static AppProto FTPQuitProbingParser(
918 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
925 if (
SCMemcmp(input,
"QUIT", 4) != 0) {
931 static AppProto FTPServerProbingParser(
932 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
939 if (input[0] !=
'2' || input[1] !=
'2' || input[2] !=
'0') {
943 if (input[3] !=
' ' && input[3] !=
'-') {
949 if (memchr(input + 4,
'\n',
len - 4) != NULL) {
956 static int FTPRegisterPatternsForProtocolDetection(
void)
959 IPPROTO_TCP,
ALPROTO_FTP,
"220 (", 5, 0, STREAM_TOCLIENT) < 0) {
963 IPPROTO_TCP,
ALPROTO_FTP,
"FEAT", 4, 0, STREAM_TOSERVER) < 0) {
967 STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
972 IPPROTO_TCP,
ALPROTO_FTP,
"PORT ", 5, 0, STREAM_TOSERVER) < 0) {
978 "tcp", IPPROTO_TCP,
"ftp",
ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
982 FTPQuitProbingParser, FTPServerProbingParser);
1000 const uint8_t *input = StreamSliceGetData(&stream_slice);
1001 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
1002 const bool eof = (direction & STREAM_TOSERVER)
1008 ftpdata_state->
tx_data.
file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
1009 if (direction & STREAM_TOSERVER) {
1018 SCLogDebug(
"FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len,
flags, direction,
1019 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient", eof ?
"true" :
"false");
1022 if (input_len && ftpdata_state->
files == NULL) {
1023 FtpTransferCmd *data =
1030 if ((direction & data->direction) == 0) {
1032 SCLogDebug(
"input %u not for our direction (%s): %s/%s", input_len,
1033 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1034 data->cmd == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1035 (data->direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1040 if (ftpdata_state->
files == NULL) {
1045 ftpdata_state->
file_name = data->file_name;
1046 ftpdata_state->
file_len = data->file_len;
1047 data->file_name = NULL;
1050 ftpdata_state->
command = data->cmd;
1051 switch (data->cmd) {
1052 case FTP_COMMAND_STOR:
1053 ftpdata_state->
direction = data->direction;
1055 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1057 case FTP_COMMAND_RETR:
1058 ftpdata_state->
direction = data->direction;
1060 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1069 0ULL, (uint8_t *) ftpdata_state->
file_name,
1071 input, input_len,
flags) != 0) {
1078 if (ftpdata_state->
state == FTPDATA_STATE_FINISHED) {
1083 if ((direction & ftpdata_state->
direction) == 0) {
1087 SCLogDebug(
"input %u not for us (%s): %s/%s", input_len,
1088 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1089 ftpdata_state->
command == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1090 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1093 if (input_len != 0) {
1097 SCLogDebug(
"FileAppendData() - file no longer being extracted");
1099 }
else if (ret < 0) {
1100 SCLogDebug(
"FileAppendData() failed: %d", ret);
1110 ftpdata_state->
state = FTPDATA_STATE_FINISHED;
1111 SCLogDebug(
"closed because of eof: state now FTPDATA_STATE_FINISHED");
1123 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1129 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1134 static uint64_t ftpdata_state_memuse = 0;
1135 static uint64_t ftpdata_state_memcnt = 0;
1138 static void *FTPDataStateAlloc(
void *orig_state,
AppProto proto_orig)
1145 state->
state = FTPDATA_STATE_IN_PROGRESS;
1149 ftpdata_state_memcnt++;
1156 static void FTPDataStateFree(
void *s)
1171 ftpdata_state_memcnt--;
1189 static void FTPDataStateTransactionFree(
void *state, uint64_t tx_id)
1194 static void *FTPDataGetTx(
void *state, uint64_t tx_id)
1200 static uint64_t FTPDataGetTxCnt(
void *state)
1206 static int FTPDataGetAlstateProgress(
void *tx, uint8_t direction)
1209 if (direction == ftpdata_state->
direction)
1210 return ftpdata_state->
state;
1212 return FTPDATA_STATE_FINISHED;
1220 if (direction == ftpdata_state->
direction)
1221 files.
fc = ftpdata_state->
files;
1226 static void FTPSetMpmState(
void)
1229 if (
unlikely(ftp_mpm_ctx == NULL)) {
1234 SCFTPSetMpmState(ftp_mpm_ctx);
1238 static void FTPFreeMpmState(
void)
1240 if (ftp_mpm_ctx != NULL) {
1258 if (state->
un.
ptr == NULL) {
1264 while (tx_ptr->
tx_id < min_tx_id) {
1270 if (tx_ptr->
tx_id >= max_tx_id) {
1277 .has_next = (state->
un.
ptr != NULL),
1287 const char *proto_name =
"ftp";
1288 const char *proto_data_name =
"ftp-data";
1293 if (FTPRegisterPatternsForProtocolDetection() < 0 )
1305 IPPROTO_TCP,
ALPROTO_FTP, STREAM_TOSERVER | STREAM_TOCLIENT);
1315 FTPLocalStorageFree);
1321 ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1325 FTPDataParseRequest);
1327 FTPDataParseResponse);
1353 sbcfg.
Calloc = FTPCalloc;
1355 sbcfg.
Free = FTPFree;
1359 SCLogInfo(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1388 if (!buffer || *buffer ==
'\0') {
1392 char *c = strchr(buffer,
'\n');
1393 return c == NULL ?
len : (uint16_t)(c - buffer + 1);
1399 SCJbOpenObject(jb,
"ftp_data");
1402 SCJbSetStringFromBytes(jb,
"filename", ftp_state->
file_name, ftp_state->
file_len);
1405 case FTP_COMMAND_STOR:
1408 case FTP_COMMAND_RETR:
1432 static int FTPParserTest01(
void)
1435 uint8_t ftpbuf[] =
"PORT 192,168,1,1,0,80\r\n";
1436 uint32_t ftplen =
sizeof(ftpbuf) - 1;
1440 memset(&f, 0,
sizeof(f));
1441 memset(&ssn, 0,
sizeof(ssn));
1444 f.
proto = IPPROTO_TCP;
1450 STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1464 static int FTPParserTest11(
void)
1467 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1468 uint8_t ftpbuf2[] =
"RETR\r\n";
1469 uint8_t ftpbuf3[] =
"227 OK\r\n";
1474 memset(&f, 0,
sizeof(f));
1475 memset(&ssn, 0,
sizeof(ssn));
1478 f.
proto = IPPROTO_TCP;
1484 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1485 sizeof(ftpbuf1) - 1);
1492 sizeof(ftpbuf3) - 1);
1496 STREAM_TOSERVER, ftpbuf2,
1497 sizeof(ftpbuf2) - 1);
1512 static int FTPParserTest12(
void)
1515 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1516 uint8_t ftpbuf2[] =
"STOR\r\n";
1517 uint8_t ftpbuf3[] =
"227 OK\r\n";
1522 memset(&f, 0,
sizeof(f));
1523 memset(&ssn, 0,
sizeof(ssn));
1526 f.
proto = IPPROTO_TCP;
1532 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1533 sizeof(ftpbuf1) - 1);
1540 sizeof(ftpbuf3) - 1);
1544 STREAM_TOSERVER, ftpbuf2,
1545 sizeof(ftpbuf2) - 1);