class Aquatone::Collectors::Shodan

Constants

API_BASE_URI
API_RESULTS_PER_PAGE
DEFAULT_PAGES_TO_PROCESS

Public Instance Methods

run() click to toggle source
# File lib/aquatone/collectors/shodan.rb, line 18
def run
  request_shodan_page
end

Private Instance Methods

construct_uri(query, page) click to toggle source
# File lib/aquatone/collectors/shodan.rb, line 39
def construct_uri(query, page)
  "#{API_BASE_URI}/host/search?query=#{url_escape(query)}&page=#{page}&key=#{get_key('shodan')}"
end
next_page?(page, body) click to toggle source
# File lib/aquatone/collectors/shodan.rb, line 43
def next_page?(page, body)
  page <= pages_to_process && body["total"] && API_RESULTS_PER_PAGE * page < body["total"].to_i
end
pages_to_process() click to toggle source
# File lib/aquatone/collectors/shodan.rb, line 47
def pages_to_process
  if has_cli_option?("shodan-pages")
    return get_cli_option("shodan-pages").to_i
  end
  DEFAULT_PAGES_TO_PROCESS
end
request_shodan_page(page=1) click to toggle source
# File lib/aquatone/collectors/shodan.rb, line 24
def request_shodan_page(page=1)
  response = get_request(construct_uri("hostname:#{domain.name}", page))
  if response.code != 200
    failure(response.parsed_response["error"] || "Shodan API returned unexpected response code: #{response.code}")
  end
  return unless response.parsed_response["matches"]
  response.parsed_response["matches"].each do |match|
    next unless match["hostnames"]
    match["hostnames"].each do |hostname|
      add_host(hostname) if hostname.end_with?(".#{domain.name}")
    end
  end
  request_shodan_page(page + 1) if next_page?(page, response.parsed_response)
end