metasploit-framework/lib/msf/core/payload/python.rb

26 lines
817 B
Ruby

# -*- coding: binary -*-
require 'msf/core'
module Msf::Payload::Python
#
# Encode the given python command in base64 and wrap it with a stub
# 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.
#
# @param cmd [String] The python code to execute.
# @return [String] Full python stub to execute the command.
#
def py_create_exec_stub(cmd)
# Base64 encoding is required in order to handle Python's formatting
# requirements in the while loop
b64_stub = "import base64,sys;exec(base64.b64decode("
b64_stub << "{2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]('"
b64_stub << Rex::Text.encode_base64(cmd)
b64_stub << "')))"
b64_stub
end
end