class PassiveDNS::PDNSToolState
holds state in memory of the queue to be queried, records returned, and the level of recursion
Attributes
:debug enables verbose logging to standard output
:level is the recursion depth
Public Class Methods
Source
# File lib/passivedns/client/state.rb, line 19 def initialize @queue = [] @recs = [] @level = 0 end
creates a new, blank PDNSToolState instance
Public Instance Methods
Source
# File lib/passivedns/client/state.rb, line 57 def add_query(query, state, level = @level + 1) query = query.split(/ /, 2)[1] if /^\d+ \w+\./.match?(query) return if get_state query puts "Adding query: #{query}, #{state}, #{level}" if @debug @queue << PDNSQueueEntry.new(query, state, level) end
adding a query to the queue of things to be queried, but only if the query isn’t already queued or answered
Source
# File lib/passivedns/client/state.rb, line 31 def add_result(res) @recs << res add_query res.answer, 'pending' add_query res.query, 'pending' end
adds the record to the list of records received and tries to add the answer and query back to the queue for future query
Source
# File lib/passivedns/client/state.rb, line 66 def each_query(max_level = 20) @queue.each do |q| next unless (q.state == 'pending') || (q.state == 'failed') @level = q.level q.state = 'queried' yield q.query if q.level < max_level end end
returns each query waiting on the queue
Source
# File lib/passivedns/client/state.rb, line 49 def get_state(query) @queue.each do |q| return q.state if q.query == query end false end
returns the state of a provided query
Source
# File lib/passivedns/client/state.rb, line 26 def next_result(&) @recs.each(&) end
returns the next record
Source
# File lib/passivedns/client/state.rb, line 77 def to_gdf output = "nodedef> name,description VARCHAR(12),color,style\n" # IP "$node2,,white,1" # domain "$node2,,gray,2" # Struct.new(:query, :answer, :rrtype, :ttl, :firstseen, :lastseen) colors = { 'MX' => 'green', 'A' => 'blue', 'CNAME' => 'pink', 'NS' => 'red', 'SOA' => 'white', 'PTR' => 'purple', 'TXT' => 'brown' } nodes = {} edges = {} next_result do |i| if i nodes["#{i.query},,gray,2"] = true if /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/.match?(i.answer) nodes["#{i.answer},,white,1"] = true else nodes["#{i.answer},,gray,2"] = true end color = colors[i.rrtype] color ||= 'blue' edges["#{i.query},#{i.answer},#{color}"] = true end end nodes.each_key do |i| output += "#{i}\n" end output += "edgedef> node1,node2,color\n" edges.each_key do |i| output += "#{i}\n" end output end
transforms a set of results into GDF syntax
Source
# File lib/passivedns/client/state.rb, line 131 def to_graphml output = '<?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> <graph id="G" edgedefault="directed"> ' nodes = {} edges = {} next_result do |r| if r output += " <node id='#{r.query}'/>\n" unless nodes[r.query.to_s] nodes[r.query] = true output += " <node id='#{r.answer}'/>\n" unless nodes[r.answer.to_s] nodes[r.answer] = true output += " <edge source='#{r.query}' target='#{r.answer}'/>\n" unless edges["#{r.query}|#{r.answer}"] end end output += "</graph></graphml>\n" end
transforms a set of results into graphml syntax
Source
# File lib/passivedns/client/state.rb, line 109 def to_graphviz colors = { 'MX' => 'green', 'A' => 'blue', 'CNAME' => 'pink', 'NS' => 'red', 'SOA' => 'white', 'PTR' => 'purple', 'TXT' => 'brown' } output = "graph pdns {\n" nodes = {} next_result do |l| if l unless nodes[l.query] output += " \"#{l.query}\" [shape=ellipse, style=filled, color=gray];\n" output += if /^\d{3}\.\d{3}\.\d{3}\.\d{3}$/.match?(l.answer) " \"#{l.answer}\" [shape=box, style=filled, color=white];\n" else " \"#{l.answer}\" [shape=ellipse, style=filled, color=gray];\n" end nodes[l.query] = true end output += " \"#{l.query}\" -- \"#{l.answer}\" [color=#{colors[l.rrtype]}];\n" end end output += "}\n" end
transforms a set of results into graphviz syntax
Source
# File lib/passivedns/client/state.rb, line 175 def to_json(*_args) output = "[\n" sep = '' next_result do |rec| output += sep output += rec.to_json sep = ",\n" end output += "\n]\n" end
transforms a set of results into JSON
Source
# File lib/passivedns/client/state.rb, line 187 def to_s(sep = "\t") output = '' next_result do |rec| output += "#{rec.to_s sep}\n" end output end
transforms a set of results into a text string
Source
# File lib/passivedns/client/state.rb, line 154 def to_xml output = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" output += "<report>\n" output += " <results>\n" next_result do |rec| output += " #{rec.to_xml}\n" end output += " </results>\n" output += "</report>\n" end
transforms a set of results into XML
Source
# File lib/passivedns/client/state.rb, line 166 def to_yaml output = '' next_result do |rec| output += "#{rec.to_yaml}\n" end output end
transforms a set of results into YAML
Source
# File lib/passivedns/client/state.rb, line 38 def update_query(query, state) @queue.each do |q| next unless q.query == query puts "update_query: #{query} (#{q.state}) -> (#{state})" if @debug q.state = state break end end
sets the state of a given query