Sha256: af726bb9ec4d1575f67cb9a52e5f9e5780f6443a84cae4d2a68aa7d3bdccaaf5

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require "dnstwister"
require "resolv"
require "parallel"

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

      attr_reader :title
      attr_reader :description
      attr_reader :tags

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

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

        @title = title || "dnstwister domain lookup"
        @description = description || "query = #{query}"
        @tags = tags
      end

      def artifacts
        lookup || []
      end

      private

      def valid_type?
        type == "domain"
      end

      def api
        @api ||= ::DNSTwister::API.new
      end

      def resolvable?(domain)
        Resolv.getaddress domain
        true
      rescue Resolv::ResolvError => _e
        false
      end

      def lookup
        raise InvalidInputError, "#{query}(type: #{type || 'unknown'}) is not supported." unless valid_type?

        res = api.fuzz(query)
        fuzzy_domains = res.dig("fuzzy_domains") || []
        domains = fuzzy_domains.map { |domain| domain.dig("domain") }
        Parallel.map(domains) do |domain|
          resolvable?(domain) ? domain : nil
        end.compact
      rescue ::DNSTwister::Error => _e
        nil
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mihari-0.17.0 lib/mihari/analyzers/dnstwister.rb