metasploit-framework/tools/password/md5_lookup.rb

470 lines
13 KiB
Ruby
Raw Normal View History

#!/usr/bin/env ruby
##
2017-07-24 13:26:21 +00:00
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
###
#
# This script will look up a collection of MD5 hashes (from a file) against the following databases
2015-01-16 07:15:58 +00:00
# via md5cracker.org:
# authsecu, i337.net, md5.my-addr.com, md5.net, md5crack, md5cracker.org, md5decryption.com,
# md5online.net, md5pass, netmd5crack, tmto.
# This msf tool script was originally ported from:
# https://github.com/hasherezade/metasploit_modules/blob/master/md5_lookup.rb
#
# To-do:
# Maybe as a msf plugin one day and grab hashes directly from the workspace.
#
# Authors:
2015-01-16 03:04:12 +00:00
# * hasherezade (http://hasherezade.net, @hasherezade)
# * sinn3r (ported the module as a standalone msf tool)
#
###
#
# Load our MSF API
#
2015-01-15 18:03:52 +00:00
msfbase = __FILE__
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
2015-10-06 15:30:52 +00:00
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), '..', '..', 'lib')))
require 'msfenv'
require 'rex'
require 'msf/core'
require 'optparse'
#
# Basic prints we can't live without
#
2015-01-15 18:03:52 +00:00
# Prints with [*] that represents the message is a status
#
# @param msg [String] The message to print
# @return [void]
def print_status(msg='')
$stdout.puts "[*] #{msg}"
end
2015-01-15 18:03:52 +00:00
# Prints with [-] that represents the message is an error
#
# @param msg [String] The message to print
# @return [void]
def print_error(msg='')
$stdout.puts "[-] #{msg}"
end
module Md5LookupUtility
2015-01-24 04:31:37 +00:00
# This class manages the disclaimer
2015-01-24 04:26:41 +00:00
class Disclaimer
# @!attribute config_file
# @return [String] The config file path
attr_accessor :config_file
# @!attribute group_name
# @return [String] The name of the tool
attr_accessor :group_name
def initialize
self.config_file = Msf::Config.config_file
self.group_name = 'MD5Lookup'
end
2015-01-24 04:31:37 +00:00
# Prompts a disclaimer. The user will not be able to get out unless they acknowledge.
2015-01-24 04:26:41 +00:00
#
# @return [TrueClass] true if acknowledged.
def ack
2015-01-24 04:50:39 +00:00
print_status("WARNING: This tool will look up your MD5 hashes by submitting them")
print_status("in the clear (HTTP) to third party websites. This can expose")
print_status("sensitive data to unknown and untrusted entities.")
2015-01-24 04:26:41 +00:00
while true
$stdout.print "[*] Enter 'Y' to acknowledge: "
if $stdin.gets =~ /^y|yes$/i
return true
end
end
end
# Saves the waiver so the warning won't show again after ack
#
# @return [void]
def save_waiver
save_setting('waiver', true)
end
# Returns true if we don't have to show the warning again
#
# @return [Boolean]
def has_waiver?
load_setting('waiver') == 'true' ? true : false
end
private
# Saves a setting to Metasploit's config file
#
# @param key_name [String] The name of the setting
# @param value [String] The value of the setting
# @return [void]
def save_setting(key_name, value)
ini = Rex::Parser::Ini.new(self.config_file)
ini.add_group(self.group_name) if ini[self.group_name].nil?
ini[self.group_name][key_name] = value
ini.to_file(self.config_file)
end
# Returns the value of a specific setting
#
# @param key_name [String] The name of the setting
# @return [String]
def load_setting(key_name)
ini = Rex::Parser::Ini.new(self.config_file)
group = ini[self.group_name]
return '' if group.nil?
group[key_name].to_s
end
end
# This class is basically an auxiliary module without relying on msfconsole
class Md5Lookup < Msf::Auxiliary
include Msf::Exploit::Remote::HttpClient
# @!attribute rhost
# @return [String] Should be md5cracker.org
attr_accessor :rhost
# @!attribute rport
# @return [Integer] The port number to md5cracker.org
attr_accessor :rport
# @!attribute target_uri
# @return [String] The URI (API)
attr_accessor :target_uri
# @!attribute ssl
# @return [FalseClass] False because doesn't look like md5cracker.org supports HTTPS
attr_accessor :ssl
def initialize(opts={})
# The user should not be able to modify these settings, otherwise
# the we can't guarantee results.
self.rhost = 'md5cracker.org'
self.rport = 80
self.target_uri = '/api/api.cracker.php'
self.ssl = false
2015-01-15 21:13:03 +00:00
super(
'DefaultOptions' =>
{
'SSL' => self.ssl,
'RHOST' => self.rhost,
'RPORT' => self.rport
}
)
end
2015-01-16 02:51:27 +00:00
# Returns the found cracked MD5 hash
#
# @param md5_hash [String] The MD5 hash to lookup
2015-01-16 02:51:27 +00:00
# @param db [String] The specific database to check against
# @return [String] Found cracked MD5 hash
def lookup(md5_hash, db)
res = send_request_cgi({
'uri' => self.target_uri,
'method' => 'GET',
'vars_get' => {'database' => db, 'hash' => md5_hash}
})
2015-01-16 02:51:27 +00:00
get_json_result(res)
end
private
2015-01-16 02:51:27 +00:00
# Parses the cracked result from a JSON input
2015-01-16 02:59:47 +00:00
# @param res [Rex::Proto::Http::Response] The Rex HTTP response
# @return [String] Found cracked MD5 hash
2015-01-16 02:51:27 +00:00
def get_json_result(res)
result = ''
# Hmm, no proper response :-(
2015-01-31 01:01:49 +00:00
return result unless res && res.code == 200
2015-01-16 02:51:27 +00:00
begin
json = JSON.parse(res.body)
result = json['result'] if json['status']
rescue JSON::ParserError
# No json?
end
result
end
end
# This class parses the user-supplied options (inputs)
class OptsConsole
# The databases supported by md5cracker.org
# The hash keys (symbols) are used as choices for the user, the hash values are the original
# database values that md5cracker.org will recognize
DATABASES =
{
2015-01-15 18:03:52 +00:00
:all => nil, # This is shifted before being passed to Md5Lookup
:authsecu => 'authsecu',
:i337 => 'i337.net',
:md5_my_addr => 'md5.my-addr.com',
:md5_net => 'md5.net',
:md5crack => 'md5crack',
:md5cracker => 'md5cracker.org',
:md5decryption => 'md5decryption.com',
:md5online => 'md5online.net',
:md5pass => 'md5pass',
:netmd5crack => 'netmd5crack',
:tmto => 'tmto'
}
2015-01-16 02:51:27 +00:00
# The default file path to save the results to
DEFAULT_OUTFILE = 'md5_results.txt'
# Returns the normalized user inputs
#
# @param args [Array] This should be Ruby's ARGV
2015-01-15 18:03:52 +00:00
# @raise [OptionParser::MissingArgument] Missing arguments
# @return [Hash] The normalized options
def self.parse(args)
2015-01-15 18:03:52 +00:00
parser, options = get_parsed_options
# Set the optional datation argument (--database)
2015-01-16 07:33:09 +00:00
unless options[:databases]
2015-01-15 18:03:52 +00:00
options[:databases] = get_database_names
end
2015-01-16 02:51:27 +00:00
# Set the optional output argument (--out)
2015-01-16 07:33:09 +00:00
unless options[:outfile]
2015-01-16 02:51:27 +00:00
options[:outfile] = DEFAULT_OUTFILE
end
2015-01-15 18:03:52 +00:00
# Now let's parse it
# This may raise OptionParser::InvalidOption
parser.parse!(args)
# Final checks
if options.empty?
raise OptionParser::MissingArgument, 'No options set, try -h for usage'
2015-01-15 18:18:50 +00:00
elsif options[:input].blank?
raise OptionParser::MissingArgument, '-i is a required argument'
2015-01-15 18:03:52 +00:00
end
2015-01-15 18:03:52 +00:00
options
end
2015-01-15 18:03:52 +00:00
private
# Returns the parsed options from ARGV
2015-01-16 02:51:27 +00:00
#
2015-01-15 18:18:50 +00:00
# raise [OptionParser::InvalidOption] Invalid option found
2015-01-16 02:51:27 +00:00
# @return [OptionParser, Hash] The OptionParser object and an hash containg the options
2015-01-15 18:03:52 +00:00
def self.get_parsed_options
options = {}
parser = OptionParser.new do |opt|
opt.banner = "Usage: #{__FILE__} [options]"
opt.separator ''
opt.separator 'Specific options:'
2015-01-15 18:18:50 +00:00
opt.on('-i', '--input <file>',
'The file that contains all the MD5 hashes (one line per hash)') do |v|
2016-04-20 12:11:34 +00:00
if v && !::File.exist?(v)
2015-01-15 18:18:50 +00:00
raise OptionParser::InvalidOption, "Invalid input file: #{v}"
end
options[:input] = v
end
2015-01-15 18:18:50 +00:00
opt.on('-d','--databases <names>',
"(Optional) Select databases: #{get_database_symbols * ", "} (Default=all)") do |v|
options[:databases] = extract_db_names(v)
end
2015-01-16 03:00:52 +00:00
opt.on('-o', '--out <filepath>',
"(Optional) Save the results to a file (Default=#{DEFAULT_OUTFILE})") do |v|
2015-01-16 02:51:27 +00:00
options[:outfile] = v
end
opt.on_tail('-h', '--help', 'Show this message') do
$stdout.puts opt
exit
end
end
2015-01-15 18:03:52 +00:00
return parser, options
end
# Returns the actual database names based on what the user wants
2015-01-16 02:51:27 +00:00
#
# @param list [String] A list of user-supplied database names
2015-01-16 02:51:27 +00:00
# @return [Array<String>] All the matched database names
def self.extract_db_names(list)
new_db_list = []
list_copy = list.split(',')
if list_copy.include?('all')
return get_database_names
end
list_copy.each do |item|
2015-01-16 03:52:15 +00:00
item = item.strip.to_sym
new_db_list << DATABASES[item] if DATABASES[item]
end
new_db_list
end
# Returns a list of all of the supported database symbols
2015-01-16 02:51:27 +00:00
#
# @return [Array<Symbol>] Database symbols
def self.get_database_symbols
DATABASES.keys
end
# Returns a list of all the original database values recognized by md5cracker.org
2015-01-16 02:51:27 +00:00
#
# @return [Array<String>] Original database values
def self.get_database_names
new_db_list = DATABASES.values
new_db_list.shift #Get rid of the 'all' option
return new_db_list
end
end
2015-01-15 21:13:03 +00:00
# This class decides how this process works
class Driver
def initialize
2015-01-15 18:03:52 +00:00
begin
@opts = OptsConsole.parse(ARGV)
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e
print_error("#{e.message} (please see -h)")
exit
end
2015-01-16 02:51:27 +00:00
@output_handle = nil
begin
2015-01-17 00:25:54 +00:00
@output_handle = ::File.new(@opts[:outfile], 'wb')
2015-01-16 02:51:27 +00:00
rescue
# Not end of the world, but if this happens we won't be able to save the results.
# The user will just have to copy and paste from the screen.
print_error("Unable to create file handle, results will not be saved to #{@opts[:output]}")
end
end
2015-01-15 21:13:03 +00:00
# Main function
2015-01-16 02:51:27 +00:00
#
# @return [void]
def run
2015-01-15 21:13:03 +00:00
input = @opts[:input]
dbs = @opts[:databases]
2015-01-24 04:26:41 +00:00
disclamer = Md5LookupUtility::Disclaimer.new
unless disclamer.has_waiver?
disclamer.ack
disclamer.save_waiver
end
2015-01-16 02:51:27 +00:00
get_hash_results(input, dbs) do |result|
2015-01-15 21:13:03 +00:00
original_hash = result[:hash]
cracked_hash = result[:cracked_hash]
2015-01-16 02:51:27 +00:00
credit_db = result[:credit]
print_status("Found: #{original_hash} = #{cracked_hash} (from #{credit_db})")
save_result(result) if @output_handle
2015-01-15 21:13:03 +00:00
end
end
2015-01-24 04:26:41 +00:00
# Cleans up the output file handler if exists
#
# @return [void]
2015-01-16 02:51:27 +00:00
def cleanup
@output_handle.close if @output_handle
end
2015-01-15 21:13:03 +00:00
private
2015-01-16 02:51:27 +00:00
# Saves the MD5 result to file
#
# @param result [Hash] The result that contains the MD5 information
# @option result :hash [String] The original MD5 hash
# @option result :cracked_hash [String] The cracked MD5 hash
2015-01-16 02:51:27 +00:00
# @return [void]
def save_result(result)
@output_handle.puts "#{result[:hash]} = #{result[:cracked_hash]}"
2015-01-16 02:51:27 +00:00
end
2015-01-15 21:13:03 +00:00
# Returns the hash results by actually invoking Md5Lookup
2015-01-16 02:51:27 +00:00
#
# @param input [String] The path of the input file (MD5 hashes)
# @yield [result] Gives a hash as the found result
2015-01-16 02:51:27 +00:00
# @return [void]
def get_hash_results(input, dbs)
2015-01-15 21:13:03 +00:00
search_engine = Md5LookupUtility::Md5Lookup.new
extract_hashes(input) do |hash|
2015-01-16 02:51:27 +00:00
dbs.each do |db|
cracked_hash = search_engine.lookup(hash, db)
2015-01-16 07:33:09 +00:00
unless cracked_hash.empty?
2015-01-16 02:51:27 +00:00
result = { :hash => hash, :cracked_hash => cracked_hash, :credit => db }
yield result
end
# Awright, we already found one cracked, we don't need to keep looking,
# Let's move on to the next hash!
2015-01-16 07:33:09 +00:00
break unless cracked_hash.empty?
2015-01-16 02:51:27 +00:00
end
2015-01-15 21:13:03 +00:00
end
end
# Extracts all the MD5 hashes one by one
2015-01-16 02:51:27 +00:00
#
# @param input_file [String] The path of the input file (MD5 hashes)
# @yield [hash] The original MD5 hash
2015-01-16 02:51:27 +00:00
# @return [void]
2015-01-15 21:13:03 +00:00
def extract_hashes(input_file)
2015-01-15 22:05:35 +00:00
::File.open(input_file, 'rb') do |f|
f.each_line do |hash|
2015-01-16 07:33:09 +00:00
next unless is_md5_format?(hash)
2015-01-15 22:05:35 +00:00
yield hash.strip # Make sure no newlines
end
2015-01-15 21:13:03 +00:00
end
end
# Checks if the hash format is MD5 or not
2015-01-16 02:51:27 +00:00
#
2015-01-15 21:13:03 +00:00
# @param md5_hash [String] The MD5 hash (hex)
2016-06-02 15:49:22 +00:00
# @return [TrueClass/FalseClass] True if the format is valid, otherwise false
2015-01-15 21:13:03 +00:00
def is_md5_format?(md5_hash)
(md5_hash =~ /^[a-f0-9]{32}$/i) ? true : false
end
end
end
#
# main
#
if __FILE__ == $PROGRAM_NAME
2015-01-16 02:51:27 +00:00
driver = Md5LookupUtility::Driver.new
begin
driver.run
rescue Interrupt
$stdout.puts
$stdout.puts "Good bye"
2015-01-16 02:51:27 +00:00
ensure
driver.cleanup # Properly close resources
end
end