Files
metasploit-gs/tools/modules/module_reference.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

290 lines
7.7 KiB
Ruby
Raw Normal View History

2007-10-02 16:09:13 +00:00
#!/usr/bin/env ruby
2018-03-20 11:33:34 +00:00
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
2007-10-02 16:09:13 +00:00
#
# This script lists each module with its references
2007-10-02 16:09:13 +00:00
#
msfbase = __FILE__
2022-01-23 15:28:32 -05:00
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase)) while File.symlink?(msfbase)
2015-10-06 10:30:52 -05:00
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
2012-04-15 23:35:38 -05:00
require 'msfenv'
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
2007-10-02 16:09:13 +00:00
require 'rex'
2014-11-05 09:57:13 -06:00
require 'uri'
# See lib/msf/core/module/reference.rb
# We gsub '#{in_ctx_val}' with the actual value
def types
{
2022-01-23 15:28:32 -05:00
'ALL' => '',
'CVE' => 'https://nvd.nist.gov/vuln/detail/CVE-#{in_ctx_val}',
'CWE' => 'http://cwe.mitre.org/data/definitions/#{in_ctx_val}.html',
'BID' => 'http://www.securityfocus.com/bid/#{in_ctx_val}',
'MSB' => 'https://docs.microsoft.com/en-us/security-updates/SecurityBulletins/#{in_ctx_val}',
'EDB' => 'http://www.exploit-db.com/exploits/#{in_ctx_val}',
'US-CERT-VU' => 'http://www.kb.cert.org/vuls/id/#{in_ctx_val}',
'ZDI' => 'http://www.zerodayinitiative.com/advisories/ZDI-#{in_ctx_val}',
'WPVDB' => 'https://wpscan.com/vulnerability/#{in_ctx_val}',
2015-09-01 23:25:01 -05:00
'PACKETSTORM' => 'https://packetstormsecurity.com/files/#{in_ctx_val}',
2022-01-23 15:28:32 -05:00
'URL' => '#{in_ctx_val}'
2014-11-05 09:57:13 -06:00
}
end
2007-10-02 16:09:13 +00:00
2022-01-23 15:28:32 -05:00
STATUS_ALIVE = 'Alive'
STATUS_DOWN = 'Down'
2022-02-16 23:12:55 -06:00
STATUS_REDIRECT = 'Redirect'
2014-11-05 09:57:13 -06:00
STATUS_UNSUPPORTED = 'Unsupported'
2022-01-23 15:28:32 -05:00
sort = 0
filter = 'All'
filters = ['all', 'exploit', 'payload', 'post', 'nop', 'encoder', 'auxiliary']
type = 'ALL'
match = nil
check = false
save = nil
is_url_alive_cache = {}
2015-11-19 11:30:16 -06:00
http_timeout = 20
2022-01-23 15:28:32 -05:00
$verbose = false
opts = Rex::Parser::Arguments.new(
2022-01-23 15:28:32 -05:00
'-h' => [ false, 'Help menu.' ],
'-c' => [ false, 'Check Reference status'],
'-s' => [ false, 'Sort by Reference instead of Module Type.'],
'-r' => [ false, 'Reverse Sort'],
'-f' => [ true, 'Filter based on Module Type [All,Exploit,Payload,Post,NOP,Encoder,Auxiliary] (Default = ALL).'],
'-t' => [ true, "Type of Reference to sort by #{types.keys}"],
'-x' => [ true, 'String or RegEx to try and match against the Reference Field'],
'-o' => [ true, 'Save the results to a file'],
'--csv' => [ false, 'Save the results file in CSV format'],
'-i' => [ true, 'Set an HTTP timeout'],
'-v' => [ false, 'Verbose']
)
2014-11-05 09:57:13 -06:00
flags = []
2022-01-23 15:28:32 -05:00
opts.parse(ARGV) do |opt, _idx, val|
2013-09-30 13:47:53 -05:00
case opt
2022-01-23 15:28:32 -05:00
when '-h'
2013-09-30 13:47:53 -05:00
puts "\nMetasploit Script for Displaying Module Reference information."
2022-01-23 15:28:32 -05:00
puts '=========================================================='
2013-09-30 13:47:53 -05:00
puts opts.usage
exit
2022-01-23 15:28:32 -05:00
when '-c'
flags << 'URI Check: Yes'
2014-11-05 09:57:13 -06:00
check = true
2022-01-23 15:28:32 -05:00
when '-s'
flags << 'Order: Sorting by Reference'
2013-09-30 13:47:53 -05:00
sort = 1
2022-01-23 15:28:32 -05:00
when '-r'
flags << 'Order: Reverse Sorting'
2013-09-30 13:47:53 -05:00
sort = 2
2022-01-23 15:28:32 -05:00
when '-f'
2013-09-30 13:47:53 -05:00
unless filters.include?(val.downcase)
puts "Invalid Filter Supplied: #{val}"
2022-01-23 15:28:32 -05:00
puts "Please use one of these: #{filters.map { |f| f.capitalize }.join(', ')}"
2013-09-30 13:47:53 -05:00
exit
end
2014-11-05 09:57:13 -06:00
flags << "Module Filter: #{val}"
2013-09-30 13:47:53 -05:00
filter = val
2022-01-23 15:28:32 -05:00
when '-t'
2014-11-05 15:26:00 -06:00
val = (val || '').upcase
unless types.has_key?(val)
2013-09-30 13:47:53 -05:00
puts "Invalid Type Supplied: #{val}"
2014-11-05 09:57:13 -06:00
puts "Please use one of these: #{types.keys.inspect}"
2013-09-30 13:47:53 -05:00
exit
end
type = val
2022-01-23 15:28:32 -05:00
when '-i'
2015-11-19 11:30:16 -06:00
http_timeout = /^\d+/ === val ? val.to_i : 20
2022-01-23 15:28:32 -05:00
when '-v'
2015-11-19 11:30:16 -06:00
$verbose = true
2022-01-23 15:28:32 -05:00
when '-x'
2014-11-05 09:57:13 -06:00
flags << "Regex: #{val}"
2013-09-30 13:47:53 -05:00
match = Regexp.new(val)
2022-01-23 15:28:32 -05:00
when '-o'
flags << 'Output to file: Yes'
2014-11-05 10:58:41 -06:00
save = val
2022-01-23 15:28:32 -05:00
when '--csv'
flags << 'Output as CSV'
$csv = true
2013-09-30 13:47:53 -05:00
end
2022-01-23 15:28:32 -05:00
end
if $csv && save.nil?
abort('Error: -o flag required when using CSV output')
end
2014-11-05 09:57:13 -06:00
flags << "Type: #{type}"
2022-01-23 15:28:32 -05:00
puts flags * ' | '
2014-11-05 09:57:13 -06:00
def get_ipv4_addr(hostname)
2022-01-23 15:28:32 -05:00
Rex::Socket.getaddresses(hostname, false)[0]
2014-11-05 09:57:13 -06:00
end
2022-01-23 15:28:32 -05:00
def vprint_debug(msg = '')
2015-11-19 11:30:16 -06:00
print_debug(msg) if $verbose
end
2022-01-23 15:28:32 -05:00
def print_debug(msg = '')
warn "[*] #{msg}"
2015-11-19 11:30:16 -06:00
end
2022-02-16 23:12:55 -06:00
def is_url_alive(uri, http_timeout, cache)
2022-01-23 15:28:32 -05:00
if cache.key? uri.to_s
print_debug("Cached: #{uri} -> #{cache[uri]}")
return cache[uri.to_s]
end
2015-11-19 11:30:16 -06:00
print_debug("Checking: #{uri}")
2014-11-05 09:57:13 -06:00
begin
uri = URI(uri)
rhost = get_ipv4_addr(uri.host)
rescue SocketError, URI::InvalidURIError => e
2022-02-16 23:12:55 -06:00
vprint_debug("#{e.message} in #is_url_alive")
return STATUS_DOWN
2014-11-05 09:57:13 -06:00
end
rport = uri.port || 80
2022-01-23 15:28:32 -05:00
path = uri.path.blank? ? '/' : uri.path
2014-11-05 09:57:13 -06:00
vhost = rport == 80 ? uri.host : "#{uri.host}:#{rport}"
if uri.scheme == 'https'
2022-01-23 15:28:32 -05:00
cli = ::Rex::Proto::Http::Client.new(rhost, 443, {}, true)
2014-11-05 09:57:13 -06:00
else
cli = ::Rex::Proto::Http::Client.new(rhost, rport)
end
begin
2015-11-19 11:30:16 -06:00
cli.connect(http_timeout)
2022-01-23 15:28:32 -05:00
req = cli.request_raw('uri' => path, 'vhost' => vhost)
2015-11-19 11:30:16 -06:00
res = cli.send_recv(req, http_timeout)
2022-01-23 15:28:32 -05:00
rescue Errno::ECONNRESET, Rex::ConnectionError, Rex::ConnectionRefused, Rex::HostUnreachable, Rex::ConnectionTimeout, Rex::UnsupportedProtocol, ::Timeout::Error, Errno::ETIMEDOUT, ::Exception => e
2015-11-19 11:30:16 -06:00
vprint_debug("#{e.message} for #{uri}")
2022-02-16 23:12:55 -06:00
cache[uri.to_s] = STATUS_DOWN
return STATUS_DOWN
2014-11-05 09:57:13 -06:00
ensure
cli.close
end
2022-02-16 23:12:55 -06:00
if !res.nil? && res.code.to_s =~ %r{3\d\d}
if res.headers['Location']
vprint_debug("Redirect: #{uri} redirected to #{res.headers['Location']}")
else
print_error("Error: Couldn't find redirect location for #{uri}")
end
cache[uri.to_s] = STATUS_REDIRECT
return STATUS_REDIRECT
elsif res.nil? || res.body =~ %r{<title>.*not found</title>}i || !res.code.to_s =~ %r{2\d\d}
vprint_debug("Down: #{uri} returned a not-found response")
cache[uri.to_s] = STATUS_DOWN
return STATUS_DOWN
2014-11-05 09:57:13 -06:00
end
2015-11-19 11:30:16 -06:00
vprint_debug("Good: #{uri}")
2022-02-16 23:12:55 -06:00
cache[uri.to_s] = STATUS_ALIVE
STATUS_ALIVE
2014-11-05 09:57:13 -06:00
end
2007-10-02 16:09:13 +00:00
2014-11-05 10:58:41 -06:00
def save_results(path, results)
2022-01-23 15:28:32 -05:00
File.open(path, 'wb') do |f|
f.write(results)
2014-11-05 10:58:41 -06:00
end
2022-01-23 15:28:32 -05:00
puts "Results saved to: #{path}"
rescue Exception => e
puts "Failed to save the file: #{e.message}"
2014-11-05 10:58:41 -06:00
end
2011-06-07 01:25:12 +00:00
# Always disable the database (we never need it just to list module
# information).
framework_opts = { 'DisableDatabase' => true }
# If the user only wants a particular module type, no need to load the others
if filter.downcase != 'all'
2013-09-30 13:47:53 -05:00
framework_opts[:module_types] = [ filter.downcase ]
2011-06-07 01:25:12 +00:00
end
2007-10-02 16:09:13 +00:00
# Initialize the simplified framework instance.
2011-06-07 01:25:12 +00:00
$framework = Msf::Simple::Framework.create(framework_opts)
2014-11-05 09:57:13 -06:00
if check
columns = [ 'Module', 'Status', 'Reference' ]
else
columns = [ 'Module', 'Reference' ]
end
2007-10-02 16:09:13 +00:00
2016-08-10 13:30:09 -05:00
tbl = Rex::Text::Table.new(
2022-01-23 15:28:32 -05:00
'Header' => 'Module References',
'Indent' => 2,
2014-11-05 09:57:13 -06:00
'Columns' => columns
2007-10-02 16:09:13 +00:00
)
2022-01-23 15:28:32 -05:00
bad_refs_count = 0
2014-11-05 09:57:13 -06:00
2022-01-23 15:28:32 -05:00
$framework.modules.each do |name, mod|
if mod.nil?
elog("module_reference.rb is unable to load #{name}")
next
end
2022-01-23 15:28:32 -05:00
next if match and !(name =~ match)
2013-09-30 13:47:53 -05:00
x = mod.new
x.references.each do |r|
2014-11-05 09:57:13 -06:00
ctx_id = r.ctx_id.upcase
ctx_val = r.ctx_val
2022-01-23 15:28:32 -05:00
next unless type == 'ALL' || type == ctx_id
if check
if types.has_key?(ctx_id)
if ctx_id == 'MSB'
year = ctx_val[2..3]
2022-01-23 15:28:32 -05:00
century = year[0] == '9' ? '19' : '20'
new_ctx_val = "#{century}#{year}/#{ctx_val}"
uri = types[r.ctx_id.upcase].gsub(/\#{in_ctx_val}/, new_ctx_val)
else
uri = types[r.ctx_id.upcase].gsub(/\#{in_ctx_val}/, r.ctx_val.to_s)
2022-01-23 15:28:32 -05:00
end
2022-02-16 23:12:55 -06:00
status = is_url_alive(uri, http_timeout, is_url_alive_cache)
bad_refs_count += 1 if status == STATUS_DOWN
2022-01-23 15:28:32 -05:00
else
# The reference ID isn't supported so we don't know how to check this
bad_refs_count += 1
status = STATUS_UNSUPPORTED
2014-11-05 09:57:13 -06:00
end
2013-09-30 13:47:53 -05:00
end
2022-01-23 15:28:32 -05:00
ref = "#{r.ctx_id}-#{r.ctx_val}"
new_column = []
new_column << x.fullname
new_column << status if check
new_column << ref
tbl << new_column
2013-09-30 13:47:53 -05:00
end
2022-01-23 15:28:32 -05:00
end
if sort == 1
2013-09-30 13:47:53 -05:00
tbl.sort_rows(1)
end
if sort == 2
2013-09-30 13:47:53 -05:00
tbl.sort_rows(1)
tbl.rows.reverse
end
2007-10-02 16:09:13 +00:00
2014-11-05 09:57:13 -06:00
puts
2009-01-10 22:26:58 +00:00
puts tbl.to_s
2014-11-05 15:26:00 -06:00
puts
2014-11-05 15:07:40 -06:00
2014-11-05 15:26:00 -06:00
puts "Number of bad references found: #{bad_refs_count}" if check
2022-01-23 15:28:32 -05:00
save_results(save, $csv.nil? ? tbl.to_s : tbl.to_csv) if save