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;
485 direction = STREAM_TOCLIENT;
489 case FTP_COMMAND_EPRT:
491 case FTP_COMMAND_PORT:
510 case FTP_COMMAND_RETR:
512 case FTP_COMMAND_STOR: {
519 FtpTransferCmd *data = SCFTPTransferCmdNew();
522 FTPIncrMemuse((uint64_t)(
sizeof *data));
523 data->data_free = FtpTransferCmdFree;
530 #if SC_FILENAME_MAX > UINT16_MAX
531 #error SC_FILENAME_MAX is greater than UINT16_MAX
533 data->file_name = FTPCalloc(file_name_len + 1,
sizeof(
char));
534 if (data->file_name == NULL) {
535 FtpTransferCmdFree(data);
538 data->file_name[file_name_len] = 0;
539 data->file_len = (uint16_t)file_name_len;
540 memcpy(data->file_name, line.
buf + 5, file_name_len);
542 data->flow_id = FlowGetId(f);
543 data->direction = direction;
547 FtpTransferCmdFree(data);
551 SCLogDebug(
"Expectation created [direction: %s, dynamic port %"PRIu16
"].",
552 state->
active ?
"to server" :
"to client",
574 static int FTPParsePassiveResponse(
FtpState *state,
const uint8_t *input, uint32_t input_len)
576 uint16_t dyn_port = SCFTPParsePortPasv(input, input_len);
580 SCLogDebug(
"FTP passive mode (v4): dynamic port %"PRIu16
"", dyn_port);
589 static int FTPParsePassiveResponseV6(
FtpState *state,
const uint8_t *input, uint32_t input_len)
591 uint16_t dyn_port = SCFTPParsePortEpsv(input, input_len);
595 SCLogDebug(
"FTP passive mode (v6): dynamic port %"PRIu16
"", dyn_port);
612 static inline bool FTPIsPPR(
const uint8_t *input, uint32_t input_len)
614 return input_len >= 4 && isdigit(input[0]) && input[0] ==
'1' &&
615 isdigit(input[1]) && isdigit(input[2]) && isspace(input[3]);
632 const uint8_t *input = StreamSliceGetData(&stream_slice);
633 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
638 FtpInput ftpi = { .
buf = input, .len = input_len, .orig_len = input_len, .consumed = 0 };
639 FtpLineState line = { .
buf = NULL, .len = 0, .delim_len = 0, .lf_found =
false };
647 }
else if (res.
status == -1) {
652 tx = FTPTransactionCreate(state);
659 if (state->
command == FTP_COMMAND_UNKNOWN) {
667 case FTP_COMMAND_AUTH_TLS:
673 case FTP_COMMAND_EPRT:
682 SCLogDebug(
"FTP active mode (v6): dynamic port %" PRIu16
"", dyn_port);
685 case FTP_COMMAND_PORT:
694 SCLogDebug(
"FTP active mode (v4): dynamic port %" PRIu16
"", dyn_port);
697 case FTP_COMMAND_PASV:
699 FTPParsePassiveResponse(ftp_state, line.
buf, line.
len);
703 case FTP_COMMAND_EPSV:
705 FTPParsePassiveResponseV6(ftp_state, line.
buf, line.
len);
713 FTPResponseLine *response = SCFTPParseResponseLine((
const char *)line.
buf, line.
len);
718 if (response->truncated) {
727 SCFTPFreeResponseLine(response);
730 SCLogDebug(
"unable to parse FTP response line \"%s\"", line.
buf);
735 if (FTPIsPPR(line.
buf, line.
len)) {
754 static uint64_t ftp_state_memuse = 0;
755 static uint64_t ftp_state_memcnt = 0;
758 static void *FTPStateAlloc(
void *orig_state,
AppProto proto_orig)
760 void *s = FTPCalloc(1,
sizeof(
FtpState));
776 static void FTPStateFree(
void *s)
787 const char *command_name = NULL;
788 (void)SCGetFtpCommandInfo(
790 SCLogDebug(
"[%s] state %p id %" PRIu64
", Freeing %d bytes at %p",
796 FTPTransactionFree(tx);
819 SCLogDebug(
"NULL state object; no transactions available");
836 SCLogDebug(
"Returning OLDEST tx %p id %"PRIu64, lasttx, lasttx->
tx_id);
840 static void *FTPGetTx(
void *state, uint64_t tx_id)
846 if (ftp_state->
curr_tx == NULL)
852 if (tx->
tx_id == tx_id)
871 static void FTPStateTransactionFree(
void *state, uint64_t tx_id)
876 if (tx_id < tx->tx_id)
878 else if (tx_id > tx->
tx_id)
884 FTPTransactionFree(tx);
889 static uint64_t FTPGetTxCnt(
void *state)
900 static int FTPGetAlstateProgress(
void *vtx, uint8_t direction)
906 if (direction == STREAM_TOSERVER) {
907 return FTP_STATE_FINISHED;
910 return FTP_STATE_IN_PROGRESS;
913 return FTP_STATE_FINISHED;
916 static AppProto FTPUserProbingParser(
917 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
926 static AppProto FTPQuitProbingParser(
927 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
934 if (
SCMemcmp(input,
"QUIT", 4) != 0) {
940 static AppProto FTPServerProbingParser(
941 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
948 if (input[0] !=
'2' || input[1] !=
'2' || input[2] !=
'0') {
952 if (input[3] !=
' ' && input[3] !=
'-') {
958 if (memchr(input + 4,
'\n',
len - 4) != NULL) {
965 static int FTPRegisterPatternsForProtocolDetection(
void)
968 IPPROTO_TCP,
ALPROTO_FTP,
"220 (", 5, 0, STREAM_TOCLIENT) < 0) {
972 IPPROTO_TCP,
ALPROTO_FTP,
"FEAT", 4, 0, STREAM_TOSERVER) < 0) {
976 STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
981 IPPROTO_TCP,
ALPROTO_FTP,
"PORT ", 5, 0, STREAM_TOSERVER) < 0) {
987 "tcp", IPPROTO_TCP,
"ftp",
ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
991 FTPQuitProbingParser, FTPServerProbingParser);
1009 const uint8_t *input = StreamSliceGetData(&stream_slice);
1010 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
1011 const bool eof = (direction & STREAM_TOSERVER)
1017 ftpdata_state->
tx_data.
file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
1018 if (direction & STREAM_TOSERVER) {
1027 SCLogDebug(
"FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len,
flags, direction,
1028 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient", eof ?
"true" :
"false");
1031 if (input_len && ftpdata_state->
files == NULL) {
1032 FtpTransferCmd *data =
1039 if ((direction & data->direction) == 0) {
1041 SCLogDebug(
"input %u not for our direction (%s): %s/%s", input_len,
1042 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1043 data->cmd == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1044 (data->direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1049 if (ftpdata_state->
files == NULL) {
1054 ftpdata_state->
file_name = data->file_name;
1055 ftpdata_state->
file_len = data->file_len;
1056 data->file_name = NULL;
1059 ftpdata_state->
command = data->cmd;
1060 switch (data->cmd) {
1061 case FTP_COMMAND_STOR:
1062 ftpdata_state->
direction = data->direction;
1064 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1066 case FTP_COMMAND_RETR:
1067 ftpdata_state->
direction = data->direction;
1069 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1078 0ULL, (uint8_t *) ftpdata_state->
file_name,
1080 input, input_len,
flags) != 0) {
1087 if ((direction & ftpdata_state->
direction) == 0) {
1091 SCLogDebug(
"input %u not for us (%s): %s/%s", input_len,
1092 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1093 ftpdata_state->
command == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1094 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1097 if (ftpdata_state->
state == FTPDATA_STATE_FINISHED) {
1102 if (input_len != 0) {
1106 SCLogDebug(
"FileAppendData() - file no longer being extracted");
1108 }
else if (ret < 0) {
1109 SCLogDebug(
"FileAppendData() failed: %d", ret);
1119 ftpdata_state->
state = FTPDATA_STATE_FINISHED;
1120 SCLogDebug(
"closed because of eof: state now FTPDATA_STATE_FINISHED");
1132 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1138 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1143 static uint64_t ftpdata_state_memuse = 0;
1144 static uint64_t ftpdata_state_memcnt = 0;
1147 static void *FTPDataStateAlloc(
void *orig_state,
AppProto proto_orig)
1154 state->
state = FTPDATA_STATE_IN_PROGRESS;
1158 ftpdata_state_memcnt++;
1165 static void FTPDataStateFree(
void *s)
1180 ftpdata_state_memcnt--;
1198 static void FTPDataStateTransactionFree(
void *state, uint64_t tx_id)
1203 static void *FTPDataGetTx(
void *state, uint64_t tx_id)
1209 static uint64_t FTPDataGetTxCnt(
void *state)
1215 static int FTPDataGetAlstateProgress(
void *tx, uint8_t direction)
1218 if (direction == ftpdata_state->
direction)
1219 return ftpdata_state->
state;
1221 return FTPDATA_STATE_FINISHED;
1229 if (direction == ftpdata_state->
direction)
1230 files.
fc = ftpdata_state->
files;
1235 static void FTPSetMpmState(
void)
1238 if (
unlikely(ftp_mpm_ctx == NULL)) {
1243 SCFTPSetMpmState(ftp_mpm_ctx);
1247 static void FTPFreeMpmState(
void)
1249 if (ftp_mpm_ctx != NULL) {
1267 if (state->
un.
ptr == NULL) {
1273 while (tx_ptr->
tx_id < min_tx_id) {
1279 if (tx_ptr->
tx_id >= max_tx_id) {
1286 .has_next = (state->
un.
ptr != NULL),
1296 const char *proto_name =
"ftp";
1297 const char *proto_data_name =
"ftp-data";
1302 if (FTPRegisterPatternsForProtocolDetection() < 0 )
1314 IPPROTO_TCP,
ALPROTO_FTP, STREAM_TOSERVER | STREAM_TOCLIENT);
1324 FTPLocalStorageFree);
1330 ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1334 FTPDataParseRequest);
1336 FTPDataParseResponse);
1362 sbcfg.
Calloc = FTPCalloc;
1364 sbcfg.
Free = FTPFree;
1368 SCLogInfo(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1397 if (!buffer || *buffer ==
'\0') {
1401 const char *c = strchr(buffer,
'\n');
1402 return c == NULL ?
len : (uint16_t)(c - buffer + 1);
1408 SCJbOpenObject(jb,
"ftp_data");
1411 SCJbSetStringFromBytes(jb,
"filename", ftp_state->
file_name, ftp_state->
file_len);
1414 case FTP_COMMAND_STOR:
1417 case FTP_COMMAND_RETR:
1441 static int FTPParserTest01(
void)
1444 uint8_t ftpbuf[] =
"PORT 192,168,1,1,0,80\r\n";
1445 uint32_t ftplen =
sizeof(ftpbuf) - 1;
1449 memset(&f, 0,
sizeof(f));
1450 memset(&ssn, 0,
sizeof(ssn));
1453 f.
proto = IPPROTO_TCP;
1459 STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1473 static int FTPParserTest11(
void)
1476 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1477 uint8_t ftpbuf2[] =
"RETR\r\n";
1478 uint8_t ftpbuf3[] =
"227 OK\r\n";
1483 memset(&f, 0,
sizeof(f));
1484 memset(&ssn, 0,
sizeof(ssn));
1487 f.
proto = IPPROTO_TCP;
1493 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1494 sizeof(ftpbuf1) - 1);
1501 sizeof(ftpbuf3) - 1);
1505 STREAM_TOSERVER, ftpbuf2,
1506 sizeof(ftpbuf2) - 1);
1521 static int FTPParserTest12(
void)
1524 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1525 uint8_t ftpbuf2[] =
"STOR\r\n";
1526 uint8_t ftpbuf3[] =
"227 OK\r\n";
1531 memset(&f, 0,
sizeof(f));
1532 memset(&ssn, 0,
sizeof(ssn));
1535 f.
proto = IPPROTO_TCP;
1541 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1542 sizeof(ftpbuf1) - 1);
1549 sizeof(ftpbuf3) - 1);
1553 STREAM_TOSERVER, ftpbuf2,
1554 sizeof(ftpbuf2) - 1);