Skip to content

Commit 78733fc

Browse files
committed
Merge pull request #6 from jvazquez-r7/review_4187
Do minor cosmetic cleanup for post-exploitation module to get FW filtering rules
2 parents 80a9fa4 + 5615d65 commit 78733fc

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

modules/post/windows/recon/outbound_ports.rb

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@ class Metasploit3 < Msf::Post
1313

1414
def initialize(info={})
1515
super( update_info( info,
16-
'Name' => 'Windows Outbound-Filering Rules',
17-
'Description' => %q{
18-
This module makes some kind of TCP traceroute to get outbound-filtering rules.
19-
It will try to make a TCP connection to a certain public IP address (this IP
20-
does not need to be under your control) using different TTL incremental values.
21-
This way if you get an answer (ICMP ttl time exceeded packet) from a public IP
22-
device you can infer that the destination port is allowed. Setting STOP to
23-
true the module will stop as soon as you reach a public IP (this will generate
24-
less noise in the network).
25-
},
26-
27-
'License' => MSF_LICENSE,
28-
'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>' ],
29-
'Platform' => [ 'win' ],
30-
'SessionTypes' => [ 'meterpreter' ],
31-
'References' => [
16+
'Name' => 'Windows Outbound-Filering Rules',
17+
'Description' => %q{
18+
This module makes some kind of TCP traceroute to get outbound-filtering rules.
19+
It will try to make a TCP connection to a certain public IP address (this IP
20+
does not need to be under your control) using different TTL incremental values.
21+
This way if you get an answer (ICMP ttl time exceeded packet) from a public IP
22+
device you can infer that the destination port is allowed. Setting STOP to
23+
true the module will stop as soon as you reach a public IP (this will generate
24+
less noise in the network).
25+
},
26+
'License' => MSF_LICENSE,
27+
'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>' ],
28+
'Platform' => [ 'win' ],
29+
'SessionTypes' => [ 'meterpreter' ],
30+
'References' => [
3231
[ 'URL', 'http://www.shelliscoming.com/2014/11/getting-outbound-filtering-rules-by.html' ]
3332
]
34-
))
33+
))
34+
3535
register_options(
3636
[
3737
OptAddress.new("ADDRESS" , [ true, 'Destination IP address.']),
@@ -41,7 +41,6 @@ def initialize(info={})
4141
OptInt.new('TIMEOUT', [true, 'Timeout for the ICMP socket.', 3]),
4242
OptBool.new('STOP', [true, 'Stop when it finds a public IP.', false])
4343
], self.class)
44-
4544
end
4645

4746
def icmp_setup
@@ -87,8 +86,8 @@ def tcp_setup(ttl)
8786
end
8887
vprint_status("TCP socket created successfully")
8988

90-
fionbio = 0x8004667E
91-
r = client.railgun.ws2_32.ioctlsocket(handler['return'],fionbio,1)
89+
cmd = 0x8004667E
90+
r = client.railgun.ws2_32.ioctlsocket(handler['return'], cmd, 1)
9291
if r['GetLastError'] != 0
9392
print_error("There was an error setting the TCP socket in non-blocking mode; GetLastError: #{r['GetLastError']}")
9493
return nil
@@ -103,9 +102,9 @@ def tcp_setup(ttl)
103102
#_In_ int optlen
104103
# );
105104

106-
ipproto_ip = 0x00000000
107-
ip_ttl = 0x00000004
108-
r = client.railgun.ws2_32.setsockopt(handler['return'], ipproto_ip, ip_ttl,[ttl].pack('C'),4)
105+
ipproto_ip = 0
106+
ip_ttl = 4
107+
r = client.railgun.ws2_32.setsockopt(handler['return'], ipproto_ip, ip_ttl, [ttl].pack('C'), 4)
109108
if r['GetLastError'] != 0
110109
print_error("There was an error setting the TTL value; GetLastError: #{r['GetLastError']}")
111110
return nil
@@ -114,20 +113,20 @@ def tcp_setup(ttl)
114113
return handler['return']
115114
end
116115

117-
def connections(remote,dport,h_icmp,h_tcp, to)
118-
sockaddr = Rex::Socket.to_sockaddr(remote, dport)
119-
r = client.railgun.ws2_32.connect(h_tcp,sockaddr,16)
116+
def connections(remote, dst_port, h_icmp, h_tcp, to)
117+
sock_addr = Rex::Socket.to_sockaddr(remote, dst_port)
118+
r = client.railgun.ws2_32.connect(h_tcp, sock_addr, 16)
120119
# A GetLastError == 1035 is expected since the socket is set to non-blocking mode
121120
if r['GetLastError'] != 10035
122121
print_error("There was an error creating the connection to the peer #{remote}; GetLastError: #{r['GetLastError']}")
123122
return
124123
end
125124

126-
from=" "*16
125+
from = " " * 16
127126

128127
begin
129128
::Timeout.timeout(to) do
130-
r = client.railgun.ws2_32.recvfrom(h_icmp,"",100,0,from,16)
129+
r = client.railgun.ws2_32.recvfrom(h_icmp, "", 100, 0, from, 16)
131130
hop = Rex::Socket.addr_ntoa(r['from'][4..7])
132131
return hop
133132
end
@@ -138,17 +137,17 @@ def connections(remote,dport,h_icmp,h_tcp, to)
138137
end
139138

140139
def run
141-
if not is_admin?
140+
unless is_admin?
142141
print_error("You don't have enough privileges. Try getsystem.")
143142
return
144143
end
145144

146-
if sysinfo["OS"] =~ /XP/
147-
print_error("Windows XP is not supported")
145+
if sysinfo['OS'] =~ /XP/
146+
print_error('Windows XP is not supported')
148147
return
149148
end
150149

151-
output = cmd_exec("netsh"," advfirewall firewall add rule name=\"All ICMP v4\" dir=in action=allow protocol=icmpv4:any,any")
150+
output = cmd_exec('netsh',' advfirewall firewall add rule name="All ICMP v4" dir=in action=allow protocol=icmpv4:any,any')
152151
print_status("ICMP firewall IN rule established: #{output}")
153152

154153
session.railgun.ws2_32
@@ -159,22 +158,24 @@ def run
159158

160159
ports.each do |dport|
161160
print_status("Testing port #{dport}...")
162-
0.upto(datastore['HOPS']-1) { |i|
161+
0.upto(datastore['HOPS'] - 1) { |i|
163162
i = i + datastore['MIN_TTL']
164163
h_icmp = icmp_setup
164+
return if h_icmp.nil?
165165
h_tcp = tcp_setup(i)
166-
return if h_icmp == nil or h_tcp == nil
166+
return if h_tcp .nil?
167167

168168
hop = connections(remote, dport, h_icmp, h_tcp, to)
169169
if hop != nil
170170
print_good("#{i} #{hop}")
171-
if datastore['STOP'] == true and not Rex::Socket.is_internal?(hop)
171+
if datastore['STOP'] == true and !Rex::Socket.is_internal?(hop)
172172
print_good("Public IP reached. The port #{dport} is not filtered")
173173
break
174174
end
175175
else
176176
print_error("#{i} *")
177177
end
178+
178179
client.railgun.ws2_32.closesocket(h_tcp)
179180
client.railgun.ws2_32.closesocket(h_icmp)
180181
}

0 commit comments

Comments
 (0)