46 #define FTP_MPM mpm_default_matcher
48 static MpmCtx *ftp_mpm_ctx = NULL;
59 static void FTPParseMemcap(
void)
67 static void FTPIncrMemuse(uint64_t size)
72 static void FTPDecrMemuse(uint64_t size)
105 static int FTPCheckMemcap(uint64_t size)
113 static void *FTPCalloc(
size_t n,
size_t size)
115 if (FTPCheckMemcap((uint32_t)(n * size)) == 0) {
127 FTPIncrMemuse((uint64_t)(n * size));
131 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);
170 FTPFree(
str->str,
str->len);
176 static void *FTPLocalStorageAlloc(
void)
185 if (td->
pmq == NULL) {
198 static void FTPLocalStorageFree(
void *ptr)
202 if (td->
pmq != NULL) {
233 SCLogDebug(
"new transaction %p (state tx cnt %"PRIu64
")", tx, state->
tx_cnt);
241 if (tx->
tx_data.de_state != NULL) {
259 FTPFree(tx,
sizeof(*tx));
269 static AppLayerResult FTPGetLineForDirection(
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;
413 StreamSlice stream_slice,
void *local_data)
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;
439 if (res.status == 1) {
441 }
else if (res.status == -1) {
446 if (!FTPParseRequestCommand(thread_data, &line, &cmd_descriptor)) {
447 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 = rs_ftp_pasv_response(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 = rs_ftp_epsv_response(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]);
617 StreamSlice stream_slice,
void *local_data)
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);
704 response->
len = CopyCommandLine(&response->
str, &line);
708 &tx->
tx_data.events, FtpEventResponseCommandTooLong);
718 if (FTPIsPPR(line.
buf, line.
len)) {
736 static uint64_t ftp_state_memuse = 0;
737 static uint64_t ftp_state_memcnt = 0;
740 static void *FTPStateAlloc(
void *orig_state,
AppProto proto_orig)
742 void *s = FTPCalloc(1,
sizeof(
FtpState));
758 static void FTPStateFree(
void *s)
769 const char *command_name = NULL;
770 (void)SCGetFtpCommandInfo(
772 SCLogDebug(
"[%s] state %p id %" PRIu64
", Freeing %d bytes at %p",
778 FTPTransactionFree(tx);
801 SCLogDebug(
"NULL state object; no transactions available");
818 SCLogDebug(
"Returning OLDEST tx %p id %"PRIu64, lasttx, lasttx->
tx_id);
822 static void *FTPGetTx(
void *state, uint64_t tx_id)
828 if (ftp_state->
curr_tx == NULL)
834 if (tx->
tx_id == tx_id)
847 static AppLayerStateData *FTPGetStateData(
void *vstate)
853 static void FTPStateTransactionFree(
void *state, uint64_t tx_id)
858 if (tx_id < tx->tx_id)
860 else if (tx_id > tx->
tx_id)
866 FTPTransactionFree(tx);
871 static uint64_t FTPGetTxCnt(
void *state)
882 static int FTPGetAlstateProgress(
void *vtx, uint8_t direction)
888 if (direction == STREAM_TOSERVER &&
890 return FTP_STATE_PORT_DONE;
892 return FTP_STATE_IN_PROGRESS;
895 return FTP_STATE_FINISHED;
898 static AppProto FTPUserProbingParser(
899 Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
908 static AppProto FTPServerProbingParser(
909 Flow *f, uint8_t direction,
const uint8_t *input, uint32_t
len, uint8_t *rdir)
916 if (input[0] !=
'2' || input[1] !=
'2' || input[2] !=
'0') {
920 if (input[3] !=
' ' && input[3] !=
'-') {
926 if (memchr(input + 4,
'\n',
len - 4) != NULL) {
933 static int FTPRegisterPatternsForProtocolDetection(
void)
936 IPPROTO_TCP,
ALPROTO_FTP,
"220 (", 5, 0, STREAM_TOCLIENT) < 0) {
940 IPPROTO_TCP,
ALPROTO_FTP,
"FEAT", 4, 0, STREAM_TOSERVER) < 0) {
944 STREAM_TOSERVER, FTPUserProbingParser, 5, 5) < 0) {
948 IPPROTO_TCP,
ALPROTO_FTP,
"PASS ", 5, 0, STREAM_TOSERVER) < 0) {
952 IPPROTO_TCP,
ALPROTO_FTP,
"PORT ", 5, 0, STREAM_TOSERVER) < 0) {
958 "tcp", IPPROTO_TCP,
"ftp",
ALPROTO_FTP, 0, 5, NULL, FTPServerProbingParser)) {
962 FTPServerProbingParser);
978 AppLayerParserState *pstate, StreamSlice stream_slice,
void *local_data, uint8_t direction)
980 const uint8_t *input = StreamSliceGetData(&stream_slice);
981 uint32_t input_len = StreamSliceGetDataLen(&stream_slice);
982 const bool eof = (direction & STREAM_TOSERVER)
986 SCTxDataUpdateFileFlags(&ftpdata_state->
tx_data, ftpdata_state->
state_data.file_flags);
987 if (ftpdata_state->
tx_data.file_tx == 0)
988 ftpdata_state->
tx_data.file_tx = direction & (STREAM_TOSERVER | STREAM_TOCLIENT);
989 if (direction & STREAM_TOSERVER) {
990 ftpdata_state->
tx_data.updated_ts =
true;
992 ftpdata_state->
tx_data.updated_tc =
true;
998 SCLogDebug(
"FTP-DATA input_len %u flags %04x dir %d/%s EOF %s", input_len,
flags, direction,
999 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient", eof ?
"true" :
"false");
1002 if (input_len && ftpdata_state->
files == NULL) {
1003 FtpTransferCmd *data =
1010 if ((direction & data->direction) == 0) {
1012 SCLogDebug(
"input %u not for our direction (%s): %s/%s", input_len,
1013 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1014 data->cmd == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1015 (data->direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1020 if (ftpdata_state->
files == NULL) {
1025 ftpdata_state->
file_name = data->file_name;
1026 ftpdata_state->
file_len = data->file_len;
1027 data->file_name = NULL;
1030 ftpdata_state->
command = data->cmd;
1031 switch (data->cmd) {
1032 case FTP_COMMAND_STOR:
1033 ftpdata_state->
direction = data->direction;
1035 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1037 case FTP_COMMAND_RETR:
1038 ftpdata_state->
direction = data->direction;
1040 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1049 0ULL, (uint8_t *) ftpdata_state->
file_name,
1051 input, input_len,
flags) != 0) {
1056 ftpdata_state->
tx_data.files_opened = 1;
1058 if (ftpdata_state->
state == FTPDATA_STATE_FINISHED) {
1063 if ((direction & ftpdata_state->
direction) == 0) {
1067 SCLogDebug(
"input %u not for us (%s): %s/%s", input_len,
1068 (direction & STREAM_TOSERVER) ?
"toserver" :
"toclient",
1069 ftpdata_state->
command == FTP_COMMAND_STOR ?
"STOR" :
"RETR",
1070 (ftpdata_state->
direction & STREAM_TOSERVER) ?
"toserver" :
"toclient");
1073 if (input_len != 0) {
1077 SCLogDebug(
"FileAppendData() - file no longer being extracted");
1079 }
else if (ret < 0) {
1080 SCLogDebug(
"FileAppendData() failed: %d", ret);
1090 ftpdata_state->
state = FTPDATA_STATE_FINISHED;
1091 SCLogDebug(
"closed because of eof: state now FTPDATA_STATE_FINISHED");
1101 StreamSlice stream_slice,
void *local_data)
1103 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOSERVER);
1107 StreamSlice stream_slice,
void *local_data)
1109 return FTPDataParse(f, ftp_state, pstate, stream_slice, local_data, STREAM_TOCLIENT);
1114 static uint64_t ftpdata_state_memuse = 0;
1115 static uint64_t ftpdata_state_memcnt = 0;
1118 static void *FTPDataStateAlloc(
void *orig_state,
AppProto proto_orig)
1125 state->
state = FTPDATA_STATE_IN_PROGRESS;
1129 ftpdata_state_memcnt++;
1136 static void FTPDataStateFree(
void *s)
1140 if (fstate->
tx_data.de_state != NULL) {
1152 ftpdata_state_memcnt--;
1164 static AppLayerStateData *FTPDataGetStateData(
void *vstate)
1170 static void FTPDataStateTransactionFree(
void *state, uint64_t tx_id)
1175 static void *FTPDataGetTx(
void *state, uint64_t tx_id)
1181 static uint64_t FTPDataGetTxCnt(
void *state)
1187 static int FTPDataGetAlstateProgress(
void *tx, uint8_t direction)
1190 if (direction == ftpdata_state->
direction)
1191 return ftpdata_state->
state;
1193 return FTPDATA_STATE_FINISHED;
1196 static AppLayerGetFileState FTPDataStateGetTxFiles(
void *tx, uint8_t direction)
1199 AppLayerGetFileState files = { .fc = NULL, .cfg = &sbcfg };
1201 if (direction == ftpdata_state->
direction)
1202 files.fc = ftpdata_state->
files;
1207 static void FTPSetMpmState(
void)
1210 if (
unlikely(ftp_mpm_ctx == NULL)) {
1215 SCFTPSetMpmState(ftp_mpm_ctx);
1220 static void FTPFreeMpmState(
void)
1222 if (ftp_mpm_ctx != NULL) {
1233 static AppLayerGetTxIterTuple FTPGetTxIterator(
const uint8_t ipproto,
const AppProto alproto,
1237 AppLayerGetTxIterTuple no_tuple = { NULL, 0,
false };
1240 if (state->
un.
ptr == NULL) {
1246 while (tx_ptr->
tx_id < min_tx_id) {
1252 if (tx_ptr->
tx_id >= max_tx_id) {
1256 AppLayerGetTxIterTuple tuple = {
1259 .has_next = (state->
un.
ptr != NULL),
1269 const char *proto_name =
"ftp";
1270 const char *proto_data_name =
"ftp-data";
1275 if (FTPRegisterPatternsForProtocolDetection() < 0 )
1296 FTPLocalStorageFree);
1302 ALPROTO_FTP, FTP_STATE_FINISHED, FTP_STATE_FINISHED);
1306 FTPDataParseRequest);
1308 FTPDataParseResponse);
1333 sbcfg.
Calloc = FTPCalloc;
1335 sbcfg.
Free = FTPFree;
1339 SCLogInfo(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);
1368 if (!buffer || *buffer ==
'\0') {
1372 char *c = strchr(buffer,
'\n');
1373 return c == NULL ?
len : (uint16_t)(c - buffer + 1);
1379 jb_open_object(jb,
"ftp_data");
1382 jb_set_string_from_bytes(jb,
"filename", ftp_state->
file_name, ftp_state->
file_len);
1385 case FTP_COMMAND_STOR:
1388 case FTP_COMMAND_RETR:
1411 static int FTPParserTest01(
void)
1414 uint8_t ftpbuf[] =
"PORT 192,168,1,1,0,80\r\n";
1415 uint32_t ftplen =
sizeof(ftpbuf) - 1;
1419 memset(&f, 0,
sizeof(f));
1420 memset(&ssn, 0,
sizeof(ssn));
1423 f.
proto = IPPROTO_TCP;
1429 STREAM_TOSERVER | STREAM_EOF, ftpbuf, ftplen);
1442 static int FTPParserTest11(
void)
1445 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1446 uint8_t ftpbuf2[] =
"RETR\r\n";
1447 uint8_t ftpbuf3[] =
"227 OK\r\n";
1452 memset(&f, 0,
sizeof(f));
1453 memset(&ssn, 0,
sizeof(ssn));
1456 f.
proto = IPPROTO_TCP;
1462 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1463 sizeof(ftpbuf1) - 1);
1470 sizeof(ftpbuf3) - 1);
1474 STREAM_TOSERVER, ftpbuf2,
1475 sizeof(ftpbuf2) - 1);
1489 static int FTPParserTest12(
void)
1492 uint8_t ftpbuf1[] =
"PORT 192,168,1,1,0,80\r\n";
1493 uint8_t ftpbuf2[] =
"STOR\r\n";
1494 uint8_t ftpbuf3[] =
"227 OK\r\n";
1499 memset(&f, 0,
sizeof(f));
1500 memset(&ssn, 0,
sizeof(ssn));
1503 f.
proto = IPPROTO_TCP;
1509 STREAM_TOSERVER | STREAM_START, ftpbuf1,
1510 sizeof(ftpbuf1) - 1);
1517 sizeof(ftpbuf3) - 1);
1521 STREAM_TOSERVER, ftpbuf2,
1522 sizeof(ftpbuf2) - 1);