Class: PassiveDNS::PassiveTotal

Inherits:
PassiveDB show all
Defined in:
lib/passivedns/client/passivetotal.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PassiveTotal

Returns a new instance of PassiveTotal.



23
24
25
26
27
# File 'lib/passivedns/client/passivetotal.rb', line 23

def initialize(options={})
    @debug = options[:debug] || false
    @apikey = options["APIKEY"] || raise("#{self.class.name} requires an APIKEY")
    @url = options["URL"] || "https://www.passivetotal.org/api/passive"
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



22
23
24
# File 'lib/passivedns/client/passivetotal.rb', line 22

def debug
  @debug
end

Class Method Details

.config_section_nameObject

override



14
15
16
# File 'lib/passivedns/client/passivetotal.rb', line 14

def self.config_section_name
  "passivetotal"
end

.nameObject

override



10
11
12
# File 'lib/passivedns/client/passivetotal.rb', line 10

def self.name
  "PassiveTotal"
end

.option_letterObject

override



18
19
20
# File 'lib/passivedns/client/passivetotal.rb', line 18

def self.option_letter
  "p"
end

Instance Method Details

#lookup(label, limit = nil) ⇒ Object



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/passivedns/client/passivetotal.rb', line 50

def lookup(label, limit=nil)
  $stderr.puts "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
  Timeout::timeout(240) {
    url = @url
    $stderr.puts "DEBUG: #{self.class.name} url = #{url}" if @debug
    url = URI.parse url
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = (url.scheme == 'https')
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.verify_depth = 5
    request = Net::HTTP::Post.new(url.request_uri)
    request.add_field("User-Agent", "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}")
      request.set_form_data({"apikey" => @apikey, "value" => label})
    t1 = Time.now
    response = http.request(request)
    t2 = Time.now
    recs = parse_json(response.body, label, t2-t1)
    if limit
      recs[0,limit]
    else
      recs
    end
  }
rescue Timeout::Error => e
  $stderr.puts "#{self.class.name} lookup timed out: #{label}"
end

#parse_json(page, query, response_time = 0) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/passivedns/client/passivetotal.rb', line 29

def parse_json(page,query,response_time=0)
    res = []
  # need to remove the json_class tag or the parser will crap itself trying to find a class to align it to
  data = JSON.parse(page)
  if data['results']
      query = data['results']['value']
    data['results']['resolutions'].each do |row|
        first_seen = row['firstSeen']
        last_seen = row['lastSeen']
        value = row['value']
        source = row['source'].join(",")
      res << PDNSResult.new(self.class.name+"/"+source,response_time,
          query, value, "A", 0, first_seen, last_seen)
    end
  end
  res
rescue Exception => e
  $stderr.puts "#{self.class.name} Exception: #{e}"
  raise e
end