Class: Aquatone::Collectors::Censys

Inherits:
Aquatone::Collector show all
Defined in:
lib/aquatone/collectors/censys.rb

Constant Summary collapse

API_BASE_URI =
"https://www.censys.io/api/v1".freeze
API_RESULTS_PER_PAGE =
100.freeze
PAGES_TO_PROCESS =
10.freeze

Constants inherited from Aquatone::Collector

Aquatone::Collector::DEFAULT_PRIORITY

Instance Attribute Summary

Attributes inherited from Aquatone::Collector

#domain, #hosts

Instance Method Summary collapse

Methods inherited from Aquatone::Collector

descendants, #execute!, #initialize, meta, meta=, priority, sluggified_name

Constructor Details

This class inherits a constructor from Aquatone::Collector

Instance Method Details

#next_page?(page, body) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/aquatone/collectors/censys.rb', line 77

def next_page?(page, body)
    page <= PAGES_TO_PROCESS && body["metadata"]["pages"] && API_RESULTS_PER_PAGE * page < body["metadata"]["count"].to_i
end

#request_censys_page(page = 1) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/aquatone/collectors/censys.rb', line 19

def request_censys_page(page=1)
    # Initial version only supporting Censys Certificates API

    # Censys expects Basic Auth for requests.
    auth = {
        :username => get_key('censys_id'), 
        :password => get_key('censys_secret')
    }
   
    # Define this is JSON content
    headers = {
      'Content-Type' => 'application/json',
      'Accept' => 'application/json'
    }

    # The post body itself, as JSON
    query = {
        'query'   => url_escape("#{domain.name}"),
        'page'    => page,
        'fields'  => [ "parsed.names", "parsed.extensions.subject_alt_name.dns_names" ],
        'flatten' => true
    }

    # Search API documented at https://censys.io/api/v1/docs/search
    response = post_request(
        "#{API_BASE_URI}/search/certificates", 
        query.to_json,
        {
            :basic_auth => auth,
            :headers => headers 
        }
    )

    if response.code != 200
        failure(response.parsed_response["error"] || "Censys API encountered error: #{response.code}")
    end

    # If nothing returned from Censys, return:
    return unless response.parsed_response["results"]

    response.parsed_response["results"].each do |result|

      next unless result["parsed.extensions.subject_alt_name.dns_names"]
      result["parsed.extensions.subject_alt_name.dns_names"].each do |altdns|
          add_host(altdns) if altdns.end_with?(".#{domain.name}")
      end

      next unless result["parsed.names"]
      result["parsed.names"].each do |parsedname|
          add_host(parsedname) if parsedname.end_with?(".#{domain.name}")
      end
    end

    # Get the next page of results
    request_censys_page(page + 1) if next_page?(page, response.parsed_response)

end

#runObject



15
16
17
# File 'lib/aquatone/collectors/censys.rb', line 15

def run
  request_censys_page
end