Refactor part 4

This commit is contained in:
beffroi 2022-12-09 11:38:03 +01:00
parent 34c8d20ba2
commit 1d1e05c593
2 changed files with 70 additions and 59 deletions

View File

@ -10,6 +10,7 @@ socket2 = "0.4"
env_logger = "0.10"
rhexdump = "0.1.1"
rand = "0.8.5"
dns-lookup = "1.0.8"
[profile.release]
strip = true # Automatically strip symbols from the binary.

View File

@ -1,4 +1,5 @@
use core::time::Duration;
//use dns_lookup::lookup_host;
use core::time::Duration;
// use rand::{
// Rng,
// thread_rng,
@ -10,7 +11,7 @@ use std::{
sync::Arc,
env,
io::{self},
net::{IpAddr, Ipv4Addr, SocketAddr},
net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs},
};
use std::os::{
unix::io::AsRawFd,
@ -22,52 +23,25 @@ fn main() {
//env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
// Parse the command line arguments.
let (dest_ip,ping_request,data,payload) = parse_all_arguments();//&mut dest_ip, &mut ping_request, &mut data, &mut payload);
let (dest_addr,ping_request,data,payload) = parse_all_arguments();
let (socket_ip_address, socket2_ip_address) = create_address();
let mut socket2_ipv4_socket=create_socket(socket_ip_address, socket2_ip_address);
let (socket_ip_address, socket2_ip_address, dest_addr) = create_addresses(dest_addr);
let mut socket2_ipv4_socket=create_sockets(socket_ip_address, socket2_ip_address);
let socket_arc = create_socket_arc(&mut socket2_ipv4_socket);
// Create a UDP socket.
// let socket_arc = ;
// Create the ping request message.
let mut buf = [0u8; 64];
let ping_request = [
(ping_request >> 8) as u8, (ping_request & 0xff) as u8, // Type and Code fields
0x00, 0x00, // Checksum (filled in later)
0x00, 0x00, 0x00, 0x00, // Identifier and Sequence Number fields
];
buf[..ping_request.len()].copy_from_slice(&ping_request);
// Add the data to the ping request message.
let data = [
(data >> 8) as u8, (data & 0xff) as u8,
];
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);
// Calculate the checksum of the ping request message.
let checksum = {
let mut sum = 0;
for i in 0..(ping_request.len() / 2) {
sum += (ping_request[2 * i] as u16) << 8 | ping_request[2 * i + 1] as u16;
}
!(sum as u16)
};
buf[2] = (checksum >> 8) as u8;
buf[3] = (checksum & 0xff) as u8;
let (buffer, checksum) = create_buffer(ping_request,data,payload);
// Send the ping request to the specified destination IP address.
let dest_addr = SocketAddr::new(IpAddr::V4(dest_ip), 0);
// let dest_addr = ;
println!("[SENDING ICMP DATA]\n");
println!("CHECKSUM: {:02x} {:02x}", (checksum&0xff00)>>8, (checksum)&0xff);
println!("{}",rhexdump::hexdump(&buf));
println!("{}",rhexdump::hexdump(&buffer));
// Starting the timer
let now = Instant::now();
let result = socket_arc.send_to(&buf, dest_addr);
let result = socket_arc.send_to(&buffer, dest_addr);
match result {
Ok(bytes) => println!(" > {} bytes sent over the socket", bytes),
Ok(bytes) => println!(" > {} bytes sent to {}", bytes, dest_addr),
Err(err) => println!(" > Failed to send data over the socket: {}", err)
}
@ -112,22 +86,8 @@ fn main() {
}
// match temp_payload {
// None => {
// let mut rng = thread_rng();
// payload=(0..64)
// .map(|_| rng.sample(Alphanumeric) as char)
// .collect::<String>()
// .as_bytes()
// // .unwrap_or("Couldn't generate 64 bits of entropy")
// //b""
// }
// Some(p) => {
// payload=*p
// }, // Default payload value
// };
fn parse_all_arguments() -> (Ipv4Addr,u16,u16,Vec<u8>){
fn parse_all_arguments() -> (String,u16,u16,Vec<u8>){
// Load all inline arguments and change them in place
let args: Vec<String> = env::args().collect();
let mut ping_request : u16 = 0x0800;
@ -137,8 +97,8 @@ fn parse_all_arguments() -> (Ipv4Addr,u16,u16,Vec<u8>){
if args.len() == 1 {
exit_with_help();
}
let dest_ip=args[1].parse::<Ipv4Addr>().unwrap();
// let dest_ip=args[1].parse::<Ipv4Addr>().unwrap();
let dest_addr= args[1].clone();
for (i,arg) in args[1..].iter().enumerate(){
if arg == "--help" || arg == "-h" {
exit_with_help();
@ -150,18 +110,21 @@ fn parse_all_arguments() -> (Ipv4Addr,u16,u16,Vec<u8>){
payload = args[i + 2].as_bytes().to_vec();
}
}
(dest_ip,ping_request, data, payload)
(dest_addr,ping_request, data, payload)
}
fn create_address() -> (SocketAddr, socket2::SockAddr){
fn create_addresses(dest_ip: String) -> (SocketAddr, socket2::SockAddr, SocketAddr){
// SOURCE IP ADDRESS
let localhost = Ipv4Addr::UNSPECIFIED;
let socket_ip_address = SocketAddr::new(IpAddr::V4(localhost), 80);
let socket2_ip_address = socket_ip_address.into();
(socket_ip_address, socket2_ip_address)
let dest_addr = (dest_ip,0).to_socket_addrs().unwrap().next().unwrap();//SocketAddr::new(IpAddr::V4(dest_ip), 0);
// let dest_addr = "dandaedre.com".to_socket_addrs().unwrap();
(socket_ip_address, socket2_ip_address, dest_addr)
}
fn create_socket(socket_ip_address:SocketAddr, socket2_ip_address:socket2::SockAddr) -> socket2::Socket {
fn create_sockets(socket_ip_address:SocketAddr, socket2_ip_address:socket2::SockAddr) -> socket2::Socket {
let socket2_ipv4_socket = socket2::Socket::new(
socket2::Domain::IPV4,
socket2::Type::DGRAM,
@ -197,4 +160,51 @@ fn create_socket_arc(socket2_ipv4_socket: &mut socket2::Socket) -> Arc<std::net:
fn exit_with_help() {
eprintln!("Usage: cargo run --dest <destination IP address> [--request/-r <ping request>] [--data/-d <data>] [--payload/-p <payload>]");
std::process::exit(1);
}
}
fn create_buffer(ping_request: u16, data: u16, payload: Vec<u8>) -> ([u8; 64],u16){
// Create the ping request message.
let mut buf = [0u8; 64];
let ping_request = [
(ping_request >> 8) as u8, (ping_request & 0xff) as u8, // Type and Code fields
0x00, 0x00, // Checksum (filled in later)
0x00, 0x00, 0x00, 0x00, // Identifier and Sequence Number fields
];
buf[..ping_request.len()].copy_from_slice(&ping_request);
// Add the data to the ping request message.
let data = [
(data >> 8) as u8, (data & 0xff) as u8,
];
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);
// Calculate the checksum of the ping request message.
let checksum = {
let mut sum = 0;
for i in 0..(ping_request.len() / 2) {
sum += (ping_request[2 * i] as u16) << 8 | ping_request[2 * i + 1] as u16;
}
!(sum as u16)
};
buf[2] = (checksum >> 8) as u8;
buf[3] = (checksum & 0xff) as u8;
(buf, checksum)
}
// match temp_payload {
// None => {
// let mut rng = thread_rng();
// payload=(0..64)
// .map(|_| rng.sample(Alphanumeric) as char)
// .collect::<String>()
// .as_bytes()
// // .unwrap_or("Couldn't generate 64 bits of entropy")
// //b""
// }
// Some(p) => {
// payload=*p
// }, // Default payload value
// };