Sha256: f5e2bbfd2e0473094d6ba7d7aeb049265076cc8191e172792b7d2742973b9629

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

require "otx_ruby"

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

      param :query
      option :title, default: proc { "OTX search" }
      option :description, default: proc { "query = #{query}" }
      option :tags, default: proc { [] }

      attr_reader :type

      def initialize(*args, **kwargs)
        super

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

      def artifacts
        search || []
      end

      private

      def configuration_keys
        %w[otx_api_key]
      end

      def domain_client
        @domain_client ||= ::OTX::Domain.new(Mihari.config.otx_api_key)
      end

      def ip_client
        @ip_client ||= ::OTX::IP.new(Mihari.config.otx_api_key)
      end

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

      #
      # IP/domain search
      #
      # @return [Array<String>]
      #
      def search
        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

      #
      # Domain search
      #
      # @return [Array<String>]
      #
      def domain_search
        records = domain_client.get_passive_dns(query)
        records.filter_map do |record|
          record.address if record.record_type == "A"
        end.uniq
      end

      #
      # IP search
      #
      # @return [Array<String>]
      #
      def ip_search
        records = ip_client.get_passive_dns(query)
        records.filter_map do |record|
          record.hostname if record.record_type == "A"
        end.uniq
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
mihari-3.12.0 lib/mihari/analyzers/otx.rb
mihari-3.11.0 lib/mihari/analyzers/otx.rb
mihari-3.10.1 lib/mihari/analyzers/otx.rb
mihari-3.10.0 lib/mihari/analyzers/otx.rb
mihari-3.9.2 lib/mihari/analyzers/otx.rb
mihari-3.9.1 lib/mihari/analyzers/otx.rb
mihari-3.9.0 lib/mihari/analyzers/otx.rb
mihari-3.8.1 lib/mihari/analyzers/otx.rb
mihari-3.8.0 lib/mihari/analyzers/otx.rb
mihari-3.7.2 lib/mihari/analyzers/otx.rb
mihari-3.7.1 lib/mihari/analyzers/otx.rb
mihari-3.7.0 lib/mihari/analyzers/otx.rb
mihari-3.6.1 lib/mihari/analyzers/otx.rb