first commit

This commit is contained in:
Justine Pelletreau
2023-07-13 18:47:01 +02:00
commit b7c0d064d6
6 changed files with 717 additions and 0 deletions

119
src/main.rs Normal file
View File

@ -0,0 +1,119 @@
use clap::Parser;
use std::io::prelude::*;
use std::io::{self, Read};
use std::net::{SocketAddr, TcpStream};
use std::time::{Duration, Instant};
use std::thread;
use atty::Stream;
fn main() -> std::io::Result<()> {
let args = Args::parse();
let address = args.address;
let timeout = Duration::from_secs(args.timeout);
let sleep = Duration::from_millis(args.wait);
let ports = args.ports;
//Reading the payload from stdin (if applicable)
let mut payload = String::new();
let aatty = atty::is(Stream::Stdin);
if !aatty {
io::stdin().read_to_string(&mut payload)?;
} else {
payload = "SAKAMOTO".to_string();
}
if args.stress {
tcp_stress(payload, address, timeout, sleep, ports)?;
} else {
tcp_scan(address, timeout, sleep, ports)?;
}
Ok(())
}
///Scan the ports on address by attempting a tcp connection.
fn tcp_scan(address: String, timeout: Duration, sleep: Duration, ports: Vec<u32>) -> std::io::Result<()> {
println!("Scanning {}", address);
for i in ports {
let socket = format!("{}:{}", &address, i);
let socket = socket.parse::<SocketAddr>().unwrap();
match TcpStream::connect_timeout(&socket, timeout) {
Ok(mut s) => {
println!("TCP/{} => ACCEPT", i);
s.write(b"Some padding")?;
}
Err(_) => {
println!("TCP/{} => REJECT", i);
}
}
thread::sleep(sleep.clone());
//stream.write(b"Some great padding")?;
//stream.read(&mut [0; 128])?;
}
Ok(())
}
///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<()> {
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;
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(_) => (),
}
}
}));
}
println!("\nStressing...\n");
for handle in handles {
let _ = handle.join();
}
Ok(())
}
///A simple TCP port scanner / stresser.
///If stressing, you can pass a payload via a pipe.
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
struct Args {
///IP address to scan
#[arg(short, long)]
address: String,
///Timeout for each connection in seconds
#[arg(short, long, default_value_t = 1)]
timeout: u64,
///Number of milliseconds to wait in between scans when scanning
///OR duration of stress when stress testing (in seconds)
#[arg(short, long, default_value_t = 30)]
wait: u64,
///Ports to stress / scan, separated by commas (22,80)
#[arg(short, long, value_parser, num_args=1.., value_delimiter=',')]
ports: Vec<u32>,
///Set this flag to stress the ports instead of scanning them
#[arg(long, short, action, default_value_t = false)]
stress: bool,
}