Class: PassiveDNS::PDNSToolState

Inherits:
Object
  • Object
show all
Defined in:
lib/passivedns/client/state.rb

Direct Known Subclasses

PDNSToolStateDB

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePDNSToolState

Returns a new instance of PDNSToolState.



12
13
14
15
16
# File 'lib/passivedns/client/state.rb', line 12

def initialize
	@queue = []
	@recs = []
	@level = 0
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



9
10
11
# File 'lib/passivedns/client/state.rb', line 9

def debug
  @debug
end

#levelObject (readonly)

Returns the value of attribute level.



10
11
12
# File 'lib/passivedns/client/state.rb', line 10

def level
  @level
end

Instance Method Details

#add_query(query, state, level = @level+1) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/passivedns/client/state.rb', line 49

def add_query(query,state,level=@level+1)
	if query =~ /^\d+ \w+\./
		query = query.split(/ /,2)[1]
	end
	return if get_state(query)
	puts "Adding query: #{query}, #{state}, #{level}" if @debug
	@queue << PDNSQueueEntry.new(query,state,level)
end

#add_result(res) ⇒ Object



24
25
26
27
28
# File 'lib/passivedns/client/state.rb', line 24

def add_result(res)
	@recs << res
	add_query(res.answer,'pending')
	add_query(res.query,'pending')
end

#each_query(max_level = 20) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/passivedns/client/state.rb', line 58

def each_query(max_level=20)
	@queue.each do |q|
		if q.state == 'pending' or q.state == 'failed'
			@level = q.level
			q.state = 'queried'
			if q.level < max_level
				yield q.query
			end
		end
	end
end

#get_state(query) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/passivedns/client/state.rb', line 40

def get_state(query)
	@queue.each do |q|
		if q.query == query
			return q.state
		end
	end
	false
end

#next_resultObject



18
19
20
21
22
# File 'lib/passivedns/client/state.rb', line 18

def next_result
	@recs.each do |rec|
		yield rec
	end
end

#to_gdfObject



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
96
97
98
99
# File 'lib/passivedns/client/state.rb', line 70

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 i.answer =~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/ then
				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 do |i,j|
		output += i+"\n"
	end
	output += "edgedef> node1,node2,color\n"
	edges.each do |i,j|
		output += i+"\n"
	end
	output
end

#to_graphmlObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/passivedns/client/state.rb', line 122

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}"]
			nodes[r.query] = true
			output += "    <node id='#{r.answer}'/>\n" unless nodes["#{r.answer}"]
			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

#to_graphvizObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/passivedns/client/state.rb', line 101

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"
				if l.answer =~ /^\d{3}\.\d{3}\.\d{3}\.\d{3}$/
					output += "  \"#{l.answer}\" [shape=box, style=filled, color=white];\n"
				else
					output += "  \"#{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

#to_jsonObject



163
164
165
166
167
168
169
170
171
172
# File 'lib/passivedns/client/state.rb', line 163

def to_json
	output = "[\n"
	sep = ""
	next_result do |rec|
		output += sep
		output += rec.to_json
		sep = ",\n"
	end
	output += "\n]\n"
end

#to_s(sep = "\t") ⇒ Object



174
175
176
177
178
179
180
# File 'lib/passivedns/client/state.rb', line 174

def to_s(sep="\t")
	output = ""
	next_result do |rec|
		output += rec.to_s(sep)+"\n"
	end
	output
end

#to_xmlObject



144
145
146
147
148
149
150
151
152
153
# File 'lib/passivedns/client/state.rb', line 144

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

#to_yamlObject



155
156
157
158
159
160
161
# File 'lib/passivedns/client/state.rb', line 155

def to_yaml
	output = ""
	next_result do |rec|
		output += rec.to_yaml+"\n"
	end
	output
end

#update_query(query, state) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/passivedns/client/state.rb', line 30

def update_query(query,state)
	@queue.each do |q|
		if q.query == query
			puts "update_query: #{query} (#{q.state}) -> (#{state})" if @debug
			q.state = state
			break
		end
	end
end