Class: BetterCap::Parsers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/sniffer/parsers/base.rb

Overview

Base class for BetterCap::Parsers.

Constant Summary collapse

@@loaded =

Hash of available parsers ( parser name -> class name )

{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Initialize this parser.



85
86
87
88
89
# File 'lib/bettercap/sniffer/parsers/base.rb', line 85

def initialize
  @filters = []
  @name = 'BASE'
  @port = nil
end

Class Method Details

.availableObject

Return a list of available parsers names.



30
31
32
# File 'lib/bettercap/sniffer/parsers/base.rb', line 30

def available
  @@loaded.keys
end

.from_cmdline(v) ⇒ Object

Parse the v command line argument and return a list of parser names. Will raise BetterCap::Error if one or more parser names are not valid.

Raises:



36
37
38
39
40
41
42
43
44
45
# File 'lib/bettercap/sniffer/parsers/base.rb', line 36

def from_cmdline(v)
  raise BetterCap::Error, "No parser names provided" if v.nil?

  avail = available
  list = v.split(',').collect(&:strip).collect(&:upcase).reject{ |c| c.empty? }
  list.each do |parser|
    raise BetterCap::Error, "Invalid parser name '#{parser}'." unless avail.include?(parser) or parser == '*'
  end
  list
end

.from_exclusion_list(v) ⇒ Object

Parse the v command line argument and return a list of parser names disabling the ones specified. Will raise BetterCap::Error if one or more parser names are not valid.

Raises:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bettercap/sniffer/parsers/base.rb', line 50

def from_exclusion_list(v)
  raise BetterCap::Error, "No parser names provided" if v.nil?

  avail = available
  list = v.split(',').collect(&:strip).collect(&:upcase).reject{ |c| c.empty? }
  list.each do |parser|
    raise BetterCap::Error, "Invalid parser name '#{parser}'." unless avail.include?(parser)
  end

  avail - list
end

.inherited(subclass) ⇒ Object

Called when this base class is inherited from one of the parsers.



22
23
24
25
26
27
# File 'lib/bettercap/sniffer/parsers/base.rb', line 22

def inherited(subclass)
  name = subclass.name.split('::')[2].upcase
  if name != 'CUSTOM'
    @@loaded[name] = subclass.name
  end
end

.load_by_names(parsers) ⇒ Object

Return a list of BetterCap::Parsers instances by their parsers names.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bettercap/sniffer/parsers/base.rb', line 63

def load_by_names(parsers)
  loaded = []

  @@loaded.each do |name,cname|
    if parsers.include?(name) or parsers == ['*']
      Logger.debug "Loading parser #{name} ( #{cname} ) ..."
      loaded << BetterCap::Loader.load(cname).new
    end
  end

  loaded
end

.load_custom(expression) ⇒ Object

Load and return an instance of the BetterCap::Parsers::Custom parser given the expression Regex object.



78
79
80
81
# File 'lib/bettercap/sniffer/parsers/base.rb', line 78

def load_custom(expression)
  Logger.debug "Loading custom parser: '#{expression}' ..."
  [ BetterCap::Parsers::Custom.new(expression) ]
end

Instance Method Details

#match_port?(pkt) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bettercap/sniffer/parsers/base.rb', line 91

def match_port?( pkt )
  return true unless !@port.nil?
  begin
    if pkt.respond_to?(:tcp_dst) and pkt.tcp_dst == @port
      return true
    elsif pkt.respond_to?(:udp_dst) and pkt.udp_dst == @port
      return true
    end
  rescue; end
  false
end

#on_packet(pkt) ⇒ Object

This method will be called from the BetterCap::Sniffer for each incoming packet ( +pkt ) and will apply the parser filter to it.



105
106
107
108
109
110
111
112
113
114
# File 'lib/bettercap/sniffer/parsers/base.rb', line 105

def on_packet( pkt )
  s = pkt.to_s
  if match_port?(pkt)
    @filters.each do |filter|
      if s =~ filter
        StreamLogger.log_raw( pkt, @name, pkt.payload )
      end
    end
  end
end