Class: BetterCap::Parsers::CreditCard

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

Overview

CC parser.

Constant Summary collapse

PARSERS =
[
  # All major cards.
  /(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})/m,
  # American Express
  /(3[47][0-9]{13})/m,
  # Diners Club
  /(3(?:0[0-5]|[68][0-9])[0-9]{11})/m,
  # Discover
  /(6011[0-9]{12})/m,
  # MasterCard
  /(5[1-5][0-9]{14})/m,
  # Visa
  /(4[0-9]{12}(?:[0-9]{3})?)/m
].freeze

Instance Method Summary collapse

Methods inherited from Base

available, from_cmdline, inherited, #initialize, load_by_names, load_custom

Constructor Details

This class inherits a constructor from BetterCap::Parsers::Base

Instance Method Details

#luhn?(cc) ⇒ Boolean

Validate cc with Lughn algorithm.

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bettercap/sniffer/parsers/creditcard.rb', line 46

def luhn?(cc)
  digits = cc.split(//).map(&:to_i)
  last   = digits.pop

  products = digits.reverse.map.with_index do |n,i|
      i.even? ? n*2 : n*1
  end.reverse
  sum = products.inject(0) { |t,p| t + p.to_s.split(//).map(&:to_i).inject(:+) }
  checksum = 10 - (sum % 10)
  checksum == 10 ? 0 : checksum

  ( last == checksum )
end

#on_packet(pkt) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bettercap/sniffer/parsers/creditcard.rb', line 32

def on_packet( pkt )
  begin
    payload = pkt.to_s
    PARSERS.each do |expr|
      matches = payload.scan( expr )
      matches.each do |m|
        StreamLogger.log_raw( pkt, 'CREDITCARD', m ) if luhn?(m)
      end
      break unless matches.empty?
    end
  rescue; end
end