Skip to content

Commit 49fef9e

Browse files
committed
Do minor module clean up
1 parent 6480ae2 commit 49fef9e

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

modules/post/windows/recon/outbound_ports.rb

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ def initialize(info={})
2424
less noise in the network).
2525
},
2626
'License' => MSF_LICENSE,
27-
'Author' => [ 'Borja Merino <bmerinofe[at]gmail.com>' ],
28-
'Platform' => [ 'win' ],
29-
'SessionTypes' => [ 'meterpreter' ],
27+
'Author' => 'Borja Merino <bmerinofe[at]gmail.com>',
28+
'Platform' => 'win',
29+
'SessionTypes' => ['meterpreter'],
3030
'References' => [
31-
[ 'URL', 'http://www.shelliscoming.com/2014/11/getting-outbound-filtering-rules-by.html' ]
31+
['URL', 'http://www.shelliscoming.com/2014/11/getting-outbound-filtering-rules-by.html']
3232
]
3333
))
3434

3535
register_options(
3636
[
37-
OptAddress.new("ADDRESS" , [ true, 'Destination IP address.']),
37+
OptAddress.new('ADDRESS' , [ true, 'Destination IP address.']),
3838
OptInt.new('HOPS', [true, 'Number of hops to get.', 3]),
3939
OptInt.new('MIN_TTL', [true, 'Starting TTL value.', 1]),
4040
OptString.new('PORTS', [true, 'Ports to test (e.g. 80,443,100-110).','80,443']),
@@ -45,18 +45,20 @@ def initialize(info={})
4545

4646
def icmp_setup
4747
handler = client.railgun.ws2_32.socket("AF_INET", "SOCK_RAW", "IPPROTO_ICMP")
48-
if handler['GetLastError'] != 0
48+
if handler['GetLastError'] == 0
49+
vprint_status("ICMP raw socket created successfully")
50+
else
4951
print_error("There was an error setting the ICMP raw socket; GetLastError: #{handler['GetLastError']}")
5052
return nil
5153
end
52-
vprint_status("ICMP raw socket created successfully")
5354

5455
r = client.railgun.ws2_32.bind(handler['return'],"\x02\x00\x00\x00" << Rex::Socket.addr_aton(session.session_host) << "\x00"*8 ,16)
55-
if r['GetLastError'] != 0
56+
if r['GetLastError'] == 0
57+
vprint_status("ICMP socket successfully bound to #{session.session_host}")
58+
else
5659
print_error("There was an error binding the ICMP socket to #{session.session_host}; GetLastError: #{r['GetLastError']}")
5760
return nil
5861
end
59-
vprint_status("ICMP socket successfully bound to #{session.session_host}")
6062

6163
# int WSAIoctl(
6264
# _In_ SOCKET s,
@@ -71,32 +73,34 @@ def icmp_setup
7173
# );
7274

7375
sio_rcvall = 0x98000001
74-
r = client.railgun.ws2_32.WSAIoctl(handler['return'],sio_rcvall,"\x01",4,nil,0,4,nil,nil)
75-
if r['GetLastError'] != 0
76+
r = client.railgun.ws2_32.WSAIoctl(handler['return'], sio_rcvall, "\x01", 4, nil, 0 ,4, nil, nil)
77+
if r['GetLastError'] == 0
78+
return handler['return']
79+
else
7680
print_error("There was an error calling WSAIoctl (ICMP raw socket); GetLastError: #{r['GetLastError']}")
7781
return nil
7882
end
79-
return handler['return']
8083
end
8184

8285
def tcp_setup(ttl)
83-
handler = client.railgun.ws2_32.socket("AF_INET", "SOCK_STREAM", "IPPROTO_TCP")
84-
if handler['GetLastError'] != 0
86+
handler = client.railgun.ws2_32.socket('AF_INET', 'SOCK_STREAM', 'IPPROTO_TCP')
87+
if handler['GetLastError'] == 0
88+
vprint_status('TCP socket created successfully')
89+
else
8590
print_error("There was an error setting the TCP socket; GetLastError: #{handler['GetLastError']}")
8691
return nil
8792
end
88-
vprint_status("TCP socket created successfully")
8993

9094
# 0x8004667E = FIONBIO
9195
# Enable non-blocking mode when *argp (third parameter in ioctlsocket) is set to a nonzero value
92-
9396
cmd = 0x8004667E
9497
r = client.railgun.ws2_32.ioctlsocket(handler['return'], cmd, 1)
95-
if r['GetLastError'] != 0
98+
if r['GetLastError'] == 0
99+
vprint_status('TCP socket successfully configured in non-blocking mode')
100+
else
96101
print_error("There was an error setting the TCP socket in non-blocking mode; GetLastError: #{r['GetLastError']}")
97102
return nil
98103
end
99-
vprint_status("TCP socket successfully configured in non-blocking mode")
100104

101105
# int setsockopt(
102106
# _In_ SOCKET s,
@@ -105,28 +109,32 @@ def tcp_setup(ttl)
105109
# _In_ const char *optval,
106110
#_In_ int optlen
107111
# );
108-
109112
ipproto_ip = 0
110113
ip_ttl = 4
111114
r = client.railgun.ws2_32.setsockopt(handler['return'], ipproto_ip, ip_ttl, [ttl].pack('C'), 4)
112-
if r['GetLastError'] != 0
115+
if r['GetLastError'] == 0
116+
vprint_status("TTL value successfully set to #{ttl}")
117+
return handler['return']
118+
else
113119
print_error("There was an error setting the TTL value; GetLastError: #{r['GetLastError']}")
114120
return nil
115121
end
116-
vprint_status("TTL value successfully set to #{ttl}")
117-
return handler['return']
118122
end
119123

120124
def connections(remote, dst_port, h_icmp, h_tcp, to)
121-
r = client.railgun.ws2_32.connect(h_tcp, "\x02\x00" << [dst_port].pack("n") << Rex::Socket.addr_aton(remote) << "\x00"*8 , 16)
125+
sock_addr = "\x02\x00"
126+
sock_addr << [dst_port].pack('n')
127+
sock_addr << Rex::Socket.addr_aton(remote)
128+
sock_addr << "\x00" * 8
129+
r = client.railgun.ws2_32.connect(h_tcp, sock_addr, 16)
122130

123131
# A GetLastError == 1035 is expected since the socket is set to non-blocking mode
124-
if r['GetLastError'] != 10035
132+
unless r['GetLastError'] == 10035
125133
print_error("There was an error creating the connection to the peer #{remote}; GetLastError: #{r['GetLastError']}")
126134
return
127135
end
128136

129-
from = " " * 16
137+
from = ' ' * 16
130138

131139
begin
132140
::Timeout.timeout(to) do
@@ -137,7 +145,6 @@ def connections(remote, dst_port, h_icmp, h_tcp, to)
137145
rescue ::Timeout::Error
138146
return nil
139147
end
140-
141148
end
142149

143150
def run
@@ -163,27 +170,27 @@ def run
163170
ports.each do |dport|
164171
pub_ip = false
165172
print_status("Testing port #{dport}...")
166-
0.upto(datastore['HOPS'] - 1) { |i|
173+
0.upto(datastore['HOPS'] - 1) do |i|
167174
i = i + datastore['MIN_TTL']
168175
h_icmp = icmp_setup
169176
return if h_icmp.nil?
170177
h_tcp = tcp_setup(i)
171-
return if h_tcp .nil?
178+
return if h_tcp.nil?
172179

173180
hop = connections(remote, dport, h_icmp, h_tcp, to)
174-
if hop != nil
181+
if hop.nil?
182+
print_error("#{i} *")
183+
else
175184
print_good("#{i} #{hop}")
176-
if !Rex::Socket.is_internal?(hop)
185+
unless Rex::Socket.is_internal?(hop)
177186
pub_ip = true
178-
break if datastore['STOP'] == true
187+
break if datastore['STOP']
179188
end
180-
else
181-
print_error("#{i} *")
182189
end
183190
client.railgun.ws2_32.closesocket(h_tcp)
184191
client.railgun.ws2_32.closesocket(h_icmp)
185-
}
186-
print_good("Public IP reached. The TCP port #{dport} is not filtered") if pub_ip == true
192+
end
193+
print_good("Public IP reached. The TCP port #{dport} is not filtered") if pub_ip
187194
end
188195
end
189196
end

0 commit comments

Comments
 (0)