passivedns-client 2.1.12 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +43 -0
- data/README.md +80 -90
- data/bin/pdnstool +3 -1
- data/lib/passivedns/client/cli.rb +85 -110
- data/lib/passivedns/client/passivedb.rb +44 -9
- data/lib/passivedns/client/provider/circl.rb +54 -50
- data/lib/passivedns/client/provider/dnsdb.rb +65 -52
- data/lib/passivedns/client/provider/osc.rb +147 -0
- data/lib/passivedns/client/provider/passivetotal.rb +53 -49
- data/lib/passivedns/client/provider/virustotal.rb +68 -62
- data/lib/passivedns/client/state.rb +121 -130
- data/lib/passivedns/client/version.rb +3 -1
- data/lib/passivedns/client.rb +93 -91
- metadata +52 -49
- data/.gitignore +0 -18
- data/Gemfile +0 -4
- data/Rakefile +0 -20
- data/lib/passivedns/client/provider/bfk.rb +0 -109
- data/lib/passivedns/client/provider/cn360.rb +0 -122
- data/lib/passivedns/client/provider/mnemonic.rb +0 -112
- data/lib/passivedns/client/provider/riskiq.rb +0 -146
- data/lib/passivedns/client/provider/tcpiputils.rb +0 -129
- data/passivedns-client.gemspec +0 -31
- data/test/helper.rb +0 -6
- data/test/test_cli.rb +0 -234
- data/test/test_passivedns-client.rb +0 -262
|
@@ -1,24 +1,59 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
|
|
5
|
+
module PassiveDNS # :nodoc: don't document this
|
|
6
|
+
# raised when a passive DNS provider reports a failure, such as a bad API key or a rate limit
|
|
7
|
+
class ProviderError < StandardError; end
|
|
8
|
+
|
|
2
9
|
# abstract class that all PassiveDNS::Provider should subclass
|
|
3
10
|
class PassiveDB
|
|
11
|
+
@providers = []
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# all of the classes that subclass PassiveDB
|
|
15
|
+
attr_reader :providers
|
|
16
|
+
|
|
17
|
+
# registers each subclass as a provider
|
|
18
|
+
def inherited(subclass)
|
|
19
|
+
super
|
|
20
|
+
PassiveDB.providers << subclass
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# :debug enables verbose logging to standard output
|
|
25
|
+
# :timeout is the number of seconds to wait for a lookup
|
|
26
|
+
attr_accessor :debug, :timeout
|
|
27
|
+
|
|
4
28
|
# raises an exception that this should be implemented by the subclass
|
|
5
29
|
def self.name
|
|
6
|
-
raise
|
|
30
|
+
raise 'You should implement your own version of .name'
|
|
7
31
|
end
|
|
8
|
-
|
|
32
|
+
|
|
9
33
|
# raises an exception that this should be implemented by the subclass
|
|
10
34
|
def self.config_section_name
|
|
11
35
|
name
|
|
12
36
|
end
|
|
13
|
-
|
|
37
|
+
|
|
14
38
|
# raises an exception that this should be implemented by the subclass
|
|
15
39
|
def self.option_letter
|
|
16
|
-
raise
|
|
40
|
+
raise 'You should pick a unique letter to serve as your database option letter for the command line option -d'
|
|
17
41
|
end
|
|
18
|
-
|
|
42
|
+
|
|
19
43
|
# raises an exception that this should be implemented by the subclass
|
|
20
|
-
def lookup(
|
|
21
|
-
raise
|
|
44
|
+
def lookup(_label, _limit = nil)
|
|
45
|
+
raise 'You must implement the lookup function'
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
# returns the response when it is a success, otherwise raises a ProviderError that names the provider, the HTTP status
|
|
51
|
+
# and the first line of the body (providers report errors as plain text, HTML or JSON)
|
|
52
|
+
def check_response(response)
|
|
53
|
+
return response if response.is_a? Net::HTTPSuccess
|
|
54
|
+
|
|
55
|
+
detail = response.body.to_s.strip.lines.first.to_s.strip[0, 200]
|
|
56
|
+
raise ProviderError, "#{self.class.name}: HTTP #{response.code}#{": #{detail}" unless detail.empty?}"
|
|
22
57
|
end
|
|
23
58
|
end
|
|
24
|
-
end
|
|
59
|
+
end
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# DESCRIPTION: Module to query PassiveTotal's passive DNS repository
|
|
2
4
|
|
|
5
|
+
require 'json'
|
|
3
6
|
require 'net/http'
|
|
4
|
-
require 'net/https'
|
|
5
7
|
require 'openssl'
|
|
8
|
+
require 'time'
|
|
9
|
+
require 'timeout'
|
|
10
|
+
require 'uri'
|
|
6
11
|
|
|
7
|
-
module PassiveDNS
|
|
12
|
+
module PassiveDNS # :nodoc: don't document this
|
|
8
13
|
# The Provider module contains all the Passive DNS provider client code
|
|
9
14
|
module Provider
|
|
10
15
|
# Queries CIRCL.LU's passive DNS database
|
|
@@ -12,26 +17,29 @@ module PassiveDNS #:nodoc: don't document this
|
|
|
12
17
|
class Circl < PassiveDB
|
|
13
18
|
# Sets the modules self-reported name to "CIRCL"
|
|
14
19
|
def self.name
|
|
15
|
-
|
|
20
|
+
'CIRCL'
|
|
16
21
|
end
|
|
22
|
+
|
|
17
23
|
# Sets the configuration section name to "circl"
|
|
18
24
|
def self.config_section_name
|
|
19
|
-
|
|
25
|
+
'circl'
|
|
20
26
|
end
|
|
27
|
+
|
|
21
28
|
# Sets the command line database argument to "c"
|
|
22
29
|
def self.option_letter
|
|
23
|
-
|
|
30
|
+
'c'
|
|
24
31
|
end
|
|
25
|
-
|
|
32
|
+
|
|
26
33
|
# :debug enables verbose logging to standard output
|
|
27
34
|
attr_accessor :debug
|
|
35
|
+
|
|
28
36
|
# === Options
|
|
29
37
|
# * :debug Sets the debug flag for the module
|
|
30
38
|
# * "USERNAME" User name associated with your CIRCL account
|
|
31
39
|
# * "PASSWORD" Password associated with your CIRCL account
|
|
32
40
|
# * "AUTH_TOKEN" Authorization token associated with your CIRCL account
|
|
33
41
|
# * "URL" Alternate url for testing. Defaults to "https://www.circl.lu/pdns/query"
|
|
34
|
-
|
|
42
|
+
|
|
35
43
|
# You should either have a username+password or an authorization token to use this service
|
|
36
44
|
#
|
|
37
45
|
# === Example Instantiation
|
|
@@ -45,83 +53,79 @@ module PassiveDNS #:nodoc: don't document this
|
|
|
45
53
|
#
|
|
46
54
|
# PassiveDNS::Provider::CIRCL.new(options)
|
|
47
55
|
#
|
|
48
|
-
def initialize(
|
|
49
|
-
|
|
50
|
-
@
|
|
51
|
-
@
|
|
52
|
-
@
|
|
53
|
-
@
|
|
54
|
-
@
|
|
56
|
+
def initialize(config = {})
|
|
57
|
+
super()
|
|
58
|
+
@debug = config[:debug] || false
|
|
59
|
+
@timeout = config[:timeout] || 20
|
|
60
|
+
@username = config['USERNAME']
|
|
61
|
+
@password = config['PASSWORD']
|
|
62
|
+
@auth_token = config['AUTH_TOKEN']
|
|
63
|
+
@url = config['URL'] || 'https://www.circl.lu/pdns/query'
|
|
55
64
|
end
|
|
56
65
|
|
|
57
66
|
# Takes a label (either a domain or an IP address) and returns
|
|
58
67
|
# an array of PassiveDNS::PDNSResult instances with the answers to the query
|
|
59
|
-
def lookup(label, limit=nil)
|
|
60
|
-
|
|
68
|
+
def lookup(label, limit = nil)
|
|
69
|
+
warn "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
|
|
61
70
|
recs = []
|
|
62
|
-
Timeout
|
|
63
|
-
url = @url
|
|
64
|
-
|
|
71
|
+
Timeout.timeout @timeout do
|
|
72
|
+
url = "#{@url}/#{label}"
|
|
73
|
+
warn "DEBUG: #{self.class.name} url = #{url}" if @debug
|
|
65
74
|
begin
|
|
66
75
|
url = URI.parse url
|
|
67
76
|
rescue URI::InvalidURIError
|
|
68
|
-
|
|
77
|
+
warn "ERROR: Invalid address: #{url}"
|
|
69
78
|
return recs
|
|
70
79
|
end
|
|
71
|
-
http = Net::HTTP.new
|
|
80
|
+
http = Net::HTTP.new url.host, url.port
|
|
72
81
|
http.use_ssl = (url.scheme == 'https')
|
|
73
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
74
82
|
http.verify_depth = 5
|
|
75
|
-
request = Net::HTTP::Get.new
|
|
76
|
-
request.add_field
|
|
77
|
-
if @username
|
|
78
|
-
|
|
79
|
-
end
|
|
80
|
-
if @auth_token
|
|
81
|
-
request.add_field("Authorization", @auth_token)
|
|
82
|
-
end
|
|
83
|
+
request = Net::HTTP::Get.new url.request_uri
|
|
84
|
+
request.add_field 'User-Agent', "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}"
|
|
85
|
+
request.basic_auth @username, @password if @username
|
|
86
|
+
request.add_field 'Authorization', @auth_token if @auth_token
|
|
83
87
|
t1 = Time.now
|
|
84
|
-
0.upto
|
|
85
|
-
response = http.request
|
|
88
|
+
0.upto 9 do
|
|
89
|
+
response = http.request request
|
|
86
90
|
body = response.body
|
|
87
|
-
if body ==
|
|
88
|
-
|
|
91
|
+
if body == 'Rate Limit Exceeded'
|
|
92
|
+
warn "DEBUG: Rate Limit Exceeded. Retrying #{label}" if @debug
|
|
89
93
|
else
|
|
94
|
+
check_response response
|
|
90
95
|
t2 = Time.now
|
|
91
|
-
recs
|
|
96
|
+
recs = parse_json response.body, label, t2 - t1
|
|
92
97
|
break
|
|
93
98
|
end
|
|
94
99
|
end
|
|
95
100
|
if limit
|
|
96
|
-
recs[0,limit]
|
|
101
|
+
recs[0, limit]
|
|
97
102
|
else
|
|
98
103
|
recs
|
|
99
104
|
end
|
|
100
|
-
|
|
105
|
+
end
|
|
101
106
|
rescue Timeout::Error
|
|
102
|
-
|
|
107
|
+
warn "#{self.class.name} lookup timed out: #{label}"
|
|
103
108
|
recs
|
|
104
109
|
end
|
|
105
110
|
|
|
106
111
|
private
|
|
107
112
|
|
|
108
113
|
# parses the response of circl's JSON reply to generate an array of PDNSResult
|
|
109
|
-
def parse_json(page,
|
|
110
|
-
|
|
111
|
-
page.split(
|
|
112
|
-
row = JSON.parse
|
|
113
|
-
firstseen = Time.at
|
|
114
|
-
lastseen = Time.at
|
|
115
|
-
res << PDNSResult.new(self.class.name,response_time,
|
|
116
|
-
|
|
117
|
-
|
|
114
|
+
def parse_json(page, _query, response_time = 0)
|
|
115
|
+
res = []
|
|
116
|
+
page.split("\n").each do |line|
|
|
117
|
+
row = JSON.parse line
|
|
118
|
+
firstseen = Time.at row['time_first'].to_i
|
|
119
|
+
lastseen = Time.at row['time_last'].to_i
|
|
120
|
+
res << PDNSResult.new(self.class.name, response_time,
|
|
121
|
+
row['rrname'], row['rdata'], row['rrtype'], 0,
|
|
122
|
+
firstseen, lastseen, row['count'], 'yellow')
|
|
118
123
|
end
|
|
119
124
|
res
|
|
120
|
-
rescue
|
|
121
|
-
|
|
125
|
+
rescue StandardError => e
|
|
126
|
+
warn "#{self.class.name} Exception: #{e}"
|
|
122
127
|
raise e
|
|
123
128
|
end
|
|
124
|
-
|
|
125
129
|
end
|
|
126
130
|
CIRCL = PassiveDNS::Provider::Circl
|
|
127
131
|
end
|
|
@@ -1,29 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# DESCRIPTION: this is a module for pdns.rb, primarily used by pdnstool.rb, to query the Farsight Security passive DNS database
|
|
2
4
|
# details on the API are at https://api.dnsdb.info/
|
|
3
5
|
# to request an API key, please email dnsdb-api at farsightsecurity dot com.
|
|
6
|
+
require 'json'
|
|
4
7
|
require 'net/http'
|
|
5
|
-
require '
|
|
8
|
+
require 'openssl'
|
|
9
|
+
require 'time'
|
|
10
|
+
require 'timeout'
|
|
11
|
+
require 'uri'
|
|
6
12
|
|
|
7
|
-
module PassiveDNS
|
|
13
|
+
module PassiveDNS # :nodoc: don't document this
|
|
8
14
|
# The Provider module contains all the Passive DNS provider client code
|
|
9
15
|
module Provider
|
|
10
16
|
# Queries FarSight's passive DNS database
|
|
11
17
|
class DNSDB < PassiveDB
|
|
12
18
|
# Sets the modules self-reported name to "DNSDB"
|
|
13
19
|
def self.name
|
|
14
|
-
|
|
20
|
+
'DNSDB'
|
|
15
21
|
end
|
|
22
|
+
|
|
16
23
|
# Sets the configuration section name to "dnsdb"
|
|
17
24
|
def self.config_section_name
|
|
18
|
-
|
|
25
|
+
'dnsdb'
|
|
19
26
|
end
|
|
27
|
+
|
|
20
28
|
# Sets the command line database argument to "d"
|
|
21
29
|
def self.option_letter
|
|
22
|
-
|
|
30
|
+
'd'
|
|
23
31
|
end
|
|
24
|
-
|
|
32
|
+
|
|
25
33
|
# :debug enables verbose logging to standard output
|
|
26
34
|
attr_accessor :debug
|
|
35
|
+
|
|
27
36
|
# === Options
|
|
28
37
|
# * :debug Sets the debug flag for the module
|
|
29
38
|
# * "APIKEY" REQUIRED: The API key associated with DNSDB
|
|
@@ -39,81 +48,85 @@ module PassiveDNS #:nodoc: don't document this
|
|
|
39
48
|
#
|
|
40
49
|
# PassiveDNS::Provider::DNSDB.new(options)
|
|
41
50
|
#
|
|
42
|
-
def initialize(
|
|
43
|
-
|
|
44
|
-
@
|
|
45
|
-
@
|
|
46
|
-
@
|
|
51
|
+
def initialize(config = {})
|
|
52
|
+
super()
|
|
53
|
+
@debug = config[:debug] || false
|
|
54
|
+
@timeout = config[:timeout] || 20
|
|
55
|
+
@key = config['APIKEY'] || raise("APIKEY option required for #{self.class}")
|
|
56
|
+
@base = config['URL'] || 'https://api.dnsdb.info/lookup'
|
|
47
57
|
end
|
|
48
58
|
|
|
49
59
|
# Takes a label (either a domain or an IP address) and returns
|
|
50
60
|
# an array of PassiveDNS::PDNSResult instances with the answers to the query
|
|
51
|
-
def lookup(label, limit=nil)
|
|
52
|
-
|
|
53
|
-
Timeout
|
|
61
|
+
def lookup(label, limit = nil)
|
|
62
|
+
warn "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
|
|
63
|
+
Timeout.timeout @timeout do
|
|
54
64
|
url = nil
|
|
55
|
-
if
|
|
56
|
-
label = label.
|
|
65
|
+
if %r{^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(/\d{1,2})?$}.match? label
|
|
66
|
+
label = label.tr '/', ','
|
|
57
67
|
url = "#{@base}/rdata/ip/#{label}"
|
|
58
68
|
else
|
|
59
69
|
url = "#{@base}/rrset/name/#{label}"
|
|
60
70
|
end
|
|
61
71
|
url = URI.parse url
|
|
62
|
-
http = Net::HTTP.new
|
|
72
|
+
http = Net::HTTP.new url.host, url.port
|
|
63
73
|
http.use_ssl = (url.scheme == 'https')
|
|
64
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
65
74
|
http.verify_depth = 5
|
|
66
75
|
path = url.path
|
|
67
|
-
if limit
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
request
|
|
71
|
-
request.add_field
|
|
72
|
-
request.add_field("X-API-Key", @key)
|
|
73
|
-
request.add_field("Accept", "application/json")
|
|
76
|
+
path = "#{path}?limit=#{limit}" if limit
|
|
77
|
+
request = Net::HTTP::Get.new path
|
|
78
|
+
request.add_field 'User-Agent', "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}"
|
|
79
|
+
request.add_field 'X-API-Key', @key
|
|
80
|
+
request.add_field 'Accept', 'application/json'
|
|
74
81
|
t1 = Time.now
|
|
75
|
-
response = http.request
|
|
82
|
+
response = http.request request
|
|
83
|
+
if response.code.to_i == 404
|
|
84
|
+
warn 'DEBUG: empty response from server' if @debug
|
|
85
|
+
return
|
|
86
|
+
end
|
|
87
|
+
check_response response
|
|
76
88
|
t2 = Time.now
|
|
77
|
-
|
|
78
|
-
parse_json
|
|
79
|
-
|
|
89
|
+
warn response.body if @debug
|
|
90
|
+
parse_json response.body, t2 - t1
|
|
91
|
+
end
|
|
80
92
|
rescue Timeout::Error
|
|
81
|
-
|
|
93
|
+
warn "#{self.class.name} lookup timed out: #{label}"
|
|
82
94
|
end
|
|
83
|
-
|
|
95
|
+
|
|
84
96
|
private
|
|
85
|
-
|
|
97
|
+
|
|
86
98
|
# parses the response of DNSDB's JSON reply to generate an array of PDNSResult
|
|
87
|
-
def parse_json(page,response_time)
|
|
99
|
+
def parse_json(page, response_time)
|
|
88
100
|
res = []
|
|
89
|
-
raise
|
|
90
|
-
|
|
101
|
+
raise 'Error: unable to parse request' if page.include? 'Error: unable to parse request'
|
|
102
|
+
|
|
103
|
+
rows = page.split "\n"
|
|
91
104
|
rows.each do |row|
|
|
92
|
-
record = JSON.parse
|
|
105
|
+
record = JSON.parse row
|
|
93
106
|
answers = record['rdata']
|
|
94
|
-
answers = [record['rdata']] if record['rdata'].
|
|
95
|
-
query = record['rrname'].
|
|
107
|
+
answers = [record['rdata']] if record['rdata'].instance_of? String
|
|
108
|
+
query = record['rrname'].delete_suffix '.'
|
|
96
109
|
rrtype = record['rrtype']
|
|
97
|
-
firstseen = Time.at
|
|
98
|
-
lastseen = Time.at
|
|
110
|
+
firstseen = Time.at record['time_first'].to_i
|
|
111
|
+
lastseen = Time.at record['time_last'].to_i
|
|
99
112
|
count = record['count']
|
|
100
|
-
|
|
113
|
+
|
|
101
114
|
answers.each do |answer|
|
|
102
|
-
answer.
|
|
103
|
-
if record['time_first']
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
115
|
+
answer = answer.delete_suffix '.'
|
|
116
|
+
res << if record['time_first']
|
|
117
|
+
PDNSResult.new(self.class.name, response_time, query, answer, rrtype,
|
|
118
|
+
0, firstseen, lastseen, count, 'yellow')
|
|
119
|
+
else
|
|
120
|
+
PDNSResult.new self.class.name, response_time, query, answer, rrtype
|
|
121
|
+
end
|
|
109
122
|
end
|
|
110
123
|
end
|
|
111
124
|
res
|
|
112
|
-
rescue
|
|
113
|
-
|
|
114
|
-
|
|
125
|
+
rescue StandardError => e
|
|
126
|
+
warn "#{self.class.name} Exception: #{e}"
|
|
127
|
+
warn page
|
|
115
128
|
raise e
|
|
116
129
|
end
|
|
117
|
-
end
|
|
130
|
+
end
|
|
118
131
|
end
|
|
119
132
|
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# DESCRIPTION: this is a module for Open Source Context's PassiveDNS archive.
|
|
4
|
+
require 'json'
|
|
5
|
+
require 'net/http'
|
|
6
|
+
require 'openssl'
|
|
7
|
+
require 'time'
|
|
8
|
+
require 'timeout'
|
|
9
|
+
require 'uri'
|
|
10
|
+
|
|
11
|
+
module PassiveDNS # :nodoc: don't document this
|
|
12
|
+
# The Provider module contains all the Passive DNS provider client code
|
|
13
|
+
module Provider
|
|
14
|
+
# Queries OSContext's passive DNS database
|
|
15
|
+
class OSC < PassiveDB
|
|
16
|
+
# Sets the modules self-reported name to "OSC"
|
|
17
|
+
def self.name
|
|
18
|
+
'OSC'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Sets the configuration section name to "osc"
|
|
22
|
+
def self.config_section_name
|
|
23
|
+
'osc'
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Sets the command line database argument to "d"
|
|
27
|
+
def self.option_letter
|
|
28
|
+
'o'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# :debug enables verbose logging to standard output
|
|
32
|
+
attr_accessor :debug
|
|
33
|
+
|
|
34
|
+
# === Options
|
|
35
|
+
# * :debug Sets the debug flag for the module
|
|
36
|
+
# * "APIKEY" REQUIRED: The API key associated with OSC
|
|
37
|
+
# * "URL" Alternate url for testing. Defaults to "https://api.oscontext.com/api/v2/domainsquery"
|
|
38
|
+
#
|
|
39
|
+
# === Example Instantiation
|
|
40
|
+
#
|
|
41
|
+
# options = {
|
|
42
|
+
# :debug => true,
|
|
43
|
+
# "APIKEY" => "0123456789abcdef0123456789abcdef01234567",
|
|
44
|
+
# "URL" => "https://api.oscontext.com/api/v2/domainsquery"
|
|
45
|
+
# }
|
|
46
|
+
#
|
|
47
|
+
# PassiveDNS::Provider::OSC.new(options)
|
|
48
|
+
#
|
|
49
|
+
def initialize(config = {})
|
|
50
|
+
super()
|
|
51
|
+
@debug = config[:debug] || false
|
|
52
|
+
@timeout = config[:timeout] || 20
|
|
53
|
+
@token = config['APIKEY'] || raise("APIKEY option required for #{self.class}")
|
|
54
|
+
@url = config['URL'] || 'https://api.oscontext.com/api/v2/domainsquery'
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Takes a label (either a domain or an IP address) and returns
|
|
58
|
+
# an array of PassiveDNS::PDNSResult instances with the answers to the query
|
|
59
|
+
def lookup(label, _limit = nil)
|
|
60
|
+
warn "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
|
|
61
|
+
Timeout.timeout @timeout do
|
|
62
|
+
url = if %r{^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(/\d{1,2})?$}.match? label
|
|
63
|
+
@url + "?q=value_ip:#{label}&size=250&token=#{@token}"
|
|
64
|
+
else
|
|
65
|
+
@url + "?q=qname%3A#{label}&size=250&token=#{@token}"
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
url = URI.parse url
|
|
69
|
+
|
|
70
|
+
warn "--DEBUG: #{self.class.name} url = #{url}" if @debug
|
|
71
|
+
|
|
72
|
+
http = Net::HTTP.new url.host, url.port
|
|
73
|
+
http.use_ssl = (url.scheme == 'https')
|
|
74
|
+
http.verify_depth = 5
|
|
75
|
+
|
|
76
|
+
request = Net::HTTP::Get.new url
|
|
77
|
+
request.add_field 'content-type', 'application/x-www-form-urlencoded'
|
|
78
|
+
request.add_field 'referer', 'clitool'
|
|
79
|
+
t1 = Time.now
|
|
80
|
+
response = http.request request
|
|
81
|
+
if response.code.to_i == 404
|
|
82
|
+
warn 'DEBUG: empty response from server' if @debug
|
|
83
|
+
return
|
|
84
|
+
end
|
|
85
|
+
check_response response
|
|
86
|
+
t2 = Time.now
|
|
87
|
+
# $stderr.puts response.body if @debug
|
|
88
|
+
parse_json response.body, t2 - t1
|
|
89
|
+
end
|
|
90
|
+
rescue Timeout::Error
|
|
91
|
+
warn "#{self.class.name} lookup timed out: #{label}"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
private
|
|
95
|
+
|
|
96
|
+
# parses the response of OSC's JSON reply to generate an array of PDNSResult
|
|
97
|
+
def parse_json(page, response_time)
|
|
98
|
+
res = []
|
|
99
|
+
raise 'Error: unable to parse request' if page.include? 'Error: unable to parse request'
|
|
100
|
+
|
|
101
|
+
data = JSON.parse page
|
|
102
|
+
data['results']&.each do |row|
|
|
103
|
+
if row['qtype'].to_i == 1
|
|
104
|
+
firstseen = Time.parse row['date']
|
|
105
|
+
lastseen = (Time.parse row['last_seen'] if row['last_seen'])
|
|
106
|
+
res << PDNSResult.new(
|
|
107
|
+
self.class.name,
|
|
108
|
+
response_time,
|
|
109
|
+
row['domain'],
|
|
110
|
+
row['value'],
|
|
111
|
+
'A',
|
|
112
|
+
nil,
|
|
113
|
+
firstseen,
|
|
114
|
+
lastseen,
|
|
115
|
+
nil,
|
|
116
|
+
'amber'
|
|
117
|
+
)
|
|
118
|
+
elsif row['type'] == 'soa_email'
|
|
119
|
+
firstseen = Time.parse row['date']
|
|
120
|
+
|
|
121
|
+
lastseen = (Time.parse row['last_seen'] if row['last_seen'])
|
|
122
|
+
|
|
123
|
+
res << PDNSResult.new(
|
|
124
|
+
self.class.name,
|
|
125
|
+
response_time,
|
|
126
|
+
row['domain'],
|
|
127
|
+
row['value'],
|
|
128
|
+
'SOA',
|
|
129
|
+
nil,
|
|
130
|
+
firstseen,
|
|
131
|
+
lastseen,
|
|
132
|
+
nil,
|
|
133
|
+
'amber'
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
res
|
|
140
|
+
rescue StandardError => e
|
|
141
|
+
warn "#{self.class.name} Exception: #{e}"
|
|
142
|
+
# $stderr.puts page
|
|
143
|
+
raise e
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|