Skip to content

Commit c7075a1

Browse files
committed
Scripting engine implementation.
1 parent b3a6427 commit c7075a1

File tree

10 files changed

+542
-61
lines changed

10 files changed

+542
-61
lines changed

Cargo.lock

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ serde_derive = "1.0.116"
3636
cidr-utils = "0.5.0"
3737
itertools = "0.9.0"
3838
trust-dns-resolver = { version = "0.19.5", features = ["dns-over-rustls"] }
39+
anyhow = "1.0.32"
40+
subprocess = "0.2.6"
41+
text_placeholder = { version = "0.3", features = ["struct_context"] }
3942

4043
[dev-dependencies]
4144
wait-timeout = "0.2"

fixtures/test_rustscan_scripts.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Test/Example ScriptConfig file
2+
3+
# Tags to filter on scripts. Only scripts containing all these tags will run.
4+
tags = ["core_approved", "example"]
5+
6+
# If it's present then only those scripts will run which has a tag ports = "80". Not yet implemented.
7+
#
8+
# ex.:
9+
# ports = [80]
10+
# ports = [80,81,8080]
11+
ports = [80]
12+
13+
# Only this developer(s) scripts to run. Not yet implemented.
14+
developer = ["example"]

fixtures/test_script.pl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/perl
2+
#tags = ["core_approved", "example",]
3+
#developer = [ "example", "https://example.org" ]
4+
#join_ports = ","
5+
#call_format = "perl {{script}} {{ip}} {{port}}"
6+
7+
# Sriptfile parser stops at the first blank line with parsing.
8+
# This script will run itself as an argument with the system installed perl interpreter, ports will be concatenated with "," .
9+
# Unused field: port = "80"
10+
# get total arg passed to this script
11+
my $total = $#ARGV + 1;
12+
my $counter = 1;
13+
14+
# get script name
15+
my $scriptname = $0;
16+
17+
print "Total args passed to $scriptname : $total\n";
18+
19+
# Use loop to print all args stored in an array called @ARGV
20+
foreach my $a(@ARGV) {
21+
print "Arg # $counter : $a\n";
22+
$counter++;
23+
}

fixtures/test_script.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python3
2+
#tags = ["core_approved", "example",]
3+
#developer = [ "example", "https://example.org" ]
4+
#port = "80"
5+
#call_format = "python3 {{script}} {{ip}} {{port}}"
6+
7+
# Sriptfile parser stops at the first blank line with parsing.
8+
# This script will run itself as an argument with the system installed python interpreter, only scanning port 80.
9+
# Unused filed: join_ports = ","
10+
11+
import sys
12+
13+
print('Python script ran with arguments', str(sys.argv))

fixtures/test_script.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
#tags = ["core_approved", "example",]
3+
#developer = [ "example", "https://example.org" ]
4+
#join_ports = ","
5+
#call_format = "bash {{script}} {{ip}} {{port}}"
6+
7+
# Sriptfile parser stops at the first blank line with parsing.
8+
# This script will run itself as an argument with the system installed bash interpreter, scanning all ports concatenated with "," .
9+
# Unused filed: port = "80"
10+
11+
# print all arguments passed to the script
12+
echo $@

fixtures/test_script.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!intentional_blank_line
2+
#tags = ["core_approved", "example"]
3+
#developer = [ "example", "https://example.org" ]
4+
#join_ports = ","
5+
#call_format = "nmap -vvv -p {{port}} {{ip}}"
6+
7+
# Sriptfile parser stops at the first blank line with parsing.
8+
# This script will run the system installed nmap, ports will be concatenated with "," .
9+
# Unused field: port = "80"

src/input.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ pub struct Opts {
8080
#[structopt(long)]
8181
pub accessible: bool,
8282

83-
/// Turns off Nmap.
84-
#[structopt(long)]
85-
pub no_nmap: bool,
86-
8783
/// The batch size for port scanning, it increases or slows the speed of
8884
/// scanning. Depends on the open file limit of your OS. If you do 65535
8985
/// it will do every port at the same time. Although, your OS may not
@@ -105,17 +101,21 @@ pub struct Opts {
105101
#[structopt(long, possible_values = &ScanOrder::variants(), case_insensitive = true, default_value = "serial")]
106102
pub scan_order: ScanOrder,
107103

108-
/// The Nmap arguments to run.
104+
/// Use scriptconfig file and available scripts. Default value will run the embedded basic nmap scan.
105+
#[structopt(long, possible_values = &["none", "default", "custom"], case_insensitive = true, default_value = "default")]
106+
pub scripts: String,
107+
108+
/// Use the top 1000 ports.
109+
#[structopt(long)]
110+
pub top: bool,
111+
112+
/// The extra arguments to run.
109113
/// To use the argument -A, end RustScan's args with '-- -A'.
110114
/// Example: 'rustscan -T 1500 127.0.0.1 -- -A -sC'.
111115
/// This command adds -Pn -vvv -p $PORTS automatically to nmap.
112116
/// For things like --script '(safe and vuln)' enclose it in quotations marks \"'(safe and vuln)'\"")
113117
#[structopt(last = true)]
114118
pub command: Vec<String>,
115-
116-
/// Use the top 1000 ports.
117-
#[structopt(long)]
118-
pub top: bool,
119119
}
120120

121121
#[cfg(not(tarpaulin_include))]
@@ -153,7 +153,9 @@ impl Opts {
153153
}
154154
}
155155

156-
merge_required!(addresses, greppable, accessible, batch_size, timeout, scan_order, command);
156+
merge_required!(
157+
addresses, greppable, accessible, batch_size, timeout, scan_order, scripts, command
158+
);
157159
}
158160

159161
fn merge_optional(&mut self, config: &Config) {
@@ -199,6 +201,7 @@ pub struct Config {
199201
ulimit: Option<rlimit::rlim>,
200202
scan_order: Option<ScanOrder>,
201203
command: Option<Vec<String>>,
204+
scripts: Option<String>,
202205
}
203206

204207
#[cfg(not(tarpaulin_include))]
@@ -257,6 +260,7 @@ mod tests {
257260
command: Some(vec!["-A".to_owned()]),
258261
accessible: Some(true),
259262
scan_order: Some(ScanOrder::Random),
263+
scripts: None,
260264
}
261265
}
262266
}
@@ -273,9 +277,9 @@ mod tests {
273277
ulimit: None,
274278
command: vec![],
275279
accessible: false,
276-
no_nmap: false,
277280
scan_order: ScanOrder::Serial,
278281
no_config: true,
282+
scripts: "default".into(),
279283
top: false,
280284
}
281285
}

0 commit comments

Comments
 (0)