Allow randomized order for manual ports

Before this change we would always scan manually specified ports in
sequential way, this is not the case anymore. This is an important
change since we are planning to introduce a config file with the top 1K
ports and we would like to scan those in a randomized way.
This commit is contained in:
Bernardo Chaves committed 2020-08-21 13:09:41 -04:00
1 parent 85c1cbf445
commit e069991c8d
3 files changed
+32 -27

No files matched your search

+1 -2
View File
@@ -230,8 +230,7 @@ OPTIONS:
-b, --batch-size <batch-size> The batch size for port scanning, it increases or slows the speed of scanning.
Depends on the open file limit of your OS. If you do 65535 it will do every port
at the same time. Although, your OS may not support this [default: 4500]
-p, --ports <ports>... A list of comma separed ports to be scanned. Example: 80,443,8080. These ports will
be scanned sequentially
-p, --ports <ports>... A list of comma separed ports to be scanned. Example: 80,443,8080.
-r, --range <range> A range of ports with format start-end. Example: 1-1000
--scan-order <scan-order> The order of scanning to be performed. The "serial" option will scan ports in
ascending order while the "random" option will scan ports randomly [default:
-1
View File
@@ -82,7 +82,6 @@ struct Opts {
ips_or_hosts: Vec<String>,
/// A list of comma separed ports to be scanned. Example: 80,443,8080.
/// These ports will be scanned sequentially.
#[structopt(short, long, use_delimiter = true)]
ports: Option<Vec<u16>>,
+31 -24
View File
@@ -1,5 +1,7 @@
mod range_iterator;
use super::{PortRange, ScanOrder};
use rand::seq::SliceRandom;
use rand::thread_rng;
use range_iterator::RangeIterator;
/// Represents options of port scanning.
@@ -15,26 +17,27 @@ pub enum PortStrategy {
impl PortStrategy {
pub fn pick(range: Option<PortRange>, ports: Option<Vec<u16>>, order: ScanOrder) -> Self {
match order {
ScanOrder::Serial => match ports.is_none() {
true => {
let range = range.as_ref().unwrap();
PortStrategy::Serial(SerialRange {
start: range.start,
end: range.end,
})
}
false => PortStrategy::Manual(ports.unwrap()),
},
ScanOrder::Random => match ports.is_none() {
true => {
let range = range.as_ref().unwrap();
PortStrategy::Random(RandomRange {
start: range.start,
end: range.end,
})
}
false => PortStrategy::Manual(ports.unwrap()),
},
ScanOrder::Serial if ports.is_none() => {
let range = range.as_ref().unwrap();
PortStrategy::Serial(SerialRange {
start: range.start,
end: range.end,
})
}
ScanOrder::Random if ports.is_none() => {
let range = range.as_ref().unwrap();
PortStrategy::Random(RandomRange {
start: range.start,
end: range.end,
})
}
ScanOrder::Serial => PortStrategy::Manual(ports.unwrap()),
ScanOrder::Random => {
let mut rng = thread_rng();
let mut ports = ports.unwrap();
ports.shuffle(&mut rng);
PortStrategy::Manual(ports)
}
}
}
@@ -121,9 +124,13 @@ mod tests {
}
#[test]
fn random_strategy_with_ports_does_not_randomize_ports() {
let strategy = PortStrategy::pick(None, Some(vec![80, 443]), ScanOrder::Random);
let result = strategy.order();
assert_eq!(vec![80, 443], result);
fn random_strategy_with_ports() {
let strategy = PortStrategy::pick(None, Some((1..10).collect()), ScanOrder::Random);
let mut result = strategy.order();
let expected_range = (1..10).into_iter().collect::<Vec<u16>>();
assert_ne!(expected_range, result);
result.sort();
assert_eq!(expected_range, result);
}
}