From f83a8303dd340b5dcb7b9e14b40909fe54b4c178 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 14 Dec 2022 21:15:29 +0100 Subject: [PATCH] So slowww; I'm so sick of being sick --- Cargo.toml | 1 + src/libs.rs | 5 ++- src/libs/rp_args.rs | 102 +++++++++++++++++++------------------------- src/libs/rp_icmp.rs | 44 +++++++++---------- src/main.rs | 5 ++- 5 files changed, 74 insertions(+), 83 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5e041b3..86b5263 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] rhexdump = "0.1.1" +hex = " 0.4.3" argh = "0.1.9" rand = "0.8.5" pnet = "0.31.0" diff --git a/src/libs.rs b/src/libs.rs index 2228dc0..e43e879 100644 --- a/src/libs.rs +++ b/src/libs.rs @@ -7,7 +7,10 @@ pub use rp_icmp::{ // generate_ping_request, // calculate_checksum }; -pub use rp_args::parse_all_arguments; +pub use rp_args::{ + parse_all_arguments, + get_payload_from_str, +}; pub use rp_ipv4::{ create_ethernet_packet, create_interface, diff --git a/src/libs/rp_args.rs b/src/libs/rp_args.rs index 3bf585d..cab1d5c 100644 --- a/src/libs/rp_args.rs +++ b/src/libs/rp_args.rs @@ -1,5 +1,6 @@ // use std::env; use argh::FromArgs; +use hex; #[derive(FromArgs)] /// rping, sending very custom icmp requests @@ -26,10 +27,9 @@ pub struct Config{ /// the actual ping payload, #[argh(option, default = "String::new()")] - // pub payload : Vec, pub payload : String, - /// the payload size + /// the total icmp payload size #[argh(option, default = "64")] pub payload_size : usize, @@ -72,56 +72,56 @@ pub struct Config{ /// the fragment offset (if fragmented) #[argh(option, default = "0")] pub offset : u16, + } +pub fn get_payload_from_str(inputp: &str) -> Vec { + // If you start your payload by 0x all following will be parsed as hex (ignoring spaces) + let mut payload = Vec::::new(); + payload.resize(0,4); // The identifier and sequence are in the payload for dumb reasons + + if inputp.len()<2 { + return payload + } + + if &inputp[..2]=="0x"{ + let nopx = inputp.trim_start_matches("0x"); + hex::decode_to_slice(nopx,&mut payload[4..]).expect("Couldn't parse the payload"); + } else { + payload = inputp.as_bytes().to_vec(); + } + payload +} +pub fn sanitize_config(config: &mut Config){ + // Expected behaviour is as follows: + // If you specify a payload, it will increase the payload length and total length as needed + // If you have length set longer than your payloads zeroes will be added as needed + + // if config.payload.len()<=4{ + // config.payload.resize(config.payload_size+4,0); // Resize payload to match the size given + // } + // if config.packet_size + // if config.payload.len()>=2{ + // if &config.payload[..2]=="0x"{ + // let nopx = config.payload.trim_start_matches("0x"); + // hex::decode_to_slice(nopx,&mut payload).expect("Couldn't parse the payload"); + // } else { + // payload = config.payload.as_bytes().to_vec(); + // } + // } + // if config.packet_size < (config.payload.len()-4) as u8{ + // config.packet_size = (config.payload.len()-4) as u8; + // } + +} + pub fn parse_all_arguments() -> Config { // Load all inline arguments and create a config structure // let args: Vec = env::args().collect(); let mut_config = argh::from_env(); - // 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{ @@ -131,18 +131,4 @@ pub fn parse_all_arguments() -> 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 +} \ No newline at end of file diff --git a/src/libs/rp_icmp.rs b/src/libs/rp_icmp.rs index bd59d56..da7facb 100644 --- a/src/libs/rp_icmp.rs +++ b/src/libs/rp_icmp.rs @@ -28,17 +28,17 @@ use rand::{ Rng, }; -pub fn create_icmp_packet<'a>(config: &Config) -> icmp::MutableIcmpPacket<'a>{ +pub fn create_icmp_packet<'a>(config: &Config, icmp_payload: &Vec) -> icmp::MutableIcmpPacket<'a>{ // Create a new mutable ICMP packet let mut binding = vec![0u8;config.packet_size as usize]; - // Loading a custom Id, because I didn't want to split it into two functions - binding[4]=(config.identifier >> 8) as u8; - binding[5]=(config.identifier&0xff) as u8; + // // Loading a custom Id, because I didn't want to split it into two functions + // binding[4]=(config.identifier >> 8) as u8; + // binding[5]=(config.identifier&0xff) as u8; - // Loading a custom Seq Number, because I didn't wat to split it into two - binding[6]=(config.sequence>>8) as u8; - binding[7]=(config.sequence&0xff) as u8; + // // Loading a custom Seq Number, because I didn't wat to split it into two + // binding[6]=(config.sequence>>8) as u8; + // binding[7]=(config.sequence&0xff) as u8; let mut packet = icmp::MutableIcmpPacket::owned(binding).expect("Couldn't create packet"); @@ -46,22 +46,22 @@ pub fn create_icmp_packet<'a>(config: &Config) -> icmp::MutableIcmpPacket<'a>{ packet.set_icmp_type(icmp::IcmpTypes::EchoRequest); packet.set_icmp_code(pnet::packet::icmp::IcmpCode(0)); + packet.set_payload(&icmp_payload); + // if config.payload.len()==0{ + // // let payload=vec![4u8;(config.packet_size-4).into()]; // For a fixed payload + // let mut payload=Vec::::new(); + // // Loading a custom Id, because I didn't want to split it into two functions + // payload.push((config.identifier >> 8) as u8); + // payload.push((config.identifier&0xff) as u8); - if config.payload.len()==0{ - // let payload=vec![4u8;(config.packet_size-4).into()]; // For a fixed payload - let mut payload=Vec::::new(); - // Loading a custom Id, because I didn't want to split it into two functions - payload.push((config.identifier >> 8) as u8); - payload.push((config.identifier&0xff) as u8); - - // Loading a custom Seq Number, because I didn't wat to split it into two - payload.push((config.sequence>>8) as u8); - 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()); - } + // // Loading a custom Seq Number, because I didn't wat to split it into two + // payload.push((config.sequence>>8) as u8); + // 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())); packet diff --git a/src/main.rs b/src/main.rs index 9f52b6c..d1cfc56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,9 +9,10 @@ use pnet::packet::Packet; fn main() { // Parse the command line arguments. - let config = libs::parse_all_arguments(); + let mut config = libs::parse_all_arguments(); + // let icmp_payload = libs::get_payload_from_config(&mut config); let mut packet = libs::create_ethernet_packet(&config); - let payload = libs::create_icmp_packet(&config); + let payload = libs::create_icmp_packet(&config, &libs::get_payload_from_str(&config.payload)); 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"));