ronin-masscan 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.document +4 -0
  3. data/.github/workflows/ruby.yml +47 -0
  4. data/.gitignore +14 -0
  5. data/.rspec +1 -0
  6. data/.rubocop.yml +11 -0
  7. data/.ruby-version +1 -0
  8. data/.yardopts +1 -0
  9. data/COPYING.txt +165 -0
  10. data/ChangeLog.md +10 -0
  11. data/Gemfile +40 -0
  12. data/README.md +204 -0
  13. data/Rakefile +43 -0
  14. data/bin/ronin-masscan +34 -0
  15. data/data/completions/ronin-masscan +83 -0
  16. data/data/templates/script.rb.erb +43 -0
  17. data/gemspec.yml +42 -0
  18. data/lib/ronin/masscan/cli/command.rb +40 -0
  19. data/lib/ronin/masscan/cli/commands/completion.rb +61 -0
  20. data/lib/ronin/masscan/cli/commands/convert.rb +133 -0
  21. data/lib/ronin/masscan/cli/commands/dump.rb +194 -0
  22. data/lib/ronin/masscan/cli/commands/grep.rb +235 -0
  23. data/lib/ronin/masscan/cli/commands/import.rb +94 -0
  24. data/lib/ronin/masscan/cli/commands/new.rb +203 -0
  25. data/lib/ronin/masscan/cli/commands/print.rb +162 -0
  26. data/lib/ronin/masscan/cli/commands/scan.rb +206 -0
  27. data/lib/ronin/masscan/cli/filtering_options.rb +312 -0
  28. data/lib/ronin/masscan/cli/importable.rb +68 -0
  29. data/lib/ronin/masscan/cli/port_list.rb +102 -0
  30. data/lib/ronin/masscan/cli.rb +50 -0
  31. data/lib/ronin/masscan/converter.rb +129 -0
  32. data/lib/ronin/masscan/converters/csv.rb +108 -0
  33. data/lib/ronin/masscan/converters/json.rb +142 -0
  34. data/lib/ronin/masscan/converters.rb +54 -0
  35. data/lib/ronin/masscan/exceptions.rb +47 -0
  36. data/lib/ronin/masscan/importer.rb +214 -0
  37. data/lib/ronin/masscan/root.rb +28 -0
  38. data/lib/ronin/masscan/version.rb +26 -0
  39. data/lib/ronin/masscan.rb +114 -0
  40. data/man/ronin-masscan-completion.1 +76 -0
  41. data/man/ronin-masscan-completion.1.md +78 -0
  42. data/man/ronin-masscan-convert.1 +37 -0
  43. data/man/ronin-masscan-convert.1.md +40 -0
  44. data/man/ronin-masscan-dump.1 +116 -0
  45. data/man/ronin-masscan-dump.1.md +94 -0
  46. data/man/ronin-masscan-grep.1 +56 -0
  47. data/man/ronin-masscan-grep.1.md +59 -0
  48. data/man/ronin-masscan-import.1 +52 -0
  49. data/man/ronin-masscan-import.1.md +57 -0
  50. data/man/ronin-masscan-new.1 +78 -0
  51. data/man/ronin-masscan-new.1.md +70 -0
  52. data/man/ronin-masscan-print.1 +53 -0
  53. data/man/ronin-masscan-print.1.md +56 -0
  54. data/man/ronin-masscan-scan.1 +86 -0
  55. data/man/ronin-masscan-scan.1.md +84 -0
  56. data/man/ronin-masscan.1 +61 -0
  57. data/man/ronin-masscan.1.md +58 -0
  58. data/ronin-masscan.gemspec +62 -0
  59. data/scripts/setup +161 -0
  60. metadata +168 -0
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ronin/masscan'
4
+
5
+ <%- if @script_type == :parser -%>
6
+ <%- if @output_file -%>
7
+ output_file = Ronin::Masscan.parse(<%= @output_file.inspect %>)
8
+ <%- else -%>
9
+ output_file = Ronin::Masscan.parse(ARGV[0])
10
+ <%- end -%>
11
+ <%- else -%>
12
+ output_file = Ronin::Masscan.scan do |masscan|
13
+ <%- case @ips.length -%>
14
+ <%- when 0 -%>
15
+ masscan.ips = ARGV
16
+ <%- when 1 -%>
17
+ masscan.ips = <%= @ips[0].inspect %>
18
+ <%- else -%>
19
+ masscan.ips = <%= @ips.inspect %>
20
+ <%- end -%>
21
+ <%- if @ports -%>
22
+ masscan.ports = <%= @ports.inspect %>
23
+ <%- else -%>
24
+ # masscan.ports = [22, 80, 443, 8000..9000]
25
+ <%- end -%>
26
+ <%- if @output_file -%>
27
+ masscan.output_file = <%= @output_file.inspect %>
28
+ <%- else -%>
29
+ # masscan.output_file = "path/to/masscan.bin"
30
+ <%- end -%>
31
+ end
32
+ <%- end -%>
33
+ <% if @features[:printing] -%>
34
+
35
+ output_file.each do |record|
36
+ p record
37
+ end
38
+ <%- end -%>
39
+ <%- if @features[:import] -%>
40
+
41
+ Ronin::DB.connect
42
+ Ronin::Masscan::Importer.import(output_file)
43
+ <%- end -%>
data/gemspec.yml ADDED
@@ -0,0 +1,42 @@
1
+ name: ronin-masscan
2
+ summary: A Ruby library and CLI for working with masscan.
3
+ description: |
4
+ ronin-masscan is a Ruby library for working with masscan. ronin-masscan can
5
+ parse masscan scan files, convert masscan files into JSON or CSV, or import
6
+ masscan scan data into the ronin-db database.
7
+
8
+ license: LGPL-3.0
9
+ authors: Postmodern
10
+ email: postmodern.mod3@gmail.com
11
+ homepage: https://ronin-rb.dev/
12
+ has_yard: true
13
+
14
+ metadata:
15
+ documentation_uri: https://ronin-rb.dev/docs/ronin-masscan
16
+ source_code_uri: https://github.com/ronin-rb/ronin-masscan
17
+ bug_tracker_uri: https://github.com/ronin-rb/ronin-masscan/issues
18
+ changelog_uri: https://github.com/ronin-rb/ronin-masscan/blob/main/ChangeLog.md
19
+ rubygems_mfa_required: 'true'
20
+
21
+ generated_files:
22
+ - data/completions/ronin-masscan
23
+ - man/ronin-masscan.1
24
+ - man/ronin-masscan-completion.1
25
+ - man/ronin-masscan-convert.1
26
+ - man/ronin-masscan-dump.1
27
+ - man/ronin-masscan-grep.1
28
+ - man/ronin-masscan-import.1
29
+ - man/ronin-masscan-new.1
30
+ - man/ronin-masscan-print.1
31
+ - man/ronin-masscan-scan.1
32
+
33
+ required_ruby_version: ">= 3.0.0"
34
+
35
+ dependencies:
36
+ ruby-masscan: ~> 0.1
37
+ # Ronin dependencies:
38
+ ronin-core: ~> 0.2.0.rc1
39
+ ronin-db: ~> 0.2.0.rc1
40
+
41
+ development_dependencies:
42
+ bundler: ~> 2.0
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # ronin-masscan - A Ruby library and CLI for working with masscan.
4
+ #
5
+ # Copyright (c) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com)
6
+ #
7
+ # ronin-masscan is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ronin-masscan is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ronin-masscan. If not, see <https://www.gnu.org/licenses/>.
19
+ #
20
+
21
+ require 'ronin/masscan/root'
22
+
23
+ require 'ronin/core/cli/command'
24
+
25
+ module Ronin
26
+ module Masscan
27
+ class CLI
28
+ #
29
+ # Base class for all other `ronin-masscan` commands.
30
+ #
31
+ class Command < Core::CLI::Command
32
+
33
+ man_dir File.join(ROOT,'man')
34
+
35
+ bug_report_url 'https://github.com/ronin-rb/ronin-masscan/issues/new'
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # ronin-masscan - A Ruby library and CLI for working with masscan.
4
+ #
5
+ # Copyright (c) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com)
6
+ #
7
+ # ronin-masscan is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ronin-masscan is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ronin-masscan. If not, see <https://www.gnu.org/licenses/>.
19
+ #
20
+
21
+ require 'ronin/masscan/root'
22
+ require 'ronin/core/cli/completion_command'
23
+
24
+ module Ronin
25
+ module Masscan
26
+ class CLI
27
+ module Commands
28
+ #
29
+ # Manages the shell completion rules for `ronin-masscan`.
30
+ #
31
+ # ## Usage
32
+ #
33
+ # ronin-masscan completion [options]
34
+ #
35
+ # ## Options
36
+ #
37
+ # --print Prints the shell completion file
38
+ # --install Installs the shell completion file
39
+ # --uninstall Uninstalls the shell completion file
40
+ # -h, --help Print help information
41
+ #
42
+ # ## Examples
43
+ #
44
+ # ronin-masscan completion --print
45
+ # ronin-masscan completion --install
46
+ # ronin-masscan completion --uninstall
47
+ #
48
+ class Completion < Core::CLI::CompletionCommand
49
+
50
+ completion_file File.join(ROOT,'data','completions','ronin-masscan')
51
+
52
+ man_dir File.join(ROOT,'man')
53
+ man_page 'ronin-masscan-completion.1'
54
+
55
+ description 'Manages the shell completion rules for ronin-masscan'
56
+
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # ronin-masscan - A Ruby library and CLI for working with masscan.
4
+ #
5
+ # Copyright (c) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com)
6
+ #
7
+ # ronin-masscan is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ronin-masscan is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ronin-masscan. If not, see <https://www.gnu.org/licenses/>.
19
+ #
20
+
21
+ require 'ronin/masscan/cli/command'
22
+ require 'ronin/masscan/converter'
23
+
24
+ module Ronin
25
+ module Masscan
26
+ class CLI
27
+ module Commands
28
+ #
29
+ # Converts an masscan scan file to JSON or CSV.
30
+ #
31
+ # ## Usage
32
+ #
33
+ # ronin-masscan convert [options] INPUT_FILE [OUTPUT_FILE]
34
+ #
35
+ # ## Option
36
+ #
37
+ # -I binary|list|json|ndjson, The input format
38
+ # --input-format
39
+ # -F, --format json|csv The desired output format
40
+ # -h, --help Print help information
41
+ #
42
+ # ## Arguments
43
+ #
44
+ # INPUT_FILE The input masscan scan file to parse
45
+ # OUTPUT_FILE The output file
46
+ #
47
+ class Convert < Command
48
+
49
+ usage '[options] MASSCAN_FILE [OUTPUT_FILE]'
50
+
51
+ option :input_format, short: '-I',
52
+ value: {
53
+ type: [:binary, :list, :json, :ndjson]
54
+ },
55
+ desc: 'The input format'
56
+
57
+ option :format, short: '-F',
58
+ value: {
59
+ type: [:json, :csv],
60
+ required: true
61
+ },
62
+ desc: 'The desired output format'
63
+
64
+ argument :input_file, required: true,
65
+ desc: 'The input masscan scan file to convert'
66
+
67
+ argument :output_file, required: false,
68
+ desc: 'The output file'
69
+
70
+ description "Converts an masscan scan file to JSON or CSV"
71
+
72
+ man_page 'ronin-masscan-convert.1'
73
+
74
+ #
75
+ # Runs the `ronin-masscan convert` command.
76
+ #
77
+ # @param [String] input_file
78
+ # The masscan scan file to parse.
79
+ #
80
+ # @param [String] output_file
81
+ # The output file to write to.
82
+ #
83
+ def run(input_file,output_file=nil)
84
+ unless File.file?(input_file)
85
+ print_error "no such file or directory: #{input_file}"
86
+ exit(-1)
87
+ end
88
+
89
+ masscan_file = open_masscan_file(input_file)
90
+
91
+ if output_file
92
+ format = options.fetch(:format) do
93
+ Converter.infer_format_for(output_file)
94
+ end
95
+
96
+ File.open(output_file,'w') do |output|
97
+ Converter.convert(masscan_file,output, format: format)
98
+ end
99
+ else
100
+ unless (format = options[:format])
101
+ print_error "must specify a --format if no output file is given"
102
+ exit(-1)
103
+ end
104
+
105
+ Converter.convert(masscan_file,stdout, format: format)
106
+ end
107
+ end
108
+
109
+ #
110
+ # Opens a masscan scan file.
111
+ #
112
+ # @param [String] path
113
+ # The path to the masscan scan file.
114
+ #
115
+ # @return [::Masscan::OutputFile]
116
+ # The opened masscan scan file.
117
+ #
118
+ def open_masscan_file(path)
119
+ if options[:input_format]
120
+ ::Masscan::OutputFile.new(path, format: options[:input_format])
121
+ else
122
+ ::Masscan::OutputFile.new(path)
123
+ end
124
+ rescue ArgumentError => error
125
+ print_error(error.message)
126
+ exit(1)
127
+ end
128
+
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,194 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # ronin-masscan - A Ruby library and CLI for working with masscan.
4
+ #
5
+ # Copyright (c) 2023-2024 Hal Brodigan (postmodern.mod3@gmail.com)
6
+ #
7
+ # ronin-masscan is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ronin-masscan is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ronin-masscan. If not, see <https://www.gnu.org/licenses/>.
19
+ #
20
+
21
+ require 'ronin/masscan/cli/command'
22
+ require 'ronin/masscan/cli/filtering_options'
23
+ require 'masscan/output_file'
24
+
25
+ require 'set'
26
+
27
+ module Ronin
28
+ module Masscan
29
+ class CLI
30
+ module Commands
31
+ #
32
+ # Dumps the scanned ports from masscan scan file(s).
33
+ #
34
+ # ## Usage
35
+ #
36
+ # ronin-masscan dump [options] MASSCAN_FILE [...]
37
+ #
38
+ # ## Options
39
+ #
40
+ # --print-ips Print all IP addresses
41
+ # --print-hosts Print all hostnames
42
+ # --print-ip-ports Print IP:PORT pairs. (Default)
43
+ # --print-host-ports Print HOST:PORT pairs
44
+ # --print-uris Print URIs
45
+ # -P, --protocol tcp|udp Filters the targets by protocol
46
+ # --ip IP Filters the targets by IP
47
+ # --ip-range CIDR Filter the targets by IP range
48
+ # -p, --ports {PORT | PORT1-PORT2},...
49
+ # Filter targets by port number
50
+ # -h, --help Print help information
51
+ #
52
+ # ## Arguments
53
+ #
54
+ # MASSCAN_FILE ... The masscan scan file(s) to parse
55
+ #
56
+ # ## Examples
57
+ #
58
+ # ronin-masscan dump --print-ip-ports masscan.bin
59
+ # ronin-masscan dump --print-ip-ports --ports 22,80,443 masscan.bin
60
+ # ronin-masscan dump --print-host-ports masscan.bin
61
+ # ronin-masscan dump --print-hosts --with-port 22 masscan.bin
62
+ # ronin-masscan dump --print-uris masscan.bin
63
+ #
64
+ class Dump < Command
65
+
66
+ usage '[options] MASSCAN_FILE [...]'
67
+
68
+ option :print_ips, desc: 'Print all IP addresses' do
69
+ @mode = :ips
70
+ end
71
+
72
+ option :print_hosts, desc: 'Print all hostnames' do
73
+ @mode = :hostnames
74
+ end
75
+
76
+ option :print_ip_ports, desc: 'Print IP:PORT pairs. (Default)' do
77
+ @mode = :ip_ports
78
+ end
79
+
80
+ option :print_host_ports, desc: 'Print HOST:PORT pairs' do
81
+ @mode = :host_ports
82
+ end
83
+
84
+ option :print_uris, desc: 'Print URIs' do
85
+ @mode = :uris
86
+ end
87
+
88
+ include FilteringOptions
89
+
90
+ argument :masscan_file, required: true,
91
+ repeats: true,
92
+ desc: 'The masscan scan file(s) to parse'
93
+
94
+ examples [
95
+ '--print-ip-ports masscan.bin',
96
+ '--print-ip-ports --ports 22,80,443 masscan.bin',
97
+ '--print-host-ports masscan.bin',
98
+ '--print-hosts --with-port 22 masscan.bin',
99
+ '--print-uris masscan.bin'
100
+ ]
101
+
102
+ description 'Dumps the scanned ports from masscan scan file(s)'
103
+
104
+ man_page 'ronin-masscan-dump.1'
105
+
106
+ #
107
+ # Initializes the command.
108
+ #
109
+ # @param [Hash{Symbol => Object}] kwargs
110
+ # Additional keywords for the command.
111
+ #
112
+ def initialize(**kwargs)
113
+ super(**kwargs)
114
+
115
+ @mode = :ip_ports
116
+ end
117
+
118
+ #
119
+ # Runs the `ronin-masscan dump` command.
120
+ #
121
+ # @param [Array<String>] masscan_files
122
+ # The masscan scan file(s) to parse.
123
+ #
124
+ def run(*masscan_files)
125
+ masscan_files.each do |masscan_file|
126
+ output_file = begin
127
+ ::Masscan::OutputFile.new(masscan_file)
128
+ rescue ArgumentError => error
129
+ print_error(error.message)
130
+ exit(1)
131
+ end
132
+
133
+ filter_records(output_file).each do |target|
134
+ print_target(target)
135
+ end
136
+ end
137
+ end
138
+
139
+ #
140
+ # Prints the target.
141
+ #
142
+ # @param [::Masscan::Status, ::Masscan::Banner] host
143
+ #
144
+ def print_target(host)
145
+ case @mode
146
+ when :ips then print_ip(host)
147
+ when :ip_ports then print_ip_ports(host)
148
+ when :uris then print_uri(host)
149
+ end
150
+ end
151
+
152
+ #
153
+ # Prints the IPs for the target.
154
+ #
155
+ # @param [::Masscan::Status, ::Masscan::Banner] target
156
+ #
157
+ def print_ip(target)
158
+ puts target.ip
159
+ end
160
+
161
+ #
162
+ # Prints the `IP:PORT` pair for the target.
163
+ #
164
+ # @param [::Masscan::Status, ::Masscan::Banner] target
165
+ #
166
+ def print_ip_ports(target)
167
+ puts "#{target.ip}:#{target.port}"
168
+ end
169
+
170
+ #
171
+ # Prints the URIs for the target.
172
+ #
173
+ # @param [::Masscan::Status, ::Masscan::Banner] target
174
+ #
175
+ def print_uri(target)
176
+ case target.port
177
+ when 80
178
+ puts URI::HTTP.build(
179
+ host: target.ip.to_s,
180
+ port: target.port
181
+ )
182
+ when 443
183
+ puts URI::HTTPS.build(
184
+ host: target.ip.to_s,
185
+ port: target.port
186
+ )
187
+ end
188
+ end
189
+
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end