Fixed minor errors and updated stdout messages

This commit is contained in:
Giuliano Bellini s294739 2022-08-13 22:11:20 +02:00
parent a0d2b33ee5
commit a78e73067a
12 changed files with 44 additions and 237 deletions

View file

@ -4,13 +4,13 @@
<><> Packets are sniffed from adapter 'en0'
<><>
<><> Report updates info
<><> Report start time: 13/08/2022 17:36:21
<><> Report last update: 13/08/2022 17:37:52
<><> Report update frequency: every 7 seconds
<><> Number of times report was updated: 13
<><> Report start time: 13/08/2022 22:08:02
<><> Report last update: 13/08/2022 22:08:12
<><> Report update frequency: every 5 seconds
<><> Number of times report was updated: 2
<><>
<><> Filters
<><> Considering only addresses featured by more than 500 packets
<><> Considering addresses featured by any number of packets
<><> Considering all port numbers (from 0 to 65535)
<><>
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
@ -18,213 +18,3 @@
Address: 185.126.207.169:8080
Sent data
Sent Bytes: 297.30 kB
Sent packets: 5179
Received data
Received Bytes: 12.67 MB
Received packets: 9428
Timestamps
Initial Timestamp: 13/08/2022 17:36:36
Final Timestamp: 13/08/2022 17:36:57
Protocols
Transport layer protocols: TCP
Application layer protocols: unable to identify any level 7 protocol
Address: 151.99.54.108:443
Sent data
Sent Bytes: 8.73 MB
Sent packets: 5965
Received data
Received Bytes: 101.85 kB
Received packets: 2475
Timestamps
Initial Timestamp: 13/08/2022 17:36:22
Final Timestamp: 13/08/2022 17:37:01
Protocols
Transport layer protocols: TCP
Application layer protocols: HTTPS
Address: 192.168.1.233:60486
Sent data
Sent Bytes: 6.46 MB
Sent packets: 4400
Received data
Received Bytes: 80.66 kB
Received packets: 1641
Timestamps
Initial Timestamp: 13/08/2022 17:36:36
Final Timestamp: 13/08/2022 17:36:57
Protocols
Transport layer protocols: TCP
Application layer protocols: unable to identify any level 7 protocol
Address: 192.168.1.233:60485
Sent data
Sent Bytes: 3.65 MB
Sent packets: 3084
Received data
Received Bytes: 114.22 kB
Received packets: 2105
Timestamps
Initial Timestamp: 13/08/2022 17:36:36
Final Timestamp: 13/08/2022 17:36:53
Protocols
Transport layer protocols: TCP
Application layer protocols: unable to identify any level 7 protocol
Address: 192.168.1.233:60478
Sent data
Sent Bytes: 54.55 kB
Sent packets: 1349
Received data
Received Bytes: 4.60 MB
Received packets: 3135
Timestamps
Initial Timestamp: 13/08/2022 17:36:22
Final Timestamp: 13/08/2022 17:36:46
Protocols
Transport layer protocols: TCP
Application layer protocols: HTTPS
Address: 192.168.1.233:49518
Sent data
Sent Bytes: 371.86 kB
Sent packets: 1908
Received data
Received Bytes: 1.74 MB
Received packets: 2294
Timestamps
Initial Timestamp: 13/08/2022 17:36:29
Final Timestamp: 13/08/2022 17:37:17
Protocols
Transport layer protocols: UDP
Application layer protocols: HTTPS
Address: 172.224.58.14:443
Sent data
Sent Bytes: 1.74 MB
Sent packets: 2294
Received data
Received Bytes: 371.86 kB
Received packets: 1908
Timestamps
Initial Timestamp: 13/08/2022 17:36:29
Final Timestamp: 13/08/2022 17:37:17
Protocols
Transport layer protocols: UDP
Application layer protocols: HTTPS
Address: 192.168.1.233:60479
Sent data
Sent Bytes: 46.94 kB
Sent packets: 1117
Received data
Received Bytes: 4.14 MB
Received packets: 2821
Timestamps
Initial Timestamp: 13/08/2022 17:36:53
Final Timestamp: 13/08/2022 17:37:01
Protocols
Transport layer protocols: TCP
Application layer protocols: HTTPS
Address: 74.125.99.60:443
Sent data
Sent Bytes: 2.37 MB
Sent packets: 2005
Received data
Received Bytes: 56.84 kB
Received packets: 942
Timestamps
Initial Timestamp: 13/08/2022 17:36:42
Final Timestamp: 13/08/2022 17:36:54
Protocols
Transport layer protocols: UDP, TCP
Application layer protocols: HTTPS
Address: 192.168.1.233:58853
Sent data
Sent Bytes: 56.75 kB
Sent packets: 939
Received data
Received Bytes: 2.37 MB
Received packets: 2003
Timestamps
Initial Timestamp: 13/08/2022 17:36:42
Final Timestamp: 13/08/2022 17:36:54
Protocols
Transport layer protocols: UDP
Application layer protocols: HTTPS
Address: 192.168.1.233:60487
Sent data
Sent Bytes: 1.26 MB
Sent packets: 877
Received data
Received Bytes: 36.05 kB
Received packets: 690
Timestamps
Initial Timestamp: 13/08/2022 17:36:36
Final Timestamp: 13/08/2022 17:36:54
Protocols
Transport layer protocols: TCP
Application layer protocols: unable to identify any level 7 protocol
Address: 192.168.1.233:60488
Sent data
Sent Bytes: 1.29 MB
Sent packets: 890
Received data
Received Bytes: 33.62 kB
Received packets: 593
Timestamps
Initial Timestamp: 13/08/2022 17:36:36
Final Timestamp: 13/08/2022 17:36:54
Protocols
Transport layer protocols: TCP
Application layer protocols: unable to identify any level 7 protocol
Address: 188.114.96.7:443
Sent data
Sent Bytes: 853.09 kB
Sent packets: 901
Received data
Received Bytes: 121.52 kB
Received packets: 446
Timestamps
Initial Timestamp: 13/08/2022 17:37:02
Final Timestamp: 13/08/2022 17:37:17
Protocols
Transport layer protocols: UDP
Application layer protocols: HTTPS
Address: 192.168.1.233:55560
Sent data
Sent Bytes: 70.95 kB
Sent packets: 242
Received data
Received Bytes: 494.17 kB
Received packets: 512
Timestamps
Initial Timestamp: 13/08/2022 17:37:02
Final Timestamp: 13/08/2022 17:37:15
Protocols
Transport layer protocols: UDP
Application layer protocols: HTTPS

