Class: Watobo::Scanner2

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/watobo/core/scanner.rb

Overview

:nodoc: all

Constant Summary

Constants included from Constants

Constants::AC_GROUP_APACHE, Constants::AC_GROUP_DOMINO, Constants::AC_GROUP_ENUMERATION, Constants::AC_GROUP_FILE_INCLUSION, Constants::AC_GROUP_FLASH, Constants::AC_GROUP_GENERIC, Constants::AC_GROUP_JBOSS, Constants::AC_GROUP_JOOMLA, Constants::AC_GROUP_SAP, Constants::AC_GROUP_SQL, Constants::AC_GROUP_TYPO3, Constants::AC_GROUP_XSS, Constants::AUTH_TYPE_BASIC, Constants::AUTH_TYPE_DIGEST, Constants::AUTH_TYPE_NONE, Constants::AUTH_TYPE_NTLM, Constants::AUTH_TYPE_UNKNOWN, Constants::CHAT_SOURCE_AUTO_SCAN, Constants::CHAT_SOURCE_FUZZER, Constants::CHAT_SOURCE_INTERCEPT, Constants::CHAT_SOURCE_MANUAL, Constants::CHAT_SOURCE_MANUAL_SCAN, Constants::CHAT_SOURCE_PROXY, Constants::CHAT_SOURCE_UNDEF, Constants::DEFAULT_PORT_HTTP, Constants::DEFAULT_PORT_HTTPS, Constants::FINDING_TYPE_HINT, Constants::FINDING_TYPE_INFO, Constants::FINDING_TYPE_UNDEFINED, Constants::FINDING_TYPE_VULN, Constants::FIRST_TIME_FILE, Constants::GUI_REGULAR_FONT_SIZE, Constants::GUI_SMALL_FONT_SIZE, Constants::ICON_PATH, Constants::LOG_DEBUG, Constants::LOG_INFO, Constants::SCAN_CANCELED, Constants::SCAN_FINISHED, Constants::SCAN_PAUSED, Constants::SCAN_STARTED, Constants::TE_CHUNKED, Constants::TE_COMPRESS, Constants::TE_DEFLATE, Constants::TE_GZIP, Constants::TE_IDENTITY, Constants::TE_NONE, Constants::VULN_RATING_CRITICAL, Constants::VULN_RATING_HIGH, Constants::VULN_RATING_INFO, Constants::VULN_RATING_LOW, Constants::VULN_RATING_MEDIUM, Constants::VULN_RATING_UNDEFINED

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chat_list = [], active_checks = [], passive_checks = [], prefs = {}) ⇒ Scanner2

Returns a new instance of Scanner2.



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/watobo/core/scanner.rb', line 265

def initialize(chat_list=[], active_checks=[], passive_checks=[], prefs={})
  # @project = project        # needed for centralized session management

  @numTotalChecks = 0
  @numChecksPerModule = Hash.new
  @chat_list = chat_list
  @active_checks = active_checks
  @passive_checks = passive_checks

  @login_in_progress = false
  @login_mutex = Mutex.new
  @login_cv = ConditionVariable.new

  @filtered_chats = []

  #  @thread_list = Hash.new
  @scan_pool = []
  @scan_pool_mutex = Mutex.new
  @scan_pool_cv = ConditionVariable.new

  @check_list = []

  @sites_online = Hash.new
  @event_dispatcher_listeners = Hash.new
  @status = :stopped

  # @onlineCheck = OnlineCheck.new(@project)
  msg = "Initializing Scanner ..."
  notify(:logger, LOG_INFO, msg)
  puts msg

  @prefs = {
    #:root_path => [],
    #:excluded_chats => [],
    :smart_scan => true,
    :excluded_parms => [],
    #:non_uniq_parms => [],
    :login_chat_ids => [],
    :auto_login => true,
    # :valid_sids => Hash.new,
    :sid_patterns => [],
    :run_passive_checks => false,
    :proxy => '',
    :scanlog_dir => '',
    :scan_session => Digest::MD5.hexdigest(Time.now.to_f.to_s),
    :check_online => true,
    :source => CHAT_SOURCE_UNDEF
  }

  @prefs.update prefs
  #puts "set up scanner"
  #puts @prefs[:login_chats]
  #puts @prefs[:logout_signatures]
  puts "= create scanner =" if $DEBUG
  puts @prefs.to_yaml if $DEBUG

  @filtered_chat_list = filteredChats(@chat_list, @prefs)
  puts "#ActiveModules: #{@active_checks.length}"

  @active_checks.uniq.each do |m|
    puts m.class
    check = m
    check = m.new(:dummy) if check.respond_to? :new
    check.resetCounters()
    # puts "* updating counters for module #{m.info[:check_name]}"
    #  puts "* updating #{@chat_list.length} chats"
    @filtered_chat_list.each do |chat|
      print "."
      check.updateCounters(chat, @prefs)
      puts "* [#{chat.id}] CheckCounter: #{check.info[:check_name]} - #{check.numChecks}" if $DEBUG
    end

    @numTotalChecks += check.numChecks
    cn = check.info[:check_name]
    # puts "+ add check: #{cn}"
    notify(:logger, LOG_INFO, "add check #{cn}")
    @numChecksPerModule[cn] = check.numChecks
  end
  msg = "Scanner Ready!"
  notify(:logger, LOG_INFO, msg)
  puts msg
