Class: HttpScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/http_scanner.rb,
lib/http_scanner/version.rb

Constant Summary collapse

VERSION =
'0.2.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttpScanner

Returns a new instance of HttpScanner.



11
12
13
14
15
# File 'lib/http_scanner.rb', line 11

def initialize
  @mutex = Mutex.new
  @queue = Queue.new
  @results = []
end

Instance Attribute Details

#loggerObject



17
18
19
20
21
# File 'lib/http_scanner.rb', line 17

def logger
  @logger ||= Logger.new(STDERR).tap do |logger|
    logger.progname = 'http_scanner'
  end
end

Instance Method Details

#scan(signature, opts = {}) ⇒ Array

Scans the local network and returns an array of IP addresses of systems that return text containing the signature.

Parameters:

  • signature (String)

    Case sensitive string to be searched for in the HTML of scanned systems.

  • opts (Hash) (defaults to: {})

    Additional options.

Options Hash (opts):

  • :threads (Fixnum)

    Size of the thread pool to use to scan the local network. Defaults to 255 threads.

Returns:

  • (Array)

    IP addresses of systems with a positive match.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/http_scanner.rb', line 32

def scan(signature, opts = {})
  ip_range = local_ip_range
  addresses = get_ips_in_range(ip_range[:ip_start], ip_range[:ip_end])
  addresses.each do |address|
    @queue << address
  end
  threads = []

  (opts[:threads] || 255).times do
    threads << Thread.new do
      thread_results = scan_thread(@queue, signature)
      @mutex.synchronize do
        @results.concat(thread_results)
      end
    end
  end

  threads.each do |thread|
    begin
      thread.join
    rescue => e
      logger.error "Caught error in thread:"
      logger.error e
    end
  end
  @results
end