Added a cthreads argument
This commit is contained in:
		
							
								
								
									
										57
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								src/main.rs
									
									
									
									
									
								
							@ -13,6 +13,7 @@ fn main() -> std::io::Result<()> {
 | 
			
		||||
    let timeout = Duration::from_secs(args.timeout);
 | 
			
		||||
    let sleep = Duration::from_millis(args.wait);
 | 
			
		||||
    let ports = args.ports;
 | 
			
		||||
    let threads = args.cthreads;
 | 
			
		||||
    
 | 
			
		||||
    //Reading the payload from stdin (if applicable)
 | 
			
		||||
    let mut payload = String::new();
 | 
			
		||||
@ -25,7 +26,7 @@ fn main() -> std::io::Result<()> {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if args.stress {
 | 
			
		||||
        tcp_stress(payload, address, timeout, sleep, ports)?;
 | 
			
		||||
        tcp_stress(payload, address, timeout, sleep, ports, threads)?;
 | 
			
		||||
    } else {
 | 
			
		||||
        tcp_scan(address, timeout, sleep, ports)?;
 | 
			
		||||
    }
 | 
			
		||||
@ -59,36 +60,42 @@ fn tcp_scan(address: String, timeout: Duration, sleep: Duration, ports: Vec<u32>
 | 
			
		||||
 | 
			
		||||
///Try to stress the remote machine by attempting to every given port at once and sending random
 | 
			
		||||
///data
 | 
			
		||||
fn tcp_stress(payload: String, address: String, timeout: Duration, interval: Duration, ports: Vec<u32>) -> std::io::Result<()> {
 | 
			
		||||
fn tcp_stress(payload: String, address: String, timeout: Duration, interval: Duration, ports: Vec<u32>, threads: u32) -> std::io::Result<()> {
 | 
			
		||||
    let mut handles = vec![];
 | 
			
		||||
 | 
			
		||||
    for port in ports {
 | 
			
		||||
        tcp_scan(address.clone(), timeout, interval, vec![port])?;
 | 
			
		||||
        let add = address.clone();
 | 
			
		||||
        let pay = payload.clone();
 | 
			
		||||
        let int = interval * 1000;
 | 
			
		||||
        let end_time = Instant::now() + int;
 | 
			
		||||
        let mut threads_left = threads;
 | 
			
		||||
 | 
			
		||||
        loop {
 | 
			
		||||
            let add = address.clone();
 | 
			
		||||
            let pay = payload.clone();
 | 
			
		||||
            let int = interval * 1000;
 | 
			
		||||
            let end_time = Instant::now() + int;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        handles.push(thread::spawn( move || {
 | 
			
		||||
            let socket = format!("{}:{}", &add, &port);
 | 
			
		||||
            let socket = socket.parse::<SocketAddr>().unwrap();
 | 
			
		||||
            loop {
 | 
			
		||||
                //Check if it is time to stop
 | 
			
		||||
                if Instant::now() >= end_time { break; }
 | 
			
		||||
                match TcpStream::connect_timeout(&socket, timeout) {
 | 
			
		||||
                    Ok(mut s) => {
 | 
			
		||||
                        s.write(pay.as_bytes()).unwrap();
 | 
			
		||||
                        s.flush().unwrap();
 | 
			
		||||
                        //s.read(&mut [0; 128]).unwrap();
 | 
			
		||||
                        thread::sleep(Duration::from_millis(10));
 | 
			
		||||
                    },
 | 
			
		||||
                    Err(_) => (),
 | 
			
		||||
            handles.push(thread::spawn( move || {
 | 
			
		||||
                let socket = format!("{}:{}", &add, &port);
 | 
			
		||||
                let socket = socket.parse::<SocketAddr>().unwrap();
 | 
			
		||||
                loop {
 | 
			
		||||
                    //Check if it is time to stop
 | 
			
		||||
                    if Instant::now() >= end_time { break; }
 | 
			
		||||
                    match TcpStream::connect_timeout(&socket, timeout) {
 | 
			
		||||
                        Ok(mut s) => {
 | 
			
		||||
                            s.write(pay.as_bytes()).unwrap();
 | 
			
		||||
                            s.flush().unwrap();
 | 
			
		||||
                            //s.read(&mut [0; 128]).unwrap();
 | 
			
		||||
                            thread::sleep(Duration::from_millis(10));
 | 
			
		||||
                        },
 | 
			
		||||
                        Err(_) => (),
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }));
 | 
			
		||||
            }));
 | 
			
		||||
            *&mut threads_left -=1;
 | 
			
		||||
            if threads_left == 0 { break; }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    println!("\nStressing...\n");
 | 
			
		||||
    println!("\nStressing {} using {} concurrent threads per port for {:?} \n", address, threads, interval * 1000);
 | 
			
		||||
    for handle in handles {
 | 
			
		||||
        let _ = handle.join();
 | 
			
		||||
    }
 | 
			
		||||
@ -116,4 +123,8 @@ struct Args {
 | 
			
		||||
    ///Set this flag to stress the ports instead of scanning them
 | 
			
		||||
    #[arg(long, short, action, default_value_t = false)]
 | 
			
		||||
    stress: bool,
 | 
			
		||||
    ///How many threads per port when stressing
 | 
			
		||||
    ///(Concurrent Threads)
 | 
			
		||||
    #[arg(short, long, default_value_t = 5)]
 | 
			
		||||
    cthreads: u32,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user