Sha256: a28f302b88df810bd8fa993ecd6d44c1900b96cff2f5cc49a7eaffa3fbacd403

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "ssh_scan"

module Apullo
  module Fingerprint
    class SSH < Base
      DEFAULT_OPTIONS = { "timeout" => 3 }.freeze
      DEFAULT_PORTS = [22, 2222].freeze

      private

      def build_results
        pluck_fingerprints
      end

      def pluck_fingerprints
        result = scan
        keys = result.dig("keys") || {}
        keys.map do |cipher, data|
          raw = data.dig("raw")
          fingerprints = data.dig("fingerprints") || []
          normalized_fingerprints = fingerprints.map do |hash, value|
            [hash, value.delete(":")]
          end.to_h
          [
            cipher,
            { raw: raw, fingerprints: normalized_fingerprints }
          ]
        end.to_h
      end

      def _scan(target, port: 22)
        return nil unless target.host

        engine = SSHScan::ScanEngine.new
        dest = "#{target.host}:#{port}"
        result = engine.scan_target(dest, DEFAULT_OPTIONS)
        result.to_hash
      end

      def scan
        [target].product(DEFAULT_PORTS).each do |target, port|
          result = _scan(target, port: port)
          keys = result.dig("keys") || {}
          return result unless keys.empty?
        end
        {}
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apullo-0.1.5 lib/apullo/fingerprints/ssh.rb