Class: Baffle::Probe

Inherits:
Object
  • Object
show all
Defined in:
lib/baffle/probe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Probe

Returns a new instance of Probe.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/baffle/probe.rb', line 59

def initialize(name, &block)
  @name             = name
  @training_data    = Hash.new {|hash, key| hash[key] = []}
  @names            = []
  @injection_values = nil
  @injection_proc   = nil
  @filter_procs     = {}
  @capture_procs    = {}
  @repeat           = 1
  
  instance_eval(&block)
end

Instance Attribute Details

#capture_procsObject (readonly)

Returns the value of attribute capture_procs.



57
58
59
# File 'lib/baffle/probe.rb', line 57

def capture_procs
  @capture_procs
end

#filter_procsObject (readonly)

Returns the value of attribute filter_procs.



57
58
59
# File 'lib/baffle/probe.rb', line 57

def filter_procs
  @filter_procs
end

#injection_procObject (readonly)

Returns the value of attribute injection_proc.



57
58
59
# File 'lib/baffle/probe.rb', line 57

def injection_proc
  @injection_proc
end

#injection_valuesObject (readonly)

Returns the value of attribute injection_values.



57
58
59
# File 'lib/baffle/probe.rb', line 57

def injection_values
  @injection_values
end

#nameObject (readonly)

Returns the value of attribute name.



57
58
59
# File 'lib/baffle/probe.rb', line 57

def name
  @name
end

#training_dataObject (readonly)

Returns the value of attribute training_data.



57
58
59
# File 'lib/baffle/probe.rb', line 57

def training_data
  @training_data
end

Instance Method Details

#capture(name, &block) ⇒ Object



128
129
130
# File 'lib/baffle/probe.rb', line 128

def capture(name, &block)
  @capture_procs[name] = block
end

#compute_vector(&block) ⇒ Object



120
121
122
# File 'lib/baffle/probe.rb', line 120

def compute_vector(&block)
  @compute_vector = block
end

#filter(name, &block) ⇒ Object



124
125
126
# File 'lib/baffle/probe.rb', line 124

def filter(name, &block)
  @filter_procs[name] = block
end

#hypothesize(vector) ⇒ Object

Build a hash of hypotheses on the given vector, with confidence ratings on each hypothesis



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/baffle/probe.rb', line 171

def hypothesize(vector)
  similarities = []
  
  vector_embedded = Linalg::DMatrix[vector] * @u2 * @eig2.inv
  
  @v2.rows.each_with_index do |row, i|
    similarities << [@names[i], vector_embedded.transpose.dot(row.transpose) / (row.norm * vector_embedded.norm)]
  end
  
  sorted_similarities = similarities.delete_if { |name, score| score.nan? }.sort_by { |name, score| -score }
  name, score = sorted_similarities.first
  
  name ||= 'Unknown'
  score ||= 'NaN'
  
  "#{name} (#{score})"
end

#inject(car, *cdr, &block) ⇒ Object



114
115
116
117
118
# File 'lib/baffle/probe.rb', line 114

def inject(car, *cdr, &block)
  @injection_proc   = block
  @injection_values = car.to_a rescue []
  @injection_values = @injection_values.product(*cdr.map{ |v| v.to_a rescue []})
end

#learnObject

Gets called when all training samples have been loaded



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/baffle/probe.rb', line 141

def learn
  # The code below assumes at least two training values, and doing it with any fewer
  # doesn't make much sense anyway
  return false if @training_data.size < 2
  
  # Doing it this way to make sure we have the same row/column order in names as we do in our matrix.
  # (there are no guarantees that two iterations over the pairs in a hash will have the same order)
  row_matrix, @names = @training_data.inject([[], []]) do |result, pair| 
    result[0] += pair[1]
    pair[1].length.times { result[1] << pair[0] }
    result
  end
  
  # We need a matrix of column vectors
  column_matrix = row_matrix.transpose
        
  m = Linalg::DMatrix[*column_matrix]
  
  u, s, vt = m.singular_value_decomposition
  vt = vt.transpose
  
  # Do we want more than 2 dimensions? TODO: test other numbers of dimensions
  @u2 = Linalg::DMatrix.join_columns [u.column(0), u.column(1)]
  @v2 = Linalg::DMatrix.join_columns [vt.column(0), vt.column(1)]
  @eig2 = Linalg::DMatrix.columns [s.column(0).to_a.flatten[0,2], s.column(1).to_a.flatten[0,2]]
  
  true
end

#repeat(count) ⇒ Object



132
133
134
# File 'lib/baffle/probe.rb', line 132

def repeat(count)
  @repeat = count
end

#repeatsObject



136
137
138
# File 'lib/baffle/probe.rb', line 136

def repeats
  @repeat
end

#run(options, &block) ⇒ Object



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
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/baffle/probe.rb', line 72

def run(options, &block)
  unless options.train?
    return nil unless learn
  end

  samples = []
  
  @repeat.times do |i|
    samples[i] = []
    
    filters = {}
    @filter_procs.each do |name, filter_proc|
      filter = filter_proc.call(options)
      
      case filter
      when Dot11::Packet
        filter = Dot11::PacketSet.new(Dot11::Dot11, :payload => filter) if !filter.kind_of?(Dot11::Dot11)
      when Dot11::PacketSet
        puts "filter is Dot11::PacketSet"
        filter = Dot11::PacketSet.new(Dot11::Dot11, :payload => filter) if filter.packet_class != Dot11::Dot11
      end
      
      filters[name] = Capture::Filter.new(filter.to_filter)
    end
    
    sniff_thread = Thread.new do
      filter = filters.values.map { |filter| "(#{filter.expression})" }.join(" || ")
      
      Baffle::sniff(:device => options.capture, :filter => filter) do |packet|
        filters.each do |name, filter|
          samples[i] << @capture_procs[name].call(packet) if filter =~ packet.data
        end
      end
    end
    
    Baffle::emit(options, @injection_proc, @injection_values, &block)
    sniff_thread.kill
  end
  
  @vector = @compute_vector.call(samples)
end