passivedns-client 2.1.13 → 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/LICENSE.txt +0 -0
- data/README.md +51 -43
- data/bin/pdnstool +3 -1
- data/lib/passivedns/client/cli.rb +85 -105
- 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 -89
- metadata +52 -45
- data/.gitignore +0 -18
- data/Gemfile +0 -4
- data/Rakefile +0 -20
- data/lib/passivedns/client/provider/riskiq.rb +0 -146
- data/passivedns-client.gemspec +0 -31
- data/test/helper.rb +0 -6
- data/test/test_cli.rb +0 -219
- data/test/test_passivedns-client.rb +0 -162
|
@@ -1,119 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
# DESCRIPTION: this is a module for pdns.rb, primarily used by pdnstool.rb, to query VirusTotal's passive DNS database
|
|
4
|
+
require 'json'
|
|
2
5
|
require 'net/http'
|
|
3
|
-
require 'net/https'
|
|
4
6
|
require 'openssl'
|
|
7
|
+
require 'time'
|
|
8
|
+
require 'timeout'
|
|
9
|
+
require 'uri'
|
|
5
10
|
|
|
6
|
-
module PassiveDNS
|
|
11
|
+
module PassiveDNS # :nodoc: don't document this
|
|
7
12
|
# The Provider module contains all the Passive DNS provider client code
|
|
8
13
|
module Provider
|
|
9
14
|
# Queries VirusTotal's passive DNS database
|
|
10
15
|
class VirusTotal < PassiveDB
|
|
11
16
|
# Sets the modules self-reported name to "VirusTotal"
|
|
12
17
|
def self.name
|
|
13
|
-
|
|
18
|
+
'VirusTotal'
|
|
14
19
|
end
|
|
20
|
+
|
|
15
21
|
# Sets the configuration section name to "virustotal"
|
|
16
22
|
def self.config_section_name
|
|
17
|
-
|
|
23
|
+
'virustotal'
|
|
18
24
|
end
|
|
25
|
+
|
|
19
26
|
# Sets the command line database argument to "v"
|
|
20
27
|
def self.option_letter
|
|
21
|
-
|
|
28
|
+
'v'
|
|
22
29
|
end
|
|
23
|
-
|
|
30
|
+
|
|
24
31
|
# :debug enables verbose logging to standard output
|
|
25
32
|
attr_accessor :debug
|
|
26
|
-
|
|
33
|
+
|
|
27
34
|
# === Options
|
|
28
35
|
# * :debug Sets the debug flag for the module
|
|
29
36
|
# * "APIKEY" Mandatory. API Key associated with your VirusTotal account
|
|
30
|
-
# * "URL" Alternate url for testing. Defaults to https://www.virustotal.com/
|
|
37
|
+
# * "URL" Alternate url for testing. Defaults to https://www.virustotal.com/api/v3/
|
|
31
38
|
#
|
|
32
39
|
# === Example Instantiation
|
|
33
40
|
#
|
|
34
41
|
# options = {
|
|
35
42
|
# :debug => true,
|
|
36
43
|
# "APIKEY" => "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
|
37
|
-
# "URL" => "https://www.virustotal.com/
|
|
44
|
+
# "URL" => "https://www.virustotal.com/api/v3/"
|
|
38
45
|
# }
|
|
39
46
|
#
|
|
40
47
|
# PassiveDNS::Provider::VirusTotal.new(options)
|
|
41
48
|
#
|
|
42
|
-
def initialize(
|
|
43
|
-
|
|
44
|
-
@
|
|
45
|
-
@
|
|
46
|
-
@
|
|
49
|
+
def initialize(config = {})
|
|
50
|
+
super()
|
|
51
|
+
@debug = config[:debug] || false
|
|
52
|
+
@timeout = config[:timeout] || 20
|
|
53
|
+
@apikey = config['APIKEY'] || raise("#{self.class.name} requires an APIKEY. See README.md")
|
|
54
|
+
@url = config['URL'] || 'https://www.virustotal.com/api/v3/'
|
|
55
|
+
@url += '/' unless @url.end_with? '/'
|
|
47
56
|
end
|
|
48
57
|
|
|
58
|
+
# the most resolutions that VirusTotal returns in one response
|
|
59
|
+
MAX_LIMIT = 40
|
|
60
|
+
|
|
49
61
|
# Takes a label (either a domain or an IP address) and returns
|
|
50
|
-
# an array of PassiveDNS::PDNSResult instances with the answers to the query
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
end
|
|
60
|
-
$stderr.puts "DEBUG: #{self.class.name} url = #{url}" if @debug
|
|
62
|
+
# an array of PassiveDNS::PDNSResult instances with the answers to the query.
|
|
63
|
+
# VirusTotal returns at most MAX_LIMIT resolutions per query.
|
|
64
|
+
def lookup(label, limit = nil)
|
|
65
|
+
warn "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
|
|
66
|
+
Timeout.timeout @timeout do
|
|
67
|
+
kind = /^[\d.]+$|:/.match?(label) ? 'ip_addresses' : 'domains'
|
|
68
|
+
count = [limit || MAX_LIMIT, MAX_LIMIT].min
|
|
69
|
+
url = "#{@url}#{kind}/#{URI.encode_uri_component label}/resolutions?limit=#{count}"
|
|
70
|
+
warn "DEBUG: #{self.class.name} url = #{url}" if @debug
|
|
61
71
|
begin
|
|
62
72
|
url = URI.parse url
|
|
63
73
|
rescue URI::InvalidURIError
|
|
64
|
-
|
|
74
|
+
warn "ERROR: Invalid address: #{url}"
|
|
65
75
|
return
|
|
66
76
|
end
|
|
67
|
-
http = Net::HTTP.new
|
|
77
|
+
http = Net::HTTP.new url.host, url.port
|
|
68
78
|
http.use_ssl = (url.scheme == 'https')
|
|
69
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
70
79
|
http.verify_depth = 5
|
|
71
|
-
request = Net::HTTP::Get.new
|
|
72
|
-
request.add_field
|
|
80
|
+
request = Net::HTTP::Get.new url.request_uri
|
|
81
|
+
request.add_field 'User-Agent', "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}"
|
|
82
|
+
request.add_field 'Accept', 'application/json'
|
|
83
|
+
request.add_field 'x-apikey', @apikey
|
|
73
84
|
t1 = Time.now
|
|
74
|
-
response = http.request
|
|
75
|
-
if response.code.to_i == 204
|
|
76
|
-
$stderr.puts "DEBUG: empty response from server" if @debug
|
|
77
|
-
return
|
|
78
|
-
end
|
|
85
|
+
response = http.request request
|
|
79
86
|
t2 = Time.now
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
else
|
|
84
|
-
recs
|
|
87
|
+
if response.code.to_i == 404
|
|
88
|
+
warn 'DEBUG: empty response from server' if @debug
|
|
89
|
+
return
|
|
85
90
|
end
|
|
86
|
-
|
|
91
|
+
check_response response
|
|
92
|
+
recs = parse_json response.body, label, t2 - t1
|
|
93
|
+
limit ? recs[0, limit] : recs
|
|
94
|
+
end
|
|
87
95
|
rescue Timeout::Error
|
|
88
|
-
|
|
96
|
+
warn "#{self.class.name} lookup timed out: #{label}"
|
|
89
97
|
end
|
|
90
|
-
|
|
98
|
+
|
|
91
99
|
private
|
|
92
|
-
|
|
93
|
-
# parses the response of virustotal's JSON reply to generate an array of PDNSResult
|
|
94
|
-
def parse_json(page,query,response_time=0)
|
|
100
|
+
|
|
101
|
+
# parses the response of virustotal's v3 JSON reply to generate an array of PDNSResult
|
|
102
|
+
def parse_json(page, query, response_time = 0)
|
|
95
103
|
res = []
|
|
96
|
-
return res
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
return res unless page
|
|
105
|
+
|
|
106
|
+
data = JSON.parse page
|
|
107
|
+
raise "#{self.class.name} Error: #{data['error']['message']}" if data['error']
|
|
108
|
+
|
|
109
|
+
data['data']&.each do |row|
|
|
110
|
+
attributes = row['attributes'] || {}
|
|
111
|
+
lastseen = Time.at attributes['date'].to_i
|
|
112
|
+
if attributes['ip_address']
|
|
113
|
+
res << PDNSResult.new(self.class.name, response_time, query, attributes['ip_address'], 'A', nil, nil, lastseen, nil, 'yellow')
|
|
114
|
+
elsif attributes['host_name']
|
|
115
|
+
res << PDNSResult.new(self.class.name, response_time, attributes['host_name'], query, 'A', nil, nil, lastseen, nil, 'yellow')
|
|
106
116
|
end
|
|
107
117
|
end
|
|
108
|
-
if data['response_code'] == 0
|
|
109
|
-
$stderr.puts "DEBUG: server returned error: #{data['verbose_msg']}" if @debug
|
|
110
|
-
end
|
|
111
118
|
res
|
|
112
|
-
rescue
|
|
113
|
-
|
|
119
|
+
rescue StandardError => e
|
|
120
|
+
warn "VirusTotal Exception: #{e}"
|
|
114
121
|
raise e
|
|
115
122
|
end
|
|
116
|
-
|
|
117
123
|
end
|
|
118
124
|
end
|
|
119
125
|
end
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'sqlite3'
|
|
2
4
|
require 'yaml'
|
|
3
5
|
require 'structformatter'
|
|
4
6
|
|
|
5
7
|
module PassiveDNS # :nodoc:
|
|
6
8
|
# struct to hold pending entries for query
|
|
7
|
-
|
|
9
|
+
PDNSQueueEntry = Struct.new :query, :state, :level
|
|
8
10
|
|
|
9
11
|
# holds state in memory of the queue to be queried, records returned, and the level of recursion
|
|
10
12
|
class PDNSToolState
|
|
@@ -19,62 +21,55 @@ module PassiveDNS # :nodoc:
|
|
|
19
21
|
@recs = []
|
|
20
22
|
@level = 0
|
|
21
23
|
end
|
|
22
|
-
|
|
23
|
-
# returns the next record
|
|
24
|
-
def next_result
|
|
25
|
-
@recs.each
|
|
26
|
-
yield rec
|
|
27
|
-
end
|
|
24
|
+
|
|
25
|
+
# returns the next record
|
|
26
|
+
def next_result(&)
|
|
27
|
+
@recs.each(&)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
# adds the record to the list of records received and tries to add the answer and query back to the queue for future query
|
|
31
31
|
def add_result(res)
|
|
32
32
|
@recs << res
|
|
33
|
-
add_query
|
|
34
|
-
add_query
|
|
33
|
+
add_query res.answer, 'pending'
|
|
34
|
+
add_query res.query, 'pending'
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
# sets the state of a given query
|
|
38
|
-
def update_query(query,state)
|
|
38
|
+
def update_query(query, state)
|
|
39
39
|
@queue.each do |q|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
next unless q.query == query
|
|
41
|
+
|
|
42
|
+
puts "update_query: #{query} (#{q.state}) -> (#{state})" if @debug
|
|
43
|
+
q.state = state
|
|
44
|
+
break
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
# returns the state of a provided query
|
|
49
49
|
def get_state(query)
|
|
50
50
|
@queue.each do |q|
|
|
51
|
-
if q.query == query
|
|
52
|
-
return q.state
|
|
53
|
-
end
|
|
51
|
+
return q.state if q.query == query
|
|
54
52
|
end
|
|
55
53
|
false
|
|
56
54
|
end
|
|
57
55
|
|
|
58
56
|
# adding a query to the queue of things to be queried, but only if the query isn't already queued or answered
|
|
59
|
-
def add_query(query,state,level
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return if get_state(query)
|
|
57
|
+
def add_query(query, state, level = @level + 1)
|
|
58
|
+
query = query.split(/ /, 2)[1] if /^\d+ \w+\./.match?(query)
|
|
59
|
+
return if get_state query
|
|
60
|
+
|
|
64
61
|
puts "Adding query: #{query}, #{state}, #{level}" if @debug
|
|
65
|
-
@queue << PDNSQueueEntry.new(query,state,level)
|
|
62
|
+
@queue << PDNSQueueEntry.new(query, state, level)
|
|
66
63
|
end
|
|
67
64
|
|
|
68
65
|
# returns each query waiting on the queue
|
|
69
|
-
def each_query(max_level=20)
|
|
66
|
+
def each_query(max_level = 20)
|
|
70
67
|
@queue.each do |q|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
end
|
|
77
|
-
end
|
|
68
|
+
next unless (q.state == 'pending') || (q.state == 'failed')
|
|
69
|
+
|
|
70
|
+
@level = q.level
|
|
71
|
+
q.state = 'queried'
|
|
72
|
+
yield q.query if q.level < max_level
|
|
78
73
|
end
|
|
79
74
|
end
|
|
80
75
|
|
|
@@ -84,46 +79,46 @@ module PassiveDNS # :nodoc:
|
|
|
84
79
|
# IP "$node2,,white,1"
|
|
85
80
|
# domain "$node2,,gray,2"
|
|
86
81
|
# Struct.new(:query, :answer, :rrtype, :ttl, :firstseen, :lastseen)
|
|
87
|
-
colors = {
|
|
82
|
+
colors = { 'MX' => 'green', 'A' => 'blue', 'CNAME' => 'pink', 'NS' => 'red', 'SOA' => 'white', 'PTR' => 'purple', 'TXT' => 'brown' }
|
|
88
83
|
nodes = {}
|
|
89
84
|
edges = {}
|
|
90
85
|
next_result do |i|
|
|
91
|
-
if i
|
|
92
|
-
nodes[i.query
|
|
93
|
-
if
|
|
94
|
-
nodes[i.answer
|
|
95
|
-
else
|
|
96
|
-
nodes[i.answer
|
|
86
|
+
if i
|
|
87
|
+
nodes["#{i.query},,gray,2"] = true
|
|
88
|
+
if /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/.match?(i.answer)
|
|
89
|
+
nodes["#{i.answer},,white,1"] = true
|
|
90
|
+
else
|
|
91
|
+
nodes["#{i.answer},,gray,2"] = true
|
|
97
92
|
end
|
|
98
93
|
color = colors[i.rrtype]
|
|
99
|
-
color ||=
|
|
100
|
-
edges[i.query
|
|
94
|
+
color ||= 'blue'
|
|
95
|
+
edges["#{i.query},#{i.answer},#{color}"] = true
|
|
101
96
|
end
|
|
102
97
|
end
|
|
103
|
-
nodes.
|
|
104
|
-
output += i
|
|
98
|
+
nodes.each_key do |i|
|
|
99
|
+
output += "#{i}\n"
|
|
105
100
|
end
|
|
106
101
|
output += "edgedef> node1,node2,color\n"
|
|
107
|
-
edges.
|
|
108
|
-
output += i
|
|
102
|
+
edges.each_key do |i|
|
|
103
|
+
output += "#{i}\n"
|
|
109
104
|
end
|
|
110
105
|
output
|
|
111
106
|
end
|
|
112
107
|
|
|
113
108
|
# transforms a set of results into graphviz syntax
|
|
114
109
|
def to_graphviz
|
|
115
|
-
colors = {
|
|
110
|
+
colors = { 'MX' => 'green', 'A' => 'blue', 'CNAME' => 'pink', 'NS' => 'red', 'SOA' => 'white', 'PTR' => 'purple', 'TXT' => 'brown' }
|
|
116
111
|
output = "graph pdns {\n"
|
|
117
112
|
nodes = {}
|
|
118
113
|
next_result do |l|
|
|
119
114
|
if l
|
|
120
115
|
unless nodes[l.query]
|
|
121
116
|
output += " \"#{l.query}\" [shape=ellipse, style=filled, color=gray];\n"
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
117
|
+
output += if /^\d{3}\.\d{3}\.\d{3}\.\d{3}$/.match?(l.answer)
|
|
118
|
+
" \"#{l.answer}\" [shape=box, style=filled, color=white];\n"
|
|
119
|
+
else
|
|
120
|
+
" \"#{l.answer}\" [shape=ellipse, style=filled, color=gray];\n"
|
|
121
|
+
end
|
|
127
122
|
nodes[l.query] = true
|
|
128
123
|
end
|
|
129
124
|
output += " \"#{l.query}\" -- \"#{l.answer}\" [color=#{colors[l.rrtype]}];\n"
|
|
@@ -135,7 +130,7 @@ module PassiveDNS # :nodoc:
|
|
|
135
130
|
# transforms a set of results into graphml syntax
|
|
136
131
|
def to_graphml
|
|
137
132
|
output = '<?xml version="1.0" encoding="UTF-8"?>
|
|
138
|
-
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
|
|
133
|
+
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
|
|
139
134
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
140
135
|
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
|
|
141
136
|
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
|
|
@@ -145,41 +140,41 @@ module PassiveDNS # :nodoc:
|
|
|
145
140
|
edges = {}
|
|
146
141
|
next_result do |r|
|
|
147
142
|
if r
|
|
148
|
-
output += " <node id='#{r.query}'/>\n" unless nodes[
|
|
143
|
+
output += " <node id='#{r.query}'/>\n" unless nodes[r.query.to_s]
|
|
149
144
|
nodes[r.query] = true
|
|
150
|
-
output += " <node id='#{r.answer}'/>\n" unless nodes[
|
|
145
|
+
output += " <node id='#{r.answer}'/>\n" unless nodes[r.answer.to_s]
|
|
151
146
|
nodes[r.answer] = true
|
|
152
147
|
output += " <edge source='#{r.query}' target='#{r.answer}'/>\n" unless edges["#{r.query}|#{r.answer}"]
|
|
153
148
|
end
|
|
154
149
|
end
|
|
155
|
-
output +=
|
|
150
|
+
output += "</graph></graphml>\n"
|
|
156
151
|
end
|
|
157
152
|
|
|
158
153
|
# transforms a set of results into XML
|
|
159
154
|
def to_xml
|
|
160
|
-
output =
|
|
161
|
-
output +=
|
|
162
|
-
output +=
|
|
155
|
+
output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
|
|
156
|
+
output += "<report>\n"
|
|
157
|
+
output += " <results>\n"
|
|
163
158
|
next_result do |rec|
|
|
164
|
-
output +=
|
|
159
|
+
output += " #{rec.to_xml}\n"
|
|
165
160
|
end
|
|
166
|
-
output +=
|
|
167
|
-
output +=
|
|
161
|
+
output += " </results>\n"
|
|
162
|
+
output += "</report>\n"
|
|
168
163
|
end
|
|
169
164
|
|
|
170
165
|
# transforms a set of results into YAML
|
|
171
166
|
def to_yaml
|
|
172
|
-
output =
|
|
167
|
+
output = ''
|
|
173
168
|
next_result do |rec|
|
|
174
|
-
output += rec.to_yaml
|
|
169
|
+
output += "#{rec.to_yaml}\n"
|
|
175
170
|
end
|
|
176
171
|
output
|
|
177
172
|
end
|
|
178
173
|
|
|
179
174
|
# transforms a set of results into JSON
|
|
180
|
-
def to_json
|
|
175
|
+
def to_json(*_args)
|
|
181
176
|
output = "[\n"
|
|
182
|
-
sep =
|
|
177
|
+
sep = ''
|
|
183
178
|
next_result do |rec|
|
|
184
179
|
output += sep
|
|
185
180
|
output += rec.to_json
|
|
@@ -189,113 +184,109 @@ module PassiveDNS # :nodoc:
|
|
|
189
184
|
end
|
|
190
185
|
|
|
191
186
|
# transforms a set of results into a text string
|
|
192
|
-
def to_s(sep="\t")
|
|
193
|
-
output =
|
|
187
|
+
def to_s(sep = "\t")
|
|
188
|
+
output = ''
|
|
194
189
|
next_result do |rec|
|
|
195
|
-
output += rec.to_s
|
|
190
|
+
output += "#{rec.to_s sep}\n"
|
|
196
191
|
end
|
|
197
192
|
output
|
|
198
193
|
end
|
|
199
|
-
end
|
|
200
|
-
|
|
194
|
+
end
|
|
201
195
|
|
|
202
196
|
# creates persistence to the tool state by leveraging SQLite3
|
|
203
197
|
class PDNSToolStateDB < PDNSToolState
|
|
204
198
|
attr_reader :level
|
|
199
|
+
|
|
205
200
|
# creates an SQLite3-based Passive DNS Client state
|
|
206
201
|
# only argument is the filename of the sqlite3 database
|
|
207
|
-
def initialize(sqlitedb=nil)
|
|
202
|
+
def initialize(sqlitedb = nil)
|
|
203
|
+
super()
|
|
208
204
|
@debug = false
|
|
209
205
|
puts "PDNSToolState initialize #{sqlitedb}" if @debug
|
|
210
206
|
@level = 0
|
|
211
207
|
@sqlitedb = sqlitedb
|
|
212
|
-
raise
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
res
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
@level = row[0].to_i
|
|
224
|
-
puts "changed @level = #{@level}" if @debug
|
|
225
|
-
end
|
|
208
|
+
raise 'Cannot use this class without a database file' unless @sqlitedb
|
|
209
|
+
|
|
210
|
+
newdb = true unless File.exist? @sqlitedb
|
|
211
|
+
@sqlitedbh = SQLite3::Database.new @sqlitedb
|
|
212
|
+
create_tables if newdb
|
|
213
|
+
res = @sqlitedbh.execute "select min(level) from queue where state = 'pending'"
|
|
214
|
+
return unless res
|
|
215
|
+
|
|
216
|
+
res.each do |row|
|
|
217
|
+
@level = row[0].to_i
|
|
218
|
+
puts "changed @level = #{@level}" if @debug
|
|
226
219
|
end
|
|
227
220
|
end
|
|
228
221
|
|
|
229
222
|
# creates the sqlite3 tables needed to track the state of this tool as itqueries and recurses
|
|
230
223
|
def create_tables
|
|
231
|
-
puts
|
|
232
|
-
@sqlitedbh.execute
|
|
233
|
-
@sqlitedbh.execute
|
|
234
|
-
@sqlitedbh.execute
|
|
235
|
-
@sqlitedbh.execute
|
|
236
|
-
@sqlitedbh.execute
|
|
237
|
-
@sqlitedbh.execute
|
|
224
|
+
puts 'creating tables' if @debug
|
|
225
|
+
@sqlitedbh.execute 'create table results (query, answer, rrtype, ttl, firstseen, lastseen, ts REAL)'
|
|
226
|
+
@sqlitedbh.execute 'create table queue (query, state, level INTEGER, ts REAL)'
|
|
227
|
+
@sqlitedbh.execute 'create index residx on results (ts)'
|
|
228
|
+
@sqlitedbh.execute 'create unique index queue_unique on queue (query)'
|
|
229
|
+
@sqlitedbh.execute 'create index queue_level_idx on queue (level)'
|
|
230
|
+
@sqlitedbh.execute 'create index queue_state_idx on queue (state)'
|
|
238
231
|
end
|
|
239
232
|
|
|
240
|
-
# returns the next record
|
|
233
|
+
# returns the next record
|
|
241
234
|
def next_result
|
|
242
|
-
rows = @sqlitedbh.execute
|
|
235
|
+
rows = @sqlitedbh.execute 'select query, answer, rrtype, ttl, firstseen, lastseen from results order by ts'
|
|
243
236
|
rows.each do |row|
|
|
244
|
-
|
|
237
|
+
# the database does not keep the source or the response time
|
|
238
|
+
yield PDNSResult.new(nil, nil, *row)
|
|
245
239
|
end
|
|
246
240
|
end
|
|
247
241
|
|
|
248
242
|
# adds the record to the list of records received and tries to add the answer and query back to the queue for future query
|
|
249
243
|
def add_result(res)
|
|
250
|
-
puts "adding result: #{res
|
|
251
|
-
curtime = Time.now
|
|
252
|
-
@sqlitedbh.execute(
|
|
244
|
+
puts "adding result: #{res}" if @debug
|
|
245
|
+
curtime = Time.now.to_f
|
|
246
|
+
@sqlitedbh.execute('insert into results values (?, ?, ?, ?, ?, ?, ?)',
|
|
247
|
+
[res.query, res.answer, res.rrtype, res.ttl, res.firstseen, res.lastseen, curtime].map do |v|
|
|
248
|
+
v.nil? || v.is_a?(Float) ? v : v.to_s
|
|
249
|
+
end)
|
|
253
250
|
|
|
254
|
-
add_query
|
|
255
|
-
add_query
|
|
251
|
+
add_query res.answer, 'pending'
|
|
252
|
+
add_query res.query, 'pending'
|
|
256
253
|
end
|
|
257
254
|
|
|
258
255
|
# adding a query to the queue of things to be queried, but only if the query isn't already queued or answered
|
|
259
|
-
def add_query(query,state,level
|
|
260
|
-
return if get_state
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
256
|
+
def add_query(query, state, level = @level + 1)
|
|
257
|
+
return if get_state query
|
|
258
|
+
|
|
259
|
+
curtime = Time.now.to_f
|
|
260
|
+
puts "add_query(#{query},#{state},level=#{level})" if @debug
|
|
261
|
+
@sqlitedbh.execute 'insert into queue values (?, ?, ?, ?)', [query.to_s, state, level, curtime]
|
|
262
|
+
rescue SQLite3::ConstraintException
|
|
263
|
+
# the query is already queued
|
|
267
264
|
end
|
|
268
265
|
|
|
269
266
|
# sets the state of a given query
|
|
270
|
-
def update_query(query,state)
|
|
271
|
-
@sqlitedbh.execute
|
|
267
|
+
def update_query(query, state)
|
|
268
|
+
@sqlitedbh.execute 'update queue set state = ? where query = ?', [state, query.to_s]
|
|
272
269
|
end
|
|
273
270
|
|
|
274
271
|
# returns each query waiting on the queue
|
|
275
272
|
def get_state(query)
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
rows.each do |row|
|
|
279
|
-
return row[0]
|
|
280
|
-
end
|
|
281
|
-
end
|
|
282
|
-
false
|
|
273
|
+
row = @sqlitedbh.get_first_row 'select state from queue where query = ?', [query.to_s]
|
|
274
|
+
row ? row[0] : false
|
|
283
275
|
end
|
|
284
276
|
|
|
285
277
|
# returns each query waiting on the queue
|
|
286
|
-
def each_query(max_level=20)
|
|
278
|
+
def each_query(max_level = 20)
|
|
287
279
|
puts "each_query max_level=#{max_level} curlevel=#{@level}" if @debug
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
end
|
|
280
|
+
row = @sqlitedbh.get_first_row "select query, state, level from queue where state = 'failed' or state = 'pending' " \
|
|
281
|
+
'order by level limit 1'
|
|
282
|
+
return unless row
|
|
283
|
+
|
|
284
|
+
query, state, level = row
|
|
285
|
+
puts " #{query},#{state},#{level}" if @debug
|
|
286
|
+
return unless level < max_level
|
|
287
|
+
|
|
288
|
+
update_query query, 'queried'
|
|
289
|
+
yield query
|
|
299
290
|
end
|
|
300
|
-
end
|
|
291
|
+
end
|
|
301
292
|
end
|