Added -x for checking patterns

This commit is contained in:
Luca Deri
2020-10-22 23:28:18 +02:00
parent 833d0eee53
commit 9b85669a64
+63 -2
View File
@@ -70,6 +70,7 @@ static char *_customCategoryFilePath= NULL; /**< Custom categories file path */
static u_int8_t live_capture = 0; static u_int8_t live_capture = 0;
static u_int8_t undetected_flows_deleted = 0; static u_int8_t undetected_flows_deleted = 0;
FILE *csv_fp = NULL; /**< for CSV export */ FILE *csv_fp = NULL; /**< for CSV export */
static char* domain_to_check = NULL;
/** User preferences **/ /** User preferences **/
u_int8_t enable_protocol_guess = 1, enable_payload_analyzer = 0, num_bin_clusters = 0; u_int8_t enable_protocol_guess = 1, enable_payload_analyzer = 0, num_bin_clusters = 0;
u_int8_t verbose = 0, enable_joy_stats = 0; u_int8_t verbose = 0, enable_joy_stats = 0;
@@ -212,6 +213,54 @@ extern void ndpi_report_payload_stats();
FILE *trace = NULL; FILE *trace = NULL;
#endif #endif
/* *********************************************** */
void ndpiCheckHostStringMatch(char *testChar) {
ndpi_protocol_match_result match = { NDPI_PROTOCOL_UNKNOWN,
NDPI_PROTOCOL_CATEGORY_UNSPECIFIED, NDPI_PROTOCOL_UNRATED };
int testRes;
u_int8_t is_host_match = 1;
char appBufStr[64];
ndpi_protocol detected_protocol;
struct ndpi_detection_module_struct *ndpi_str;
if(!testChar)
return;
ndpi_str = ndpi_init_detection_module(ndpi_no_prefs);
ndpi_finalize_initalization(ndpi_str);
// Display ALL Host strings ie host_match[] ?
// void ac_automata_display (AC_AUTOMATA_t * thiz, char repcast);
//
// ac_automata_display( module->host_automa.ac_automa, 'n');
testRes = ndpi_match_string_subprotocol(ndpi_str,
testChar, strlen(testChar), &match,
is_host_match);
if(testRes) {
memset( &detected_protocol, 0, sizeof(ndpi_protocol) );
detected_protocol.app_protocol = match.protocol_id;
detected_protocol.master_protocol = 0;
detected_protocol.category = match.protocol_category;
ndpi_protocol2name( ndpi_str, detected_protocol, appBufStr,
sizeof(appBufStr));
printf("Match Found for string [%s] -> P(%d) B(%d) C(%d) => %s %s %s\n",
testChar, match.protocol_id, match.protocol_breed,
match.protocol_category,
appBufStr,
ndpi_get_proto_breed_name( ndpi_str, match.protocol_breed ),
ndpi_category_get_name( ndpi_str, match.protocol_category));
} else
printf("Match NOT Found for string: %s\n\n", testChar );
ndpi_exit_detection_module(ndpi_str);
}
/********************** FUNCTIONS ********************* */ /********************** FUNCTIONS ********************* */
/** /**
@@ -344,7 +393,7 @@ static void help(u_int long_help) {
"[-f <filter>][-s <duration>][-m <duration>][-b <num bin clusters>]\n" "[-f <filter>][-s <duration>][-m <duration>][-b <num bin clusters>]\n"
" [-p <protos>][-l <loops> [-q][-d][-J][-h][-e <len>][-t][-v <level>]\n" " [-p <protos>][-l <loops> [-q][-d][-J][-h][-e <len>][-t][-v <level>]\n"
" [-n <threads>][-w <file>][-c <file>][-C <file>][-j <file>][-x <file>]\n" " [-n <threads>][-w <file>][-c <file>][-C <file>][-j <file>][-x <file>]\n"
" [-T <num>][-U <num>]\n\n" " [-T <num>][-U <num>] [-x <domain>]\n\n"
"Usage:\n" "Usage:\n"
" -i <file.pcap|device> | Specify a pcap file/playlist to read packets from or a\n" " -i <file.pcap|device> | Specify a pcap file/playlist to read packets from or a\n"
" | device for live capture (comma-separated list)\n" " | device for live capture (comma-separated list)\n"
@@ -390,6 +439,7 @@ static void help(u_int long_help) {
" | only the last instance will be considered\n" " | only the last instance will be considered\n"
" -T <num> | Max number of TCP processed packets before giving up [default: %u]\n" " -T <num> | Max number of TCP processed packets before giving up [default: %u]\n"
" -U <num> | Max number of UDP processed packets before giving up [default: %u]\n" " -U <num> | Max number of UDP processed packets before giving up [default: %u]\n"
" -x <domain> | Check domain name [Test only]\n"
, ,
human_readeable_string_len, human_readeable_string_len,
min_pattern_len, max_pattern_len, max_num_packets_per_flow, max_packet_payload_dissection, min_pattern_len, max_pattern_len, max_num_packets_per_flow, max_packet_payload_dissection,
@@ -665,7 +715,7 @@ static void parseOptions(int argc, char **argv) {
} }
#endif #endif
while((opt = getopt_long(argc, argv, "b:e:c:C:df:g:i:hp:P:l:s:tv:V:u:n:Jrp:w:q0123:456:7:89:m:T:U:", while((opt = getopt_long(argc, argv, "b:e:c:C:df:g:i:hp:P:l:s:tu:v:V:n:Jrp:x:w:q0123:456:7:89:m:T:U:",
longopts, &option_idx)) != EOF) { longopts, &option_idx)) != EOF) {
#ifdef DEBUG_TRACE #ifdef DEBUG_TRACE
if(trace) fprintf(trace, " #### -%c [%s] #### \n", opt, optarg ? optarg : ""); if(trace) fprintf(trace, " #### -%c [%s] #### \n", opt, optarg ? optarg : "");
@@ -839,6 +889,10 @@ static void parseOptions(int argc, char **argv) {
if(max_num_tcp_dissected_pkts < 3) max_num_tcp_dissected_pkts = 3; if(max_num_tcp_dissected_pkts < 3) max_num_tcp_dissected_pkts = 3;
break; break;
case 'x':
domain_to_check = optarg;
break;
case 'U': case 'U':
max_num_udp_dissected_pkts = atoi(optarg); max_num_udp_dissected_pkts = atoi(optarg);
if(max_num_udp_dissected_pkts < 3) max_num_udp_dissected_pkts = 3; if(max_num_udp_dissected_pkts < 3) max_num_udp_dissected_pkts = 3;
@@ -859,6 +913,7 @@ static void parseOptions(int argc, char **argv) {
extcap_capture(); extcap_capture();
} }
if(!domain_to_check) {
if(_pcap_file[0] == NULL) if(_pcap_file[0] == NULL)
help(0); help(0);
@@ -874,6 +929,7 @@ static void parseOptions(int argc, char **argv) {
for(thread_id = 1; thread_id < num_threads; thread_id++) for(thread_id = 1; thread_id < num_threads; thread_id++)
_pcap_file[thread_id] = _pcap_file[0]; _pcap_file[thread_id] = _pcap_file[0];
} }
}
#ifdef linux #ifdef linux
for(thread_id = 0; thread_id < num_threads; thread_id++) for(thread_id = 0; thread_id < num_threads; thread_id++)
@@ -3534,6 +3590,11 @@ int orginal_main(int argc, char **argv) {
parseOptions(argc, argv); parseOptions(argc, argv);
if(domain_to_check) {
ndpiCheckHostStringMatch(domain_to_check);
exit(0);
}
if(!quiet_mode) { if(!quiet_mode) {
printf("\n-----------------------------------------------------------\n" printf("\n-----------------------------------------------------------\n"
"* NOTE: This is demo app to show *some* nDPI features.\n" "* NOTE: This is demo app to show *some* nDPI features.\n"