dnsbl-client 1.1.1 → 2.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.
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resolv'
4
+
5
+ # This is a monkeypatch for the built-in Ruby DNS resolver to specify nameservers
6
+ class Resolv
7
+ class DNS
8
+ class Config
9
+ # Monkeypatch the nameservers to set a default if there are no defined nameservers
10
+ def nameservers
11
+ return @nameservers if defined?(@nameservers)
12
+
13
+ lazy_initialize
14
+ if respond_to? :nameserver_port
15
+ @nameservers = nameserver_port
16
+ else
17
+ @nameserver ||= ['4.2.2.2',
18
+ '4.2.2.5',
19
+ '8.8.4.4',
20
+ '8.8.8.8',
21
+ '208.67.222.222',
22
+ '208.67.220.220'].shuffle
23
+ @nameservers ||= @nameserver.map { |i| [i, 53] }
24
+ end
25
+ @nameservers
26
+ end
27
+ end
28
+ end
29
+ end
@@ -3,6 +3,6 @@
3
3
  module DNSBL # :nodoc:
4
4
  class Client
5
5
  # Current version of the dnsbl-client gem
6
- VERSION = '1.1.1'
6
+ VERSION = '2.0.0'
7
7
  end
8
8
  end
data/lib/dnsbl/client.rb CHANGED
@@ -7,32 +7,7 @@ require 'resolv'
7
7
  require 'socket'
8
8
  require 'yaml'
9
9
  require 'ipaddr'
10
-
11
- # This is a monkeypatch for the built-in Ruby DNS resolver to specify nameservers
12
- class Resolv
13
- class DNS
14
- class Config
15
- # Monkeypatch the nameservers to set a default if there are no defined nameservers
16
- def nameservers
17
- return @nameservers if defined?(@nameservers)
18
-
19
- lazy_initialize
20
- if respond_to? :nameserver_port
21
- @nameservers = nameserver_port
22
- else
23
- @nameserver ||= ['4.2.2.2',
24
- '4.2.2.5',
25
- '8.8.4.4',
26
- '8.8.8.8',
27
- '208.67.222.222',
28
- '208.67.220.220'].shuffle
29
- @nameservers ||= @nameserver.map { |i| [i, 53] }
30
- end
31
- @nameservers
32
- end
33
- end
34
- end
35
- end
10
+ require 'dnsbl/client/resolv_patch'
36
11
 
37
12
  module DNSBL # :nodoc:
38
13
  # DNSBLResult holds the result of a DNSBL lookup
@@ -51,7 +26,7 @@ module DNSBL # :nodoc:
51
26
  # initialize a new DNSBL::Client object
52
27
  # the config file automatically points to a YAML file containing the list of DNSBLs and their return codes
53
28
  # the two-level-tlds file lists most of the two level tlds, needed for hostname to domain normalization
