Sha256: 44ace42432dd840cf0e417903db66a215070f97df59dbb62925fe8f829742943

Contents?: true

Size: 1.45 KB

Versions: 21

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require "greynoise"

module Mihari
  module Analyzers
    class GreyNoise < Base
      param :query

      # @return [String, nil]
      attr_reader :api_key

      def initialize(*args, **kwargs)
        super(*args, **kwargs)

        @api_key = kwargs[:api_key] || Mihari.config.greynoise_api_key
      end

      def artifacts
        res = Structs::GreyNoise::Response.from_dynamic!(search)
        res.data.map do |datum|
          build_artifact datum
        end
      end

      private

      PAGE_SIZE = 10_000

      def configuration_keys
        %w[greynoise_api_key]
      end

      def api
        @api ||= ::GreyNoise::API.new(key: api_key)
      end

      #
      # Search
      #
      # @return [Hash]
      #
      def search
        api.experimental.gnql(query, size: PAGE_SIZE)
      end

      #
      # Build an artifact from a GreyNoise search API response
      #
      # @param [Structs::GreyNoise::Datum] datum
      #
      # @return [Artifact]
      #
      def build_artifact(datum)
        as = AutonomousSystem.new(asn: normalize_asn(datum.metadata.asn))

        geolocation = Geolocation.new(
          country: datum.metadata.country,
          country_code: datum.metadata.country_code
        )

        Artifact.new(
          data: datum.ip,
          source: source,
          metadata: datum.metadata_,
          autonomous_system: as,
          geolocation: geolocation
        )
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
mihari-5.1.0 lib/mihari/analyzers/greynoise.rb
mihari-5.0.1 lib/mihari/analyzers/greynoise.rb
mihari-5.0.0 lib/mihari/analyzers/greynoise.rb
mihari-4.12.0 lib/mihari/analyzers/greynoise.rb
mihari-4.11.0 lib/mihari/analyzers/greynoise.rb
mihari-4.10.0 lib/mihari/analyzers/greynoise.rb
mihari-4.9.0 lib/mihari/analyzers/greynoise.rb
mihari-4.8.0 lib/mihari/analyzers/greynoise.rb
mihari-4.7.4 lib/mihari/analyzers/greynoise.rb
mihari-4.7.3 lib/mihari/analyzers/greynoise.rb
mihari-4.7.2 lib/mihari/analyzers/greynoise.rb
mihari-4.7.1 lib/mihari/analyzers/greynoise.rb
mihari-4.7.0 lib/mihari/analyzers/greynoise.rb
mihari-4.6.1 lib/mihari/analyzers/greynoise.rb
mihari-4.6.0 lib/mihari/analyzers/greynoise.rb
mihari-4.5.3 lib/mihari/analyzers/greynoise.rb
mihari-4.5.2 lib/mihari/analyzers/greynoise.rb
mihari-4.5.1 lib/mihari/analyzers/greynoise.rb
mihari-4.5.0 lib/mihari/analyzers/greynoise.rb
mihari-4.4.1 lib/mihari/analyzers/greynoise.rb