Class: DNSTraverse::Fingerprint

Inherits:
Object
  • Object
show all
Includes:
FingerprintRules
Defined in:
lib/dnstraverse/fingerprint.rb

Constant Summary collapse

FINGERPRINT_TIMEOUT =
'query timed out'.freeze

Constants included from FingerprintRules

FingerprintRules::INITRULE, FingerprintRules::IQ, FingerprintRules::QY, FingerprintRules::RULESET

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Fingerprint

Returns a new instance of Fingerprint.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dnstraverse/fingerprint.rb', line 29

def initialize(args = {})
  @version_style = args[:version_style] || :append
  cfg = Dnsruby::Config.new
  rescfg = { :nameserver => cfg.nameserver, :ndots => cfg.ndots,
    :apply_domain => false, :apply_search_list => false}
  resargs = { :config_info => rescfg, :use_tcp => false, :recurse => false,
    :packet_timeout => 2, :retry_times => 1, :retry_delay => 1,
    :dnssec => false, :ignore_truncation => true, :src_address => '0.0.0.0',
    :udp_size => 512 }
  @resolver = Dnsruby::Resolver.new(resargs)
  @resolver.udp_size = 512
  #Dnsruby.log.level = Logger::DEBUG
  #Log.level = Logger::DEBUG
end

Instance Attribute Details

#version_styleObject

:none, :override, :append



27
28
29
# File 'lib/dnstraverse/fingerprint.rb', line 27

def version_style
  @version_style
end

Instance Method Details

#decode_query(querystr) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/dnstraverse/fingerprint.rb', line 145

def decode_query(querystr)
  qname, qclass, qtype = querystr.split(/\s+/)
  if qtype == "CLASS0" then # crappy data
    qtype = qclass
    qclass = "CLASS0"
  end
  q = Dnsruby::Question.new(qname, qtype, qclass)
  return q
end

#fingerprint(ip) ⇒ Object



44
45
46
# File 'lib/dnstraverse/fingerprint.rb', line 44

def fingerprint(ip)
  return process(ip, INITRULE[:header], INITRULE[:query], RULESET)
end

#fp2header(headerstr) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dnstraverse/fingerprint.rb', line 124

def fp2header(headerstr)
  list = headerstr.split(/,/)
  h = Dnsruby::Header.new
  h.qr = list.shift == '1' ? true : false
  opcode = list.shift
  opcode = "Notify" if opcode == "NS_NOTIFY_OP"
  h.opcode = opcode
  h.aa = list.shift == '1' ? true : false
  h.tc = list.shift == '1' ? true : false
  h.rd = list.shift == '1' ? true : false
  h.ra = list.shift == '1' ? true : false
  h.ad = list.shift == '1' ? true : false
  h.cd = list.shift == '1' ? true : false
  h.rcode = list.shift
  h.qdcount = list.shift.to_i
  h.ancount = list.shift.to_i
  h.nscount = list.shift.to_i
  h.arcount = list.shift.to_i
  return h
end

#header2fp(header) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dnstraverse/fingerprint.rb', line 111

def header2fp(header)
  list = [ header.qr, header.opcode, header.aa, header.tc, header.rd,
  header.ra, header.ad, header.cd, header.get_header_rcode, header.qdcount,
  header.ancount, header.nscount, header.arcount ]
  list.map! do | item |
    next '0' if item.instance_of? FalseClass
    next '1' if item.instance_of? TrueClass
    next "NS_NOTIFY_OP" if item == "Notify"
    item
  end
  return list.join(',').upcase
end

#probe(ip, headerstr, query) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dnstraverse/fingerprint.rb', line 155

def probe(ip, headerstr, query)
  @resolver.nameserver = ip
  @resolver.dnssec = false
  msg = Dnsruby::Message.new
  msg.header = fp2header(headerstr)
  msg.add_question(decode_query(query))
  result, error = @resolver.send_plain_message(msg)
  ans = result || error
  return nil, FINGERPRINT_TIMEOUT if ans.is_a? Dnsruby::ResolvTimeout
  return nil, ans.to_s if ans.is_a? Exception
  return ans, nil
end

#process(ip, header, query, ruleset) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dnstraverse/fingerprint.rb', line 48

def process(ip, header, query, ruleset)
  ret = Hash.new
  answer, errstr = probe(ip, header, query)
  id = answer ? header2fp(answer.header) : errstr
  Log.debug { "query = #{query}, id = #{id}" }
  for rule in ruleset do
    raise "Missing fingerprint" unless rule.has_key?(:fingerprint)
    next unless id =~ /#{rule[:fingerprint]}/
    if rule.has_key?(:result) then
      result = rule[:result]
      if result.is_a? String
        ret[:state] = result
        ret[:error] = result
        ret[:id] = id
        return ret
      end
      for k in [:vendor, :product, :option] do
        ret[k] = result[k] if result.has_key?(k) and result[k].length > 0
      end
      case @version_style
      when :none
        ret[:version] = result[:version]
      when :append
        ver = query_version(ip, result[:qv])
        ver = query_version(ip, "version.bind") unless ver
        ret[:version] = result[:version]
        ret[:version]+= " (#{ver})" if ver
      when :override
        ver = query_version(ip, result[:qv])
        ver = query_version(ip, "version.bind") unless ver
        ret[:version] = ver ? ver : result[:version]
      end
      return ret
    end
    if rule.has_key?(:state) then
      ret[:state] = rule[:state]
      ret[:error] = "No match found"
      ret[:id] = id
      return ret
    end
    query = rule[:query] if rule.has_key?(:query)
    if rule.has_key?(:header) and rule.has_key?(:ruleset) then
      return process(ip, rule[:header], query, rule[:ruleset])
    end
    raise "Invalid ruleset -- no next step"
  end
  raise "Invalid ruleset -- fell off end"
end

#query_version(ip, name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dnstraverse/fingerprint.rb', line 97

def query_version(ip, name)
  @resolver.nameserver = ip
  @resolver.dnssec = false
  begin
    msg = @resolver.query(name, 'TXT', 'CH')
    if msg.answer.size > 0 then
      ver = msg.answer[0].data.sub(/[^0-9a-zA-Z. :!?-]/, '')
      return ver.length > 0 ? ver : nil
    end
  rescue Exception
    return nil
  end
end