2015-05-18 04:29:42 +00:00
|
|
|
# -*- coding: binary -*-
|
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
module Msf::Payload::Python
|
|
|
|
|
2015-05-25 01:51:01 +00:00
|
|
|
#
|
|
|
|
# Encode the given python command in base64 and wrap it with a stub
|
2015-11-26 18:59:44 +00: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 01:51:01 +00:00
|
|
|
#
|
|
|
|
# @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
|
|
|
|
|
2015-05-18 04:29:42 +00:00
|
|
|
end
|