From 472a8c36648f1575fa4738a0122f234e0b81f3bf Mon Sep 17 00:00:00 2001 From: yuvraj Date: Tue, 6 Feb 2024 03:19:50 +0530 Subject: [PATCH] init commit --- .vscode/settings.json | 3 ++ Cargo.lock | 7 ++++ Cargo.toml | 3 ++ scan-1/.gitignore | 1 + scan-1/Cargo.toml | 8 ++++ scan-1/src/main.rs | 91 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 scan-1/.gitignore create mode 100644 scan-1/Cargo.toml create mode 100755 scan-1/src/main.rs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..51fd3bd --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.checkOnSave": true +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..762bbda --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "scan-1" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ad40242 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,3 @@ +[workspace] +members = ["scan-1",""] +resolver = "2" \ No newline at end of file diff --git a/scan-1/.gitignore b/scan-1/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/scan-1/.gitignore @@ -0,0 +1 @@ +/target diff --git a/scan-1/Cargo.toml b/scan-1/Cargo.toml new file mode 100644 index 0000000..cb72c7b --- /dev/null +++ b/scan-1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "scan-1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/scan-1/src/main.rs b/scan-1/src/main.rs new file mode 100755 index 0000000..b3958e4 --- /dev/null +++ b/scan-1/src/main.rs @@ -0,0 +1,91 @@ +use std::fs; +use std::sync::{mpsc, Arc, Mutex}; +use std::thread; +use std::time::Duration; +use std::net::TcpStream; +use std::io::{Write, stdout}; + +fn perform_nmap_scan(target_hostname: &str, scan_type: &str) -> Result<(String, String, String), String> { + let scan_args = format!("nmap -p 1-65535 {} {}", scan_type, target_hostname); + let result = std::process::Command::new("sh") + .arg("-c") + .arg(&scan_args) + .output() + .map_err(|e| format!("Error executing Nmap: {}", e))?; + + Ok(( + target_hostname.to_string(), + scan_type.to_string(), + String::from_utf8_lossy(&result.stdout).to_string(), + )) +} + +fn save_progress(index: u32) -> Result<(), std::io::Error> { + fs::write("progress.txt", index.to_string()) +} + +fn load_progress() -> u32 { + match fs::read_to_string("progress.txt") { + Ok(content) => content.trim().parse().unwrap_or(0), + Err(_) => 0, + } +} + +fn is_internet_available() -> bool { + TcpStream::connect("www.google.com:80").is_ok() +} + +fn main() { + println!("Starting the script..."); + + let all_results: Arc>> = Arc::new(Mutex::new(Vec::new())); + let start_i = load_progress(); + + while !is_internet_available() { + println!("Waiting for internet connection..."); + thread::sleep(Duration::from_secs(10)); + } + + let total_tasks = 99 * 100; + + let (tx, rx) = mpsc::channel(); + + for i in start_i..100 { + for j in 1..100 { + let hostname = format!("e2e-{:02}-{:02}.ssdcloudindia.net", i, j); + let scan_types: Vec = vec![" -A -T4".to_string()]; + for scan_type in scan_types.iter() { + let tx = tx.clone(); + let all_results_clone = Arc::clone(&all_results); + let hostname_clone = hostname.clone(); // Clone the hostname for each iteration + let scan_type_clone = scan_type.clone(); // Clone the scan_type for each iteration + thread::spawn(move || { + println!("Scanning {}...", hostname_clone); + let result = perform_nmap_scan(&hostname_clone, &scan_type_clone); + tx.send(result.clone()).unwrap(); + + let mut results = all_results_clone.lock().unwrap(); + results.push(result.unwrap()); + }); + } + + save_progress(i + 1).unwrap(); + } + } + + drop(tx); + + let mut summary_file = fs::File::create("report.txt").expect("Error creating report.txt"); + + for (idx, result) in rx.iter().enumerate() { + let (hostname, scan_type, scan_result) = result.unwrap(); + summary_file.write_all(format!("Scan Type: {}\nHostname: {}\n{}\n", scan_type, hostname, scan_result).as_bytes()) + .expect("Error writing to report.txt"); + + let progress = ((idx + 1) as f64 / total_tasks as f64) * 100.0; + print!("\rProgress: {:.2}%", progress); + stdout().flush().unwrap(); + } + + println!("\nScript completed."); +}