@@ -10,7 +10,9 @@ class Metasploit3 < Msf::Auxiliary
10
10
def initialize
11
11
super (
12
12
'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} ,
14
16
'Author' => [ 'Bruno Morisson <bm[at]integrity.pt>' , # metasploit module
15
17
'nmonkee' ] , # saprouter packet building code from sapcat.rb
16
18
'References' =>
@@ -65,34 +67,32 @@ def build_ni_packet(routes)
65
67
end
66
68
67
69
def parse_response_packet ( response , ip , port )
68
- report = [ ]
69
70
70
71
vprint_error ( "#{ ip } :#{ port } - response packet: #{ response } " )
71
72
72
73
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" )
92
87
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" )
94
95
end
95
- report
96
96
97
97
end
98
98
@@ -110,57 +110,51 @@ def run_host(ip)
110
110
end
111
111
112
112
print_status ( "Scanning #{ ip } " )
113
+ thread = [ ]
114
+ ports . each do |port |
113
115
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 }
156
116
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
160
151
end
161
-
162
- report . each { |res | report_service ( :host => res [ 0 ] , :port => res [ 1 ] , :state => res [ 2 ] ) }
163
152
end
153
+ thread . each { |x | x . join }
154
+
155
+ rescue ::Timeout ::Error
156
+ ensure
157
+ thread . each { |x | x . kill rescue nil }
164
158
end
165
159
166
160
end
0 commit comments