2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2011-12-17 00:39:09 +00:00
|
|
|
require 'rex/socket'
|
|
|
|
require 'rex/proto/tftp'
|
2011-12-19 21:50:50 +00:00
|
|
|
require 'tempfile'
|
2011-12-17 00:39:09 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Proto
|
|
|
|
module TFTP
|
|
|
|
|
|
|
|
#
|
|
|
|
# TFTP Client class
|
|
|
|
#
|
|
|
|
# Note that TFTP has blocks, and so does Ruby. Watch out with the variable names!
|
2011-12-20 00:15:19 +00:00
|
|
|
#
|
|
|
|
# The big gotcha right now is that setting the mode between octet, netascii, or
|
|
|
|
# anything else doesn't actually do anything other than declare it to the
|
|
|
|
# server.
|
|
|
|
#
|
|
|
|
# Also, since TFTP clients act as both clients and servers, we use two
|
|
|
|
# threads to handle transfers, regardless of the direction. For this reason,
|
2011-12-20 03:56:03 +00:00
|
|
|
# the transfer actions are nonblocking; if you need to see the
|
2011-12-20 00:15:19 +00:00
|
|
|
# results of a transfer before doing something else, check the boolean complete
|
2011-12-20 03:56:03 +00:00
|
|
|
# attribute and any return data in the :status attribute. It's a little
|
2011-12-20 00:15:19 +00:00
|
|
|
# weird like that.
|
|
|
|
#
|
|
|
|
# Finally, most (all?) clients will alter the data in netascii mode in order
|
|
|
|
# to try to conform to the RFC standard for what "netascii" means, but there are
|
|
|
|
# ambiguities in implementations on things like if nulls are allowed, what
|
|
|
|
# to do with Unicode, and all that. For this reason, "octet" is default, and
|
|
|
|
# if you want to send "netascii" data, it's on you to fix up your source data
|
|
|
|
# prior to sending it.
|
|
|
|
#
|
2011-12-17 00:39:09 +00:00
|
|
|
class Client
|
|
|
|
|
|
|
|
attr_accessor :local_host, :local_port, :peer_host, :peer_port
|
2011-12-19 18:14:40 +00:00
|
|
|
attr_accessor :threads, :context, :server_sock, :client_sock
|
2011-12-19 21:50:50 +00:00
|
|
|
attr_accessor :local_file, :remote_file, :mode, :action
|
2011-12-20 03:56:03 +00:00
|
|
|
attr_accessor :complete, :recv_tempfile, :status
|
2011-12-20 16:03:04 +00:00
|
|
|
attr_accessor :block_size # This definitely breaks spec, should only use for fuzz/sploit.
|
2011-12-19 03:05:27 +00:00
|
|
|
|
2011-12-19 21:50:50 +00:00
|
|
|
# Returns an array of [code, type, msg]. Data packets
|
2011-12-19 22:33:25 +00:00
|
|
|
# specifically will /not/ unpack, since that would drop any trailing spaces or nulls.
|
2011-12-20 03:56:03 +00:00
|
|
|
def parse_tftp_response(str)
|
2011-12-19 03:05:27 +00:00
|
|
|
return nil unless str.length >= 4
|
|
|
|
ret = str.unpack("nnA*")
|
2011-12-19 22:33:25 +00:00
|
|
|
ret[2] = str[4,str.size] if ret[0] == OpData
|
2011-12-19 03:05:27 +00:00
|
|
|
return ret
|
|
|
|
end
|
2011-12-17 00:39:09 +00:00
|
|
|
|
|
|
|
def initialize(params)
|
2011-12-19 18:14:40 +00:00
|
|
|
self.threads = []
|
2011-12-17 00:39:09 +00:00
|
|
|
self.local_host = params["LocalHost"] || "0.0.0.0"
|
|
|
|
self.local_port = params["LocalPort"] || (1025 + rand(0xffff-1025))
|
|
|
|
self.peer_host = params["PeerHost"] || (raise ArgumentError, "Need a peer host.")
|
|
|
|
self.peer_port = params["PeerPort"] || 69
|
2012-05-11 02:20:29 +00:00
|
|
|
self.context = params["Context"]
|
2011-12-20 00:15:19 +00:00
|
|
|
self.local_file = params["LocalFile"]
|
2011-12-21 22:23:56 +00:00
|
|
|
self.remote_file = params["RemoteFile"] || (::File.split(self.local_file).last if self.local_file)
|
2011-12-19 03:05:27 +00:00
|
|
|
self.mode = params["Mode"] || "octet"
|
2011-12-19 21:50:50 +00:00
|
|
|
self.action = params["Action"] || (raise ArgumentError, "Need an action.")
|
2011-12-20 16:03:04 +00:00
|
|
|
self.block_size = params["BlockSize"] || 512
|
2011-12-17 00:39:09 +00:00
|
|
|
end
|
|
|
|
|
2011-12-19 21:50:50 +00:00
|
|
|
#
|
|
|
|
# Methods for both upload and download
|
|
|
|
#
|
|
|
|
|
2011-12-19 18:14:40 +00:00
|
|
|
def start_server_socket
|
|
|
|
self.server_sock = Rex::Socket::Udp.create(
|
2011-12-17 00:39:09 +00:00
|
|
|
'LocalHost' => local_host,
|
|
|
|
'LocalPort' => local_port,
|
|
|
|
'Context' => context
|
|
|
|
)
|
2011-12-19 18:14:40 +00:00
|
|
|
if self.server_sock and block_given?
|
2011-12-17 00:39:09 +00:00
|
|
|
yield "Started TFTP client listener on #{local_host}:#{local_port}"
|
|
|
|
end
|
2011-12-19 18:14:40 +00:00
|
|
|
self.threads << Rex::ThreadFactory.spawn("TFTPServerMonitor", false) {
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
monitor_server_sock {|msg| yield msg}
|
|
|
|
else
|
|
|
|
monitor_server_sock
|
|
|
|
end
|
2011-12-17 00:39:09 +00:00
|
|
|
}
|
2011-12-19 18:14:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def monitor_server_sock
|
2011-12-19 21:50:50 +00:00
|
|
|
yield "Listening for incoming ACKs" if block_given?
|
|
|
|
res = self.server_sock.recvfrom(65535)
|
|
|
|
if res and res[0]
|
2011-12-20 03:56:03 +00:00
|
|
|
code, type, data = parse_tftp_response(res[0])
|
|
|
|
if code == OpAck and self.action == :upload
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
yield "WRQ accepted, sending the file." if type == 0
|
|
|
|
send_data(res[1], res[2]) {|msg| yield msg}
|
|
|
|
else
|
|
|
|
send_data(res[1], res[2])
|
|
|
|
end
|
2011-12-20 03:56:03 +00:00
|
|
|
elsif code == OpData and self.action == :download
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
recv_data(res[1], res[2], data) {|msg| yield msg}
|
|
|
|
else
|
|
|
|
recv_data(res[1], res[2], data)
|
|
|
|
end
|
2011-12-20 03:56:03 +00:00
|
|
|
elsif code == OpError
|
|
|
|
yield("Aborting, got error type:%d, message:'%s'" % [type, data]) if block_given?
|
|
|
|
self.status = {:error => [code, type, data]}
|
2011-12-19 21:50:50 +00:00
|
|
|
else
|
2011-12-20 03:56:03 +00:00
|
|
|
yield("Aborting, got code:%d, type:%d, message:'%s'" % [code, type, data]) if block_given?
|
|
|
|
self.status = {:error => [code, type, data]}
|
2011-12-19 18:14:40 +00:00
|
|
|
end
|
2011-12-19 21:50:50 +00:00
|
|
|
end
|
2011-12-19 18:14:40 +00:00
|
|
|
stop
|
2011-12-17 00:39:09 +00:00
|
|
|
end
|
|
|
|
|
2011-12-19 21:50:50 +00:00
|
|
|
def monitor_client_sock
|
|
|
|
res = self.client_sock.recvfrom(65535)
|
|
|
|
if res[1] # Got a response back, so that's never good; Acks come back on server_sock.
|
2011-12-20 03:56:03 +00:00
|
|
|
code, type, data = parse_tftp_response(res[0])
|
|
|
|
yield("Aborting, got code:%d, type:%d, message:'%s'" % [code, type, data]) if block_given?
|
|
|
|
self.status = {:error => [code, type, data]}
|
2011-12-19 21:50:50 +00:00
|
|
|
stop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
self.complete = true
|
2011-12-20 16:03:04 +00:00
|
|
|
begin
|
|
|
|
self.server_sock.close
|
|
|
|
self.client_sock.close
|
|
|
|
self.server_sock = nil
|
|
|
|
self.client_sock = nil
|
|
|
|
self.threads.each {|t| t.kill}
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
2011-12-19 21:50:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Methods for download
|
|
|
|
#
|
|
|
|
|
|
|
|
def rrq_packet
|
|
|
|
req = [OpRead, self.remote_file, self.mode]
|
|
|
|
packstr = "na#{self.remote_file.length+1}a#{self.mode.length+1}"
|
|
|
|
req.pack(packstr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ack_packet(blocknum=0)
|
|
|
|
req = [OpAck, blocknum].pack("nn")
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_read_request(&block)
|
2011-12-20 03:56:03 +00:00
|
|
|
self.status = nil
|
2011-12-20 00:15:19 +00:00
|
|
|
self.complete = false
|
2011-12-19 21:50:50 +00:00
|
|
|
if block_given?
|
|
|
|
start_server_socket {|msg| yield msg}
|
|
|
|
else
|
|
|
|
start_server_socket
|
|
|
|
end
|
|
|
|
self.client_sock = Rex::Socket::Udp.create(
|
|
|
|
'PeerHost' => peer_host,
|
|
|
|
'PeerPort' => peer_port,
|
|
|
|
'LocalHost' => local_host,
|
|
|
|
'LocalPort' => local_port,
|
|
|
|
'Context' => context
|
|
|
|
)
|
|
|
|
self.client_sock.sendto(rrq_packet, peer_host, peer_port)
|
|
|
|
self.threads << Rex::ThreadFactory.spawn("TFTPClientMonitor", false) {
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
monitor_client_sock {|msg| yield msg}
|
|
|
|
else
|
|
|
|
monitor_client_sock
|
|
|
|
end
|
2011-12-19 21:50:50 +00:00
|
|
|
}
|
2011-12-20 00:15:19 +00:00
|
|
|
until self.complete
|
2011-12-20 03:56:03 +00:00
|
|
|
return self.status
|
2011-12-20 00:15:19 +00:00
|
|
|
end
|
2011-12-19 21:50:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def recv_data(host, port, first_block)
|
|
|
|
self.recv_tempfile = Rex::Quickfile.new('msf-tftp')
|
|
|
|
recvd_blocks = 1
|
|
|
|
if block_given?
|
|
|
|
yield "Source file: #{self.remote_file}, destination file: #{self.local_file}"
|
|
|
|
yield "Received and acknowledged #{first_block.size} in block #{recvd_blocks}"
|
|
|
|
end
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
write_and_ack_data(first_block,1,host,port) {|msg| yield msg}
|
|
|
|
else
|
|
|
|
write_and_ack_data(first_block,1,host,port)
|
|
|
|
end
|
2011-12-19 21:50:50 +00:00
|
|
|
current_block = first_block
|
|
|
|
while current_block.size == 512
|
|
|
|
res = self.server_sock.recvfrom(65535)
|
|
|
|
if res and res[0]
|
2011-12-20 03:56:03 +00:00
|
|
|
code, block_num, current_block = parse_tftp_response(res[0])
|
2011-12-19 21:50:50 +00:00
|
|
|
if code == 3
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
write_and_ack_data(current_block,block_num,host,port) {|msg| yield msg}
|
|
|
|
else
|
|
|
|
write_and_ack_data(current_block,block_num,host,port)
|
|
|
|
end
|
2011-12-19 21:50:50 +00:00
|
|
|
recvd_blocks += 1
|
|
|
|
else
|
|
|
|
yield("Aborting, got code:%d, type:%d, message:'%s'" % [code, type, msg]) if block_given?
|
|
|
|
stop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
yield("Transferred #{self.recv_tempfile.size} bytes in #{recvd_blocks} blocks, download complete!")
|
|
|
|
end
|
2011-12-20 03:56:03 +00:00
|
|
|
self.status = {:success => [
|
2011-12-20 00:15:19 +00:00
|
|
|
self.local_file,
|
|
|
|
self.remote_file,
|
|
|
|
self.recv_tempfile.size,
|
|
|
|
recvd_blocks.size]
|
|
|
|
}
|
2011-12-19 21:50:50 +00:00
|
|
|
self.recv_tempfile.close
|
|
|
|
stop
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_and_ack_data(data,blocknum,host,port)
|
|
|
|
self.recv_tempfile.write(data)
|
|
|
|
self.recv_tempfile.flush
|
|
|
|
req = ack_packet(blocknum)
|
|
|
|
self.server_sock.sendto(req, host, port)
|
2011-12-20 00:15:19 +00:00
|
|
|
yield "Received and acknowledged #{data.size} in block #{blocknum}" if block_given?
|
2011-12-19 21:50:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Methods for upload
|
|
|
|
#
|
2011-12-20 03:56:03 +00:00
|
|
|
|
2011-12-17 00:39:09 +00:00
|
|
|
def wrq_packet
|
2011-12-19 22:33:25 +00:00
|
|
|
req = [OpWrite, self.remote_file, self.mode]
|
|
|
|
packstr = "na#{self.remote_file.length+1}a#{self.mode.length+1}"
|
|
|
|
req.pack(packstr)
|
2011-12-17 00:39:09 +00:00
|
|
|
end
|
|
|
|
|
2011-12-20 00:15:19 +00:00
|
|
|
# Note that the local filename for uploading need not be a real filename --
|
2011-12-20 03:56:03 +00:00
|
|
|
# if it begins with DATA: it can be any old string of bytes. If it's missing
|
|
|
|
# completely, then just quit.
|
2011-12-20 00:15:19 +00:00
|
|
|
def blockify_file_or_data
|
|
|
|
if self.local_file =~ /^DATA:(.*)/m
|
|
|
|
data = $1
|
2011-12-20 03:56:03 +00:00
|
|
|
elsif ::File.file?(self.local_file) and ::File.readable?(self.local_file)
|
|
|
|
data = ::File.open(self.local_file, "rb") {|f| f.read f.stat.size} rescue []
|
2011-12-20 00:15:19 +00:00
|
|
|
else
|
2011-12-20 03:56:03 +00:00
|
|
|
return []
|
2011-12-20 00:15:19 +00:00
|
|
|
end
|
2011-12-20 16:03:04 +00:00
|
|
|
data_blocks = data.scan(/.{1,#{block_size}}/m)
|
|
|
|
# Drop any trailing empty blocks
|
|
|
|
if data_blocks.size > 1 and data_blocks.last.empty?
|
|
|
|
data_blocks.pop
|
|
|
|
end
|
|
|
|
return data_blocks
|
2011-12-19 21:50:50 +00:00
|
|
|
end
|
|
|
|
|
2011-12-17 00:39:09 +00:00
|
|
|
def send_write_request(&block)
|
2011-12-20 03:56:03 +00:00
|
|
|
self.status = nil
|
2011-12-20 00:15:19 +00:00
|
|
|
self.complete = false
|
2011-12-17 00:39:09 +00:00
|
|
|
if block_given?
|
2011-12-19 18:14:40 +00:00
|
|
|
start_server_socket {|msg| yield msg}
|
2011-12-17 00:39:09 +00:00
|
|
|
else
|
2011-12-19 21:50:50 +00:00
|
|
|
start_server_socket
|
2011-12-17 00:39:09 +00:00
|
|
|
end
|
2011-12-19 18:14:40 +00:00
|
|
|
self.client_sock = Rex::Socket::Udp.create(
|
2011-12-17 00:39:09 +00:00
|
|
|
'PeerHost' => peer_host,
|
|
|
|
'PeerPort' => peer_port,
|
|
|
|
'LocalHost' => local_host,
|
|
|
|
'LocalPort' => local_port,
|
|
|
|
'Context' => context
|
|
|
|
)
|
2011-12-19 18:14:40 +00:00
|
|
|
self.client_sock.sendto(wrq_packet, peer_host, peer_port)
|
|
|
|
self.threads << Rex::ThreadFactory.spawn("TFTPClientMonitor", false) {
|
2011-12-20 00:15:19 +00:00
|
|
|
if block_given?
|
|
|
|
monitor_client_sock {|msg| yield msg}
|
|
|
|
else
|
|
|
|
monitor_client_sock
|
|
|
|
end
|
2011-12-19 18:14:40 +00:00
|
|
|
}
|
2011-12-20 00:15:19 +00:00
|
|
|
until self.complete
|
2011-12-20 03:56:03 +00:00
|
|
|
return self.status
|
2011-12-20 00:15:19 +00:00
|
|
|
end
|
2011-12-19 18:14:40 +00:00
|
|
|
end
|
|
|
|
|
2011-12-19 21:50:50 +00:00
|
|
|
def send_data(host,port)
|
2011-12-20 03:56:03 +00:00
|
|
|
self.status = {:write_allowed => true}
|
2011-12-20 00:15:19 +00:00
|
|
|
data_blocks = blockify_file_or_data()
|
2011-12-20 03:56:03 +00:00
|
|
|
if data_blocks.empty?
|
|
|
|
yield "Closing down since there is no data to send." if block_given?
|
|
|
|
self.status = {:success => [self.local_file, self.local_file, 0, 0]}
|
|
|
|
return nil
|
|
|
|
end
|
2011-12-19 21:50:50 +00:00
|
|
|
sent_data = 0
|
|
|
|
sent_blocks = 0
|
|
|
|
expected_blocks = data_blocks.size
|
|
|
|
expected_size = data_blocks.join.size
|
|
|
|
if block_given?
|
2011-12-20 00:15:19 +00:00
|
|
|
yield "Source file: #{self.local_file =~ /^DATA:/ ? "(Data)" : self.remote_file}, destination file: #{self.remote_file}"
|
2011-12-19 21:50:50 +00:00
|
|
|
yield "Sending #{expected_size} bytes (#{expected_blocks} blocks)"
|
|
|
|
end
|
|
|
|
data_blocks.each_with_index do |data_block,idx|
|
2011-12-19 22:33:25 +00:00
|
|
|
req = [OpData, (idx + 1), data_block].pack("nnA*")
|
2011-12-19 21:50:50 +00:00
|
|
|
if self.server_sock.sendto(req, host, port) > 0
|
|
|
|
sent_data += data_block.size
|
|
|
|
end
|
|
|
|
res = self.server_sock.recvfrom(65535)
|
|
|
|
if res
|
2011-12-20 03:56:03 +00:00
|
|
|
code, type, msg = parse_tftp_response(res[0])
|
2011-12-19 21:50:50 +00:00
|
|
|
if code == 4
|
|
|
|
sent_blocks += 1
|
|
|
|
yield "Sent #{data_block.size} bytes in block #{sent_blocks}" if block_given?
|
|
|
|
else
|
|
|
|
if block_given?
|
|
|
|
yield "Got an unexpected response: Code:%d, Type:%d, Message:'%s'. Aborting." % [code, type, msg]
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if block_given?
|
|
|
|
if(sent_data == expected_size)
|
2011-12-20 00:15:19 +00:00
|
|
|
yield("Transferred #{sent_data} bytes in #{sent_blocks} blocks, upload complete!")
|
2011-12-19 21:50:50 +00:00
|
|
|
else
|
|
|
|
yield "Upload complete, but with errors."
|
|
|
|
end
|
2011-12-19 03:05:27 +00:00
|
|
|
end
|
2011-12-20 00:15:19 +00:00
|
|
|
if sent_data == expected_size
|
2011-12-20 03:56:03 +00:00
|
|
|
self.status = {:success => [
|
2011-12-20 00:15:19 +00:00
|
|
|
self.local_file,
|
|
|
|
self.remote_file,
|
|
|
|
sent_data,
|
|
|
|
sent_blocks
|
|
|
|
] }
|
|
|
|
end
|
2011-12-17 00:39:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|