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,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
|
data/lib/passivedns/client.rb
CHANGED
|
@@ -1,128 +1,130 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# DESCRIPTION: queries passive DNS databases
|
|
3
4
|
# This code is released under the LGPL: http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
4
|
-
# Please note that use of any passive dns database is subject to the terms of use of that passive dns database. Use of this script in
|
|
5
|
-
#
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
# Please note that use of any passive dns database is subject to the terms of use of that passive dns database. Use of this script in
|
|
6
|
+
# violation of their terms is not encouraged in any way. Also, please do not add any obfuscation to try to work around their terms of
|
|
7
|
+
# service. If you need special services, ask the providers for help/permission.
|
|
8
|
+
# Remember, these passive DNS operators are my friends. I don't want to have a row with them because some asshat used this library to
|
|
9
|
+
# abuse them.
|
|
10
|
+
require 'iniparse'
|
|
11
|
+
require_relative 'client/version'
|
|
12
|
+
require_relative 'client/state'
|
|
13
|
+
require_relative 'client/passivedb'
|
|
8
14
|
|
|
9
15
|
# load all the providers
|
|
10
|
-
|
|
11
|
-
provider_path = File.dirname(__FILE__)+"/client/provider/*.rb"
|
|
12
|
-
Dir.glob(provider_path).each do |provider|
|
|
13
|
-
name = File.basename(provider, '.rb')
|
|
14
|
-
require "passivedns/client/provider/#{name}.rb"
|
|
15
|
-
if name != 'bfk'
|
|
16
|
-
$passivedns_providers << name
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
require 'configparser'
|
|
16
|
+
Dir.glob(File.join(__dir__, 'client', 'provider', '*.rb')).each { |provider| require provider }
|
|
21
17
|
|
|
22
18
|
module PassiveDNS # :nodoc:
|
|
23
|
-
|
|
24
19
|
class SecurityControl
|
|
25
|
-
def allow(
|
|
26
|
-
raise
|
|
20
|
+
def allow(_user_level)
|
|
21
|
+
raise 'unimplemented'
|
|
27
22
|
end
|
|
28
23
|
end
|
|
29
|
-
|
|
24
|
+
|
|
30
25
|
class TLPSecurityControl < SecurityControl
|
|
31
|
-
LEVELS = [
|
|
32
|
-
|
|
26
|
+
LEVELS = %w[white green yellow red].freeze
|
|
27
|
+
|
|
33
28
|
def initialize(tlp)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
29
|
+
super()
|
|
30
|
+
raise "Unknown TLP setting, #{tlp}" unless /(white|green|yellow|red)/i.match?(tlp)
|
|
31
|
+
|
|
32
|
+
@tlp = tlp.downcase
|
|
33
|
+
@tlp_level = LEVELS.index @tlp
|
|
40
34
|
end
|
|
41
|
-
|
|
42
|
-
def allow(user_level)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
return(user_level >= @tlp_level)
|
|
35
|
+
|
|
36
|
+
def allow(user_level) # rubocop:disable Naming/PredicateMethod
|
|
37
|
+
level = LEVELS.index user_level.downcase
|
|
38
|
+
raise "Invalid user level, #{user_level}" if level.nil?
|
|
39
|
+
|
|
40
|
+
level >= @tlp_level
|
|
48
41
|
end
|
|
49
|
-
|
|
50
|
-
def to_s
|
|
42
|
+
|
|
43
|
+
def to_s
|
|
51
44
|
@tlp
|
|
52
45
|
end
|
|
53
46
|
end
|
|
54
|
-
|
|
47
|
+
|
|
55
48
|
# struct to contain the results from a PassiveDNS lookup
|
|
56
|
-
|
|
49
|
+
# (the :count member intentionally shadows Struct#count)
|
|
50
|
+
PDNSResult = Struct.new :source, :response_time, :query, :answer, :rrtype, :ttl, :firstseen, :lastseen, :count, :security # rubocop:disable Lint/StructNewOverride
|
|
57
51
|
|
|
58
52
|
# coodinates the lookups accross all configured PassiveDNS providers
|
|
59
53
|
class Client
|
|
60
|
-
|
|
54
|
+
# the record types that are reported
|
|
55
|
+
RRTYPES = %w[A AAAA NS CNAME PTR SOA].freeze
|
|
56
|
+
|
|
57
|
+
# default location of the configuration file
|
|
58
|
+
DEFAULT_CONFIG_FILE = File.join Dir.home, '.passivedns-client'
|
|
59
|
+
|
|
60
|
+
# the names of all available providers, e.g., ["circl", "dnsdb", ...]
|
|
61
|
+
def self.provider_names
|
|
62
|
+
names = PassiveDB.providers.map(&:config_section_name)
|
|
63
|
+
names.sort!
|
|
64
|
+
names
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# reads an INI-style configuration file and returns a hash of section name to a hash of settings.
|
|
68
|
+
# A missing file yields an empty configuration and a warning.
|
|
69
|
+
def self.load_config(configfile = DEFAULT_CONFIG_FILE)
|
|
70
|
+
unless File.exist? configfile
|
|
71
|
+
warn "Could not find config file at #{configfile}. Using a blank configuration."
|
|
72
|
+
return {}
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
IniParse.parse(File.read(configfile)).to_hash
|
|
76
|
+
rescue IniParse::IniParseError => e
|
|
77
|
+
raise "Unable to parse the config file #{configfile}: #{e.message}"
|
|
78
|
+
end
|
|
79
|
+
|
|
61
80
|
# instantiate and configure all specified PassiveDNS providers
|
|
62
81
|
# pdns array of passivedns provider names, e.g., ["dnsdb","virustotal"]
|
|
63
|
-
# configfile filename of the passivedns-client configuration
|
|
64
|
-
def initialize(pdns
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
class_map = {}
|
|
73
|
-
PassiveDNS::Provider.constants.each do |const|
|
|
74
|
-
if PassiveDNS::Provider.const_get(const).is_a?(Class) and PassiveDNS::Provider.const_get(const).superclass == PassiveDNS::PassiveDB
|
|
75
|
-
class_map[PassiveDNS::Provider.const_get(const).config_section_name] = PassiveDNS::Provider.const_get(const)
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
@pdnsdbs = []
|
|
80
|
-
pdns.uniq.each do |pd|
|
|
81
|
-
if class_map[pd]
|
|
82
|
-
@pdnsdbs << class_map[pd].new(cp[pd] || {})
|
|
83
|
-
else
|
|
84
|
-
raise "Unknown Passive DNS provider: #{pd}"
|
|
85
|
-
end
|
|
82
|
+
# configfile filename of the passivedns-client configuration
|
|
83
|
+
def initialize(pdns = self.class.provider_names, configfile = DEFAULT_CONFIG_FILE)
|
|
84
|
+
config = self.class.load_config configfile
|
|
85
|
+
class_map = PassiveDB.providers.to_h { |provider| [provider.config_section_name, provider] }
|
|
86
|
+
|
|
87
|
+
@pdnsdbs = pdns.uniq.map do |name| # rubocop:disable Performance/ChainArrayAllocation
|
|
88
|
+
raise "Unknown Passive DNS provider: #{name}" unless class_map[name]
|
|
89
|
+
|
|
90
|
+
class_map[name].new(config[name] || {})
|
|
86
91
|
end
|
|
92
|
+
end
|
|
87
93
|
|
|
88
|
-
end #initialize
|
|
89
|
-
|
|
90
94
|
# set the debug flag
|
|
91
|
-
def debug=(
|
|
95
|
+
def debug=(value)
|
|
92
96
|
@pdnsdbs.each do |pdnsdb|
|
|
93
|
-
pdnsdb.debug =
|
|
97
|
+
pdnsdb.debug = value
|
|
94
98
|
end
|
|
95
99
|
end
|
|
96
|
-
|
|
97
|
-
|
|
100
|
+
|
|
101
|
+
# set the timeout, in seconds, for each provider's lookups
|
|
102
|
+
def timeout=(value)
|
|
98
103
|
@pdnsdbs.each do |pdnsdb|
|
|
99
|
-
pdnsdb.timeout =
|
|
104
|
+
pdnsdb.timeout = value
|
|
100
105
|
end
|
|
101
106
|
end
|
|
102
|
-
|
|
107
|
+
|
|
103
108
|
# perform the query lookup accross all configured PassiveDNS providers
|
|
104
|
-
def query(item, limit=nil)
|
|
105
|
-
threads =
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
pdnsdb.lookup
|
|
109
|
+
def query(item, limit = nil)
|
|
110
|
+
threads = @pdnsdbs.map do |pdnsdb|
|
|
111
|
+
Thread.new item do |q|
|
|
112
|
+
Thread.current.report_on_exception = false
|
|
113
|
+
pdnsdb.lookup q, limit
|
|
109
114
|
end
|
|
110
115
|
end
|
|
111
|
-
|
|
116
|
+
|
|
112
117
|
results = []
|
|
113
118
|
threads.each do |thr|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
rv.each do |r|
|
|
117
|
-
if ["A","AAAA","NS","CNAME","PTR"].index(r.rrtype)
|
|
118
|
-
results << r
|
|
119
|
-
end
|
|
120
|
-
end
|
|
119
|
+
thr.value&.each do |r|
|
|
120
|
+
results << r if RRTYPES.include? r.rrtype
|
|
121
121
|
end
|
|
122
|
+
rescue StandardError => e
|
|
123
|
+
# one failing provider should not lose the answers from the others
|
|
124
|
+
warn "ERROR: #{e.message}"
|
|
122
125
|
end
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
end # PassiveDNS
|
|
126
|
+
|
|
127
|
+
results
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|