From 199de6f3d43363d0741e11c3ad0f915334ef70d3 Mon Sep 17 00:00:00 2001 From: beffroi Date: Fri, 9 Dec 2022 12:36:00 +0100 Subject: [PATCH] Refactoring step 5 --- Cargo.toml | 2 +- src/main.rs | 88 +++++++++++++++++++++++++++++++---------------------- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 37078e4..ec42fa4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ socket2 = "0.4" env_logger = "0.10" rhexdump = "0.1.1" rand = "0.8.5" -dns-lookup = "1.0.8" +#dns-lookup = "1.0.8" [profile.release] strip = true # Automatically strip symbols from the binary. diff --git a/src/main.rs b/src/main.rs index 17d14f2..63d3a46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,61 +32,37 @@ fn main() { let (buffer, checksum) = create_buffer(ping_request,data,payload); // Send the ping request to the specified destination IP address. - // let dest_addr = ; - println!("[SENDING ICMP DATA]\n"); - println!("CHECKSUM: {:02x} {:02x}", (checksum&0xff00)>>8, (checksum)&0xff); - println!("{}",rhexdump::hexdump(&buffer)); + print_send_pre_message(checksum, buffer); // Starting the timer let now = Instant::now(); let result = socket_arc.send_to(&buffer, dest_addr); match result { - Ok(bytes) => println!(" > {} bytes sent to {}", bytes, dest_addr), - Err(err) => println!(" > Failed to send data over the socket: {}", err) + Ok(bytes) => print_send_post_messages(bytes, dest_addr), + Err(err) => println!(" [ERROR] Failed to send data over the socket: {}", err) } - + // Wait for a response from the destination. let mut buf = [0; 64]; let timeout = std::time::Duration::from_secs(1); + + socket_arc.set_read_timeout(Some(timeout)).unwrap(); let (bytes_recv, _src_addr) = match socket_arc.recv_from(&mut buf) { Ok(res) => res, Err(ref err) if err.kind() == io::ErrorKind::WouldBlock => { - println!("No response from destination"); - println!("Error {:?}", err); + println!(" [ERROR] No response from destination"); + println!(" [ERROR] {:?}", err); std::process::exit(0); } - Err(err) => panic!("Error receiving data: {}", err), + Err(err) => panic!(" [ERROR] Error receiving data: {}", err), }; let elapsed = now.elapsed(); - // Print the response from the destination. - // print!("{} bytes from {}: icmp_seq={} ", - // Parse the response from the destination. - let icmp_type = buf[0]; - let icmp_code = buf[1]; - let icmp_checksum = ((buf[2] as u16) << 8) | (buf[3] as u16); - let icmp_id = ((buf[4] as u16) << 8) | (buf[5] as u16); - let icmp_seq = ((buf[6] as u16) << 8) | (buf[7] as u16); - //let data = &buf[8..bytes_recv]; - - // Print the parsed response. - println!("\n[ANALYSIS] \n"); - println!("CHECKSUM: {:02x} {:02x}", (icmp_checksum&0xff00)>>8, (icmp_checksum)&0xff); - // println!("CHECKSUM: [{:04x}]",icmp_checksum); - println!("{}",rhexdump::hexdump(&buf)); - println!(" > {} bytes recv over the socket in {:.2} milliseconds", bytes_recv, (elapsed.as_micros() as f64)/1000.); - println!(" > icmp_type = {:width$} icmp_code = {:width$}", icmp_type, icmp_code, width=2); - println!(" > icmp_id = {:width$} icmp_seq = {:width$}", icmp_id, icmp_seq, width=2); - // print!("data="); - // for b in data { - // print!("{:02x}", b); - // } - println!(); + print_recv_post_message(buf, bytes_recv, elapsed); } - fn parse_all_arguments() -> (String,u16,u16,Vec){ // Load all inline arguments and change them in place let args: Vec = env::args().collect(); @@ -162,8 +138,7 @@ fn exit_with_help() { std::process::exit(1); } -fn create_buffer(ping_request: u16, data: u16, payload: Vec) -> ([u8; 64],u16){ - +fn create_buffer(ping_request: u16, data: u16, mut payload: Vec) -> ([u8; 64],u16){ // Create the ping request message. let mut buf = [0u8; 64]; let ping_request = [ @@ -177,6 +152,10 @@ fn create_buffer(ping_request: u16, data: u16, payload: Vec) -> ([u8; 64],u1 let data = [ (data >> 8) as u8, (data & 0xff) as u8, ]; + + if payload.len() == 0{ + generate_payload(&mut payload); + } buf[ping_request.len()..(ping_request.len() + data.len())].copy_from_slice(&data); buf[ping_request.len() + data.len()..(ping_request.len() + data.len()+payload.len())].copy_from_slice(&payload); @@ -194,6 +173,43 @@ fn create_buffer(ping_request: u16, data: u16, payload: Vec) -> ([u8; 64],u1 (buf, checksum) } + +fn print_send_pre_message(checksum:u16, buffer:[u8;64]){ + println!("[SENDING ICMP DATA]\n"); + println!("CHECKSUM: {:02x} {:02x}", (checksum&0xff00)>>8, (checksum)&0xff); + println!("{}",rhexdump::hexdump(&buffer)); +} + +fn print_send_post_messages(bytes:usize, dest_addr:std::net::SocketAddr){ + println!(" > {} bytes sent to {}", bytes, dest_addr); +} + +fn print_recv_post_message(buf: [u8;64], bytes_recv:usize, elapsed:Duration){ + // Parse the response from the destination. + let icmp_type = buf[0]; + let icmp_code = buf[1]; + let icmp_checksum = ((buf[2] as u16) << 8) | (buf[3] as u16); + let icmp_id = ((buf[4] as u16) << 8) | (buf[5] as u16); + let icmp_seq = ((buf[6] as u16) << 8) | (buf[7] as u16); + //let data = &buf[8..bytes_recv]; + + // Print the parsed response. + println!("\n[ANALYSIS] \n"); + println!("CHECKSUM: {:02x} {:02x}", (icmp_checksum&0xff00)>>8, (icmp_checksum)&0xff); + println!("{}",rhexdump::hexdump(&buf)); + println!(" > {} bytes recv over the socket in {:.2} milliseconds", bytes_recv, (elapsed.as_micros() as f64)/1000.); + println!(" > icmp_type = {:width$} icmp_code = {:width$}", icmp_type, icmp_code, width=2); + println!(" > icmp_id = {:width$} icmp_seq = {:width$}", icmp_id, icmp_seq, width=2); + println!(); +} + +fn generate_payload(payload: &mut Vec/*,size:usize*/){ + // Generate a payload of size lenght + for i in [..32].iter(){ + // payload. + } + +} // match temp_payload { // None => { // let mut rng = thread_rng();