Module: MetasploitPayloads

Defined in:
lib/metasploit-payloads.rb,
lib/metasploit-payloads/version.rb

Overview

This module dispenses Metasploit payload binary files

Constant Summary collapse

EXTENSION_PREFIX =
'ext_server_'
METERPRETER_SUBFOLDER =
'meterpreter'
USER_DATA_SUBFOLDER =
'payloads'
VERSION =
'1.3.73'

Class Method Summary collapse

Class Method Details

.data_directoryObject

Full path to the local gem folder containing the base data



99
100
101
# File 'lib/metasploit-payloads.rb', line 99

def self.data_directory
  ::File.realpath(::File.join(::File.dirname(__FILE__), '..', 'data'))
end

.list_meterpreter_extensions(binary_suffix) ⇒ Object

List all the available extensions for the given suffix.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/metasploit-payloads.rb', line 74

def self.list_meterpreter_extensions(binary_suffix)
  extensions = []

  root_dirs = [local_meterpreter_dir]

  # Find the valid extensions in the data folder first, if MSF
  # is installed.
  if metasploit_installed?
    root_dirs.unshift(msf_meterpreter_dir)
    root_dirs.unshift(user_meterpreter_dir)
  end

  root_dirs.each do |dir|
    # Merge in any that don't already exist in the collection.
    meterpreter_enum_ext(dir, binary_suffix).each do |e|
      extensions.push(e) unless extensions.include?(e)
    end
  end

  extensions
end

.local_meterpreter_dirObject

Full path to the local gem folder which contains the meterpreter binaries.



120
121
122
# File 'lib/metasploit-payloads.rb', line 120

def self.local_meterpreter_dir
  ::File.join(data_directory, METERPRETER_SUBFOLDER)
end

.meterpreter_enum_ext(root_dir, binary_suffix) ⇒ Object

Enumerate extensions in the given root folder based on the suffix.



127
128
129
130
131
132
133
134
135
136
# File 'lib/metasploit-payloads.rb', line 127

def self.meterpreter_enum_ext(root_dir, binary_suffix)
  exts = []
  ::Dir.entries(root_dir).each do |f|
    if ::File.readable?(::File.join(root_dir, f)) && \
       f =~ /#{EXTENSION_PREFIX}(.*)\.#{binary_suffix}/
      exts.push($1)
    end
  end
  exts
end

.meterpreter_ext_path(ext_name, binary_suffix) ⇒ Object

Get the path to an extension based on its name (no prefix).



18
19
20
# File 'lib/metasploit-payloads.rb', line 18

def self.meterpreter_ext_path(ext_name, binary_suffix)
  path(METERPRETER_SUBFOLDER, "#{EXTENSION_PREFIX}#{ext_name}.#{binary_suffix}")
end

.meterpreter_path(name, binary_suffix) ⇒ Object

Get the path to a meterpreter binary by full name.



42
43
44
# File 'lib/metasploit-payloads.rb', line 42

def self.meterpreter_path(name, binary_suffix)
  path(METERPRETER_SUBFOLDER, "#{name}.#{binary_suffix}".downcase)
end

.msf_meterpreter_dirObject

Full path to the MSF data folder which contains the meterpreter binaries.



106
107
108
# File 'lib/metasploit-payloads.rb', line 106

def self.msf_meterpreter_dir
  ::File.join(Msf::Config.data_directory, METERPRETER_SUBFOLDER)
end

.path(*path_parts) ⇒ Object

Get the full path to any file packaged in this gem by local path and name.



49
50
51
52
53
54
55
56
# File 'lib/metasploit-payloads.rb', line 49

def self.path(*path_parts)
  gem_path = expand(data_directory, ::File.join(path_parts))
  if metasploit_installed?
    user_path = expand(Msf::Config.config_directory, ::File.join(USER_DATA_SUBFOLDER, path_parts))
    msf_path = expand(Msf::Config.data_directory, ::File.join(path_parts))
  end
  readable_path(gem_path, user_path, msf_path)
end

.read(*path_parts) ⇒ Object

Get the contents of any file packaged in this gem by local path and name.



61
62
63
64
65
66
67
68
69
# File 'lib/metasploit-payloads.rb', line 61

def self.read(*path_parts)
  file_path = path(path_parts)
  if file_path.nil?
    full_path = ::File.join(path_parts)
    fail RuntimeError, "#{full_path} not found", caller
  end

  ::File.binread(file_path)
end

.readable_path(gem_path, *extra_paths) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/metasploit-payloads.rb', line 22

def self.readable_path(gem_path, *extra_paths)
  # Try the MSF path first to see if the file exists, allowing the MSF data
  # folder to override what is in the gem. This is very helpful for
  # testing/development without having to move the binaries to the gem folder
  # each time. We only do this is MSF is installed.
  extra_paths.each do |extra_path|
    if ::File.readable? extra_path
      warn_local_path(extra_path) if ::File.readable? gem_path
      return extra_path
    end
  end

  return gem_path if ::File.readable? gem_path

  nil
end

.user_meterpreter_dirObject

Full path to the user’s MSF data folder which contains the meterpreter binaries.



113
114
115
# File 'lib/metasploit-payloads.rb', line 113

def self.user_meterpreter_dir
  ::File.join(Msf::Config.config_directory, USER_DATA_SUBFOLDER, METERPRETER_SUBFOLDER)
end

.versionObject



5
6
7
# File 'lib/metasploit-payloads/version.rb', line 5

def self.version
  VERSION
end