Class: BetterCap::Sniffer

Inherits:
Object
  • Object
show all
Includes:
PacketFu
Defined in:
lib/bettercap/sniffer/sniffer.rb

Overview

network packet sniffing and dumping.

Constant Summary collapse

@@ctx =
nil
@@parsers =
nil
@@pcap =
nil
@@cap =
nil

Class Method Summary collapse

Class Method Details

.start(ctx) ⇒ Object

Start a new thread that will sniff packets from the network and pass each one of them to the BetterCap::Parsers instances loaded inside the ctx BetterCap::Context instance.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/bettercap/sniffer/sniffer.rb', line 28

def self.start( ctx )
  Thread.new {
    Logger.debug 'Starting sniffer ...'

    setup( ctx )

    tmin = System.cpu_count
    tmax = tmin * 4

    start     = Time.now
    skipped   = 0
    processed = 0
    lock      = Mutex.new
    pool      = BetterCap::Proxy::ThreadPool.new( tmin, tmax ) do |raw_packet|
      begin
        parsed = PacketFu::Packet.parse(raw_packet)
      rescue Exception => e
        parsed = nil
      end

      if skip_packet?(parsed)
        skipped += 1
      else
        processed += 1
        parse_packet parsed
        unless @@pcap.nil?
          lock.synchronize {
            append_packet raw_packet
          }
        end
      end
    end

    self.stream.each do |raw_packet|
      break unless @@ctx.running
      pool << raw_packet
    end

    while pool.backlog != 0
      sleep(0.1)
    end

    stop = Time.now
    delta = ( stop - start ) * 1000.0
    total = skipped + processed

    Logger.info "[#{'SNIFFER'.green}] #{total} packets processed in #{delta} ms ( #{skipped} skipped packets, #{processed} processed packets )"
  }
end