Path: blob/master/modules/auxiliary/analyze/crack_webapps.rb
21545 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Auxiliary::PasswordCracker78def initialize9super(10'Name' => 'Password Cracker: Webapps',11'Description' => %(12This module uses John the Ripper or Hashcat to identify weak passwords that have been13acquired from various web applications.14Atlassian uses PBKDF2-HMAC-SHA1 which is 12001 in hashcat.15PHPass uses phpass which is 400 in hashcat.16Mediawiki is MD5 based and is 3711 in hashcat.17Apache Superset, some Flask and Werkzeug apps is pbkdf2-sha256 and is 10900 in hashcat18),19'Author' => [20'h00die'21],22'License' => MSF_LICENSE, # JtR itself is GPLv2, but this wrapper is MSF (BSD)23'Actions' => [24['john', { 'Description' => 'Use John the Ripper' }],25['hashcat', { 'Description' => 'Use Hashcat' }],26['auto', { 'Description' => 'Auto-selection of cracker' }]27],28'DefaultAction' => 'auto',29'Notes' => {30'Stability' => [CRASH_SAFE],31'SideEffects' => [],32'Reliability' => []33}34)3536register_options(37[38OptBool.new('ATLASSIAN', [false, 'Include Atlassian hashes', true]),39OptBool.new('MEDIAWIKI', [false, 'Include MediaWiki hashes', true]),40OptBool.new('PHPASS', [false, 'Include Wordpress/PHPass, Joomla, phpBB3 hashes', true]),41OptBool.new('PBKDF2', [false, 'Apache Superset, some Flask and Werkzeug apps hashes', true]),42OptBool.new('INCREMENTAL', [false, 'Run in incremental mode', true]),43OptBool.new('WORDLIST', [false, 'Run in wordlist mode', true])44]45)46end4748def show_command(cracker_instance)49return unless datastore['ShowCommand']5051if @cracker_type == 'john'52cmd = cracker_instance.john_crack_command53elsif @cracker_type == 'hashcat'54cmd = cracker_instance.hashcat_crack_command55end56print_status(" Cracking Command: #{cmd.join(' ')}")57end5859def check_results(passwords, results, hash_type, method)60passwords.each do |password_line|61password_line.chomp!62next if password_line.blank?6364fields = password_line.split(':')65cred = { 'hash_type' => hash_type, 'method' => method }66# If we don't have an expected minimum number of fields, this is probably not a hash line67if @cracker_type == 'john'68next unless fields.count >= 36970cred['username'] = fields.shift71cred['core_id'] = fields.pop72cred['password'] = fields.join(':') # Anything left must be the password. This accounts for passwords with semi-colons in it73elsif @cracker_type == 'hashcat'74next unless fields.count >= 27576cred['core_id'] = fields.shift77cred['hash'] = fields.shift78cred['password'] = fields.join(':') # Anything left must be the password. This accounts for passwords with semi-colons in it79next if cred['core_id'].include?("Hashfile '") && cred['core_id'].include?("' on line ") # skip error lines8081# we don't have the username since we overloaded it with the core_id (since its a better fit for us)82# so we can now just go grab the username from the DB83cred['username'] = framework.db.creds(workspace: myworkspace, id: cred['core_id'])[0].public.username84end85results = process_cracker_results(results, cred)86end8788results89end9091def run92tbl = cracker_results_table93cracker = new_password_cracker(action.name)94if action.name == 'auto'95@cracker_type = cracker.get_type96else97@cracker_type = action.name98end99100hash_types_to_crack = []101hash_types_to_crack << 'PBKDF2-HMAC-SHA1' if datastore['ATLASSIAN']102hash_types_to_crack << 'phpass' if datastore['PHPASS']103hash_types_to_crack << 'mediawiki' if datastore['MEDIAWIKI']104hash_types_to_crack << 'pbkdf2-sha256' if datastore['PBKDF2']105jobs_to_do = []106107# build our job list108hash_types_to_crack.each do |hash_type|109job = hash_job(hash_type, @cracker_type)110if job.nil?111print_status("No #{hash_type} found to crack")112else113jobs_to_do << job114end115end116117# bail early of no jobs to do118if jobs_to_do.empty?119print_good("No uncracked password hashes found for: #{hash_types_to_crack.join(', ')}")120return121end122123# array of arrays for cracked passwords.124# Inner array format: db_id, hash_type, username, password, method_of_crack125results = []126127# generate our wordlist and close the file handle.128wordlist = wordlist_file129unless wordlist130print_error('This module cannot run without a database connected. Use db_connect to connect to a database.')131return132end133134wordlist.close135print_status "Wordlist file written out to #{wordlist.path}"136137cleanup_files = [wordlist.path]138139jobs_to_do.each do |job|140format = job['type']141hash_file = Rex::Quickfile.new("hashes_#{job['type']}_")142hash_file.puts job['formatted_hashlist']143hash_file.close144cracker.hash_path = hash_file.path145cleanup_files << hash_file.path146# dupe our original cracker so we can safely change options between each run147cracker_instance = cracker.dup148cracker_instance.format = format149if @cracker_type == 'john'150cracker_instance.fork = datastore['FORK']151end152153# first check if anything has already been cracked so we don't report it incorrectly154print_status "Checking #{format} hashes already cracked..."155results = check_results(cracker_instance.each_cracked_password, results, format, 'Already Cracked/POT')156vprint_good(append_results(tbl, results)) unless results.empty?157job['cred_ids_left_to_crack'] = job['cred_ids_left_to_crack'] - results.map { |i| i[0].to_i } # remove cracked hashes from the hash list158next if job['cred_ids_left_to_crack'].empty?159160if @cracker_type == 'john'161print_status "Cracking #{format} hashes in single mode..."162cracker_instance.mode_single(wordlist.path)163show_command cracker_instance164cracker_instance.crack do |line|165vprint_status line.chomp166end167results = check_results(cracker_instance.each_cracked_password, results, format, 'Single')168vprint_good(append_results(tbl, results)) unless results.empty?169job['cred_ids_left_to_crack'] = job['cred_ids_left_to_crack'] - results.map { |i| i[0].to_i } # remove cracked hashes from the hash list170next if job['cred_ids_left_to_crack'].empty?171172print_status "Cracking #{format} hashes in normal mode..."173cracker_instance.mode_normal174show_command cracker_instance175cracker_instance.crack do |line|176vprint_status line.chomp177end178results = check_results(cracker_instance.each_cracked_password, results, format, 'Normal')179vprint_good(append_results(tbl, results)) unless results.empty?180job['cred_ids_left_to_crack'] = job['cred_ids_left_to_crack'] - results.map { |i| i[0].to_i } # remove cracked hashes from the hash list181next if job['cred_ids_left_to_crack'].empty?182end183184if datastore['INCREMENTAL']185print_status "Cracking #{format} hashes in incremental mode..."186cracker_instance.mode_incremental187show_command cracker_instance188cracker_instance.crack do |line|189vprint_status line.chomp190end191results = check_results(cracker_instance.each_cracked_password, results, format, 'Incremental')192vprint_good(append_results(tbl, results)) unless results.empty?193job['cred_ids_left_to_crack'] = job['cred_ids_left_to_crack'] - results.map { |i| i[0].to_i } # remove cracked hashes from the hash list194next if job['cred_ids_left_to_crack'].empty?195end196197if datastore['WORDLIST']198print_status "Cracking #{format} hashes in wordlist mode..."199cracker_instance.mode_wordlist(wordlist.path)200# Turn on KoreLogic rules if the user asked for it201if @cracker_type == 'john' && datastore['KORELOGIC']202cracker_instance.rules = 'KoreLogicRules'203print_status 'Applying KoreLogic ruleset...'204end205show_command cracker_instance206cracker_instance.crack do |line|207vprint_status line.chomp208end209210results = check_results(cracker_instance.each_cracked_password, results, format, 'Wordlist')211vprint_good(append_results(tbl, results)) unless results.empty?212job['cred_ids_left_to_crack'] = job['cred_ids_left_to_crack'] - results.map { |i| i[0].to_i } # remove cracked hashes from the hash list213next if job['cred_ids_left_to_crack'].empty?214end215216# give a final print of results217print_good(append_results(tbl, results))218end219if datastore['DeleteTempFiles']220cleanup_files.each do |f|221File.delete(f)222end223end224end225end226227228