Skip to content

Commit

Permalink
Allow Masscan.scan to accept no arguments and only a block.
Browse files Browse the repository at this point in the history
* Use the block style of `Masscan.scan` in the `ronin-masscan new`
  command output.
  • Loading branch information
postmodern committed Jun 14, 2024
1 parent b91144a commit 883147e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 66 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ require 'ronin/masscan'

output_file = Ronin::Masscan.scan('192.168.1.1', ports: [80,443])
# => #<Masscan::OutputFile:...>

output_file = Ronin::Masscan.scan do |masscan|
masscan.ports = [80,443]
masscan.ips = '192.168.1.1'
end
# => #<Masscan::OutputFile:...>
```

Accessing the masscan scan data:
Expand Down
18 changes: 9 additions & 9 deletions data/templates/script.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ output_file = Ronin::Masscan.parse(<%= @output_file.inspect %>)
output_file = Ronin::Masscan.parse(ARGV[0])
<%- end -%>
<%- else -%>
output_file = Ronin::Masscan.scan(
output_file = Ronin::Masscan.scan do |masscan|
<%- case @ips.length -%>
<%- when 0 -%>
*ARGV,
masscan.ips = ARGV
<%- when 1 -%>
<%= @ips[0].inspect %>,
masscan.ips = <%= @ips[0].inspect %>
<%- else -%>
*<%= @ips.inspect %>,
masscan.ips = <%= @ips.inspect %>
<%- end -%>
<%- if @ports -%>
ports: <%= @ports.inspect %>,
masscan.ports = <%= @ports.inspect %>
<%- else -%>
# ports: [22, 80, 443, 8000..9000],
# masscan.ports = [22, 80, 443, 8000..9000]
<%- end -%>
<%- if @output_file -%>
output_file: <%= @output_file.inspect %>
masscan.output_file = <%= @output_file.inspect %>
<%- else -%>
# output_file: "path/to/masscan.bin"
# masscan.output_file = "path/to/masscan.bin"
<%- end -%>
)
end
<%- end -%>
<% if @features[:printing] -%>
Expand Down
6 changes: 2 additions & 4 deletions lib/ronin/masscan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,10 @@ module Masscan
# @api public
#
def self.scan(*ips,**kwargs,&block)
if ips.empty?
raise(ArgumentError,"must specify at least one IP address")
end

masscan = ::Masscan::Command.new(ips: ips, **kwargs,&block)

masscan.ips ||= ips

unless masscan.output_file
FileUtils.mkdir_p(CACHE_DIR)
tempfile = Tempfile.new(['masscan', '.json'], CACHE_DIR)
Expand Down
92 changes: 46 additions & 46 deletions spec/cli/commands/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,18 @@
subject.run(path)
end

it "must generate a new file containing a new `Ronin::Masscan.scan(...)`" do
it "must generate a new file containing a new `Ronin::Masscan.scan`" do
expect(File.read(path)).to eq(
<<~RUBY
#!/usr/bin/env ruby
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
# ports: [22, 80, 443, 8000..9000],
# output_file: "path/to/masscan.bin"
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
# masscan.ports = [22, 80, 443, 8000..9000]
# masscan.output_file = "path/to/masscan.bin"
end
RUBY
)
end
Expand Down Expand Up @@ -230,18 +230,18 @@
context "and when given the '--scanner' option" do
let(:argv) { super() + %w[--scanner] }

it "must generate a new file containing a new `Ronin::Masscan.scan(...)` instead" do
it "must generate a new file containing a new `Ronin::Masscan.scan` instead" do
expect(File.read(path)).to eq(
<<~RUBY
#!/usr/bin/env ruby
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
# ports: [22, 80, 443, 8000..9000],
# output_file: "path/to/masscan.bin"
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
# masscan.ports = [22, 80, 443, 8000..9000]
# masscan.output_file = "path/to/masscan.bin"
end
RUBY
)
end
Expand All @@ -258,11 +258,11 @@
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
# ports: [22, 80, 443, 8000..9000],
# output_file: "path/to/masscan.bin"
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
# masscan.ports = [22, 80, 443, 8000..9000]
# masscan.output_file = "path/to/masscan.bin"
end
output_file.each do |record|
p record
Expand All @@ -282,11 +282,11 @@
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
# ports: [22, 80, 443, 8000..9000],
# output_file: "path/to/masscan.bin"
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
# masscan.ports = [22, 80, 443, 8000..9000]
# masscan.output_file = "path/to/masscan.bin"
end
Ronin::DB.connect
Ronin::Masscan::Importer.import(output_file)
Expand All @@ -299,18 +299,18 @@
let(:file) { 'path/to/masscan.bin' }
let(:argv) { ['--output-file', file] }

it "must add an `output_file:` keyword argument to `Ronin::Masscan.scan(...)` with the given file" do
it "must add an `masscan.output_file =` keyword argument to `Ronin::Masscan.scan` with the given file" do
expect(File.read(path)).to eq(
<<~RUBY
#!/usr/bin/env ruby
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
# ports: [22, 80, 443, 8000..9000],
output_file: #{file.inspect}
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
# masscan.ports = [22, 80, 443, 8000..9000]
masscan.output_file = #{file.inspect}
end
RUBY
)
end
Expand All @@ -320,18 +320,18 @@
let(:ports) { [22, 80, 443] }
let(:argv) { ['--ports', "#{ports.join(',')}"] }

it "must add an `ports:` keyword argument to `Ronin::Masscan.scan(...)` with an Array of the given port numbers" do
it "must add an `masscan.ports =` keyword argument to `Ronin::Masscan.scan` with an Array of the given port numbers" do
expect(File.read(path)).to eq(
<<~RUBY
#!/usr/bin/env ruby
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
ports: #{ports.inspect},
# output_file: "path/to/masscan.bin"
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
masscan.ports = #{ports.inspect}
# masscan.output_file = "path/to/masscan.bin"
end
RUBY
)
end
Expand All @@ -353,18 +353,18 @@
['--ports', "#{start_port1}-#{stop_port1},#{start_port2}-#{stop_port2}"]
end

it "must add an `ports:` keyword argument to `Ronin::Masscan.scan(...)` with an Array of the given port ranges" do
it "must add an `masscan.ports =` keyword argument to `Ronin::Masscan.scan` with an Array of the given port ranges" do
expect(File.read(path)).to eq(
<<~RUBY
#!/usr/bin/env ruby
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
ports: #{ports.inspect},
# output_file: "path/to/masscan.bin"
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
masscan.ports = #{ports.inspect}
# masscan.output_file = "path/to/masscan.bin"
end
RUBY
)
end
Expand All @@ -390,18 +390,18 @@
['--ports', "#{port1},#{port2},#{start_port1}-#{stop_port1},#{start_port2}-#{stop_port2}"]
end

it "must add an `ports:` keyword argument to `Ronin::Masscan.scan(...)` with an Array of the given port numbers and ranges" do
it "must add an `masscan.ports =` keyword argument to `Ronin::Masscan.scan` with an Array of the given port numbers and ranges" do
expect(File.read(path)).to eq(
<<~RUBY
#!/usr/bin/env ruby
require 'ronin/masscan'
output_file = Ronin::Masscan.scan(
*ARGV,
ports: #{ports.inspect},
# output_file: "path/to/masscan.bin"
)
output_file = Ronin::Masscan.scan do |masscan|
masscan.ips = ARGV
masscan.ports = #{ports.inspect}
# masscan.output_file = "path/to/masscan.bin"
end
RUBY
)
end
Expand Down
29 changes: 22 additions & 7 deletions spec/masscan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,32 @@
describe '.scan' do
let(:ips) { '192.168.1.1' }

context 'when ip adresses are not given' do
it 'must raise an ArgumentError' do
expect {
subject.scan
}.to raise_error(ArgumentError, 'must specify at least one IP address')
let(:expected_output_filename) { %r{#{Ronin::Masscan::CACHE_DIR}\/masscan[^.]+\.json} }

context 'when IPs are given as arguments' do
it 'must pass the IPs to the `masscan` command' do
expect(Kernel).to receive(:system).with({}, 'masscan', '-p', '80', '--output-filename', match(expected_output_filename), ips).and_return(true)

subject.scan(ips, ports: 80)
end
end

context 'when masscan command was successful' do
let(:expected_output_filename) { %r{#{Ronin::Masscan::CACHE_DIR}\/masscan[^.]+\.json} }
context "when IPs are not given as arguments" do
context "but a block is given" do
context "and the IPs are set in the block" do
it 'must pass the IPs to the `masscan` command' do
expect(Kernel).to receive(:system).with({}, 'masscan', '-p', '80', '--output-filename', match(expected_output_filename), ips).and_return(true)

subject.scan do |masscan|
masscan.ports = 80
masscan.ips = ips
end
end
end
end
end

context 'when masscan command was successful' do
before do
allow(Kernel).to receive(:system).with({}, 'masscan', '-p', '80', '--output-filename', match(expected_output_filename), ips).and_return(true)
end
Expand Down

0 comments on commit 883147e

Please sign in to comment.