2005-11-11 01:49:02 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
require 'msf/core/exploit/tcp'
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module exposes methods that may be useful to exploits that deal with
|
|
|
|
# servers that speak the File Transfer Protocol (FTP).
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Exploit::Remote::Ftp
|
|
|
|
|
|
|
|
include Exploit::Remote::Tcp
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Creates an instance of an FTP exploit module.
|
|
|
|
#
|
2005-11-11 01:49:02 +00:00
|
|
|
def initialize(info = {})
|
|
|
|
super
|
|
|
|
|
|
|
|
# Register the options that all FTP exploits may make use of.
|
|
|
|
register_options(
|
|
|
|
[
|
|
|
|
Opt::RHOST,
|
|
|
|
Opt::RPORT(21),
|
2006-02-08 01:07:47 +00:00
|
|
|
OptString.new('FTPUSER', [ false, 'The username to authenticate as', 'anonymous']),
|
|
|
|
OptString.new('FTPPASS', [ false, 'The password for the specified username', 'mozilla@example.com'])
|
2005-11-11 01:49:02 +00:00
|
|
|
], Msf::Exploit::Remote::Ftp)
|
2009-11-18 03:04:25 +00:00
|
|
|
|
|
|
|
register_advanced_options(
|
|
|
|
[
|
2010-07-14 14:04:27 +00:00
|
|
|
OptInt.new('FTPTimeout', [ true, 'The number of seconds to wait for a reply from an FTP command', 16]),
|
|
|
|
OptBool.new('FTPDEBUG', [ false, 'Whether or not to print verbose debug statements', false ])
|
2009-11-18 03:04:25 +00:00
|
|
|
], Msf::Exploit::Remote::Ftp)
|
|
|
|
|
2009-10-28 18:04:50 +00:00
|
|
|
register_autofilter_ports([ 21, 2121])
|
2009-11-18 03:04:25 +00:00
|
|
|
register_autofilter_services(%W{ ftp })
|
|
|
|
|
2009-12-04 19:20:14 +00:00
|
|
|
@ftpbuff = ""
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2005-11-11 01:49:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method establishes an FTP connection to host and port specified by
|
|
|
|
# the RHOST and RPORT options, respectively. After connecting, the banner
|
|
|
|
# message is read in and stored in the 'banner' attribute.
|
|
|
|
#
|
2010-11-08 16:59:20 +00:00
|
|
|
def connect(global = true, verbose = nil)
|
|
|
|
verbose ||= datastore['FTPDEBUG']
|
|
|
|
verbose ||= datastore['VERBOSE']
|
|
|
|
|
2006-02-18 17:12:25 +00:00
|
|
|
print_status("Connecting to FTP server #{rhost}:#{rport}...") if verbose
|
2005-11-11 01:49:02 +00:00
|
|
|
|
2006-02-18 17:12:25 +00:00
|
|
|
fd = super(global)
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2005-11-11 01:49:02 +00:00
|
|
|
# Wait for a banner to arrive...
|
2009-12-03 20:56:08 +00:00
|
|
|
self.banner = recv_ftp_resp(fd)
|
2005-11-11 01:49:02 +00:00
|
|
|
|
2006-02-18 17:12:25 +00:00
|
|
|
print_status("Connected to target FTP server.") if verbose
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2005-11-11 01:49:02 +00:00
|
|
|
# Return the file descriptor to the caller
|
|
|
|
fd
|
|
|
|
end
|
|
|
|
|
2010-07-14 14:04:27 +00:00
|
|
|
#
|
|
|
|
# This method handles establishing datasocket for data channel
|
|
|
|
#
|
|
|
|
def data_connect(mode = nil, nsock = self.sock)
|
|
|
|
if mode
|
|
|
|
res = send_cmd([ 'TYPE' , mode ], true, nsock)
|
|
|
|
return nil if not res =~ /^200/
|
|
|
|
end
|
|
|
|
|
|
|
|
# force datasocket to renegotiate
|
2011-07-12 23:07:51 +00:00
|
|
|
self.datasocket.shutdown if self.datasocket != nil
|
2010-07-14 14:04:27 +00:00
|
|
|
|
|
|
|
res = send_cmd(['PASV'], true, nsock)
|
|
|
|
return nil if not res =~ /^227/
|
|
|
|
|
|
|
|
# 227 Entering Passive Mode (127,0,0,1,196,5)
|
|
|
|
if res =~ /\((\d+)\,(\d+),(\d+),(\d+),(\d+),(\d+)/
|
|
|
|
# convert port to FTP syntax
|
|
|
|
datahost = "#{$1}.#{$2}.#{$3}.#{$4}"
|
|
|
|
dataport = ($5.to_i * 256) + $6.to_i
|
|
|
|
self.datasocket = Rex::Socket::Tcp.create('PeerHost' => datahost, 'PeerPort' => dataport)
|
|
|
|
end
|
|
|
|
self.datasocket
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2011-07-05 17:22:19 +00:00
|
|
|
# This method handles disconnecting our data channel
|
2010-07-14 14:04:27 +00:00
|
|
|
#
|
|
|
|
def data_disconnect
|
2011-07-12 23:07:51 +00:00
|
|
|
self.datasocket.shutdown
|
2010-07-14 14:04:27 +00:00
|
|
|
self.datasocket = nil
|
|
|
|
end
|
|
|
|
|
2005-11-11 01:49:02 +00:00
|
|
|
#
|
2009-11-18 03:04:25 +00:00
|
|
|
# Connect and login to the remote FTP server using the credentials
|
2005-11-11 01:49:02 +00:00
|
|
|
# that have been supplied in the exploit options.
|
|
|
|
#
|
2010-11-08 16:59:20 +00:00
|
|
|
def connect_login(global = true, verbose = nil)
|
|
|
|
verbose ||= datastore['FTPDEBUG']
|
|
|
|
verbose ||= datastore['VERBOSE']
|
2006-02-18 17:12:25 +00:00
|
|
|
ftpsock = connect(global, verbose)
|
2005-11-11 01:49:02 +00:00
|
|
|
|
2009-10-25 17:18:23 +00:00
|
|
|
if !(user and pass)
|
2010-03-05 17:05:12 +00:00
|
|
|
print_error("No username and password were supplied, unable to login")
|
2005-12-05 05:00:27 +00:00
|
|
|
return false
|
|
|
|
end
|
2005-11-11 01:49:02 +00:00
|
|
|
|
2006-02-18 17:12:25 +00:00
|
|
|
print_status("Authenticating as #{user} with password #{pass}...") if verbose
|
2005-12-05 05:00:27 +00:00
|
|
|
res = send_user(user, ftpsock)
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2005-12-05 05:00:27 +00:00
|
|
|
if (res !~ /^(331|2)/)
|
2010-03-05 17:05:12 +00:00
|
|
|
print_error("The server rejected our username") if verbose
|
2005-12-05 05:00:27 +00:00
|
|
|
return false
|
|
|
|
end
|
2005-11-25 20:02:21 +00:00
|
|
|
|
2005-12-05 05:00:27 +00:00
|
|
|
if (pass)
|
2006-02-18 17:12:25 +00:00
|
|
|
print_status("Sending password...") if verbose
|
2005-12-05 05:00:27 +00:00
|
|
|
res = send_pass(pass, ftpsock)
|
|
|
|
if (res !~ /^2/)
|
2010-03-05 17:05:12 +00:00
|
|
|
print_error("The server rejected our password") if verbose
|
2005-12-05 05:00:27 +00:00
|
|
|
return false
|
2005-11-11 01:49:02 +00:00
|
|
|
end
|
|
|
|
end
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2005-12-05 05:00:27 +00:00
|
|
|
return true
|
2005-11-11 01:49:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method logs in as the supplied user by transmitting the FTP
|
|
|
|
# 'USER <user>' command.
|
|
|
|
#
|
|
|
|
def send_user(user, nsock = self.sock)
|
2009-12-03 20:56:08 +00:00
|
|
|
raw_send("USER #{user}\r\n", nsock)
|
2009-12-03 21:11:56 +00:00
|
|
|
recv_ftp_resp(nsock)
|
2005-11-11 01:49:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# This method completes user authentication by sending the supplied
|
|
|
|
# password using the FTP 'PASS <pass>' command.
|
|
|
|
#
|
|
|
|
def send_pass(pass, nsock = self.sock)
|
2009-12-03 20:56:08 +00:00
|
|
|
raw_send("PASS #{pass}\r\n", nsock)
|
2009-12-03 21:11:56 +00:00
|
|
|
recv_ftp_resp(nsock)
|
2005-11-11 01:49:02 +00:00
|
|
|
end
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2010-06-01 18:17:26 +00:00
|
|
|
#
|
2010-07-14 14:04:27 +00:00
|
|
|
# This method sends a QUIT command.
|
2010-06-01 18:17:26 +00:00
|
|
|
#
|
|
|
|
def send_quit(nsock = self.sock)
|
|
|
|
raw_send("QUIT\r\n", nsock)
|
|
|
|
recv_ftp_resp(nsock)
|
|
|
|
end
|
|
|
|
|
2005-11-24 17:45:00 +00:00
|
|
|
#
|
|
|
|
# This method sends one command with zero or more parameters
|
|
|
|
#
|
|
|
|
def send_cmd(args, recv = true, nsock = self.sock)
|
2005-11-25 20:02:21 +00:00
|
|
|
cmd = args.join(" ") + "\r\n"
|
2009-12-04 19:20:14 +00:00
|
|
|
ret = raw_send(cmd, nsock)
|
2005-11-24 17:45:00 +00:00
|
|
|
if (recv)
|
2009-12-04 19:20:14 +00:00
|
|
|
return recv_ftp_resp(nsock)
|
2005-11-24 17:45:00 +00:00
|
|
|
end
|
2009-12-04 19:20:14 +00:00
|
|
|
return ret
|
2005-11-24 17:45:00 +00:00
|
|
|
end
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2010-07-14 14:04:27 +00:00
|
|
|
#
|
|
|
|
# This method transmits the command in args and receives / uploads DATA via data channel
|
|
|
|
# For commands not needing data, it will fall through to the original send_cmd
|
|
|
|
#
|
|
|
|
# For commands that send data only, the return will be the server response.
|
|
|
|
# For commands returning both data and a server response, an array will be returned.
|
|
|
|
#
|
|
|
|
# NOTE: This function always waits for a response from the server.
|
|
|
|
#
|
|
|
|
def send_cmd_data(args, data, mode = 'a', nsock = self.sock)
|
|
|
|
type = nil
|
|
|
|
# implement some aliases for various commands
|
|
|
|
if (args[0] =~ /^DIR$/i || args[0] =~ /^LS$/i)
|
|
|
|
# TODO || args[0] =~ /^MDIR$/i || args[0] =~ /^MLS$/i
|
|
|
|
args[0] = "LIST"
|
|
|
|
type = "get"
|
|
|
|
elsif (args[0] =~ /^GET$/i)
|
|
|
|
args[0] = "RETR"
|
|
|
|
type = "get"
|
|
|
|
elsif (args[0] =~ /^PUT$/i)
|
|
|
|
args[0] = "STOR"
|
|
|
|
type = "put"
|
|
|
|
end
|
|
|
|
|
|
|
|
# fall back if it's not a supported data command
|
|
|
|
if not type
|
|
|
|
return send_cmd(args, true, nsock)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set the transfer mode and connect to the remove server
|
|
|
|
return nil if not data_connect(mode)
|
|
|
|
|
|
|
|
# Our pending command should have got a connection now.
|
|
|
|
res = send_cmd(args, true, nsock)
|
|
|
|
# make sure could open port
|
2011-07-08 04:22:47 +00:00
|
|
|
return nil unless res =~ /^(150|125) /
|
2010-07-14 14:04:27 +00:00
|
|
|
|
|
|
|
# dispatch to the proper method
|
|
|
|
if (type == "get")
|
|
|
|
# failed listings jsut disconnect..
|
|
|
|
begin
|
|
|
|
data = self.datasocket.get_once(-1, ftp_timeout)
|
|
|
|
rescue ::EOFError
|
|
|
|
data = nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sent = self.datasocket.put(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
# close data channel so command channel updates
|
|
|
|
data_disconnect
|
|
|
|
|
|
|
|
# get status of transfer
|
|
|
|
ret = nil
|
|
|
|
if (type == "get")
|
|
|
|
ret = recv_ftp_resp(nsock)
|
|
|
|
ret = [ ret, data ]
|
|
|
|
else
|
|
|
|
ret = recv_ftp_resp(nsock)
|
|
|
|
end
|
|
|
|
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
2005-11-11 01:49:02 +00:00
|
|
|
#
|
2005-11-24 17:45:00 +00:00
|
|
|
# This method transmits a FTP command and waits for a response. If one is
|
2005-11-11 01:49:02 +00:00
|
|
|
# received, it is returned to the caller.
|
|
|
|
#
|
2005-11-24 17:45:00 +00:00
|
|
|
def raw_send_recv(cmd, nsock = self.sock)
|
|
|
|
nsock.put(cmd)
|
2009-11-18 03:04:25 +00:00
|
|
|
nsock.get_once(-1, ftp_timeout)
|
2005-11-24 17:45:00 +00:00
|
|
|
end
|
2010-07-14 14:04:27 +00:00
|
|
|
|
2009-12-03 20:56:08 +00:00
|
|
|
#
|
2009-12-03 21:52:59 +00:00
|
|
|
# This method reads an FTP response based on FTP continuation stuff
|
2009-12-03 20:56:08 +00:00
|
|
|
#
|
|
|
|
def recv_ftp_resp(nsock = self.sock)
|
2010-07-14 14:04:27 +00:00
|
|
|
found_end = false
|
2009-12-03 20:56:08 +00:00
|
|
|
resp = ""
|
|
|
|
left = ""
|
2009-12-04 19:20:14 +00:00
|
|
|
if !@ftpbuff.empty?
|
|
|
|
left << @ftpbuff
|
|
|
|
@ftpbuff = ""
|
|
|
|
end
|
2010-07-14 14:04:27 +00:00
|
|
|
while true
|
2009-12-03 21:52:59 +00:00
|
|
|
data = nsock.get_once(-1, ftp_timeout)
|
2009-12-04 19:20:14 +00:00
|
|
|
if not data
|
|
|
|
@ftpbuff << resp
|
|
|
|
@ftpbuff << left
|
|
|
|
return data
|
|
|
|
end
|
2010-07-14 14:04:27 +00:00
|
|
|
|
2009-12-03 21:52:59 +00:00
|
|
|
got = left + data
|
|
|
|
left = ""
|
2010-07-14 14:04:27 +00:00
|
|
|
|
2009-12-03 21:52:59 +00:00
|
|
|
# handle the end w/o newline case
|
2009-12-08 23:50:29 +00:00
|
|
|
enlidx = got.rindex(0x0a.chr)
|
2009-12-03 21:52:59 +00:00
|
|
|
if enlidx != (got.length-1)
|
|
|
|
if not enlidx
|
|
|
|
left << got
|
|
|
|
next
|
|
|
|
else
|
|
|
|
left << got.slice!((enlidx+1)..got.length)
|
|
|
|
end
|
|
|
|
end
|
2010-07-14 14:04:27 +00:00
|
|
|
|
2009-12-03 21:52:59 +00:00
|
|
|
# split into lines
|
2009-12-03 20:56:08 +00:00
|
|
|
rarr = got.split(/\r?\n/)
|
|
|
|
rarr.each do |ln|
|
|
|
|
if not found_end
|
|
|
|
resp << ln
|
|
|
|
resp << "\r\n"
|
|
|
|
if ln.length > 3 and ln[3,1] == ' '
|
2010-07-14 14:04:27 +00:00
|
|
|
found_end = true
|
2009-12-03 20:56:08 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
left << ln
|
|
|
|
left << "\r\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if found_end
|
2009-12-04 19:20:14 +00:00
|
|
|
@ftpbuff << left
|
2010-07-14 14:04:27 +00:00
|
|
|
print_status("FTP recv: #{resp.inspect}") if datastore['FTPDEBUG']
|
2009-12-03 20:56:08 +00:00
|
|
|
return resp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-07-14 14:04:27 +00:00
|
|
|
|
2005-11-24 17:45:00 +00:00
|
|
|
#
|
|
|
|
# This method transmits a FTP command and does not wait for a response
|
|
|
|
#
|
2005-11-11 01:49:02 +00:00
|
|
|
def raw_send(cmd, nsock = self.sock)
|
2010-07-14 14:04:27 +00:00
|
|
|
print_status("FTP send: #{cmd.inspect}") if datastore['FTPDEBUG']
|
2005-11-11 01:49:02 +00:00
|
|
|
nsock.put(cmd)
|
|
|
|
end
|
2009-11-18 03:04:25 +00:00
|
|
|
|
2005-11-11 01:49:02 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Wrappers for getters
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
2006-02-08 01:07:47 +00:00
|
|
|
# Returns the user string from the 'FTPUSER' option.
|
2005-11-11 01:49:02 +00:00
|
|
|
#
|
|
|
|
def user
|
2010-07-14 14:04:27 +00:00
|
|
|
datastore['FTPUSER']
|
2005-11-11 01:49:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2006-02-08 01:07:47 +00:00
|
|
|
# Returns the user string from the 'FTPPASS' option.
|
2005-11-11 01:49:02 +00:00
|
|
|
#
|
|
|
|
def pass
|
2006-02-08 01:07:47 +00:00
|
|
|
datastore['FTPPASS']
|
2005-11-11 01:49:02 +00:00
|
|
|
end
|
|
|
|
|
2009-11-18 03:04:25 +00:00
|
|
|
#
|
|
|
|
# Returns the number of seconds to wait for a FTP reply
|
|
|
|
#
|
|
|
|
def ftp_timeout
|
|
|
|
(datastore['FTPTimeout'] || 10).to_i
|
|
|
|
end
|
|
|
|
|
2005-11-11 01:49:02 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
#
|
|
|
|
# This attribute holds the banner that was read in after a successful call
|
|
|
|
# to connect or connect_login.
|
|
|
|
#
|
2010-07-14 14:04:27 +00:00
|
|
|
attr_accessor :banner, :datasocket
|
2005-11-11 01:49:02 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-10-28 18:04:50 +00:00
|
|
|
end
|