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
data/lib/passivedns/client.rb
CHANGED
|
@@ -1,126 +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
|
-
$passivedns_providers << name
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
require 'configparser'
|
|
16
|
+
Dir.glob(File.join(__dir__, 'client', 'provider', '*.rb')).each { |provider| require provider }
|
|
19
17
|
|
|
20
18
|
module PassiveDNS # :nodoc:
|
|
21
|
-
|
|
22
19
|
class SecurityControl
|
|
23
|
-
def allow(
|
|
24
|
-
raise
|
|
20
|
+
def allow(_user_level)
|
|
21
|
+
raise 'unimplemented'
|
|
25
22
|
end
|
|
26
23
|
end
|
|
27
|
-
|
|
24
|
+
|
|
28
25
|
class TLPSecurityControl < SecurityControl
|
|
29
|
-
LEVELS = [
|
|
30
|
-
|
|
26
|
+
LEVELS = %w[white green yellow red].freeze
|
|
27
|
+
|
|
31
28
|
def initialize(tlp)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
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
|
|
38
34
|
end
|
|
39
|
-
|
|
40
|
-
def allow(user_level)
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
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
|
|
46
41
|
end
|
|
47
|
-
|
|
48
|
-
def to_s
|
|
42
|
+
|
|
43
|
+
def to_s
|
|
49
44
|
@tlp
|
|
50
45
|
end
|
|
51
46
|
end
|
|
52
|
-
|
|
47
|
+
|
|
53
48
|
# struct to contain the results from a PassiveDNS lookup
|
|
54
|
-
|
|
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
|
|
55
51
|
|
|
56
52
|
# coodinates the lookups accross all configured PassiveDNS providers
|
|
57
53
|
class Client
|
|
58
|
-
|
|
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
|
+
|
|
59
80
|
# instantiate and configure all specified PassiveDNS providers
|
|
60
81
|
# pdns array of passivedns provider names, e.g., ["dnsdb","virustotal"]
|
|
61
|
-
# configfile filename of the passivedns-client configuration
|
|
62
|
-
def initialize(pdns
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
class_map = {}
|
|
71
|
-
PassiveDNS::Provider.constants.each do |const|
|
|
72
|
-
if PassiveDNS::Provider.const_get(const).is_a?(Class) and PassiveDNS::Provider.const_get(const).superclass == PassiveDNS::PassiveDB
|
|
73
|
-
class_map[PassiveDNS::Provider.const_get(const).config_section_name] = PassiveDNS::Provider.const_get(const)
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
@pdnsdbs = []
|
|
78
|
-
pdns.uniq.each do |pd|
|
|
79
|
-
if class_map[pd]
|
|
80
|
-
@pdnsdbs << class_map[pd].new(cp[pd] || {})
|
|
81
|
-
else
|
|
82
|
-
raise "Unknown Passive DNS provider: #{pd}"
|
|
83
|
-
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] || {})
|
|
84
91
|
end
|
|
92
|
+
end
|
|
85
93
|
|
|
86
|
-
end #initialize
|
|
87
|
-
|
|
88
94
|
# set the debug flag
|
|
89
|
-
def debug=(
|
|
95
|
+
def debug=(value)
|
|
90
96
|
@pdnsdbs.each do |pdnsdb|
|
|
91
|
-
pdnsdb.debug =
|
|
97
|
+
pdnsdb.debug = value
|
|
92
98
|
end
|
|
93
99
|
end
|
|
94
|
-
|
|
95
|
-
|
|
100
|
+
|
|
101
|
+
# set the timeout, in seconds, for each provider's lookups
|
|
102
|
+
def timeout=(value)
|
|
96
103
|
@pdnsdbs.each do |pdnsdb|
|
|
97
|
-
pdnsdb.timeout =
|
|
104
|
+
pdnsdb.timeout = value
|
|
98
105
|
end
|
|
99
106
|
end
|
|
100
|
-
|
|
107
|
+
|
|
101
108
|
# perform the query lookup accross all configured PassiveDNS providers
|
|
102
|
-
def query(item, limit=nil)
|
|
103
|
-
threads =
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
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
|
|
107
114
|
end
|
|
108
115
|
end
|
|
109
|
-
|
|
116
|
+
|
|
110
117
|
results = []
|
|
111
118
|
threads.each do |thr|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
rv.each do |r|
|
|
115
|
-
if ["A","AAAA","NS","CNAME","PTR"].index(r.rrtype)
|
|
116
|
-
results << r
|
|
117
|
-
end
|
|
118
|
-
end
|
|
119
|
+
thr.value&.each do |r|
|
|
120
|
+
results << r if RRTYPES.include? r.rrtype
|
|
119
121
|
end
|
|
122
|
+
rescue StandardError => e
|
|
123
|
+
# one failing provider should not lose the answers from the others
|
|
124
|
+
warn "ERROR: #{e.message}"
|
|
120
125
|
end
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
end # PassiveDNS
|
|
126
|
+
|
|
127
|
+
results
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
metadata
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: passivedns-client
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 3.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- chrislee35
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
13
|
+
name: getoptlong
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
16
|
- - ">="
|
|
@@ -25,35 +24,35 @@ dependencies:
|
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
25
|
version: '0'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
27
|
+
name: iniparse
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- - "
|
|
30
|
+
- - "~>"
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
32
|
+
version: '1.5'
|
|
34
33
|
type: :runtime
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- - "
|
|
37
|
+
- - "~>"
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
39
|
+
version: '1.5'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
|
-
name:
|
|
41
|
+
name: sqlite3
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
44
|
- - ">="
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: '
|
|
46
|
+
version: '1.4'
|
|
48
47
|
type: :runtime
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
51
|
- - ">="
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: '
|
|
53
|
+
version: '1.4'
|
|
55
54
|
- !ruby/object:Gem::Dependency
|
|
56
|
-
name:
|
|
55
|
+
name: structformatter
|
|
57
56
|
requirement: !ruby/object:Gem::Requirement
|
|
58
57
|
requirements:
|
|
59
58
|
- - ">="
|
|
@@ -70,46 +69,60 @@ dependencies:
|
|
|
70
69
|
name: minitest
|
|
71
70
|
requirement: !ruby/object:Gem::Requirement
|
|
72
71
|
requirements:
|
|
73
|
-
- - "
|
|
72
|
+
- - "~>"
|
|
74
73
|
- !ruby/object:Gem::Version
|
|
75
|
-
version: '0'
|
|
74
|
+
version: '6.0'
|
|
76
75
|
type: :development
|
|
77
76
|
prerelease: false
|
|
78
77
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
78
|
requirements:
|
|
80
|
-
- - "
|
|
79
|
+
- - "~>"
|
|
81
80
|
- !ruby/object:Gem::Version
|
|
82
|
-
version: '0'
|
|
81
|
+
version: '6.0'
|
|
83
82
|
- !ruby/object:Gem::Dependency
|
|
84
|
-
name:
|
|
83
|
+
name: rake
|
|
85
84
|
requirement: !ruby/object:Gem::Requirement
|
|
86
85
|
requirements:
|
|
87
|
-
- - "
|
|
86
|
+
- - "~>"
|
|
88
87
|
- !ruby/object:Gem::Version
|
|
89
|
-
version: '0'
|
|
88
|
+
version: '13.0'
|
|
90
89
|
type: :development
|
|
91
90
|
prerelease: false
|
|
92
91
|
version_requirements: !ruby/object:Gem::Requirement
|
|
93
92
|
requirements:
|
|
94
|
-
- - "
|
|
93
|
+
- - "~>"
|
|
95
94
|
- !ruby/object:Gem::Version
|
|
96
|
-
version: '0'
|
|
95
|
+
version: '13.0'
|
|
97
96
|
- !ruby/object:Gem::Dependency
|
|
98
|
-
name:
|
|
97
|
+
name: rubocop
|
|
99
98
|
requirement: !ruby/object:Gem::Requirement
|
|
100
99
|
requirements:
|
|
101
|
-
- - "
|
|
100
|
+
- - "~>"
|
|
102
101
|
- !ruby/object:Gem::Version
|
|
103
|
-
version: '0'
|
|
102
|
+
version: '1.0'
|
|
104
103
|
type: :development
|
|
105
104
|
prerelease: false
|
|
106
105
|
version_requirements: !ruby/object:Gem::Requirement
|
|
107
106
|
requirements:
|
|
108
|
-
- - "
|
|
107
|
+
- - "~>"
|
|
109
108
|
- !ruby/object:Gem::Version
|
|
110
|
-
version: '0'
|
|
109
|
+
version: '1.0'
|
|
110
|
+
- !ruby/object:Gem::Dependency
|
|
111
|
+
name: rubocop-performance
|
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - "~>"
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: '1.0'
|
|
117
|
+
type: :development
|
|
118
|
+
prerelease: false
|
|
119
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
120
|
+
requirements:
|
|
121
|
+
- - "~>"
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: '1.0'
|
|
111
124
|
description: This provides interfaces to various passive DNS databases to do the query
|
|
112
|
-
and to normalize the responses.
|
|
125
|
+
and to normalize the responses. The query tool also allows for recursive queries,
|
|
113
126
|
using an SQLite3 database to keep state.
|
|
114
127
|
email:
|
|
115
128
|
- rubygems@chrislee.dhs.org
|
|
@@ -118,31 +131,29 @@ executables:
|
|
|
118
131
|
extensions: []
|
|
119
132
|
extra_rdoc_files: []
|
|
120
133
|
files:
|
|
121
|
-
-
|
|
122
|
-
- Gemfile
|
|
134
|
+
- CHANGELOG.md
|
|
123
135
|
- LICENSE.txt
|
|
124
136
|
- README.md
|
|
125
|
-
- Rakefile
|
|
126
137
|
- bin/pdnstool
|
|
127
138
|
- lib/passivedns/client.rb
|
|
128
139
|
- lib/passivedns/client/cli.rb
|
|
129
140
|
- lib/passivedns/client/passivedb.rb
|
|
130
141
|
- lib/passivedns/client/provider/circl.rb
|
|
131
142
|
- lib/passivedns/client/provider/dnsdb.rb
|
|
143
|
+
- lib/passivedns/client/provider/osc.rb
|
|
132
144
|
- lib/passivedns/client/provider/passivetotal.rb
|
|
133
|
-
- lib/passivedns/client/provider/riskiq.rb
|
|
134
145
|
- lib/passivedns/client/provider/virustotal.rb
|
|
135
146
|
- lib/passivedns/client/state.rb
|
|
136
147
|
- lib/passivedns/client/version.rb
|
|
137
|
-
- passivedns-client.gemspec
|
|
138
|
-
- test/helper.rb
|
|
139
|
-
- test/test_cli.rb
|
|
140
|
-
- test/test_passivedns-client.rb
|
|
141
148
|
homepage: https://github.com/chrislee35/passivedns-client
|
|
142
149
|
licenses:
|
|
143
150
|
- MIT
|
|
144
|
-
metadata:
|
|
145
|
-
|
|
151
|
+
metadata:
|
|
152
|
+
source_code_uri: https://github.com/chrislee35/passivedns-client
|
|
153
|
+
bug_tracker_uri: https://github.com/chrislee35/passivedns-client/issues
|
|
154
|
+
changelog_uri: https://github.com/chrislee35/passivedns-client/blob/master/CHANGELOG.md
|
|
155
|
+
documentation_uri: https://chrislee35.github.io/passivedns-client/
|
|
156
|
+
rubygems_mfa_required: 'true'
|
|
146
157
|
rdoc_options: []
|
|
147
158
|
require_paths:
|
|
148
159
|
- lib
|
|
@@ -150,18 +161,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
150
161
|
requirements:
|
|
151
162
|
- - ">="
|
|
152
163
|
- !ruby/object:Gem::Version
|
|
153
|
-
version: '
|
|
164
|
+
version: '3.2'
|
|
154
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
155
166
|
requirements:
|
|
156
167
|
- - ">="
|
|
157
168
|
- !ruby/object:Gem::Version
|
|
158
169
|
version: '0'
|
|
159
170
|
requirements: []
|
|
160
|
-
rubygems_version: 3.
|
|
161
|
-
signing_key:
|
|
171
|
+
rubygems_version: 3.7.2
|
|
162
172
|
specification_version: 4
|
|
163
173
|
summary: Query passive DNS databases
|
|
164
|
-
test_files:
|
|
165
|
-
- test/helper.rb
|
|
166
|
-
- test/test_cli.rb
|
|
167
|
-
- test/test_passivedns-client.rb
|
|
174
|
+
test_files: []
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env rake
|
|
2
|
-
require "bundler/gem_tasks"
|
|
3
|
-
|
|
4
|
-
require 'rake/testtask'
|
|
5
|
-
require 'rdoc/task'
|
|
6
|
-
|
|
7
|
-
Rake::TestTask.new do |t|
|
|
8
|
-
t.libs << 'lib'
|
|
9
|
-
t.test_files = FileList['test/test_*.rb']
|
|
10
|
-
t.verbose = true
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
RDoc::Task.new do |rd|
|
|
14
|
-
rd.main = "README.doc"
|
|
15
|
-
rd.rdoc_files.include("README.md", "lib/**/*.rb")
|
|
16
|
-
rd.options << "--all"
|
|
17
|
-
rd.options << "--verbose"
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
task :default => :test
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
# DESCRIPTION: Module to query PassiveTotal's passive DNS repository
|
|
2
|
-
|
|
3
|
-
require 'net/http'
|
|
4
|
-
require 'net/https'
|
|
5
|
-
require 'openssl'
|
|
6
|
-
require 'pp'
|
|
7
|
-
|
|
8
|
-
module PassiveDNS #:nodoc: don't document this
|
|
9
|
-
# The Provider module contains all the Passive DNS provider client code
|
|
10
|
-
module Provider
|
|
11
|
-
# Queries RiskIQ's passive DNS database
|
|
12
|
-
class RiskIQ < PassiveDB
|
|
13
|
-
# Sets the modules self-reported name to "RiskIQ"
|
|
14
|
-
def self.name
|
|
15
|
-
"RiskIQ"
|
|
16
|
-
end
|
|
17
|
-
# Sets the configuration section name to "riskiq"
|
|
18
|
-
def self.config_section_name
|
|
19
|
-
"riskiq"
|
|
20
|
-
end
|
|
21
|
-
# Sets the command line database argument to "r"
|
|
22
|
-
def self.option_letter
|
|
23
|
-
"r"
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# :debug enables verbose logging to standard output
|
|
27
|
-
attr_accessor :debug
|
|
28
|
-
# === Options
|
|
29
|
-
# * :debug Sets the debug flag for the module
|
|
30
|
-
# * "API_TOKEN" REQUIRED: User name associated with your RiskIQ account
|
|
31
|
-
# * "API_PRIVATE_KEY" REQUIRED: Password associated with your RiskIQ account
|
|
32
|
-
# * "API_SERVER" Alternate server for testing. Defaults to "ws.riskiq.net"
|
|
33
|
-
# * "API_VERSION" Alternate version of the API to test. Defaults to "V1"
|
|
34
|
-
#
|
|
35
|
-
# === Example Instantiation
|
|
36
|
-
#
|
|
37
|
-
# options = {
|
|
38
|
-
# :debug => true,
|
|
39
|
-
# "API_TOKEN" => "riskiq_token",
|
|
40
|
-
# "API_PRIVATE_KEY" => "riskiq_private_key",
|
|
41
|
-
# "API_SERVER" => "ws.riskiq.net",
|
|
42
|
-
# "API_VERSION" => "v1"
|
|
43
|
-
# }
|
|
44
|
-
#
|
|
45
|
-
# PassiveDNS::Provider::RiskIQ.new(options)
|
|
46
|
-
#
|
|
47
|
-
def initialize(options={})
|
|
48
|
-
@debug = options[:debug] || false
|
|
49
|
-
@timeout = options[:timeout] || 20
|
|
50
|
-
@token = options["API_TOKEN"] || raise("#{self.class.name} requires an API_TOKEN")
|
|
51
|
-
@privkey = options["API_PRIVATE_KEY"] || raise("#{self.class.name} requires an API_PRIVATE_KEY")
|
|
52
|
-
@version = options["API_VERSION"] || "v1"
|
|
53
|
-
@server = options["API_SERVER"] || api_settings[@version][:server]
|
|
54
|
-
@url = "https://#{@server}/#{@version}"
|
|
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
|
-
$stderr.puts "DEBUG: #{self.class.name}.lookup(#{label})" if @debug
|
|
61
|
-
Timeout::timeout(@timeout) {
|
|
62
|
-
url = nil
|
|
63
|
-
params = {"rrType" => "", "maxResults" => limit || 1000}
|
|
64
|
-
|
|
65
|
-
if label =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
|
|
66
|
-
url = @url+"/dns/data"
|
|
67
|
-
params["ip"] = label
|
|
68
|
-
else
|
|
69
|
-
resource = api_settings[@version][:resource]
|
|
70
|
-
param = api_settings[@version][:param]
|
|
71
|
-
url = @url+"/dns/#{resource}"
|
|
72
|
-
params[param] = label
|
|
73
|
-
end
|
|
74
|
-
url << "?"
|
|
75
|
-
params.each do |k,v|
|
|
76
|
-
url << "#{k}=#{v}&"
|
|
77
|
-
end
|
|
78
|
-
url.gsub!(/\&$/,"")
|
|
79
|
-
|
|
80
|
-
$stderr.puts "DEBUG: #{self.class.name} url = #{url}" if @debug
|
|
81
|
-
url = URI.parse url
|
|
82
|
-
http = Net::HTTP.new(url.host, url.port)
|
|
83
|
-
http.use_ssl = (url.scheme == 'https')
|
|
84
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
85
|
-
http.verify_depth = 5
|
|
86
|
-
request = Net::HTTP::Get.new(url.request_uri)
|
|
87
|
-
request.add_field("User-Agent", "Ruby/#{RUBY_VERSION} passivedns-client rubygem v#{PassiveDNS::Client::VERSION}")
|
|
88
|
-
request.add_field('Accept', 'Application/JSON')
|
|
89
|
-
request.add_field('Content-Type', 'Application/JSON')
|
|
90
|
-
request.basic_auth(@token, @privkey)
|
|
91
|
-
t1 = Time.now
|
|
92
|
-
response = http.request(request)
|
|
93
|
-
t2 = Time.now
|
|
94
|
-
recs = parse_json(response.body, label, t2-t1)
|
|
95
|
-
if limit
|
|
96
|
-
recs[0,limit]
|
|
97
|
-
else
|
|
98
|
-
recs
|
|
99
|
-
end
|
|
100
|
-
}
|
|
101
|
-
rescue Timeout::Error
|
|
102
|
-
$stderr.puts "#{self.class.name} lookup timed out: #{label}"
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
private
|
|
106
|
-
|
|
107
|
-
def api_settings
|
|
108
|
-
@api_settings ||= {
|
|
109
|
-
'v1' => { server: "ws.riskiq.net", resource: 'name', param: 'name' },
|
|
110
|
-
'v2' => { server: "api.passivetotal.org", resource: 'passive', param: 'query' }
|
|
111
|
-
}
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
# parses the response of riskiq's JSON reply to generate an array of PDNSResult
|
|
115
|
-
def parse_json(page,query,response_time=0)
|
|
116
|
-
res = []
|
|
117
|
-
data = JSON.parse(page)
|
|
118
|
-
if data['message']
|
|
119
|
-
if data['message'] =~ /quota_exceeded/
|
|
120
|
-
$stderr.puts "ERROR: quota exceeded."
|
|
121
|
-
return res
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
if data['records']
|
|
125
|
-
data['records'].each do |record|
|
|
126
|
-
name = record['name'].gsub!(/\.$/,'')
|
|
127
|
-
type = record['rrtype']
|
|
128
|
-
last_seen = Time.parse(record['lastSeen'])
|
|
129
|
-
first_seen = Time.parse(record['firstSeen'])
|
|
130
|
-
count = record['count']
|
|
131
|
-
record['data'].each do |datum|
|
|
132
|
-
datum.gsub!(/\.$/,'')
|
|
133
|
-
res << PDNSResult.new(self.class.name,response_time,
|
|
134
|
-
name, datum, type, 0, first_seen, last_seen, count, 'yellow')
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
res
|
|
139
|
-
rescue Exception => e
|
|
140
|
-
$stderr.puts "#{self.class.name} Exception: #{e}"
|
|
141
|
-
raise e
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
end
|
|
145
|
-
end
|
|
146
|
-
end
|
data/passivedns-client.gemspec
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require 'passivedns/client/version'
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |spec|
|
|
7
|
-
spec.name = "passivedns-client"
|
|
8
|
-
spec.version = PassiveDNS::Client::VERSION
|
|
9
|
-
spec.authors = ["chrislee35"]
|
|
10
|
-
spec.email = ["rubygems@chrislee.dhs.org"]
|
|
11
|
-
spec.description = %q{This provides interfaces to various passive DNS databases to do the query and to normalize the responses. The query tool also allows for recursive queries, using an SQLite3 database to keep state.}
|
|
12
|
-
spec.summary = %q{Query passive DNS databases}
|
|
13
|
-
spec.homepage = "https://github.com/chrislee35/passivedns-client"
|
|
14
|
-
spec.license = "MIT"
|
|
15
|
-
|
|
16
|
-
spec.files = `git ls-files`.split($/)
|
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
-
spec.require_paths = ["lib"]
|
|
20
|
-
|
|
21
|
-
spec.add_runtime_dependency 'json'
|
|
22
|
-
spec.add_runtime_dependency 'sqlite3'
|
|
23
|
-
spec.add_runtime_dependency 'structformatter'
|
|
24
|
-
spec.add_runtime_dependency 'configparser'
|
|
25
|
-
spec.add_development_dependency "minitest"
|
|
26
|
-
spec.add_development_dependency "bundler"
|
|
27
|
-
spec.add_development_dependency "rake"
|
|
28
|
-
|
|
29
|
-
#spec.signing_key = "#{File.dirname(__FILE__)}/../gem-private_key.pem"
|
|
30
|
-
#spec.cert_chain = ["#{File.dirname(__FILE__)}/../gem-public_cert.pem"]
|
|
31
|
-
end
|