2009-07-22 19:10:45 +00:00
|
|
|
##
|
2010-04-30 08:40:19 +00:00
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
2009-07-22 19:10:45 +00:00
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
2012-02-21 01:40:50 +00:00
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
2009-07-22 19:10:45 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Auxiliary
|
|
|
|
|
|
|
|
include Msf::Exploit::Remote::FtpServer
|
|
|
|
include Msf::Auxiliary::Report
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super(
|
|
|
|
'Name' => 'FTP File Server',
|
|
|
|
'Description' => %q{
|
|
|
|
This module provides a FTP service
|
|
|
|
},
|
|
|
|
'Author' => ['hdm'],
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Actions' =>
|
|
|
|
[
|
2010-09-20 08:05:50 +00:00
|
|
|
[ 'Service' ]
|
2009-07-22 19:10:45 +00:00
|
|
|
],
|
2010-04-30 08:40:19 +00:00
|
|
|
'PassiveActions' =>
|
2009-07-22 19:10:45 +00:00
|
|
|
[
|
2010-09-20 08:05:50 +00:00
|
|
|
'Service'
|
2009-07-22 19:10:45 +00:00
|
|
|
],
|
2010-09-20 08:05:50 +00:00
|
|
|
'DefaultAction' => 'Service'
|
2009-07-22 19:10:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
2009-07-25 04:18:37 +00:00
|
|
|
OptString.new('FTPROOT', [ true, "The FTP root directory to serve files from", '/tmp/ftproot' ]),
|
|
|
|
OptString.new('FTPUSER', [ false, "Configure a specific username that should be allowed access"]),
|
|
|
|
OptString.new('FTPPASS', [ false, "Configure a specific password that should be allowed access"]),
|
2009-07-22 19:10:45 +00:00
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
exploit()
|
|
|
|
end
|
|
|
|
|
2009-07-25 04:18:37 +00:00
|
|
|
def on_client_command_user(c,arg)
|
|
|
|
@state[c][:user] = arg
|
|
|
|
if(not datastore['FTPUSER'] or (arg == datastore['FTPUSER']))
|
|
|
|
c.put "331 User name okay, need password...\r\n"
|
|
|
|
else
|
|
|
|
c.put "500 User name invalid\r\n"
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-25 04:18:37 +00:00
|
|
|
def on_client_command_pass(c,arg)
|
|
|
|
@state[c][:pass] = arg
|
|
|
|
if(not datastore['FTPPASS'] or (arg == datastore['FTPPASS']))
|
|
|
|
c.put "230 Login OK\r\n"
|
|
|
|
@state[c][:auth] = true
|
|
|
|
else
|
|
|
|
c.put "500 Password invalid\r\n"
|
|
|
|
@state[c][:auth] = false
|
|
|
|
end
|
|
|
|
return
|
2010-04-30 08:40:19 +00:00
|
|
|
end
|
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
def on_client_command_retr(c,arg)
|
|
|
|
print_status("#{@state[c][:name]} FTP download request for #{arg}")
|
|
|
|
|
2009-07-25 04:18:37 +00:00
|
|
|
if(not @state[c][:auth])
|
|
|
|
c.put "500 Access denied\r\n"
|
|
|
|
return
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2011-01-25 02:24:37 +00:00
|
|
|
path = ::File.join(datastore['FTPROOT'], Rex::FileUtils.clean_path(arg))
|
2009-07-22 19:10:45 +00:00
|
|
|
if(not ::File.exists?(path))
|
|
|
|
c.put "550 File does not exist\r\n"
|
|
|
|
return
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
conn = establish_data_connection(c)
|
|
|
|
if(not conn)
|
|
|
|
c.put("425 Can't build data connection\r\n")
|
|
|
|
return
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
c.put("150 Opening BINARY mode data connection for #{arg}\r\n")
|
|
|
|
conn.put(::File.read(path, ::File.size(path)))
|
|
|
|
c.put("226 Transfer complete.\r\n")
|
|
|
|
conn.close
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
def on_client_command_list(c,arg)
|
2009-07-25 04:18:37 +00:00
|
|
|
|
|
|
|
if(not @state[c][:auth])
|
|
|
|
c.put "500 Access denied\r\n"
|
|
|
|
return
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
conn = establish_data_connection(c)
|
|
|
|
if(not conn)
|
|
|
|
c.put("425 Can't build data connection\r\n")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2011-10-14 23:07:09 +00:00
|
|
|
pwd = ::File.join(datastore['FTPROOT'], @state[c][:cwd])
|
2009-07-22 19:10:45 +00:00
|
|
|
buf = ''
|
2011-11-20 02:12:07 +00:00
|
|
|
|
2011-10-14 23:07:09 +00:00
|
|
|
begin
|
|
|
|
Dir.new(pwd).entries.each do |ent|
|
|
|
|
path = ::File.join(datastore['FTPROOT'], ent)
|
|
|
|
if(::File.directory?(path))
|
|
|
|
buf << "drwxr-xr-x 2 0 0 512 Jan 1 2000 #{ent}\r\n"
|
|
|
|
end
|
|
|
|
if(::File.file?(path))
|
|
|
|
buf << "-rw-r--r-- 1 0 0 #{::File.size(path)} Jan 1 2000 #{ent}\r\n"
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
end
|
2011-10-14 23:07:09 +00:00
|
|
|
rescue ::Exception
|
2009-07-22 19:10:45 +00:00
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
c.put("150 Opening ASCII mode data connection for /bin/ls\r\n")
|
|
|
|
conn.put("total #{buf.length}\r\n" + buf)
|
2010-04-30 08:40:19 +00:00
|
|
|
c.put("226 Transfer complete.\r\n")
|
2009-07-22 19:10:45 +00:00
|
|
|
conn.close
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
def on_client_command_size(c,arg)
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-25 04:18:37 +00:00
|
|
|
if(not @state[c][:auth])
|
|
|
|
c.put "500 Access denied\r\n"
|
|
|
|
return
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2011-10-14 23:07:09 +00:00
|
|
|
path = ::File.join(datastore['FTPROOT'], Rex::FileUtils.clean_path(arg))
|
2009-07-22 19:10:45 +00:00
|
|
|
if(not ::File.exists?(path))
|
|
|
|
c.put "550 File does not exist\r\n"
|
|
|
|
return
|
|
|
|
end
|
2010-04-30 08:40:19 +00:00
|
|
|
|
2009-07-22 19:10:45 +00:00
|
|
|
c.put("213 #{::File.size(path)}\r\n")
|
|
|
|
end
|
|
|
|
|
2011-10-14 23:07:09 +00:00
|
|
|
|
|
|
|
def on_client_command_cwd(c,arg)
|
|
|
|
|
|
|
|
if(not @state[c][:auth])
|
|
|
|
c.put "500 Access denied\r\n"
|
|
|
|
return
|
|
|
|
end
|
2011-11-20 02:12:07 +00:00
|
|
|
|
2011-10-14 23:07:09 +00:00
|
|
|
upath = ::File.expand_path(datastore['FTPROOT'])
|
|
|
|
npath = ::File.expand_path(::File.join(datastore['FTPROOT'], @state[c][:cwd], arg))
|
|
|
|
bpath = npath[upath.length, npath.length - upath.length]
|
|
|
|
|
|
|
|
# Check for traversal above the root directory
|
|
|
|
if not (npath[0, upath.length] == upath or bpath == '')
|
|
|
|
bpath = '/'
|
|
|
|
end
|
|
|
|
|
|
|
|
npath = ::File.expand_path(::File.join(datastore['FTPROOT'], bpath))
|
|
|
|
if not (::File.exists?(npath) and ::File.directory?(npath))
|
|
|
|
c.put "550 Directory does not exist\r\n"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
bpath = '/' if bpath == ''
|
|
|
|
@state[c][:cwd] = bpath
|
|
|
|
|
|
|
|
c.put "250 CWD command successful.\r\n"
|
|
|
|
end
|
2009-07-22 19:10:45 +00:00
|
|
|
end
|