end

Instance Attribute Details

#numChecksPerModuleObject (readonly)

Returns the value of attribute numChecksPerModule.



6
7
8
# File 'lib/watobo/core/scanner.rb', line 6

def numChecksPerModule
  @numChecksPerModule
end

#numTotalChecksObject (readonly)

Returns the value of attribute numTotalChecks.



5
6
7
# File 'lib/watobo/core/scanner.rb', line 5

def numTotalChecks
  @numTotalChecks
end

#progressObject (readonly)

Returns the value of attribute progress.



7
8
9
# File 'lib/watobo/core/scanner.rb', line 7

def progress
  @progress
end

Instance Method Details

#cancel(reason = "undef") ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/watobo/core/scanner.rb', line 46

def cancel(reason="undef")
  begin
    notify(:scanner_canceled, reason)
    #  puts "* cancel active checks" unless @check_list.empty?
    @status = :canceled
    @check_list.each do |check|
      check.kill
    end
    #@active_checks.each do |mod|
    #  mod.cancel()
    #end
  rescue => bang
    puts bang
    puts bang.backtrace if $DEBUG
  end
end

#clearEvents(event) ⇒ Object



15
16
17
# File 'lib/watobo/core/scanner.rb', line 15

def clearEvents(event)
  @event_dispatcher_listener[event].clear
end

#continueObject



156
157
158
159
160
161
162
# File 'lib/watobo/core/scanner.rb', line 156

def continue()
  puts "!!! Scan Running !!!"
  @active_checks.each do |mod|
    mod.run()
  end
  @status_running = true
end

#notify(event, *args) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/watobo/core/scanner.rb', line 19

def notify(event, *args)
  if @event_dispatcher_listeners[event]
    @event_dispatcher_listeners[event].each do |m|
      m.call(*args) if m.respond_to? :call
    end
  end
end

#run(check_prefs = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/watobo/core/scanner.rb', line 164

def run( check_prefs={} )
  @status_running = true
  @sites_online.clear
  @uniqueRequests = Hash.new
  @status = :running
  @check_list = []
  @login_count = 0
  @max_login_count = 20

  # counter for fired checks
  checks = 0
  puts "= run scan =" if $DEBUG
  puts check_prefs.to_yaml if $DEBUG
  msg = "\n[Scanner] Starting Scan ..."
  notify(:logger, LOG_INFO, msg )
  puts msg
  #scan_session = Time.now.to_i

  @active_checks.uniq.each do |mod|
    check = mod
    #check = mod.new(@prefs[:scan_session], @prefs ) if mod.respond_to? :new
    puts "* subscribe for logout" if $DEBUG
    check.subscribe(:logout) { |m|
      next if @login_count > @max_login_count or @prefs[:auto_login] == false
      if @login_mutex.try_lock
        begin
          m.waitLogin(true)
          Watobo.print_debug("LOGOUT DETECTED") if $DEBUG
          @login_count += 1
          m.runLogin(@prefs[:login_chats])

          m.waitLogin(false) if m
        rescue => bang
          Watobo.print_debug("Could not relogin") if $DEBUG
          puts bang
          puts bang.backtrace if $DEBUG
        ensure

        end
      @login_mutex.unlock
      end

    }

    puts "* subscribe for :check_finished" if $DEBUG
    check.clearEvents(:check_finished)

    check.subscribe(:check_finished) do |m, request, response|
    # update progress
      @check_count ||= 0
      @check_count += 1
      puts "CheckCount: #{@check_count}" if $DEBUG
      notify( :progress, m )
      unless @prefs[:scanlog_name].nil?            
          chat = Chat.new(request, response, :id => 0, :chat_source => @prefs[:chat_source])
          Watobo::DataStore.add_scan_log(chat, @prefs[:scanlog_name])            
      end
    end

    puts "* subscribe for :new_finding" if $DEBUG
    check.clearEvents(:new_finding)
    check.subscribe(:new_finding) do |f|
    #    p "* NEW FINDING"
    #   p f.details[:module]
      notify(:new_finding, f)
    end

  end

  tlist = []
  @filtered_chat_list.uniq.each do |chat|
   # puts "CHAT --> #{chat.id}"
    @active_checks.uniq.each do |mod|
    #  puts "MOD"
      print "---> #{mod.class}"
      # accept Class- and Check-Types
      check = mod

      # reset check counters and variables
      check.reset()
      if @prefs[:online_check] == false or siteAlive?(chat) then
        @check_list << Thread.new(check, chat, check_prefs){|m, c, p|
          begin
          m_name = m.class.to_s.gsub(/.*::/,'')
          notify(:module_started, m_name)
          m.run_checks(c,p)
          rescue => bang
            puts bang
            puts bang.backtrace
          end
          notify(:logger, LOG_INFO, "finished checks: #{m.class} on chat #{c.id}")
          notify(:module_finished, m)
        }
      end
    end
  end

  @check_list.each {|ct| ct.join }
  puts "*[#{self}] Scan Finished"
