Class: SSHScan::FingerprintDatabase

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_scan/fingerprint_database.rb

Overview

Create and/or maintain a fingerprint database using YAML Store.

Instance Method Summary collapse

Constructor Details

#initialize(database_name) ⇒ FingerprintDatabase

Returns a new instance of FingerprintDatabase.



6
7
8
# File 'lib/ssh_scan/fingerprint_database.rb', line 6

def initialize(database_name)
  @store = YAML::Store.new(database_name)
end

Instance Method Details

#add_fingerprint(fingerprint, ip) ⇒ Object

Insert a (fingerprint, IP) record.

Parameters:

  • fingerprint (String)

    fingerprint to insert

  • ip (String)

    IP for which fingerprint has to be added



22
23
24
25
26
27
# File 'lib/ssh_scan/fingerprint_database.rb', line 22

def add_fingerprint(fingerprint, ip)
  @store.transaction do
    @store[ip] = [] if @store[ip].nil?
    @store[ip] << fingerprint
  end
end

#clear_fingerprints(ip) ⇒ Object

Empty the fingerprints database for given IP.

Parameters:

  • ip (String)

    IP for which fingerprints should be cleared.



13
14
15
16
17
# File 'lib/ssh_scan/fingerprint_database.rb', line 13

def clear_fingerprints(ip)
  @store.transaction do
    @store[ip] = []
  end
end

#find_fingerprints(fingerprint) ⇒ Array<String>

Find IPs that have the given fingerprint.

Parameters:

  • fingerprint (String)

    fingerprint for which search should be performed

Returns:

  • (Array<String>)

    return unique IPs for which the given fingerprint has an entry



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ssh_scan/fingerprint_database.rb', line 34

def find_fingerprints(fingerprint)
  ip_matches = []

  @store.transaction(true) do
    @store.roots.each do |ip|
      @store[ip].each do |other_fingerprint|
        if fingerprint == other_fingerprint
          ip_matches << ip
        end
      end
    end
  end

  return ip_matches.uniq
end