Sha256: f77e55316550d3ca40b3d9658abac3ddcc310e894f21eab61b001b9313bc7c01

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require "virustotal"

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

      param :query

      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[virustotal_api_key]
      end

      def api
        @api = ::VirusTotal::API.new(key: Mihari.config.virustotal_api_key)
      end

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

      #
      # Search
      #
      # @return [Array<Mihari::Artifact>]
      #
      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<Mihari::Artifact>]
      #
      def domain_search
        res = api.domain.resolutions(query)

        data = res["data"] || []
        data.filter_map do |item|
          data = item.dig("attributes", "ip_address")
          data.nil? ? nil : Artifact.new(data: data, source: source, metadata: item)
        end
      end

      #
      # IP search
      #
      # @return [Array<Mihari::Artifact>]
      #
      def ip_search
        res = api.ip_address.resolutions(query)

        data = res["data"] || []
        data.filter_map do |item|
          data = item.dig("attributes", "host_name")
          Artifact.new(data: data, source: source, metadata: item)
        end.uniq
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mihari-4.3.0 lib/mihari/analyzers/virustotal.rb
mihari-4.2.0 lib/mihari/analyzers/virustotal.rb
mihari-4.1.2 lib/mihari/analyzers/virustotal.rb
mihari-4.1.1 lib/mihari/analyzers/virustotal.rb
mihari-4.1.0 lib/mihari/analyzers/virustotal.rb
mihari-4.0.0 lib/mihari/analyzers/virustotal.rb