2020-05-13 00:03:45 +02:00
|
|
|
extern crate bitfield;
|
2020-05-21 00:54:56 +02:00
|
|
|
extern crate byteorder;
|
2020-05-13 00:03:45 +02:00
|
|
|
extern crate eui48;
|
|
|
|
|
mod packet_handler;
|
2020-06-04 21:13:48 +02:00
|
|
|
use eui48::{MacAddress};
|
2020-05-21 00:54:56 +02:00
|
|
|
use pcap::Capture;
|
2020-05-21 01:48:13 +02:00
|
|
|
use regex::bytes::Regex;
|
|
|
|
|
use std::str;
|
2020-06-04 21:13:48 +02:00
|
|
|
//use std::thread::{spawn, JoinHandle};
|
|
|
|
|
//use std::sync::mpsc::{channel, Receiver};
|
|
|
|
|
|
2020-05-13 00:03:45 +02:00
|
|
|
|
2020-05-21 00:54:56 +02:00
|
|
|
/* protocol ids, LittleEndian */
|
|
|
|
|
const ETH_P_IPV6: usize = 0xDD86;
|
|
|
|
|
const ETH_P_IP: usize = 0x08;
|
|
|
|
|
const TCP: usize = 0x06;
|
2020-05-22 19:09:05 +02:00
|
|
|
const UDP: usize = 0x11;
|
2020-05-31 00:44:47 +02:00
|
|
|
const ETH_P_ARP: usize = 0x0608;
|
|
|
|
|
const ETH_P_RARP: usize = 0x3580;
|
2020-05-19 19:34:19 +02:00
|
|
|
|
2020-05-21 00:54:56 +02:00
|
|
|
fn build_ether() -> packet_handler::EtherHeader {
|
2020-05-13 00:03:45 +02:00
|
|
|
packet_handler::EtherHeader {
|
2020-06-04 21:13:48 +02:00
|
|
|
ether_dhost: MacAddress::new([0;6]),
|
|
|
|
|
ether_shost: MacAddress::new([0;6]),
|
2020-05-21 00:54:56 +02:00
|
|
|
ether_type: 0,
|
2020-05-13 00:03:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 01:08:16 +02:00
|
|
|
/*
|
|
|
|
|
QryData could be written in the sense of QryData{ ... frame: .., packet: .., segment:.. }
|
|
|
|
|
On the one hand, only the actual type of frame/packet/segment would be contained in the resulting struct.
|
|
|
|
|
So, increased benefit in serialization/cpu time, could result in less data to be serialized, depending on layout.
|
|
|
|
|
On the other hand, each datagram::type needs to implement traits which would need to be dynamically dispatched by returning any of these types per iso level from a single function each. The result would be a performance decrease.
|
|
|
|
|
See: https://doc.rust-lang.org/book/ch17-02-trait-objects.html#trait-objects-perform-dynamic-dispatch
|
|
|
|
|
Then again, parser logic would be fewer lines + more unified using the latter method. Maybe better optimizable as well? Maybe this is a nice tradeoff?
|
|
|
|
|
TODO: Implement and benchmark dynamically dispatched packet data in conjunction with restructured QryData.
|
|
|
|
|
*/
|
2020-05-13 00:03:45 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2020-05-21 00:54:56 +02:00
|
|
|
pub struct QryData {
|
|
|
|
|
pub id: i32,
|
|
|
|
|
pub time: f64,
|
|
|
|
|
pub data: Option<Vec<u8>>,
|
|
|
|
|
pub ether_header: packet_handler::EtherHeader,
|
|
|
|
|
pub ipv4_header: Option<packet_handler::IpV4Header>,
|
|
|
|
|
pub ipv6_header: Option<packet_handler::IpV6Header>,
|
|
|
|
|
pub tcp_header: Option<packet_handler::TcpHeader>,
|
2020-05-22 19:09:05 +02:00
|
|
|
pub udp_header: Option<packet_handler::UdpHeader>,
|
2020-05-31 00:44:47 +02:00
|
|
|
pub arp_header: Option<packet_handler::ArpHeader>,
|
2020-05-25 00:51:10 +02:00
|
|
|
pub reg_res: Option<String>,
|
2020-05-13 00:03:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-07 23:56:53 +02:00
|
|
|
fn init_qrydata( ) -> Result<QryData, core::fmt::Error> {
|
|
|
|
|
let ether_init = build_ether();
|
|
|
|
|
Ok(QryData {
|
|
|
|
|
id: 0,
|
|
|
|
|
time: 0.0,
|
|
|
|
|
data: None,
|
|
|
|
|
ether_header: ether_init,
|
|
|
|
|
ipv4_header: None::<packet_handler::IpV4Header>,
|
|
|
|
|
ipv6_header: None::<packet_handler::IpV6Header>,
|
|
|
|
|
tcp_header: None::<packet_handler::TcpHeader>,
|
|
|
|
|
udp_header: None::<packet_handler::UdpHeader>,
|
|
|
|
|
arp_header: None::<packet_handler::ArpHeader>,
|
|
|
|
|
reg_res: None::<String>,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 01:08:16 +02:00
|
|
|
enum EncapsulationType {
|
|
|
|
|
ETHER = 1,
|
|
|
|
|
RAWIP = 107,
|
|
|
|
|
}
|
2020-06-07 23:56:53 +02:00
|
|
|
|
2020-06-11 01:08:16 +02:00
|
|
|
impl QryData {
|
|
|
|
|
// This is not cool!
|
|
|
|
|
// This will get modularized into subfunctions
|
|
|
|
|
fn encap_ether( packet_data: &[u8] ) -> Option<QryData> {
|
|
|
|
|
let mut pkg: QryData = init_qrydata().unwrap();
|
|
|
|
|
pkg.ether_header = packet_handler::ethernet_handler(packet_data);
|
|
|
|
|
match pkg.ether_header.ether_type as usize {
|
2020-05-21 01:48:13 +02:00
|
|
|
ETH_P_IP => {
|
2020-06-11 01:08:16 +02:00
|
|
|
//pkg.ipv6_header = None::<packet_handler::IpV6Header>;
|
|
|
|
|
pkg.ipv4_header = Some(packet_handler::ip_handler(packet_data)).unwrap();
|
|
|
|
|
match pkg.ipv4_header.unwrap().ip_protocol as usize {
|
2020-05-25 00:51:10 +02:00
|
|
|
TCP => {
|
2020-06-11 01:08:16 +02:00
|
|
|
// pkg.udp_header = None::<packet_handler::UdpHeader>;
|
|
|
|
|
pkg.tcp_header = Some(packet_handler::tcp_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
packet_data,
|
2020-05-25 00:51:10 +02:00
|
|
|
))
|
|
|
|
|
.unwrap();
|
2020-06-11 01:08:16 +02:00
|
|
|
pkg.data = Some(packet_handler::payload_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
pkg.tcp_header.unwrap().data_offset,
|
|
|
|
|
packet_data,
|
2020-06-01 20:55:11 +02:00
|
|
|
)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
|
|
|
|
UDP => {
|
2020-06-11 01:08:16 +02:00
|
|
|
// pkg.tcp_header = None::<packet_handler::TcpHeader>;
|
|
|
|
|
pkg.udp_header = Some(packet_handler::udp_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
packet_data,
|
2020-05-25 00:51:10 +02:00
|
|
|
))
|
|
|
|
|
.unwrap();
|
2020-06-11 01:08:16 +02:00
|
|
|
pkg.data = Some(packet_handler::payload_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
2020-05-25 00:51:10 +02:00
|
|
|
7,
|
2020-06-11 01:08:16 +02:00
|
|
|
packet_data,
|
2020-06-01 20:55:11 +02:00
|
|
|
)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
2020-06-11 01:08:16 +02:00
|
|
|
_ => println!("Transport layer protocol not implemented"),
|
2020-05-21 01:48:13 +02:00
|
|
|
}
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
2020-05-21 01:48:13 +02:00
|
|
|
ETH_P_IPV6 => {
|
2020-06-11 01:08:16 +02:00
|
|
|
//pkg.ipv4_header = None::<packet_handler::IpV4Header>;
|
|
|
|
|
pkg.ipv6_header = Some(packet_handler::ipv6_handler(packet_data)).unwrap();
|
|
|
|
|
match pkg.ipv6_header.unwrap().next_header as usize {
|
2020-05-25 00:51:10 +02:00
|
|
|
TCP => {
|
2020-06-11 01:08:16 +02:00
|
|
|
// pkg.udp_header = None::<packet_handler::UdpHeader>;
|
|
|
|
|
pkg.tcp_header = Some(packet_handler::tcp_handler(10, packet_data)).unwrap();
|
|
|
|
|
pkg.data = Some(packet_handler::payload_handler(
|
2020-05-22 19:09:05 +02:00
|
|
|
10,
|
2020-06-11 01:08:16 +02:00
|
|
|
pkg.tcp_header.unwrap().data_offset,
|
|
|
|
|
packet_data,
|
2020-06-01 20:55:11 +02:00
|
|
|
)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
|
|
|
|
UDP => {
|
2020-06-11 01:08:16 +02:00
|
|
|
// pkg.tcp_header = None::<packet_handler::TcpHeader>;
|
|
|
|
|
pkg.udp_header = Some(packet_handler::udp_handler(10, packet_data)).unwrap();
|
|
|
|
|
pkg.data = Some(packet_handler::payload_handler(10, 7, packet_data)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
2020-06-11 01:08:16 +02:00
|
|
|
_ => println!("Transport layer protocol not implemented"),
|
2020-05-21 01:48:13 +02:00
|
|
|
}
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
2020-05-31 00:44:47 +02:00
|
|
|
ETH_P_ARP | ETH_P_RARP => {
|
2020-06-11 01:08:16 +02:00
|
|
|
pkg.arp_header = Some(packet_handler::arp_handler(packet_data)).unwrap();
|
|
|
|
|
pkg.data = None;
|
2020-05-31 00:44:47 +02:00
|
|
|
}
|
2020-06-11 01:08:16 +02:00
|
|
|
_ => println!("Network protocol not implemented"),
|
2020-05-21 01:48:13 +02:00
|
|
|
}
|
2020-06-11 01:08:16 +02:00
|
|
|
Some(pkg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn encap_rawip ( packet_data: &[u8] ) -> Option<QryData> {
|
|
|
|
|
let mut pkg: QryData = init_qrydata().unwrap();
|
|
|
|
|
//pkg.ether_header = None::<packet_handler::EtherHeader>;
|
|
|
|
|
//pkg.ipv6_header = None::<packet_handler::IpV6Header>;
|
|
|
|
|
pkg.ipv4_header = Some(packet_handler::ip_handler(packet_data)).unwrap();
|
|
|
|
|
match pkg.ipv4_header.unwrap().ip_protocol as usize {
|
|
|
|
|
TCP => {
|
|
|
|
|
pkg.udp_header = None::<packet_handler::UdpHeader>;
|
|
|
|
|
pkg.tcp_header = Some(packet_handler::tcp_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
packet_data,
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
pkg.data = Some(packet_handler::payload_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
pkg.tcp_header.unwrap().data_offset,
|
|
|
|
|
packet_data,
|
|
|
|
|
)).unwrap();
|
|
|
|
|
}
|
|
|
|
|
UDP => {
|
|
|
|
|
pkg.tcp_header = None::<packet_handler::TcpHeader>;
|
|
|
|
|
pkg.udp_header = Some(packet_handler::udp_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
packet_data,
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
|
|
|
|
pkg.data = Some(packet_handler::payload_handler(
|
|
|
|
|
pkg.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
7,
|
|
|
|
|
packet_data,
|
|
|
|
|
)).unwrap();
|
|
|
|
|
}
|
|
|
|
|
_ => println!("Transport layer protocol not implemented"),
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Some(pkg)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Regex parse _complete_ package */
|
|
|
|
|
fn flag_carnage(re: &Regex, payload: &[u8]) -> Option<String> {
|
|
|
|
|
let mut flags: String = String::new();
|
|
|
|
|
for mat in re.find_iter(payload) {
|
|
|
|
|
// TODO: Test benchmark format! vs. push_str()
|
|
|
|
|
// flags.push_str(&format!("{} ",std::str::from_utf8(mat.as_bytes()).unwrap()));
|
|
|
|
|
// See: https://github.com/hoodie/concatenation_benchmarks-rs
|
|
|
|
|
flags.push_str(std::str::from_utf8(mat.as_bytes()).unwrap());
|
|
|
|
|
flags.push_str(";");
|
|
|
|
|
}
|
|
|
|
|
match 0 < flags.len() {
|
|
|
|
|
false => None,
|
|
|
|
|
true => Some(flags),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn parse(parse_file: &std::path::Path, filter_str: &str, regex_filter: &str, encap: u16) -> Vec<QryData> {
|
|
|
|
|
let mut me: QryData = init_qrydata().unwrap();
|
|
|
|
|
let mut v: Vec<QryData> = Vec::new();
|
|
|
|
|
|
|
|
|
|
let mut cap = Capture::from_file(parse_file).unwrap();
|
|
|
|
|
Capture::filter(&mut cap, &filter_str).unwrap();
|
|
|
|
|
let re = Regex::new(regex_filter).unwrap();
|
|
|
|
|
while let Ok(packet) = cap.next() {
|
|
|
|
|
|
|
|
|
|
match encap {
|
|
|
|
|
// Syntax is clunky, but no num_derive + num_traits dependencies.
|
|
|
|
|
encap if encap == EncapsulationType::ETHER as u16 => me = QryData::encap_ether(packet.data).unwrap(),
|
|
|
|
|
encap if encap == EncapsulationType::RAWIP as u16 => me = QryData::encap_rawip(packet.data).unwrap(),
|
|
|
|
|
_ => (),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
me.time = (packet.header.ts.tv_usec as f64 / 1000000.0) + packet.header.ts.tv_sec as f64;
|
|
|
|
|
//me.data = Some(packet.data.to_vec());
|
|
|
|
|
me.reg_res = flag_carnage(&re, packet.data); // Regex overhead is between 4-9% --single threaded-- on complete packet [u8] data
|
|
|
|
|
v.push(me.clone());
|
2020-05-13 00:03:45 +02:00
|
|
|
}
|
|
|
|
|
v
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 21:13:48 +02:00
|
|
|
|
|
|
|
|
/* This could need some love */
|
2020-06-06 03:29:38 +02:00
|
|
|
pub fn parse_device(parse_device: &str, filter_str: &str, insert_max: &usize, regex_filter: &str) -> Vec<QryData> {
|
2020-06-07 23:56:53 +02:00
|
|
|
let mut me: QryData = init_qrydata ( ).unwrap();
|
2020-05-13 00:03:45 +02:00
|
|
|
let mut v: Vec<QryData> = Vec::new();
|
|
|
|
|
let mut cap = Capture::from_device(parse_device).unwrap().open().unwrap();
|
|
|
|
|
Capture::filter(&mut cap, &filter_str).unwrap();
|
|
|
|
|
|
2020-06-06 03:29:38 +02:00
|
|
|
let re = Regex::new(regex_filter).unwrap();
|
2020-05-21 00:54:56 +02:00
|
|
|
'parse: while let Ok(packet) = cap.next() {
|
2020-06-11 01:08:16 +02:00
|
|
|
me.time = (packet.header.ts.tv_usec as f64 / 1000000.0) + packet.header.ts.tv_sec as f64;
|
2020-05-21 00:54:56 +02:00
|
|
|
me.data = Some(packet.data.to_vec());
|
2020-05-21 17:27:30 +02:00
|
|
|
me.reg_res = flag_carnage(&re, packet.data);
|
2020-05-21 00:54:56 +02:00
|
|
|
me.ether_header = packet_handler::ethernet_handler(packet.data);
|
2020-05-21 01:48:13 +02:00
|
|
|
match me.ether_header.ether_type as usize {
|
|
|
|
|
ETH_P_IP => {
|
|
|
|
|
me.ipv6_header = None::<packet_handler::IpV6Header>;
|
|
|
|
|
me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap();
|
2020-05-22 19:09:05 +02:00
|
|
|
match me.ipv4_header.unwrap().ip_protocol as usize {
|
2020-05-25 00:51:10 +02:00
|
|
|
TCP => {
|
|
|
|
|
me.tcp_header = Some(packet_handler::tcp_handler(
|
2020-05-22 19:09:05 +02:00
|
|
|
me.ipv4_header.unwrap().ip_ihl,
|
2020-05-25 00:51:10 +02:00
|
|
|
packet.data,
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
2020-06-01 20:55:11 +02:00
|
|
|
me.data = Some(packet_handler::payload_handler(
|
2020-05-25 00:51:10 +02:00
|
|
|
me.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
me.tcp_header.unwrap().data_offset,
|
|
|
|
|
packet.data,
|
2020-06-01 20:55:11 +02:00
|
|
|
)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
|
|
|
|
UDP => {
|
|
|
|
|
me.udp_header = Some(packet_handler::udp_handler(
|
|
|
|
|
me.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
packet.data,
|
|
|
|
|
))
|
|
|
|
|
.unwrap();
|
2020-06-01 20:55:11 +02:00
|
|
|
me.data = Some(packet_handler::payload_handler(
|
2020-05-25 00:51:10 +02:00
|
|
|
me.ipv4_header.unwrap().ip_ihl,
|
|
|
|
|
7,
|
|
|
|
|
packet.data,
|
2020-06-01 20:55:11 +02:00
|
|
|
)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
2020-05-22 19:09:05 +02:00
|
|
|
_ => println!("network protocol not implemented"),
|
2020-05-21 01:48:13 +02:00
|
|
|
}
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
2020-05-21 01:48:13 +02:00
|
|
|
ETH_P_IPV6 => {
|
|
|
|
|
me.ipv4_header = None::<packet_handler::IpV4Header>;
|
|
|
|
|
me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap();
|
2020-05-22 19:09:05 +02:00
|
|
|
match me.ipv6_header.unwrap().next_header as usize {
|
2020-05-25 00:51:10 +02:00
|
|
|
TCP => {
|
2020-05-22 19:09:05 +02:00
|
|
|
me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap();
|
2020-06-01 20:55:11 +02:00
|
|
|
me.data = Some(packet_handler::payload_handler(
|
2020-05-25 00:51:10 +02:00
|
|
|
10,
|
|
|
|
|
me.tcp_header.unwrap().data_offset,
|
|
|
|
|
packet.data,
|
2020-06-01 20:55:11 +02:00
|
|
|
)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
|
|
|
|
UDP => {
|
|
|
|
|
me.udp_header = Some(packet_handler::udp_handler(10, packet.data)).unwrap();
|
2020-06-01 20:55:11 +02:00
|
|
|
me.data = Some(packet_handler::payload_handler(10, 7, packet.data)).unwrap();
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
|
|
|
|
_ => println!("network protocol not implemented"),
|
2020-05-21 01:48:13 +02:00
|
|
|
}
|
2020-05-25 00:51:10 +02:00
|
|
|
}
|
2020-05-31 00:44:47 +02:00
|
|
|
ETH_P_ARP | ETH_P_RARP => {
|
|
|
|
|
me.arp_header = Some(packet_handler::arp_handler(packet.data)).unwrap();
|
2020-06-01 20:55:11 +02:00
|
|
|
me.data = None;
|
2020-05-31 00:44:47 +02:00
|
|
|
}
|
2020-05-25 00:51:10 +02:00
|
|
|
_ => println!("network protocol not implemented"),
|
2020-05-21 01:48:13 +02:00
|
|
|
}
|
2020-05-21 00:54:56 +02:00
|
|
|
v.push(QryData {
|
|
|
|
|
id: 0,
|
|
|
|
|
time: me.time,
|
|
|
|
|
data: me.data,
|
|
|
|
|
ether_header: me.ether_header,
|
|
|
|
|
ipv4_header: me.ipv4_header,
|
|
|
|
|
ipv6_header: me.ipv6_header,
|
|
|
|
|
tcp_header: me.tcp_header,
|
2020-05-22 19:09:05 +02:00
|
|
|
udp_header: me.udp_header,
|
2020-05-31 00:44:47 +02:00
|
|
|
arp_header: me.arp_header,
|
2020-05-22 19:09:05 +02:00
|
|
|
reg_res: me.reg_res,
|
2020-05-21 00:54:56 +02:00
|
|
|
});
|
2020-05-25 00:51:10 +02:00
|
|
|
|
2020-05-13 00:03:45 +02:00
|
|
|
if &v.len() >= insert_max {
|
|
|
|
|
break 'parse;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
v
|
|
|
|
|
}
|