Skip to content

Commit d318c1c

Browse files
committed
included feedback
1 parent d03379f commit d318c1c

File tree

1 file changed

+64
-70
lines changed

1 file changed

+64
-70
lines changed

modules/auxiliary/scanner/sap/sap_router_portscanner.rb

Lines changed: 64 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class Metasploit3 < Msf::Auxiliary
1010
def initialize
1111
super(
1212
'Name' => 'SAPRouter Port Scanner',
13-
'Description' => 'This module allows for mapping ACLs and identify open/closed ports accessible on hosts through a saprouter',
13+
'Description' => %q{
14+
This module allows for mapping ACLs and identify open/closed ports
15+
accessible on hosts through a saprouter},
1416
'Author' => ['Bruno Morisson <bm[at]integrity.pt>', # metasploit module
1517
'nmonkee'], # saprouter packet building code from sapcat.rb
1618
'References' =>
@@ -65,34 +67,32 @@ def build_ni_packet(routes)
6567
end
6668

6769
def parse_response_packet(response, ip, port)
68-
report=[]
6970

7071
vprint_error("#{ip}:#{port} - response packet: #{response}")
7172

7273
case response
73-
when /NI_RTERR/
74-
case response
75-
when /timed out/
76-
print_error ("#{ip}:#{port} - connection timed out")
77-
when /refused/
78-
print_error("#{ip}:#{port} - TCP closed")
79-
report << [ip, port, 'closed']
80-
when /denied/
81-
print_error("#{ip}:#{port} - blocked by ACL")
82-
when /invalid/
83-
print_error("#{ip}:#{port} - invalid route")
84-
when /reacheable/
85-
print_error("#{ip}:#{port} - unreachable")
86-
else
87-
print_error("#{ip}:#{port} - unknown error message")
88-
end
89-
when /NI_PONG/
90-
print_good("#{ip}:#{port} - TCP OPEN")
91-
report << [ip, port, 'open']
74+
when /NI_RTERR/
75+
case response
76+
when /timed out/
77+
print_error ("#{ip}:#{port} - connection timed out")
78+
when /refused/
79+
print_error("#{ip}:#{port} - TCP closed")
80+
report_service(:host => ip, :port => port, :state => 'closed')
81+
when /denied/
82+
print_error("#{ip}:#{port} - blocked by ACL")
83+
when /invalid/
84+
print_error("#{ip}:#{port} - invalid route")
85+
when /reacheable/
86+
print_error("#{ip}:#{port} - unreachable")
9287
else
93-
print_error("#{ip}:#{port} - unknown response")
88+
print_error("#{ip}:#{port} - unknown error message")
89+
end
90+
when /NI_PONG/
91+
print_good("#{ip}:#{port} - TCP OPEN")
92+
report_service(:host => ip, :port => port, :state => 'open')
93+
else
94+
print_error("#{ip}:#{port} - unknown response")
9495
end
95-
report
9696

9797
end
9898

@@ -110,57 +110,51 @@ def run_host(ip)
110110
end
111111

112112
print_status("Scanning #{ip}")
113+
thread = []
114+
ports.each do |port|
113115

114-
while (ports.length > 0)
115-
thread = []
116-
report = []
117-
begin
118-
1.upto(datastore['CONCURRENCY']) do
119-
this_port = ports.shift
120-
break if not this_port
121-
thread << framework.threads.spawn("Module(#{self.refname})-#{ip}:#{this_port}", false, this_port) do |port|
122-
123-
begin
124-
s = connect(false,
125-
{
126-
'RPORT' => sap_port,
127-
'RHOST' => sap_host,
128-
'ConnectTimeout' => (timeout / 1000.0)
129-
}
130-
)
131-
132-
# create ni_packet to send to saprouter
133-
routes = {sap_host => sap_port, ip => port}
134-
ni_packet = build_ni_packet(routes)
135-
136-
s.write(ni_packet, ni_packet.length)
137-
response = s.get()
138-
139-
report = parse_response_packet(response, ip, port)
140-
141-
rescue ::Rex::ConnectionRefused
142-
print_error("#{ip}:#{port} - Unable to connect to SAPRouter #{sap_host}:#{sap_port} - Connection Refused")
143-
144-
rescue ::Rex::ConnectionError, ::IOError, ::Timeout::Error
145-
rescue ::Rex::Post::Meterpreter::RequestError
146-
rescue ::Interrupt
147-
raise $!
148-
rescue ::Exception => e
149-
print_error("#{ip}:#{port} exception #{e.class} #{e} #{e.backtrace}")
150-
ensure
151-
disconnect(s) rescue nil
152-
end
153-
end
154-
end
155-
thread.each { |x| x.join }
156116

157-
rescue ::Timeout::Error
158-
ensure
159-
thread.each { |x| x.kill rescue nil }
117+
if thread.length >= datastore['CONCURRENCY']
118+
# Assume the first thread will be among the earliest to finish
119+
thread.first.join
120+
end
121+
thread << framework.threads.spawn("Module(#{self.refname})-#{ip}:#{port}", false) do
122+
123+
begin
124+
s = connect(false,
125+
{
126+
'RPORT' => sap_port,
127+
'RHOST' => sap_host,
128+
'ConnectTimeout' => (timeout / 1000.0)
129+
}
130+
)
131+
132+
# create ni_packet to send to saprouter
133+
routes = {sap_host => sap_port, ip => port}
134+
ni_packet = build_ni_packet(routes)
135+
136+
s.write(ni_packet, ni_packet.length)
137+
response = s.get()
138+
139+
parse_response_packet(response, ip, port)
140+
141+
rescue ::Rex::ConnectionRefused
142+
print_error("#{ip}:#{port} - Unable to connect to SAPRouter #{sap_host}:#{sap_port} - Connection Refused")
143+
144+
rescue ::Rex::ConnectionError, ::IOError, ::Timeout::Error
145+
rescue ::Rex::Post::Meterpreter::RequestError
146+
rescue ::Interrupt
147+
raise $!
148+
ensure
149+
disconnect(s) rescue nil
150+
end
160151
end
161-
162-
report.each { |res| report_service(:host => res[0], :port => res[1], :state => res[2]) }
163152
end
153+
thread.each { |x| x.join }
154+
155+
rescue ::Timeout::Error
156+
ensure
157+
thread.each { |x| x.kill rescue nil }
164158
end
165159

166160
end

0 commit comments

Comments
 (0)