Sha256: f0c2a965aa96da9cffc468962a3397225e60518a10f20e9529ad0e123a362205

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "base64"

module Mihari
  module Clients
    class CIRCL < Base
      #
      # @param [String] base_url
      # @param [String, nil] username
      # @param [String, nil] password
      # @param [Hash] headers
      #
      def initialize(base_url = "https://www.circl.lu", username:, password:, headers: {})
        raise(ArgumentError, "'username' argument is required") if username.nil?
        raise(ArgumentError, "'password' argument is required") if password.nil?

        headers["authorization"] = "Basic #{Base64.strict_encode64("#{username}:#{password}")}"

        super(base_url, headers: headers)
      end

      #
      # Passive DNS search
      #
      # @param [String] query
      #
      # @return [Array<String>]
      #
      def passive_dns_search(query)
        results = dns_query(query)
        results.filter_map do |result|
          type = result["rrtype"]
          (type == "A") ? result["rdata"] : nil
        end.uniq
      end

      #
      # Passive SSL search
      #
      # @param [String] query
      #
      # @return [Array<String>]
      #
      def passive_ssl_search(query)
        result = ssl_cquery(query)
        seen = result["seen"] || []
        seen.uniq
      end

      #
      # @param [String] query
      #
      # @return [Hash]
      #
      def dns_query(query)
        _get("/pdns/query/#{query}")
      end

      #
      # @param [String] query
      #
      # @return [Hash]
      #
      def ssl_cquery(query)
        _get("/v2pssl/cquery/#{query}")
      end

      private

      #
      # @param [String] path
      # @param [Hash] params
      #
      def _get(path, params: {})
        res = get(path, params: params)
        body = res.body.to_s
        content_type = res["Content-Type"].to_s

        return JSON.parse(body) if content_type.include?("application/json")

        body.lines.map { |line| JSON.parse line }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mihari-5.6.0 lib/mihari/clients/circl.rb
mihari-5.5.0 lib/mihari/clients/circl.rb
mihari-5.4.9 lib/mihari/clients/circl.rb
mihari-5.4.8 lib/mihari/clients/circl.rb
mihari-5.4.7 lib/mihari/clients/circl.rb
mihari-5.4.6 lib/mihari/clients/circl.rb
mihari-5.4.5 lib/mihari/clients/circl.rb
mihari-5.4.4 lib/mihari/clients/circl.rb
mihari-5.4.3 lib/mihari/clients/circl.rb