Implemented FTP user/pwd extraction
This commit is contained in:
@@ -947,6 +947,12 @@ void process_ndpi_collected_info(struct ndpi_workflow * workflow, struct ndpi_fl
|
||||
else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_UBNTAC2) {
|
||||
snprintf(flow->info, sizeof(flow->info), "%s", flow->ndpi_flow->protos.ubntac2.version);
|
||||
}
|
||||
/* FTP */
|
||||
else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_FTP_CONTROL) {
|
||||
snprintf(flow->info, sizeof(flow->info), "User: %s][Pwd: %s",
|
||||
flow->ndpi_flow->protos.ftp.username,
|
||||
flow->ndpi_flow->protos.ftp.password);
|
||||
}
|
||||
/* KERBEROS */
|
||||
else if(flow->detected_protocol.app_protocol == NDPI_PROTOCOL_KERBEROS) {
|
||||
if(flow->ndpi_flow->protos.kerberos.cname[0] != '\0') {
|
||||
|
||||
@@ -1234,6 +1234,10 @@ struct ndpi_flow_struct {
|
||||
u_char nat_ip[24];
|
||||
} http;
|
||||
|
||||
struct {
|
||||
char username[16], password[16];
|
||||
} ftp;
|
||||
|
||||
struct {
|
||||
/* Bittorrent hash */
|
||||
u_char hash[20];
|
||||
|
||||
@@ -881,6 +881,13 @@ int ndpi_flow2json(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
ndpi_serialize_end_of_block(serializer);
|
||||
break;
|
||||
|
||||
case NDPI_PROTOCOL_FTP_CONTROL:
|
||||
ndpi_serialize_start_of_block(serializer, "ftp");
|
||||
ndpi_serialize_string_string(serializer, "user", flow->protos.ftp.username);
|
||||
ndpi_serialize_string_string(serializer, "password", flow->protos.ftp.password);
|
||||
ndpi_serialize_end_of_block(serializer);
|
||||
break;
|
||||
|
||||
case NDPI_PROTOCOL_SSH:
|
||||
ndpi_serialize_start_of_block(serializer, "ssh");
|
||||
ndpi_serialize_string_string(serializer, "client_signature", flow->protos.ssh.client_signature);
|
||||
@@ -909,10 +916,12 @@ int ndpi_flow2json(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
ndpi_serialize_string_string(serializer, "client_cert", flow->protos.stun_ssl.ssl.client_certificate);
|
||||
ndpi_serialize_string_string(serializer, "server_cert", flow->protos.stun_ssl.ssl.server_certificate);
|
||||
ndpi_serialize_string_string(serializer, "issuer", flow->protos.stun_ssl.ssl.server_organization);
|
||||
|
||||
if(before) {
|
||||
strftime(notBefore, sizeof(notBefore), "%F %T", before);
|
||||
ndpi_serialize_string_string(serializer, "notbefore", notBefore);
|
||||
}
|
||||
|
||||
if(after) {
|
||||
strftime(notAfter, sizeof(notAfter), "%F %T", after);
|
||||
ndpi_serialize_string_string(serializer, "notafter", notAfter);
|
||||
|
||||
@@ -27,12 +27,59 @@
|
||||
|
||||
#include "ndpi_api.h"
|
||||
|
||||
// #define FTP_DEBUG
|
||||
|
||||
static void ndpi_int_ftp_control_add_connection(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) {
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow, NDPI_PROTOCOL_FTP_CONTROL, NDPI_PROTOCOL_UNKNOWN);
|
||||
/* *************************************************************** */
|
||||
|
||||
static void ndpi_int_ftp_control_add_connection(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
ndpi_set_detected_protocol(ndpi_struct, flow,
|
||||
NDPI_PROTOCOL_FTP_CONTROL, NDPI_PROTOCOL_UNKNOWN);
|
||||
}
|
||||
|
||||
static int ndpi_ftp_control_check_request(const u_int8_t *payload, size_t payload_len) {
|
||||
/* *************************************************************** */
|
||||
|
||||
static void ftp_payload_copy(u_int8_t *dest, u_int dest_len,
|
||||
const u_int8_t *src, u_int src_len) {
|
||||
u_int i, j, k = dest_len-1;
|
||||
|
||||
for(i=5, j=0; i<src_len; i++) {
|
||||
if((j == k) || ((src[i] == '\r')
|
||||
|| (src[i] == '\n')
|
||||
|| (src[i] == ' ')
|
||||
))
|
||||
break;
|
||||
|
||||
dest[j++] = src[i];
|
||||
}
|
||||
|
||||
dest[k] = '\0';
|
||||
}
|
||||
|
||||
/* *************************************************************** */
|
||||
|
||||
static int ndpi_ftp_control_check_request(struct ndpi_flow_struct *flow,
|
||||
const u_int8_t *payload,
|
||||
size_t payload_len) {
|
||||
#ifdef FTP_DEBUG
|
||||
printf("%s() [%s]\n", __FUNCTION__, payload);
|
||||
#endif
|
||||
|
||||
if(ndpi_match_strprefix(payload, payload_len, "USER")) {
|
||||
ftp_payload_copy((u_int8_t*)flow->protos.ftp.username,
|
||||
sizeof(flow->protos.ftp.username),
|
||||
payload, payload_len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(ndpi_match_strprefix(payload, payload_len, "PASS")) {
|
||||
ftp_payload_copy((u_int8_t*)flow->protos.ftp.password,
|
||||
sizeof(flow->protos.ftp.password),
|
||||
payload, payload_len);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ***************************************************** */
|
||||
|
||||
if(ndpi_match_strprefix(payload, payload_len, "ABOR")) {
|
||||
return 1;
|
||||
@@ -149,10 +196,6 @@ static int ndpi_ftp_control_check_request(const u_int8_t *payload, size_t payloa
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ndpi_match_strprefix(payload, payload_len, "PASS")) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(ndpi_match_strprefix(payload, payload_len, "PASV")) {
|
||||
return 1;
|
||||
}
|
||||
@@ -237,10 +280,6 @@ static int ndpi_ftp_control_check_request(const u_int8_t *payload, size_t payloa
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ndpi_match_strprefix(payload, payload_len, "USER")) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(ndpi_match_strprefix(payload, payload_len, "XCUP")) {
|
||||
return 1;
|
||||
}
|
||||
@@ -524,7 +563,14 @@ static int ndpi_ftp_control_check_request(const u_int8_t *payload, size_t payloa
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ndpi_ftp_control_check_response(const u_int8_t *payload, size_t payload_len) {
|
||||
/* *************************************************************** */
|
||||
|
||||
static int ndpi_ftp_control_check_response(struct ndpi_flow_struct *flow,
|
||||
const u_int8_t *payload,
|
||||
size_t payload_len) {
|
||||
#ifdef FTP_DEBUG
|
||||
printf("%s() [%s]\n", __FUNCTION__, payload);
|
||||
#endif
|
||||
|
||||
if(ndpi_match_strprefix(payload, payload_len, "110-")) {
|
||||
return 1;
|
||||
@@ -936,14 +982,15 @@ static int ndpi_ftp_control_check_response(const u_int8_t *payload, size_t paylo
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ndpi_check_ftp_control(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) {
|
||||
/* *************************************************************** */
|
||||
|
||||
static void ndpi_check_ftp_control(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
u_int32_t payload_len = packet->payload_packet_len;
|
||||
|
||||
/* Check connection over TCP */
|
||||
if(packet->tcp) {
|
||||
|
||||
/* Exclude SMTP, which uses similar commands. */
|
||||
if(packet->tcp->dest == htons(25) || packet->tcp->source == htons(25)) {
|
||||
NDPI_EXCLUDE_PROTO(ndpi_struct, flow);
|
||||
@@ -960,34 +1007,55 @@ static void ndpi_check_ftp_control(struct ndpi_detection_module_struct *ndpi_str
|
||||
if(flow->ftp_control_stage == 0) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "FTP_CONTROL stage 0: \n");
|
||||
|
||||
if ((payload_len > 0) && ndpi_ftp_control_check_request(packet->payload, payload_len)) {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "Possible FTP_CONTROL request detected, we will look further for the response..\n");
|
||||
if((payload_len > 0)
|
||||
&& ndpi_ftp_control_check_request(flow, packet->payload, payload_len)) {
|
||||
NDPI_LOG_DBG2(ndpi_struct,
|
||||
"Possible FTP_CONTROL request detected, we will look further for the response..\n");
|
||||
|
||||
/* Encode the direction of the packet in the stage, so we will know when we need to look for the response packet. */
|
||||
/*
|
||||
Encode the direction of the packet in the stage, so we will know when we need
|
||||
to look for the response packet.
|
||||
*/
|
||||
flow->ftp_control_stage = packet->packet_direction + 1;
|
||||
}
|
||||
|
||||
} else {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "FTP_CONTROL stage %u: \n", flow->ftp_control_stage);
|
||||
|
||||
/* At first check, if this is for sure a response packet (in another direction. If not, do nothing now and return. */
|
||||
/*
|
||||
At first check, if this is for sure a response packet (in another direction.
|
||||
If not, do nothing now and return.
|
||||
*/
|
||||
if((flow->ftp_control_stage - packet->packet_direction) == 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* This is a packet in another direction. Check if we find the proper response. */
|
||||
if ((payload_len > 0) && ndpi_ftp_control_check_response(packet->payload, payload_len)) {
|
||||
if((payload_len > 0)
|
||||
&& ndpi_ftp_control_check_response(flow, packet->payload, payload_len)) {
|
||||
NDPI_LOG_INFO(ndpi_struct, "found FTP_CONTROL\n");
|
||||
|
||||
#ifdef FTP_DEBUG
|
||||
printf("%s() [user: %s][pwd: %s]\n", __FUNCTION__,
|
||||
flow->protos.ftp.username, flow->protos.ftp.password);
|
||||
#endif
|
||||
|
||||
if(flow->protos.ftp.password[0] == '\0')
|
||||
flow->ftp_control_stage = 0;
|
||||
else
|
||||
ndpi_int_ftp_control_add_connection(ndpi_struct, flow);
|
||||
} else {
|
||||
NDPI_LOG_DBG2(ndpi_struct, "The reply did not seem to belong to FTP_CONTROL, resetting the stage to 0\n");
|
||||
NDPI_LOG_DBG2(ndpi_struct, "The reply did not seem to belong to FTP_CONTROL, "
|
||||
"resetting the stage to 0\n");
|
||||
flow->ftp_control_stage = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ndpi_search_ftp_control(struct ndpi_detection_module_struct *ndpi_struct, struct ndpi_flow_struct *flow) {
|
||||
/* *************************************************************** */
|
||||
|
||||
void ndpi_search_ftp_control(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
struct ndpi_flow_struct *flow) {
|
||||
struct ndpi_packet_struct *packet = &flow->packet;
|
||||
|
||||
NDPI_LOG_DBG(ndpi_struct, "search FTP_CONTROL\n");
|
||||
@@ -1000,9 +1068,10 @@ void ndpi_search_ftp_control(struct ndpi_detection_module_struct *ndpi_struct, s
|
||||
}
|
||||
}
|
||||
|
||||
/* *************************************************************** */
|
||||
|
||||
void init_ftp_control_dissector(struct ndpi_detection_module_struct *ndpi_struct, u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask)
|
||||
{
|
||||
void init_ftp_control_dissector(struct ndpi_detection_module_struct *ndpi_struct,
|
||||
u_int32_t *id, NDPI_PROTOCOL_BITMASK *detection_bitmask) {
|
||||
ndpi_set_bitmask_protocol_detection("FTP_CONTROL", ndpi_struct, detection_bitmask, *id,
|
||||
NDPI_PROTOCOL_FTP_CONTROL,
|
||||
ndpi_search_ftp_control,
|
||||
|
||||
@@ -2,7 +2,7 @@ Unknown 1115 1122198 1
|
||||
FTP_CONTROL 68 5571 1
|
||||
FTP_DATA 9 1819 1
|
||||
|
||||
1 TCP 192.168.1.212:50694 <-> 90.130.70.73:21 [proto: 1/FTP_CONTROL][cat: Download-FileTransfer-FileSharing/7][41 pkts/2892 bytes <-> 27 pkts/2679 bytes][bytes ratio: 0.038 (Mixed)][IAT c2s/s2c min/avg/max/stddev: 0/0 235.6/108.5 4743/1377 848.9/304.6][Pkt Len c2s/s2c min/avg/max/stddev: 66/66 70.5/99.2 96/307 6.7/44.9][PLAIN TEXT (vsFTPd 3.0.3)]
|
||||
1 TCP 192.168.1.212:50694 <-> 90.130.70.73:21 [proto: 1/FTP_CONTROL][cat: Download-FileTransfer-FileSharing/7][41 pkts/2892 bytes <-> 27 pkts/2679 bytes][User: anonymous][Pwd: NcFTP@][bytes ratio: 0.038 (Mixed)][IAT c2s/s2c min/avg/max/stddev: 0/0 235.6/108.5 4743/1377 848.9/304.6][Pkt Len c2s/s2c min/avg/max/stddev: 66/66 70.5/99.2 96/307 6.7/44.9][PLAIN TEXT (vsFTPd 3.0.3)]
|
||||
2 TCP 192.168.1.212:50695 <-> 90.130.70.73:25685 [proto: 175/FTP_DATA][cat: Download-FileTransfer-FileSharing/7][5 pkts/342 bytes <-> 4 pkts/1477 bytes][bytes ratio: -0.624 (Download)][IAT c2s/s2c min/avg/max/stddev: 0/28 14.2/28.5 29/29 14.3/0.5][Pkt Len c2s/s2c min/avg/max/stddev: 66/66 68.4/369.2 78/1271 4.8/520.6][PLAIN TEXT ( 1 0 0 1073741)]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user