Sha256: e3639e4d88f8ae5053f71d787b4970825334382f30c25428cb7dafb3559e5c5f

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require "spyse"
require "json"

module Mihari
  module Analyzers
    class Spyse < Base
      attr_reader :query
      attr_reader :type

      attr_reader :title
      attr_reader :description
      attr_reader :tags

      def initialize(query, title: nil, description: nil, tags: [], type: "domain")
        super()

        @query = query

        @title = title || "Spyse lookup"
        @description = description || "query = #{query}"
        @tags = tags
        @type = type
      end

      def artifacts
        lookup || []
      end

      private

      def search_params
        @search_params ||= JSON.parse(query)
      end

      def config_keys
        %w(spyse_api_key)
      end

      def api
        @api ||= ::Spyse::API.new(Mihari.config.spyse_api_key)
      end

      def valid_type?
        %w(ip domain cert).include? type
      end

      def domain_lookup
        res = api.domain.search(search_params, limit: 100)
        items = res.dig("data", "items") || []
        items.map do |item|
          item.dig("name")
        end.uniq.compact
      end

      def ip_lookup
        res = api.ip.search(search_params, limit: 100)
        items = res.dig("data", "items") || []
        items.map do |item|
          item.dig("ip")
        end.uniq.compact
      end

      def lookup
        case type
        when "domain"
          domain_lookup
        when "ip"
          ip_lookup
        else
          raise InvalidInputError, "#{query}(type: #{type || 'unknown'}) is not supported." unless valid_type?
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mihari-1.4.1 lib/mihari/analyzers/spyse.rb
mihari-1.4.0 lib/mihari/analyzers/spyse.rb
mihari-1.3.2 lib/mihari/analyzers/spyse.rb
mihari-1.3.1 lib/mihari/analyzers/spyse.rb
mihari-1.3.0 lib/mihari/analyzers/spyse.rb