Sha256: fc8d53ae67bb9914ea53f45704dc444269c6eaedfc7065b9c9eedfbb81423c2b

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Mihari
  module Analyzers
    class OTX < Base
      include Mixins::Refang

      param :query

      # @return [String, nil]
      attr_reader :type

      # @return [String, nil]
      attr_reader :api_key

      # @return [String]
      attr_reader :query

      def initialize(*args, **kwargs)
        super

        @query = refang(query)
        @type = TypeChecker.type(query)

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

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

      private

      def configuration_keys
        %w[otx_api_key]
      end

      def client
        @client ||= Mihari::Clients::OTX.new(api_key: api_key)
      end

      #
      # Check whether a type is valid or not
      #
      # @return [Boolean]
      #
      def valid_type?
        %w[ip domain].include? type
      end

      #
      # Domain search
      #
      # @return [Array<String>]
      #
      def domain_search
        res = client.query_by_domain(query)
        return [] if res.nil?

        records = res["passive_dns"] || []
        records.filter_map do |record|
          record_type = record["record_type"]
          address = record["address"]

          address if record_type == "A"
        end.uniq
      end

      #
      # IP search
      #
      # @return [Array<String>]
      #
      def ip_search
        res = client.query_by_ip(query)
        return [] if res.nil?

        records = res["passive_dns"] || []
        records.filter_map do |record|
          record_type = record["record_type"]
          hostname = record["hostname"]

          hostname if record_type == "A"
        end.uniq
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mihari-5.2.4 lib/mihari/analyzers/otx.rb
mihari-5.2.3 lib/mihari/analyzers/otx.rb
mihari-5.2.2 lib/mihari/analyzers/otx.rb
mihari-5.2.1 lib/mihari/analyzers/otx.rb
mihari-5.2.0 lib/mihari/analyzers/otx.rb
mihari-5.1.4 lib/mihari/analyzers/otx.rb
mihari-5.1.3 lib/mihari/analyzers/otx.rb