end

#running?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/watobo/core/scanner.rb', line 27

def running?()
  return true if @status == :running
  return false
end

#siteAlive?(chat) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/watobo/core/scanner.rb', line 63

def siteAlive?(chat)
  puts chat.class
  site = nil
  host = nil
  port = nil

  site = chat.request.site

  return @sites_online[site] if @sites_online.has_key?(site)

  if @prefs[:proxy].is_a? Hash
    Watobo.print_debug("Using Proxy","#{@prefs[:proxy].to_yaml}") if $DEBUG
    if @prefs[:proxy].has_key?(:name) and @prefs[:proxy].has_key?(:port)
      #  unless @prefs[:proxy][:name] == '' then
      puts "* testing proxy:"
      puts "#{@prefs[:proxy][:name]} (#{@prefs[:proxy][:host]}:#{@prefs[:proxy][:port]})"
      # print "* using forwarding proxy #{@project.settings[:proxy]}\r\n"
      use_proxy = true
      host = @prefs[:proxy][:host]
      port = @prefs[:proxy][:port]
    end
  else
    print "* check if site is alive (#{site}) ... "
  host = chat.request.host
  port = chat.request.port

  end

  return false if host.nil? or port.nil?

  begin
    tcp_socket = nil
    #  timeout(6) do

    tcp_socket = TCPSocket.new( host, port)
    tcp_socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, 1)
    tcp_socket.sync = true

    socket = tcp_socket

    if socket.class.to_s =~ /SSLSocket/
    socket.io.shutdown(2)
    else
    socket.shutdown(2)
    end
    socket.close
    print "[OK]\n"
    @sites_online[site] = true
    return true
  rescue Errno::ECONNREFUSED
    p "* connection refused (#{host}:#{port})"
  rescue Errno::ECONNRESET
    puts "* connection reset"
  rescue Errno::EHOSTUNREACH
    p "* host unreachable (#{host}:#{port})"

  rescue Timeout::Error
    p "* TimeOut (#{host}:#{port})\n"

  rescue Errno::ETIMEDOUT
    p "* TimeOut (#{host}:#{port})"

  rescue Errno::ENOTCONN
    puts "!!!ENOTCONN"
  rescue OpenSSL::SSL::SSLError
    p "* ssl error"
    socket = nil
    #  puts "!!! SSL-Error"
    print "E"
  rescue => bang
  #  puts host
  #  puts port
    puts bang
    puts bang.backtrace if $DEBUG
  end
  print "[FALSE]\n"
  @sites_online[site] = false
  return false
#        if @sites_online.has_key?(site) then
#          return @sites_online[site] ? true : false
#        end

#        if @onlineCheck.isOnline?(chat) then
#          puts "Site #{site} is online"
#          @sites_online[site] = true
#          return true
#        else
#    puts "Site #{site} is offline"
#          @sites_online[site] = false
#          return false
#        end
end

#stop(reason = "undef") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/watobo/core/scanner.rb', line 32

def stop(reason="undef")
  begin
    notify(:scanner_stopped, reason)
    @status = :stopped
    puts "* stopping #{@check_list.length} active checks"
    @check_list.each do |check|
      check.stop
    end
  rescue => bang
    puts bang
    puts bang.backtrace if $DEBUG
  end
end

#subscribe(event, &callback) ⇒ Object



11
12
13
# File 'lib/watobo/core/scanner.rb', line 11

def subscribe(event, &callback)
  (@event_dispatcher_listeners[event] ||= []) << callback
end