54
- def initialize(config = YAML.safe_load(File.read("#{File.expand_path '../../data', __dir__}/dnsbl.yaml")),
29
+ def initialize(config = YAML.safe_load_file("#{File.expand_path '../../data', __dir__}/dnsbl.yaml"),
55
30
  two_level_tldfile = "#{File.expand_path '../../data', __dir__}/two-level-tlds",
56
31
  three_level_tldfile = "#{File.expand_path '../../data', __dir__}/three-level-tlds")
57
32
  @dnsbls = config
@@ -59,12 +34,8 @@ module DNSBL # :nodoc:
59
34
  @first_only = false
60
35
  @two_level_tld = []
61
36
  @three_level_tld = []
62
- File.open(two_level_tldfile).readlines.each do |l|
63
- @two_level_tld << l.strip
64
- end
65
- File.open(three_level_tldfile).readlines.each do |l|
66
- @three_level_tld << l.strip
67
- end
37
+ @two_level_tld = File.readlines(two_level_tldfile).map(&:strip)
38
+ @three_level_tld = File.readlines(three_level_tldfile).map(&:strip)
68
39
  @sockets = []
69
40
  config = Resolv::DNS::Config.new
70
41
 
@@ -92,23 +63,25 @@ module DNSBL # :nodoc:
92
63
  end
93
64
 
94
65
  # Converts a hostname to the domain: e.g., www.google.com => google.com, science.somewhere.co.uk => somewhere.co.uk
66
+ # The domain is the longest public suffix plus one more label, using the two- and three-level suffix lists.
95
67
  def normalize(domain)
96
68
  # strip off the protocol (\w{1,20}://), the URI (/), parameters (?), port number (:), and username (.*@)
97
69
  # then split into parts via the .
98
70
  parts = domain.gsub(%r{^\w{1,20}://}, '').gsub(%r{[/?:].*}, '').gsub(/.*?@/, '').split('.')
99
- # grab the last two parts of the domain
100
- dom = parts[-2, 2].join '.'
101
- # if the dom is in the two_level_tld list, then use three parts
102
- dom = parts[-3, 3].join '.' if @two_level_tld.index dom
103
- dom = parts[-4, 4].join '.' if @three_level_tld.index dom
104
- dom
71
+ suffix_length = public_suffix_length parts
72
+ # an item that is itself just a suffix (e.g. co.uk) has no extra label to include and is returned as is
73
+ parts.last(suffix_length + 1).join '.'
105
74
  end
106
75
 
107
76
  # allows the adding of a new DNSBL to the set of configured DNSBLs
108
- def add_dnsbl(name, domain, type = 'ip', codes = { '0' => 'OK', '127.0.0.2' => 'Blacklisted' })
77
+ # set ipv6 to true only if the list answers IPv6 queries, otherwise IPv6 addresses are not sent to it.
78
+ # set bitmask to true if the list combines categories by adding the codes of each (e.g. 127.0.0.12 is both 127.0.0.4 and 127.0.0.8)
79
+ def add_dnsbl(name, domain, type = 'ip', codes = { '0' => 'OK', '127.0.0.2' => 'Blacklisted' }, ipv6: false, bitmask: false)
109
80
  @dnsbls[name] = codes
110
81
  @dnsbls[name]['domain'] = domain
111
82
  @dnsbls[name]['type'] = type
83
+ @dnsbls[name]['ipv6'] = true if ipv6
84
+ @dnsbls[name]['bitmask'] = true if bitmask
112
85
  end
113
86
 
114
87
  # returns a list of DNSBL names currently configured
@@ -130,17 +103,16 @@ module DNSBL # :nodoc:
130
103
  sent = 0
131
104
  # record the start time
132
105
  @starttime = Time.now.to_f
133
- # determine the type of query
134
- itemtype = item.match?(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) ? 'ip' : 'domain'
135
- if item.match?(/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/) # rubocop: disable Layout/LineLength
136
- itemtype = 'ip'
137
- end
106
+ # determine the type of query: an IPv4 or IPv6 address is an 'ip' lookup, anything else is a domain
107
+ ip = parse_ip item
108
+ itemtype = ip ? 'ip' : 'domain'
138
109
 
139
110
  # for each dnsbl that supports our type, create the DNS query packet and send it
140
111
  # rotate across our configured name servers and increment sent
141
112
  @dnsbls.each do |name, config|
142
113
  next if config['disabled']
143
114
  next unless config['type'] == itemtype
115
+ next if ip&.ipv6? && !config['ipv6']
144
116
 
145
117
  begin
146
118
  msg = encode_query item, itemtype, config['domain'], config['apikey']
@@ -149,7 +121,7 @@ module DNSBL # :nodoc:
149
121
  @socket_index %= @sockets.length
150
122
  sent += 1
151
123
  rescue StandardError => e
152
- puts "error for #{name}: e"
124
+ puts "error for #{name}: #{e}"
153
125
  puts e.backtrace.join("\n")
154
126
  end
155
127
  end
@@ -185,12 +157,40 @@ module DNSBL # :nodoc:
185
157
 
186
158
  private
187
159
 
160
+ # returns how many trailing labels of parts form the longest public suffix (at least one, the TLD).
161
+ # list entries are suffixes (co.uk), wildcards that make every name one label below them a suffix (*.ck),
162
+ # and exceptions to a wildcard (!www.ck), which are themselves registrable so their suffix is one label shorter
163
+ def public_suffix_length(parts)
164
+ [[3, @three_level_tld], [2, @two_level_tld]].each do |count, list|
165
+ next if parts.length < count
166
+
167
+ labels = parts.last count
168
+ name = labels.join '.'
169
+ return count - 1 if list.include? "!#{name}"
170
+ return count if list.include?(name) || list.include?("*.#{labels.drop(1).join '.'}")
171
+ end
172
+ 1
173
+ end
174
+
175
+ # parses an IPv4 or IPv6 address (ignoring surrounding whitespace and any IPv6 zone id), returns nil if the item is not an address
176
+ def parse_ip(item)
177
+ return if item.include? '/'
178
+
179
+ IPAddr.new item.strip.sub(/%.*\z/, '')
180
+ rescue IPAddr::Error
181
+ nil
182
+ end
183
+
184
+ # converts the nibble-reversed label of an IPv6 query back into a normal IPv6 address
185
+ def ipv6_from_nibbles(label)
186
+ IPAddr.new(label.delete('.').reverse.scan(/.{4}/).join(':')).to_s
187
+ end
188
+
188
189
  # converts an ip or a hostname into the DNS query packet requires to lookup the result
189
190
  def encode_query(item, itemtype, domain, apikey = nil)
190
191
  label = case itemtype
191
192
  when 'ip'
192
- ip = IPAddr.new item
193
- ip.reverse.gsub('.ip6.arpa', '').gsub('.in-addr.arpa', '')
193
+ parse_ip(item).reverse.gsub('.ip6.arpa', '').gsub('.in-addr.arpa', '')
194
194
  when 'domain'
195
195
  normalize item
196
196
  end
@@ -209,30 +209,23 @@ module DNSBL # :nodoc:
209
209
  reply = Resolv::DNS::Message.decode buf
210
210
  results = []
211
211
  reply.each_answer do |name, _ttl, data|
212
- if name.to_s =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(.+)$/
212
+ # IPv6 nibble labels must be checked first: their leading digits can also look like an IPv4 address
213
+ if name.to_s =~ /^((?:\h\.){31}\h)\.(.+)$/
214
+ results.concat ip_results(name, ipv6_from_nibbles(Regexp.last_match(1)), Regexp.last_match(2), data)
215
+ elsif name.to_s =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(.+)$/
213
216
  ip = [Regexp.last_match(4),
214
217
  Regexp.last_match(3),
215
218
  Regexp.last_match(2),
216
219
  Regexp.last_match(1)].join '.'
217
- domain = Regexp.last_match 5
218
- @dnsbls.each do |dnsblname, config|
219
- next unless data.is_a? Resolv::DNS::Resource::IN::A
220
- next unless domain == config['domain']
221
-
222
- meaning = config[data.address.to_s] || data.address.to_s
223
- results << DNSBLResult.new(dnsblname, ip, name.to_s, data.address.to_s, meaning, Time.now.to_f - @starttime)
224
- break
225
- end
220
+ results.concat ip_results(name, ip, Regexp.last_match(5), data)
226
221
  else
227
222
  @dnsbls.each do |dnsblname, config|
228
- next unless name.to_s.end_with? config['domain']
223
+ next unless name.to_s.end_with? ".#{config['domain']}"
229
224
 
230
225
  meaning = if config['decoder']
231
- send "#{config['decoder']}_decoder".to_sym, data.address.to_s
232
- elsif config[data.address.to_s]
233
- config[data.address.to_s]
226
+ send :"#{config['decoder']}_decoder", data.address.to_s
234
227
  else
235
- data.address.to_s
228
+ meaning_for config, data.address.to_s
236
229
  end
237
230
 
238
231
  results << DNSBLResult.new(dnsblname, name.to_s.gsub(".#{config['domain']}", ''),
@@ -247,6 +240,31 @@ module DNSBL # :nodoc:
247
240
  results
248
241
  end
249
242
 
243
+ # returns the meaning of a result code for a list: an exact match in the list's configuration, or, for a list with the bitmask
244
+ # option (SURBL), the meanings of every flag set in the last octet joined together (127.0.0.12 is the 4 and 8 flags).
245
+ # Falls back to the code itself when nothing is known about it.
246
+ def meaning_for(config, code)
247
+ return config[code] if config[code]
248
+ return code unless config['bitmask']
249
+
250
+ prefix, last = code.rpartition('.').values_at 0, 2
251
+ meanings = (1..7).filter_map { |bit| config["#{prefix}.#{1 << bit}"] if last.to_i.anybits? 1 << bit }
252
+ meanings.empty? ? code : meanings.join(', ')
253
+ end
254
+
255
+ # builds the result (at most one) for an answer to an IP query against the list that owns the queried domain
256
+ def ip_results(name, ip, domain, data)
257
+ return [] unless data.is_a? Resolv::DNS::Resource::IN::A
258
+
259
+ @dnsbls.each do |dnsblname, config|
260
+ next unless domain == config['domain']
261
+
262
+ meaning = meaning_for config, data.address.to_s
263
+ return [DNSBLResult.new(dnsblname, ip, name.to_s, data.address.to_s, meaning, Time.now.to_f - @starttime)]
264
+ end
265
+ []
266
+ end
267
+
250
268
  # decodes the response from Project Honey Pot's service
251
269
  def phpot_decoder(ip)
252
270
  octets = ip.split '.'
@@ -264,10 +282,10 @@ module DNSBL # :nodoc:
264
282
  days, threatscore, flags = octets[1, 3]
265
283
  flags = flags.to_i
266
284
  types = []
267
- types << 'suspicious' if (flags & 0x1) == 0x1
268
- types << 'harvester' if (flags & 0x2) == 0x2
269
- types << 'comment spammer' if (flags & 0x4) == 0x4
270
- types << 'reserved' if (flags & 0xf8).positive?
285
+ types << 'suspicious' if flags.allbits? 0x1
286
+ types << 'harvester' if flags.allbits? 0x2
287
+ types << 'comment spammer' if flags.allbits? 0x4
288
+ types << 'reserved' if flags.anybits? 0xf8
271
289
  type = types.join ','
272
290
  "days=#{days},score=#{threatscore},type=#{type}"
273
291
  end
metadata CHANGED
@@ -1,85 +1,70 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dnsbl-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - chrislee35
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-11-06 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: bundler
13
+ name: minitest
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
16
  - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: '2.3'
18
+ version: '6.0'
20
19
  type: :development
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
- version: '2.3'
25
+ version: '6.0'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: rake
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
30
  - - "~>"
32
31
  - !ruby/object:Gem::Version
33
- version: '13'
32
+ version: '13.0'
34
33
  type: :development
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: '13'
41
- - !ruby/object:Gem::Dependency
42
- name: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
39
+ version: '13.0'
55
40
  - !ruby/object:Gem::Dependency
56
41
  name: rubocop
57
42
  requirement: !ruby/object:Gem::Requirement
58
43
  requirements:
59
- - - ">="
44
+ - - "~>"
60
45
  - !ruby/object:Gem::Version
61
- version: '0'
46
+ version: '1.0'
62
47
  type: :development
63
48
  prerelease: false
64
49
  version_requirements: !ruby/object:Gem::Requirement
65
50
  requirements:
66
- - - ">="
51
+ - - "~>"
67
52
  - !ruby/object:Gem::Version
68
- version: '0'
53
+ version: '1.0'
69
54
  - !ruby/object:Gem::Dependency
70
55
  name: rubocop-performance
71
56
  requirement: !ruby/object:Gem::Requirement
72
57
  requirements:
73
- - - ">="
58
+ - - "~>"
74
59
  - !ruby/object:Gem::Version
75
- version: '0'
60
+ version: '1.0'
76
61
  type: :development
77
62
  prerelease: false
78
63
  version_requirements: !ruby/object:Gem::Requirement
79
64
  requirements:
80
- - - ">="
65
+ - - "~>"
81
66
  - !ruby/object:Gem::Version
82
- version: '0'
67
+ version: '1.0'
83
68
  description: simple interface to lookup blacklists results
84
69
  email:
85
70
  - rubygems@chrislee.dhs.org
@@ -88,19 +73,25 @@ executables:
88
73
  extensions: []
89
74
  extra_rdoc_files: []
90
75
  files:
76
+ - CHANGELOG.md
77
+ - LICENSE.txt
78
+ - README.md
91
79
  - bin/dnsbl-client
92
80
  - data/dnsbl.yaml
93
81
  - data/three-level-tlds
94
82
  - data/two-level-tlds
95
83
  - lib/dnsbl/client.rb
84
+ - lib/dnsbl/client/resolv_patch.rb
96
85
  - lib/dnsbl/client/version.rb
97
- - test/helper.rb
98
- - test/test_dnsbl-client.rb
99
- homepage: http://github.com/chrislee35/dnsbl-client
86
+ homepage: https://github.com/chrislee35/dnsbl-client
100
87
  licenses:
101
88
  - MIT
102
- metadata: {}
103
- post_install_message:
89
+ metadata:
90
+ source_code_uri: https://github.com/chrislee35/dnsbl-client
91
+ bug_tracker_uri: https://github.com/chrislee35/dnsbl-client/issues
92
+ changelog_uri: https://github.com/chrislee35/dnsbl-client/blob/master/CHANGELOG.md
93
+ documentation_uri: https://chrislee35.github.io/dnsbl-client/
94
+ rubygems_mfa_required: 'true'
104
95
  rdoc_options: []
105
96
  require_paths:
106
97
  - lib
@@ -108,17 +99,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
99
  requirements:
109
100
  - - ">="
110
101
  - !ruby/object:Gem::Version
111
- version: '2.7'
102
+ version: '3.2'
112
103
  required_rubygems_version: !ruby/object:Gem::Requirement
113
104
  requirements:
114
105
  - - ">="
115
106
  - !ruby/object:Gem::Version
116
107
  version: '0'
117
108
  requirements: []
118
- rubygems_version: 3.1.2
119
- signing_key:
109
+ rubygems_version: 3.7.2
120
110
  specification_version: 4
121
111
  summary: queries various DNS Blacklists
122
- test_files:
123
- - test/helper.rb
124
- - test/test_dnsbl-client.rb
112
+ test_files: []
data/test/helper.rb DELETED
@@ -1,6 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
-
5
- require 'dnsbl/client'
6
- require 'minitest/autorun'
@@ -1,209 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- unless Kernel.respond_to? :require_relative
4
- module Kernel
5
- def require_relative(path)
6
- require File.join(File.dirname(caller(1..1).first), path.to_str)
7
- end
8
- end
9
- end
10
-
11
- require_relative 'helper'
12
-
13
- class TestDNSBLClient < Minitest::Test
14
- NAME_SERVERS = [['4.2.2.2', 53]].freeze
15
-
16
- def test_return_no_hits_for_0_0_0_254
17
- c = DNSBL::Client.new
18
- c.nameservers = NAME_SERVERS
19
- # for some reason DRONEBL returns 127.0.0.255 when queried for 127.0.0.255, so I'll use 127.0.0.254
20
- # spfbl started returning 127.0.0.254 for 127.0.0.254, so I'll try 0.0.0.254
21
- res = c.lookup '0.0.0.254'
22
- puts res if res.length.positive?
23
- assert res.length.zero?
24
- end
25
-
26
- def test_return_all_lists_for_127_0_0_2
27
- skip 'not work anymore'
28
-
29
- c = DNSBL::Client.new
30
- c.nameservers = NAME_SERVERS
31
- res = c.lookup '127.0.0.2'
32
- c.dnsbls.each do |bls|
33
- assert (res.detect { |ci| ci.dnsbl == bls }), "#{bls} missing"
34
- end
35
- assert res.count >= c.dnsbls.count
36
- end
37
-
38
- def test_return_results_for_bad_domains
39
- c = DNSBL::Client.new
40
- c.nameservers = NAME_SERVERS
41
- res = c.lookup 'pfizer.viagra.aqybasej.gurdoctor.com'
42
- assert res.length >= 0
43
- end
44
-
45
- def test_interpret_project_honeypot_results
46
- apikey = ENV.fetch 'PHPAPIKEY', nil
47
- skip 'Project Honeypot API Key Required for this test. Please set PHPAPIKEY.' unless apikey
48
-
49
- config = YAML.safe_load("---
50
- PROJECTHONEYPOT:
51
- domain: dnsbl.httpbl.org
52
- type: ip
53
- apikey: #{apikey}
54
- decoder: phpot")
55
- c = DNSBL::Client.new config
56
- c.nameservers = NAME_SERVERS
57
- res = c.lookup '127.0.0.1'
58
- assert_equal 0, res.length
59
- res = c.lookup '127.1.1.0'
60
- assert_equal 1, res.length
61
- assert_equal "#{apikey}.0.1.1.127.dnsbl.httpbl.org", res[0].query
62
- assert_equal '127.1.1.0', res[0].result
63
- assert_equal 'type=search engine,engine=AltaVista', res[0].meaning
64
- res = c.lookup '127.1.1.1'
65
- assert_equal 1, res.length
66
- assert_equal "#{apikey}.1.1.1.127", res[0].item
67
- assert_equal "#{apikey}.1.1.1.127.dnsbl.httpbl.org", res[0].query
68
- assert_equal '127.1.1.1', res[0].result
69
- assert_equal 'days=1,score=1,type=suspicious', res[0].meaning
70
- res = c.lookup '127.1.1.2'
71
- assert_equal 1, res.length
72
- assert_equal "#{apikey}.2.1.1.127", res[0].item
73
- assert_equal "#{apikey}.2.1.1.127.dnsbl.httpbl.org", res[0].query
74
- assert_equal '127.1.1.2', res[0].result
75
- assert_equal 'days=1,score=1,type=harvester', res[0].meaning
76
- res = c.lookup '127.1.1.3'
77
- assert_equal 1, res.length
78
- assert_equal "#{apikey}.3.1.1.127", res[0].item
79
- assert_equal "#{apikey}.3.1.1.127.dnsbl.httpbl.org", res[0].query
80
- assert_equal '127.1.1.3', res[0].result
81
- assert_equal 'days=1,score=1,type=suspicious,harvester', res[0].meaning
82
- res = c.lookup '127.1.1.4'
83
- assert_equal 1, res.length
84
- assert_equal "#{apikey}.4.1.1.127", res[0].item
85
- assert_equal "#{apikey}.4.1.1.127.dnsbl.httpbl.org", res[0].query
86
- assert_equal '127.1.1.4', res[0].result
87
- assert_equal 'days=1,score=1,type=comment spammer', res[0].meaning
88
- res = c.lookup '127.1.1.5'
89
- assert_equal 1, res.length
90
- assert_equal "#{apikey}.5.1.1.127", res[0].item
91
- assert_equal "#{apikey}.5.1.1.127.dnsbl.httpbl.org", res[0].query
92
- assert_equal '127.1.1.5', res[0].result
93
- assert_equal 'days=1,score=1,type=suspicious,comment spammer', res[0].meaning
94
- res = c.lookup '127.1.1.6'
95
- assert_equal 1, res.length
96
- assert_equal "#{apikey}.6.1.1.127", res[0].item
97
- assert_equal "#{apikey}.6.1.1.127.dnsbl.httpbl.org", res[0].query
98
- assert_equal '127.1.1.6', res[0].result
99
- assert_equal 'days=1,score=1,type=harvester,comment spammer', res[0].meaning
100
- res = c.lookup '127.1.1.7'
101
- assert_equal 1, res.length
102
- assert_equal "#{apikey}.7.1.1.127", res[0].item
103
- assert_equal "#{apikey}.7.1.1.127.dnsbl.httpbl.org", res[0].query
104
- assert_equal '127.1.1.7', res[0].result
105
- assert_equal 'days=1,score=1,type=suspicious,harvester,comment spammer', res[0].meaning
106
- res = c.lookup '127.1.10.1'
107
- assert_equal 1, res.length
108
- assert_equal "#{apikey}.1.10.1.127", res[0].item
109
- assert_equal "#{apikey}.1.10.1.127.dnsbl.httpbl.org", res[0].query
110
- assert_equal '127.1.10.1', res[0].result
111
- assert_equal 'days=1,score=10,type=suspicious', res[0].meaning
112
- res = c.lookup '127.1.20.1'
113
- assert_equal 1, res.length
114
- assert_equal "#{apikey}.1.20.1.127", res[0].item
115
- assert_equal "#{apikey}.1.20.1.127.dnsbl.httpbl.org", res[0].query
116
- assert_equal '127.1.20.1', res[0].result
117
- assert_equal 'days=1,score=20,type=suspicious', res[0].meaning
118
- res = c.lookup '127.1.40.1'
119
- assert_equal 1, res.length
120
- assert_equal "#{apikey}.1.40.1.127", res[0].item
121
- assert_equal "#{apikey}.1.40.1.127.dnsbl.httpbl.org", res[0].query
122
- assert_equal '127.1.40.1', res[0].result
123
- assert_equal 'days=1,score=40,type=suspicious', res[0].meaning
124
- res = c.lookup '127.1.80.1'
125
- assert_equal 1, res.length
126
- assert_equal "#{apikey}.1.80.1.127", res[0].item
127
- assert_equal "#{apikey}.1.80.1.127.dnsbl.httpbl.org", res[0].query
128
- assert_equal '127.1.80.1', res[0].result
129
- assert_equal 'days=1,score=80,type=suspicious', res[0].meaning
130
- res = c.lookup '127.10.1.1'
131
- assert_equal 1, res.length
132
- assert_equal "#{apikey}.1.1.10.127", res[0].item
133
- assert_equal "#{apikey}.1.1.10.127.dnsbl.httpbl.org", res[0].query
134
- assert_equal '127.10.1.1', res[0].result
135
- assert_equal 'days=10,score=1,type=suspicious', res[0].meaning
136
- res = c.lookup '127.20.1.1'
137
- assert_equal 1, res.length
138
- assert_equal "#{apikey}.1.1.20.127", res[0].item
139
- assert_equal "#{apikey}.1.1.20.127.dnsbl.httpbl.org", res[0].query
140
- assert_equal '127.20.1.1', res[0].result
141
- assert_equal 'days=20,score=1,type=suspicious', res[0].meaning
142
- res = c.lookup '127.40.1.1'
143
- assert_equal 1, res.length
144
- assert_equal "#{apikey}.1.1.40.127", res[0].item
145
- assert_equal "#{apikey}.1.1.40.127.dnsbl.httpbl.org", res[0].query
146
- assert_equal '127.40.1.1', res[0].result
147
- assert_equal 'days=40,score=1,type=suspicious', res[0].meaning
148
- res = c.lookup '127.80.1.1'
149
- assert_equal 1, res.length
150
- assert_equal "#{apikey}.1.1.80.127", res[0].item
151
- assert_equal "#{apikey}.1.1.80.127.dnsbl.httpbl.org", res[0].query
152
- assert_equal '127.80.1.1', res[0].result
153
- assert_equal 'days=80,score=1,type=suspicious', res[0].meaning
154
- res = c.send :phpot_decoder, '127.0.0.0'
155
- assert_equal 'type=search engine,engine=undocumented', res
156
- res = c.send :phpot_decoder, '127.0.1.0'
157
- assert_equal 'type=search engine,engine=AltaVista', res
158
- res = c.send :phpot_decoder, '127.0.2.0'
159
- assert_equal 'type=search engine,engine=Ask', res
160
- res = c.send :phpot_decoder, '127.0.3.0'
161
- assert_equal 'type=search engine,engine=Baidu', res
162
- res = c.send :phpot_decoder, '127.0.4.0'
163
- assert_equal 'type=search engine,engine=Excite', res
164
- res = c.send :phpot_decoder, '127.0.5.0'
165
- assert_equal 'type=search engine,engine=Google', res
166
- res = c.send :phpot_decoder, '127.0.6.0'
167
- assert_equal 'type=search engine,engine=Looksmart', res
168
- res = c.send :phpot_decoder, '127.0.7.0'
169
- assert_equal 'type=search engine,engine=Lycos', res
170
- res = c.send :phpot_decoder, '127.0.8.0'
171
- assert_equal 'type=search engine,engine=MSN', res
172
- res = c.send :phpot_decoder, '127.0.9.0'
173
- assert_equal 'type=search engine,engine=Yahoo', res
174
- res = c.send :phpot_decoder, '127.0.10.0'
175
- assert_equal 'type=search engine,engine=Cuil', res
176
- res = c.send :phpot_decoder, '127.0.11.0'
177
- assert_equal 'type=search engine,engine=InfoSeek', res
178
- res = c.send :phpot_decoder, '127.0.12.0'
179
- assert_equal 'type=search engine,engine=Miscellaneous', res
180
- end
181
-
182
- def test_normalize_domains_to_two_levels_if_it_s_neither_in_two_level_nor_three_level_list
183
- c = DNSBL::Client.new
184
- c.nameservers = NAME_SERVERS
185
-
186
- assert_equal 'example.org', c.normalize('example.org')
187
- assert_equal 'example.org', c.normalize('www.example.org')
188
- assert_equal 'example.org', c.normalize('foo.bar.baz.example.org')
189
- end
190
-
191
- def test_normaize_domains_to_three_levels_if_it_s_in_two_level_list
192
- c = DNSBL::Client.new
193
- c.nameservers = NAME_SERVERS
194
-
195
- assert_equal 'example.co.uk', c.normalize('example.co.uk')
196
- assert_equal 'example.co.uk', c.normalize('www.example.co.uk')
197
- assert_equal 'example.co.uk', c.normalize('foo.bar.baz.example.co.uk')
198
- assert_equal 'example.blogspot.com', c.normalize('example.blogspot.com')
199
- end
200
-
201
- def test_normalize_domains_to_four_levels_if_it_s_in_three_level_list
202
- c = DNSBL::Client.new
203
- c.nameservers = NAME_SERVERS
204
-
205
- assert_equal 'example.act.edu.au', c.normalize('example.act.edu.au')
206
- assert_equal 'example.act.edu.au', c.normalize('www.example.act.edu.au')
207
- assert_equal 'example.act.edu.au', c.normalize('foo.bar.example.act.edu.au')
208
- end
209
- end