Class: Watobo::Scanner3

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

Overview

:nodoc: all

Defined Under Namespace

Classes: Worker

Constant Summary collapse

SCANNER_READY =
0x0000
SCANNER_RUNNING =
0x0001
GENERATION_STARTED =
0x0100
GENERATION_FINISHED =
0x0200

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 Method Summary collapse

Methods included from Subscriber

#clearEvents, #notify, #subscribe

Constructor Details

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

Returns a new instance of Scanner3.



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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/watobo/core/scanner3.rb', line 319

def initialize(chat_list=[], active_checks=[], passive_checks=[], prefs={})
  @chat_list = chat_list
  @active_checks = []
  @passive_checks = passive_checks

  @tasks = Queue.new
  @logged_out = Queue.new

  @workers = []

  @status_lock = Mutex.new

  @task_count_lock = Mutex.new
  @task_counter = {}
  
  @ctrl_thread = nil

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

  @prefs = Watobo::Conf::Scanner.to_h

  @prefs.update prefs

  puts @prefs.to_yaml

  unique_checks = {}
  active_checks.each do  |x|
    if x.respond_to? :new
    ac = x.new(self.object_id, @prefs)
    else
    ac = x
    end
    unique_checks[ac.class.to_s] = ac unless unique_checks.has_key?(ac.class.to_s)
  end
  unique_checks.each_value do |check|
    @active_checks << check
  end

  puts "#ActiveModules: #{@active_checks.length}"

  @active_checks.uniq.each do |check|

    check.resetCounters()
    @chat_list.each_with_index do |chat, index|
    #print "."
      check.updateCounters(chat, @prefs)
      puts "* [#{index}] CheckCounter #{chat.id}: #{check.check_name} - #{check.numChecks}"
    end

    # @numTotalChecks += check.numChecks
    # cn = check.info[:check_name]
    # puts "+ add check: #{cn}"
    # notify(:logger, LOG_INFO, "add check #{cn}")
    @task_counter[check.check_name] = { :total => check.numChecks,
      :progress => 0
    }
  end
  @status = SCANNER_READY
  msg = "Scanner Ready!"
  notify(:logger, LOG_INFO, msg)
  puts msg
end

Instance Method Details

#cancelObject



192
193
194
195
196
197
198
199
200
201
# File 'lib/watobo/core/scanner3.rb', line 192

def cancel()
  begin
    @workers.each do |w|
      w.stop
    end
  rescue => bang
    puts bang
    puts bang.backtrace if $DEBUG
  end
end

#continueObject



203
204
205
# File 'lib/watobo/core/scanner3.rb', line 203

def continue()
  # TODO
end

#finished?Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/watobo/core/scanner3.rb', line 145

def finished?
  # puts "num_waiting: #{@tasks.num_waiting}"
  # puts "workers: #{@workers.length}"
  # puts "generation finished? #{generation_finished?.class}"
  # puts "num tasks: #{@tasks.size}"
  # puts "running workers: #{running_workers}"
  return true if (
  status_running? &&
  ( @tasks.num_waiting == @workers.length ) &&
  ( @tasks.size == 0 ) &&
  generation_finished?
  )
  false
end

#generation_finished?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/watobo/core/scanner3.rb', line 141

def generation_finished?
  ( status & GENERATION_FINISHED ) > 0
end

#progressObject



207
208
209
210
211
# File 'lib/watobo/core/scanner3.rb', line 207

def progress
  @task_count_lock.synchronize do
    YAML.load(YAML.dump(@task_counter))
  end
end

#run(check_prefs = {}) ⇒ Object



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
264
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
# File 'lib/watobo/core/scanner3.rb', line 229

def run( check_prefs={} )
  # @sites_online.clear
  @uniqueRequests = Hash.new
  set_status_running

  @login_count = 0
  @max_login_count = 20

  @ctrl_thread = Thread.new{
    size = -1
    loop do
      if @tasks.num_waiting == @workers.length and @tasks.size == 0 and generation_finished?
        begin
        puts "[#{self}] seems scan is finished. stopping workers now ..."
        @workers.map{|w|
          #puts "[]#{self}] stopping worker #{w}"
          w.stop
          }
        # suizide!
        Thread.exit
        rescue => bang
          puts bang
          puts bang.backtrace
        end
      end

      if @logged_out.size == ( @workers.length - @tasks.num_waiting) or @tasks.num_waiting == @workers.size
        @logged_out.clear
        #puts "!LOGOUT DETECTED!\n#{@logged_out.size} - #{@workers.length} - #{@tasks.num_waiting}\n\n"
        begin
          puts "Run login ..."
          
          @workers.each do |wrkr|
           # puts "State: #{wrkr.state}"
            if wrkr.wait_for_login?
              wrkr.engine.run
            end
          end

        rescue => bang
          puts bang
          puts bang.backtrace
        end

      end

      sleep 1
    end
  }

  @prefs.update check_prefs
  msg = "\n[Scanner] Starting Scan ..."
  notify(:logger, LOG_INFO, msg )
  puts msg

  # starting workers before check generation
  start_workers( @prefs)
  @max_tasks = 1000

  # start check generation in seperate thread
  Thread.new{
    begin
      set_status GENERATION_STARTED
      @chat_list.uniq.each do |chat|
      # puts chat.request.url.to_s
        @active_checks.uniq.each do |ac|
          ac.reset()
          if site_alive?(chat) then
            ac.generateChecks(chat){ |check|
              while @tasks.size > @max_tasks
                sleep 1
              end
              task = { :module => ac,
                :check => check
              }
              @tasks.push task
            }
          end
        end
      end
    rescue => bang
      puts bang
      puts bang.backtrace if $DEBUG
    ensure
      set_status GENERATION_FINISHED
    end
  }

end

#running?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
167
168
169
170
# File 'lib/watobo/core/scanner3.rb', line 160

def running?()

  return false if (
  status_running? &&
  ( @tasks.num_waiting == @workers.length ) &&
  ( @tasks.size == 0 ) &&
  generation_finished?
  )
  return true if status_running?
  return false
end

#status_running?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/watobo/core/scanner3.rb', line 137

def status_running?
  ( status & SCANNER_RUNNING ) > 0
end

#stopObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/watobo/core/scanner3.rb', line 172

def stop()
  print "\n[#{self}] stopping ... "
  begin
    @workers.each do |w|
      w.stop
    end
    unless @ctrl_thread.nil?
      if @ctrl_thread.alive?
        puts "stop ctrl_thread"
        Thread.kill @ctrl_thread
      end
    end
    print "[OK]\n"
  rescue => bang
    print "[OUTCH]\n"
    puts bang
    puts bang.backtrace if $DEBUG
  end
end

#sum_progressObject



221
222
223
224
225
226
227
# File 'lib/watobo/core/scanner3.rb', line 221

def sum_progress
  sum = 0
  @task_count_lock.synchronize do
    sum = @task_counter.values.inject(0){|i,v| i + v[:progress] }
  end
  sum
end

#sum_totalObject



213
214
215
216
217
218
219
# File 'lib/watobo/core/scanner3.rb', line 213

def sum_total
  sum = 0
  @task_count_lock.synchronize do
    sum = @task_counter.values.inject(0){|i,v| i + v[:total] }
  end
  sum
end

#tasksObject

E N D O F W O R K E R



133
134
135
# File 'lib/watobo/core/scanner3.rb', line 133

def tasks
  @tasks
end