Managed to add argh, too sick to code :(

This commit is contained in:
Dandaedre 2022-12-13 21:37:58 +01:00
parent 0ed323b536
commit 4fe25e4080
5 changed files with 128 additions and 75 deletions

View File

@ -6,16 +6,13 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
#socket2 = { version = "0.4.7", features = ["all"] }
#env_logger = "0.10"
rhexdump = "0.1.1" rhexdump = "0.1.1"
argh = "0.1.9"
rand = "0.8.5" rand = "0.8.5"
pnet = "0.31.0" pnet = "0.31.0"
pnet_datalink = "0.31.0" pnet_datalink = "0.31.0"
pnet_transport = "0.31.0" pnet_transport = "0.31.0"
pnet_packet = "0.31.0" pnet_packet = "0.31.0"
#libc = "0.2.138"
#dns-lookup = "1.0.8"
[profile.release] [profile.release]
strip = true # Automatically strip symbols from the binary. strip = true # Automatically strip symbols from the binary.

View File

@ -1,22 +1,76 @@
use std::env; // use std::env;
use argh::FromArgs;
#[derive(FromArgs)]
/// rping, sending very custom icmp requests
pub struct Config{ pub struct Config{
/// the destination address for the request
#[argh(positional)]
pub dest_addr : String, pub dest_addr : String,
/// the source address for the request
#[argh(option, default = "String::from(\"0.0.0.0\")")]
pub src_addr : String, pub src_addr : String,
/// the type of ping request
#[argh(option, default = "8")]
pub ping_type : u8, pub ping_type : u8,
/// the ping identifier u16 number
#[argh(option, default = "0x4242")]
pub identifier : u16, pub identifier : u16,
/// the ping sequence u16 number
#[argh(option, default = "0x0001")]
pub sequence : u16, pub sequence : u16,
pub payload : Vec<u8>,
/// the actual ping payload,
#[argh(option, default = "String::new()")]
// pub payload : Vec<u8>,
pub payload : String,
/// the payload size
#[argh(option, default = "64")]
pub payload_size : usize, pub payload_size : usize,
/// force the usage of a raw packet
#[argh(option, default = "false")]
pub raw_packet : bool, pub raw_packet : bool,
/// the packet size (excluding header)
#[argh(option, default = "64")]
pub packet_size : u8, pub packet_size : u8,
/// the ip version used (4 or 6)
#[argh(option, default = "4")]
pub ipv4_version : u8, pub ipv4_version : u8,
/// inter header length (must be >=5)
#[argh(option, default = "5")]
pub ihl : u8, pub ihl : u8,
/// differentiated service code points
#[argh(option, default = "0")]
pub dcsp : u8, pub dcsp : u8,
/// explicit congestion notification
#[argh(option, default = "0")]
pub ecn : u8, pub ecn : u8,
/// time to live
#[argh(option, default = "64")]
pub ttl: u8, pub ttl: u8,
/// identification (lacking documentation)
#[argh(option, default = "52703")]
pub identification : u16, pub identification : u16,
/// flags : 2 don't fragment, 4 more fragments
#[argh(option, default = "2")]
pub flags : u8, pub flags : u8,
/// the fragment offset (if fragmented)
#[argh(option, default = "0")]
pub offset : u16, pub offset : u16,
} }
@ -24,68 +78,71 @@ pub struct Config{
pub fn parse_all_arguments() -> Config { pub fn parse_all_arguments() -> Config {
// Load all inline arguments and create a config structure // Load all inline arguments and create a config structure
let args: Vec<String> = env::args().collect(); // let args: Vec<String> = env::args().collect();
let mut config = Config { let mut_config = argh::from_env();
dest_addr : String::new(),
src_addr : String::from("0.0.0.0"),
ping_type : 0x08,
identifier : 0x4242,
sequence : 0x4242,
payload: Vec::<u8>::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(); // let mut config = Config {
for (i,arg) in args[1..].iter().enumerate(){ // dest_addr : String::new(),
if arg == "--help" || arg == "-h" { // src_addr : String::from("0.0.0.0"),
exit_with_help(); // ping_type : 0x08,
} else if arg == "--type" || arg == "-t" { // identifier : 0x4242,
config.ping_type = args[i + 2].parse::<u8>().unwrap(); // sequence : 0x4242,
} else if arg == "--identifier" || arg == "-i" { // payload: Vec::<u8>::new(),
config.identifier = args[i + 2].parse::<u16>().unwrap(); // payload_size : 64,
} else if arg == "--sequence" || arg == "-q" { // raw_packet: false,
config.sequence = args[i + 2].parse::<u16>().unwrap(); // ipv4_version: 4,
} else if arg == "--payload" || arg == "-p" { // packet_size: 64,
config.payload = args[i + 2].as_bytes().to_vec(); // ihl : 5,
} else if arg == "--size" || arg == "-s" { // dcsp : 0,
config.payload_size = args[i + 2].parse::<usize>().unwrap(); // ecn : 0,
} else if arg =="--raw" { // ttl : 64,
config.raw_packet=true; // This enable raw packets but requires root privilege // 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::<u8>().unwrap();
// } else if arg == "--identifier" || arg == "-i" {
// config.identifier = args[i + 2].parse::<u16>().unwrap();
// } else if arg == "--sequence" || arg == "-q" {
// config.sequence = args[i + 2].parse::<u16>().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::<usize>().unwrap();
// } else if arg =="--raw" {
// config.raw_packet=true; // This enable raw packets but requires root privilege
// }
// }
//Sanitize inputs and show it //Sanitize inputs and show it
if config.payload.len()!=0{ // if config.payload.len()!=0{
println!("Corrected payload size, went from {} to {} according to given input",config.payload_size,config.payload.len()/4); // 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.payload_size=config.payload.len()/4;
} // }
config // config
mut_config
} }
fn exit_with_help() { // fn exit_with_help() {
eprintln!("Usage: cargo run --dest <destination IP address> // eprintln!("Usage: cargo run --dest <destination IP address>
[--type/-t <ping type>] The type of ping request 8 for IPv4 128 for IPv6 (8b) // [--type/-t <ping type>] The type of ping request 8 for IPv4 128 for IPv6 (8b)
[--code/-c <code=0>] The mandatory 0 code (8b) // [--code/-c <code=0>] The mandatory 0 code (8b)
[--identifier/-i <id>] The Header identifier (16b) // [--identifier/-i <id>] The Header identifier (16b)
[--sequence/-q <seq>] The Sequence number (16b) // [--sequence/-q <seq>] The Sequence number (16b)
[--payload/-p <text>] The Payload, generated randomly if not given (any) // [--payload/-p <text>] The Payload, generated randomly if not given (any)
[--size/-s <size>] The Payload size (any) but smaller than ??? // [--size/-s <size>] The Payload size (any) but smaller than ???
[--raw] Enable raw packet, require root privileges // [--raw] Enable raw packet, require root privileges
[--hex] Read value as hex (no spaces) // [--hex] Read value as hex (no spaces)
"); // ");
std::process::exit(1); // std::process::exit(1);
} // }

View File

@ -28,7 +28,7 @@ use rand::{
Rng, 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 // Create a new mutable ICMP packet
let mut binding = vec![0u8;config.packet_size as usize]; 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); payload.push((config.sequence&0xff) as u8);
generate_payload(&mut payload, (config.packet_size).into()); generate_payload(&mut payload, (config.packet_size).into());
packet.set_payload(&payload); packet.set_payload(&payload);
} else {
packet.set_payload(&config.payload.as_bytes());
} }
packet.set_checksum(pnet_packet::icmp::checksum(&packet.to_immutable())); packet.set_checksum(pnet_packet::icmp::checksum(&packet.to_immutable()));

View File

@ -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 // 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>{ pub fn create_ethernet_packet<'a>(config: &'a Config) -> ipv4::MutableIpv4Packet<'a>{
// The total length is dependent on the packed sent // 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 binding = vec![0; size_tot as usize];
let mut ethernet_packet = pnet_packet::ipv4::MutableIpv4Packet::owned(binding).unwrap(); let mut ethernet_packet = pnet_packet::ipv4::MutableIpv4Packet::owned(binding).unwrap();

View File

@ -6,22 +6,19 @@ use std::net::{
use pnet::packet::Packet; use pnet::packet::Packet;
fn main() { fn main() {
// Parse the command line arguments. // Parse the command line arguments.
let config = libs::parse_all_arguments(); let config = libs::parse_all_arguments();
let mut packet = libs::create_ethernet_packet(&config); 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 (tx, rx) = libs::create_interface(&config);
let res = libs::send_packet(packet.to_immutable(),tx,config.dest_addr.parse::<Ipv4Addr>().expect("Couldn't parse the IpAddr")); let res = libs::send_packet(packet.to_immutable(),tx,config.dest_addr.parse::<Ipv4Addr>().expect("Couldn't parse the IpAddr"));
libs::recv_packet(rx);
match res { match res {
Ok(s) => println!("{:?}", s), Ok(s) => println!("{:?}", s),
Err(e) => eprintln!("Program exited with errors {:?}",e) Err(e) => eprintln!("Program exited with errors {:?}",e)
} }
libs::recv_packet(rx);
} }