Last active
January 14, 2018 14:13
-
-
Save bcoles/e4f2aabb2cb1b05b1a6df3ba66b64bac to your computer and use it in GitHub Desktop.
Searches Metasploit modules for a keyword using msfrpc-client Ruby library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# [ Example code - Don't use in production ] | |
# | |
# Searches Metasploit modules for a keyword using msfrpc-client Ruby library | |
# | |
# Start msfrpcd from msfconsole like this, but with a more secure password: | |
# load msgrpc ServerHost=127.0.0.1 Pass=abc123 SSL=y | |
# | |
require 'msfrpc-client' | |
@keyword = ARGV[0].to_s.strip | |
if @keyword.empty? | |
puts 'Usage: ./msfrpc-client-search.rb <keyword>' | |
exit 1 | |
end | |
# | |
# Login | |
# | |
begin | |
@rpc = Msf::RPC::Client.new( | |
:host => '127.0.0.1', | |
:port => 55552, | |
:user => 'msf', | |
:pass => 'abc123', | |
:ssl => true | |
) | |
rescue => e | |
puts "(-) Error: connection failed: #{e}" | |
end | |
begin | |
@rpc.login('msf', 'abc123') | |
rescue Msf::RPC::ServerException => e | |
if e.message.eql?('Login Failed') | |
puts '(-) Error: authentication failed' | |
exit 1 | |
end | |
raise e | |
end | |
if @rpc.token.nil? | |
puts '(-) Error: authentication failed' | |
exit 1 | |
end | |
# | |
# Print API info | |
# | |
version = @rpc.call('core.version') | |
puts "(*) Version: #{version}" | |
puts | |
@token = @rpc.token | |
puts "(*) Temporary Token: #{@token}" | |
puts | |
exploits = @rpc.call('module.exploits')['modules'] | |
puts "(*) Searching #{exploits.length} modules..." | |
puts | |
# | |
# Search | |
# | |
@matches = [] | |
exploits.each do |name| | |
mod = @rpc.call('module.info', 'exploit', name) | |
if mod['name'] =~ /#{@keyword}/i || | |
mod['description'] =~ /#{@keyword}/i || | |
mod['references'] =~ /#{@keyword}/i | |
@matches << [mod['name'], name] | |
end | |
end | |
# | |
# Results | |
# | |
if @matches.empty? | |
puts "(-) Found no matches for '#{@keyword}'" | |
exit 1 | |
end | |
puts "(+) Found (#{@matches.length}) matches:" | |
@matches.each do |match| | |
puts match.join ' :: ' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment