Skip to content

Commit ad1b9fb

Browse files
author
jvazquez-r7
committed
Use datastore options to avoid complex logic around args
1 parent c68986e commit ad1b9fb

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

modules/post/windows/manage/portproxy.rb

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,12 @@ def run
4141
return
4242
end
4343

44-
type = datastore['TYPE']
45-
lport = datastore['LPORT']
46-
cport = datastore['CPORT']
47-
ipv6_xp = datastore['IPV6_XP']
48-
laddress = datastore['LADDRESS']
49-
caddress = datastore['CADDRESS']
50-
51-
return unless enable_portproxy(lport,cport,laddress,caddress,type,ipv6_xp)
52-
fw_enable_ports(lport)
44+
return unless enable_portproxy
45+
fw_enable_ports
5346

5447
end
5548

56-
def enable_portproxy(lport,cport,laddress,caddress,type,ipv6_xp)
49+
def enable_portproxy
5750
rtable = Rex::Ui::Text::Table.new(
5851
'Header' => 'Port Forwarding Table',
5952
'Indent' => 3,
@@ -63,11 +56,17 @@ def enable_portproxy(lport,cport,laddress,caddress,type,ipv6_xp)
6356
# Due to a bug in Windows XP you need to install IPv6
6457
# http://support.microsoft.com/kb/555744/en-us
6558
if sysinfo["OS"] =~ /XP/
66-
return false if not check_ipv6(ipv6_xp)
59+
return false if not check_ipv6
6760
end
6861

6962
print_status("Setting PortProxy ...")
70-
output = cmd_exec("netsh","interface portproxy add #{type} listenport=#{lport} listenaddress=#{laddress} connectport=#{cport} connectaddress=#{caddress}")
63+
netsh_args = "interface portproxy "
64+
netsh_args << "add #{datastore['TYPE']} "
65+
netsh_args << "listenport=#{datastore['LPORT']} "
66+
netsh_args << "listenaddress=#{datastore['LADDRESS']} "
67+
netsh_args << "connectport=#{datastore['CPORT']} "
68+
netsh_args << "connectaddress=#{datastore['CADDRESS']}"
69+
output = cmd_exec("netsh", netsh_args)
7170
if output.size > 2
7271
print_error("Setup error. Verify parameters and syntax.")
7372
return false
@@ -92,12 +91,12 @@ def ipv6_installed()
9291
end
9392
end
9493

95-
def check_ipv6(ipv6_xp)
94+
def check_ipv6
9695
if ipv6_installed()
9796
print_status("IPv6 is already installed.")
9897
return true
9998
else
100-
if not ipv6_xp
99+
if not datastore['IPV6_XP']
101100
print_error("IPv6 is not installed. You need IPv6 to use portproxy.")
102101
return false
103102
else
@@ -113,17 +112,17 @@ def check_ipv6(ipv6_xp)
113112
end
114113
end
115114

116-
def fw_enable_ports(port)
117-
print_status ("Setting port #{port} in Windows Firewall ...")
115+
def fw_enable_ports
116+
print_status ("Setting port #{datastore['LPORT']} in Windows Firewall ...")
118117
begin
119118
if sysinfo["OS"] =~ /Windows 7|Vista|2008|2012/
120-
cmd_exec("netsh","advfirewall firewall add rule name=\"Windows Service\" dir=in protocol=TCP action=allow localport=\"#{port}\"")
119+
cmd_exec("netsh","advfirewall firewall add rule name=\"Windows Service\" dir=in protocol=TCP action=allow localport=\"#{datastore['LPORT']}\"")
121120
else
122-
cmd_exec("netsh","firewall set portopening protocol=TCP port=\"#{port}\"")
121+
cmd_exec("netsh","firewall set portopening protocol=TCP port=\"#{datastore['LPORT']}\"")
123122
end
124123
output = cmd_exec("netsh","firewall show state")
125124

126-
if output =~ /^#{port} /
125+
if output =~ /^#{datastore['LPORT']} /
127126
print_good("Port opened in Windows Firewall.")
128127
else
129128
print_error("There was an error enabling the port.")

0 commit comments

Comments
 (0)