├── .gitignore ├── .travis.yml ├── Cargo.lock ├── Cargo.toml ├── LICENSE.md ├── README.md └── src └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - stable 4 | cache: cargo 5 | before_script: (cargo install rustfmt || true) 6 | script: 7 | - export PATH=$PATH:/home/travis/.cargo/bin 8 | - cargo fmt -- --write-mode=diff 9 | - cargo build --verbose 10 | - cargo test --verbose 11 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | [root] 2 | name = "scanrs" 3 | version = "0.1.0" 4 | dependencies = [ 5 | "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", 6 | ] 7 | 8 | [[package]] 9 | name = "getopts" 10 | version = "0.2.14" 11 | source = "registry+https://github.com/rust-lang/crates.io-index" 12 | 13 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "scanrs" 3 | version = "0.1.0" 4 | authors = ["Matt Layher "] 5 | 6 | [dependencies] 7 | getopts = "0.2.14" 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | =========== 3 | 4 | Copyright (C) 2016 Matt Layher 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | scanrs [![Build Status](https://travis-ci.org/mdlayher/scanrs.svg?branch=master)](https://travis-ci.org/mdlayher/scanrs) 2 | ====== 3 | 4 | A port scanner written in Rust, as an exercise to learn more about Rust! 5 | MIT Licensed. 6 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate getopts; 2 | 3 | use getopts::Options; 4 | use std::env; 5 | use std::io::{self, Write}; 6 | use std::net::{IpAddr, TcpStream}; 7 | use std::str::FromStr; 8 | use std::sync::mpsc::{Sender, channel}; 9 | use std::thread; 10 | 11 | // Max possible port 12 | const MAX: u16 = 65535; 13 | 14 | fn main() { 15 | let args: Vec = env::args().collect(); 16 | let program = args[0].clone(); 17 | 18 | let mut opts = Options::new(); 19 | opts.optflag("h", "help", "print this help menu"); 20 | opts.optopt("j", 21 | "", 22 | "number of threads to use for concurrent scanning", 23 | "THREADS"); 24 | 25 | let matches = match opts.parse(&args[1..]) { 26 | Ok(m) => m, 27 | Err(e) => { 28 | println!("error parsing options: {}", e.to_string()); 29 | return; 30 | } 31 | }; 32 | 33 | // Present usage if '-h' or no IP address provided 34 | if matches.opt_present("h") || matches.free.is_empty() { 35 | let brief = format!("Usage: {} -j THREADS IPADDR", program); 36 | print!("{}", opts.usage(&brief)); 37 | return; 38 | } 39 | 40 | // Default to 4 threads, but allow user to specify more or less 41 | let num_threads = match matches.opt_str("j") { 42 | Some(j) => j.parse().expect("flag '-j' must be an integer"), 43 | None => 4, 44 | }; 45 | 46 | // Parse IPv4 or IPv6 address for scanning 47 | let addr = IpAddr::from_str(&matches.free[0]) 48 | .expect("IPADDR must be a valid IPv4 or IPv6 address"); 49 | 50 | // Send and receive results via channels of port numbers, scanning 51 | // concurrently using threads 52 | let (tx, rx) = channel(); 53 | for i in 0..num_threads { 54 | let tx = tx.clone(); 55 | 56 | thread::spawn(move || { 57 | scan(tx, i, addr, num_threads); 58 | }); 59 | } 60 | 61 | // Drop transmit side of channel in main thread so that for loop 62 | // will end once all worker threads complete 63 | let mut out = vec![]; 64 | drop(tx); 65 | for port in rx { 66 | out.push(port); 67 | } 68 | 69 | // Add newline after progress, sort vector and print all ports 70 | println!(""); 71 | out.sort(); 72 | for v in out { 73 | println!("{} is open", v); 74 | } 75 | } 76 | 77 | // scan scans ports at an IP address and sends any open ports it finds back on its 78 | // channel. scan exits once MAX has been reached. 79 | fn scan(tx: Sender, start_port: u16, addr: IpAddr, num_threads: u16) { 80 | let mut port: u16 = start_port + 1; 81 | 82 | loop { 83 | match TcpStream::connect((addr, port)) { 84 | Ok(_) => { 85 | // Found open port, indicate progress and send to main thread 86 | print!("."); 87 | io::stdout().flush().unwrap(); 88 | tx.send(port).unwrap(); 89 | } 90 | Err(_) => {} 91 | } 92 | 93 | // Break loop when out of ports 94 | if (MAX - port) <= num_threads { 95 | break; 96 | } 97 | 98 | port += num_threads; 99 | } 100 | } 101 | --------------------------------------------------------------------------------