From 4fe25e408015cb7643c53fe53cdb2f3e45508ddc Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 13 Dec 2022 21:37:58 +0100 Subject: [PATCH] Managed to add argh, too sick to code :( --- Cargo.toml | 5 +- src/libs/rp_args.rs | 179 +++++++++++++++++++++++++++++--------------- src/libs/rp_icmp.rs | 4 +- src/libs/rp_ipv4.rs | 2 +- src/main.rs | 13 ++-- 5 files changed, 128 insertions(+), 75 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a98a80f..5e041b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,16 +6,13 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -#socket2 = { version = "0.4.7", features = ["all"] } -#env_logger = "0.10" rhexdump = "0.1.1" +argh = "0.1.9" rand = "0.8.5" pnet = "0.31.0" pnet_datalink = "0.31.0" pnet_transport = "0.31.0" pnet_packet = "0.31.0" -#libc = "0.2.138" -#dns-lookup = "1.0.8" [profile.release] strip = true # Automatically strip symbols from the binary. diff --git a/src/libs/rp_args.rs b/src/libs/rp_args.rs index 0fd9d6c..3bf585d 100644 --- a/src/libs/rp_args.rs +++ b/src/libs/rp_args.rs @@ -1,22 +1,76 @@ -use std::env; +// use std::env; +use argh::FromArgs; +#[derive(FromArgs)] +/// rping, sending very custom icmp requests pub struct Config{ + /// the destination address for the request + #[argh(positional)] pub dest_addr : String, + + /// the source address for the request + #[argh(option, default = "String::from(\"0.0.0.0\")")] pub src_addr : String, + + /// the type of ping request + #[argh(option, default = "8")] pub ping_type : u8, + + /// the ping identifier u16 number + #[argh(option, default = "0x4242")] pub identifier : u16, + + /// the ping sequence u16 number + #[argh(option, default = "0x0001")] pub sequence : u16, - pub payload : Vec, + + /// the actual ping payload, + #[argh(option, default = "String::new()")] + // pub payload : Vec, + pub payload : String, + + /// the payload size + #[argh(option, default = "64")] pub payload_size : usize, + + /// force the usage of a raw packet + #[argh(option, default = "false")] pub raw_packet : bool, + + /// the packet size (excluding header) + #[argh(option, default = "64")] pub packet_size : u8, + + /// the ip version used (4 or 6) + #[argh(option, default = "4")] pub ipv4_version : u8, + + /// inter header length (must be >=5) + #[argh(option, default = "5")] pub ihl : u8, + + /// differentiated service code points + #[argh(option, default = "0")] pub dcsp : u8, + + /// explicit congestion notification + #[argh(option, default = "0")] pub ecn : u8, + + /// time to live + #[argh(option, default = "64")] pub ttl: u8, + + /// identification (lacking documentation) + #[argh(option, default = "52703")] pub identification : u16, + + /// flags : 2 don't fragment, 4 more fragments + #[argh(option, default = "2")] pub flags : u8, + + /// the fragment offset (if fragmented) + #[argh(option, default = "0")] pub offset : u16, } @@ -24,68 +78,71 @@ pub struct Config{ pub fn parse_all_arguments() -> Config { // Load all inline arguments and create a config structure - let args: Vec = env::args().collect(); - let mut config = Config { - dest_addr : String::new(), - src_addr : String::from("0.0.0.0"), - ping_type : 0x08, - identifier : 0x4242, - sequence : 0x4242, - payload: Vec::::new(), - payload_size : 64, - raw_packet: false, - ipv4_version: 4, - packet_size: 64, - ihl : 5, - dcsp : 0, - ecn : 0, - ttl : 64, - identification : 52703, - flags : 2, - offset: 0, - }; - if args.len() == 1 { - exit_with_help(); - } + // let args: Vec = env::args().collect(); + let mut_config = argh::from_env(); - config.dest_addr= args[1].clone(); - for (i,arg) in args[1..].iter().enumerate(){ - if arg == "--help" || arg == "-h" { - exit_with_help(); - } else if arg == "--type" || arg == "-t" { - config.ping_type = args[i + 2].parse::().unwrap(); - } else if arg == "--identifier" || arg == "-i" { - config.identifier = args[i + 2].parse::().unwrap(); - } else if arg == "--sequence" || arg == "-q" { - config.sequence = args[i + 2].parse::().unwrap(); - } else if arg == "--payload" || arg == "-p" { - config.payload = args[i + 2].as_bytes().to_vec(); - } else if arg == "--size" || arg == "-s" { - config.payload_size = args[i + 2].parse::().unwrap(); - } else if arg =="--raw" { - config.raw_packet=true; // This enable raw packets but requires root privilege - } - } + // let mut config = Config { + // dest_addr : String::new(), + // src_addr : String::from("0.0.0.0"), + // ping_type : 0x08, + // identifier : 0x4242, + // sequence : 0x4242, + // payload: Vec::::new(), + // payload_size : 64, + // raw_packet: false, + // ipv4_version: 4, + // packet_size: 64, + // ihl : 5, + // dcsp : 0, + // ecn : 0, + // ttl : 64, + // identification : 52703, + // flags : 2, + // offset: 0, + // }; + // if args.len() == 1 { + // exit_with_help(); + // } + + // config.dest_addr = args[1].clone(); + // for (i,arg) in args[1..].iter().enumerate(){ + // if arg == "--help" || arg == "-h" { + // exit_with_help(); + // } else if arg == "--type" || arg == "-t" { + // config.ping_type = args[i + 2].parse::().unwrap(); + // } else if arg == "--identifier" || arg == "-i" { + // config.identifier = args[i + 2].parse::().unwrap(); + // } else if arg == "--sequence" || arg == "-q" { + // config.sequence = args[i + 2].parse::().unwrap(); + // } else if arg == "--payload" || arg == "-p" { + // config.payload = args[i + 2].as_bytes().to_vec(); + // } else if arg == "--size" || arg == "-s" { + // config.payload_size = args[i + 2].parse::().unwrap(); + // } else if arg =="--raw" { + // config.raw_packet=true; // This enable raw packets but requires root privilege + // } + // } //Sanitize inputs and show it - if config.payload.len()!=0{ - println!("Corrected payload size, went from {} to {} according to given input",config.payload_size,config.payload.len()/4); - config.payload_size=config.payload.len()/4; + // if config.payload.len()!=0{ + // println!("Corrected payload size, went from {} to {} according to given input",config.payload_size,config.payload.len()/4); + // config.payload_size=config.payload.len()/4; - } - config + // } + // config + mut_config } -fn exit_with_help() { - eprintln!("Usage: cargo run --dest - [--type/-t ] The type of ping request 8 for IPv4 128 for IPv6 (8b) - [--code/-c ] The mandatory 0 code (8b) - [--identifier/-i ] The Header identifier (16b) - [--sequence/-q ] The Sequence number (16b) - [--payload/-p ] The Payload, generated randomly if not given (any) - [--size/-s ] The Payload size (any) but smaller than ??? - [--raw] Enable raw packet, require root privileges - [--hex] Read value as hex (no spaces) - "); - std::process::exit(1); -} \ No newline at end of file +// fn exit_with_help() { +// eprintln!("Usage: cargo run --dest +// [--type/-t ] The type of ping request 8 for IPv4 128 for IPv6 (8b) +// [--code/-c ] The mandatory 0 code (8b) +// [--identifier/-i ] The Header identifier (16b) +// [--sequence/-q ] The Sequence number (16b) +// [--payload/-p ] The Payload, generated randomly if not given (any) +// [--size/-s ] The Payload size (any) but smaller than ??? +// [--raw] Enable raw packet, require root privileges +// [--hex] Read value as hex (no spaces) +// "); +// std::process::exit(1); +// } \ No newline at end of file diff --git a/src/libs/rp_icmp.rs b/src/libs/rp_icmp.rs index 94f4e3f..bd59d56 100644 --- a/src/libs/rp_icmp.rs +++ b/src/libs/rp_icmp.rs @@ -28,7 +28,7 @@ use rand::{ Rng, }; -pub fn create_icmp_packet(config: &Config) -> icmp::MutableIcmpPacket{ +pub fn create_icmp_packet<'a>(config: &Config) -> icmp::MutableIcmpPacket<'a>{ // Create a new mutable ICMP packet let mut binding = vec![0u8;config.packet_size as usize]; @@ -59,6 +59,8 @@ pub fn create_icmp_packet(config: &Config) -> icmp::MutableIcmpPacket{ payload.push((config.sequence&0xff) as u8); generate_payload(&mut payload, (config.packet_size).into()); packet.set_payload(&payload); + } else { + packet.set_payload(&config.payload.as_bytes()); } packet.set_checksum(pnet_packet::icmp::checksum(&packet.to_immutable())); diff --git a/src/libs/rp_ipv4.rs b/src/libs/rp_ipv4.rs index a567560..ceb7051 100644 --- a/src/libs/rp_ipv4.rs +++ b/src/libs/rp_ipv4.rs @@ -35,7 +35,7 @@ use pnet::packet::icmp::{ // Takes a config structure and generate a wrapper Packet, note that it will need to be filled with a payload pub fn create_ethernet_packet<'a>(config: &'a Config) -> ipv4::MutableIpv4Packet<'a>{ // The total length is dependent on the packed sent - let size_tot: u16 = (config.ihl*4 + config.packet_size).into(); //84; // ihl*4+packet_size + let size_tot: u16 = (config.ihl*4 + config.packet_size).into(); //84; // ihl*4+packet_size let binding = vec![0; size_tot as usize]; let mut ethernet_packet = pnet_packet::ipv4::MutableIpv4Packet::owned(binding).unwrap(); diff --git a/src/main.rs b/src/main.rs index 26c3c77..9f52b6c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,22 +6,19 @@ use std::net::{ use pnet::packet::Packet; - - - - - - fn main() { // Parse the command line arguments. + let config = libs::parse_all_arguments(); let mut packet = libs::create_ethernet_packet(&config); - packet.set_payload(libs::create_icmp_packet(&config).packet()); + let payload = libs::create_icmp_packet(&config); + packet.set_payload(payload.packet()); let (tx, rx) = libs::create_interface(&config); let res = libs::send_packet(packet.to_immutable(),tx,config.dest_addr.parse::().expect("Couldn't parse the IpAddr")); + libs::recv_packet(rx); + match res { Ok(s) => println!("{:?}", s), Err(e) => eprintln!("Program exited with errors {:?}",e) } - libs::recv_packet(rx); }