View file

@ -56,30 +56,41 @@ fn main() {
let min_packets = args.minimum_packets;
let interval = args.interval;
if lowest_port > highest_port {
eprint!("ERROR: Specified lowest port is greater than specified highest port.\n");
if args.device_list == true {
print_device_list();
return;
}
if args.device_list == true {
print_device_list();
if lowest_port > highest_port {
eprint!("\n\tERROR: Specified lowest port is greater than specified highest port.\n\n");
return;
}
if interval == 0 {
eprint!("\n\tERROR: Specified time interval is null.\n\n");
return;
}
let found_device = retrieve_device(adapter);
if found_device.name.len() == 0 {
eprint!("ERROR: Specified network adapter does not exist. Use option '-d' to list all the available devices.\n");
eprint!("\n\tERROR: Specified network adapter does not exist. Use option '-d' to list all the available devices.\n\n");
return;
}
let cap = Capture::from_device(found_device.clone()).unwrap()
let cap = Capture::from_device(found_device.clone())
.expect("Capture initialization error\n")
.promisc(true)
.open().unwrap();
.buffer_size(10_000_000)
.open()
.expect("Capture initialization error\n");
let mutex_map1 = Arc::new(Mutex::new(HashMap::new()));
let mutex_map2 = mutex_map1.clone();
println!("\n\tParsing packets...");
println!("\tUpdating the file '{}' every {} seconds\n", output_file, interval);
thread::spawn(move || {
sleep_and_write_report_loop(lowest_port, highest_port, interval, min_packets, found_device.name, output_file, mutex_map2);
});
@ -137,6 +148,7 @@ fn sleep_and_write_report_loop(lowest_port: u16, highest_port: u16, interval: u6
write!(output,"{}\n\n\n", cornice_string).expect("Error writing output file\n");
let map = mutex_map.lock().expect("Error acquiring mutex\n");
let mut sorted_vec: Vec<(&AddressPort, &ReportInfo)> = map.iter().collect();
sorted_vec.sort_by(|&(_, a), &(_, b)|
(b.received_packets + b.transmitted_packets).cmp(&(a.received_packets + a.transmitted_packets)));
@ -146,7 +158,7 @@ fn sleep_and_write_report_loop(lowest_port: u16, highest_port: u16, interval: u6
write!(output, "Address: {}:{}\n{}\n\n", key.address1, key.port1, val).expect("Error writing output file\n");
}
}
println!("Report updated ({})",times_report_updated);
println!("\tReport updated ({})",times_report_updated);
}
}
@ -160,7 +172,7 @@ fn parse_packets_loop(mut cap: Capture<Active>, lowest_port: u16, highest_port:
let now = utc.format("%d/%m/%Y %H:%M:%S").to_string();
match PacketHeaders::from_ethernet_slice(&packet) {
Err(value) => println!("Err {:?}", value),
Err(_) => {continue;},
Ok(value) => {
let address1;
@ -200,13 +212,13 @@ fn parse_packets_loop(mut cap: Capture<Active>, lowest_port: u16, highest_port:
.replace(" ", "");
exchanged_bytes = ipv6header.payload_length as u32;
}
None => {continue;}
_ => {continue;}
}
match value.transport {
Some(TransportHeader::Udp(udpheader)) => {
port1 = udpheader.source_port;
port2 = udpheader.destination_port;
Some(TransportHeader::Udp(udp_header)) => {
port1 = udp_header.source_port;
port2 = udp_header.destination_port;
transport_protocol = TransProtocol::UDP;
application_protocol_1 = from_port_to_application_protocol(port1);
if application_protocol_1.is_some() {
@ -217,9 +229,9 @@ fn parse_packets_loop(mut cap: Capture<Active>, lowest_port: u16, highest_port:
application_protocols.insert(application_protocol_2.unwrap());
}
}
Some(TransportHeader::Tcp(tcpheader)) => {
port1 = tcpheader.source_port;
port2 = tcpheader.destination_port;
Some(TransportHeader::Tcp(tcp_header)) => {
port1 = tcp_header.source_port;
port2 = tcp_header.destination_port;
transport_protocol = TransProtocol::TCP;
application_protocol_1 = from_port_to_application_protocol(port1);
if application_protocol_1.is_some() {
@ -289,7 +301,6 @@ fn parse_packets_loop(mut cap: Capture<Active>, lowest_port: u16, highest_port:
}
}
Err(_) => {
println!("Err occurred while parsing .next() packet\n");
continue;
}
}
@ -299,13 +310,18 @@ fn parse_packets_loop(mut cap: Capture<Active>, lowest_port: u16, highest_port:
fn print_device_list() {
println!();
for dev in Device::list().expect("Error retrieving device list\n") {
print!("Device: {}\n\tAddresses: ", dev.name);
for addr in dev.addresses {
print!("{:?}\n\t\t ", addr.addr);
print!("\tDevice: {}\n\t\tAddresses: ", dev.name);
if dev.addresses.len() == 0 {
println!();
}
println!("\n");
for addr in dev.addresses {
print!("{:?}\n\t\t\t ", addr.addr);
}
println!();
}
println!();
}

View file

@ -74,4 +74,5 @@ impl fmt::Display for ReportInfo {
pub enum TransProtocol { TCP, UDP }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AppProtocol { FTP, SSH, Telnet, SMTP, DNS, DHCP, TFTP, HTTP, POP, NTP, NetBIOS, IMAP, SNMP, BGP, LDAP, HTTPS, LDAPS, FTPS }
pub enum AppProtocol { FTP, SSH, Telnet, SMTP, DNS, DHCP, TFTP, HTTP, POP,
NTP, NetBIOS, IMAP, SNMP, BGP, LDAP, HTTPS, LDAPS, FTPS }