diff --git a/lib/api_clients/opswat_client.rb b/lib/api_clients/opswat_client.rb index e69de29..a611a68 100644 --- a/lib/api_clients/opswat_client.rb +++ b/lib/api_clients/opswat_client.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'httparty' +require 'yaml' + +# Main class for communication with the OpSwat MetaDefender API v4 +class OpswatClient + # Load the OPSWAT API key from the config file + API_KEYS = YAML.load_file(File.join(__dir__, '../../config/api_keys.yml')) + OPSWAT_API_KEY = API_KEYS['opswat'] + BASE_URL = 'https://api.metadefender.com/v4' + + def self.scan_file(file_path) + # TODO: Implement file upload and scanning + # OPSWAT MetaDefender API requires the file to be uploaded for scanning + # The response will include a data_id which we can use to retrieve the scan results + response = upload_file(file_path) + data_id = response['data_id'] # TODO: This is hypothetical; adjust based on actual response structure + + # Retrieve the scan result using the data_id + get_scan_result(data_id) + end + + # Uploads a file to OPSWAT and returns the response + # TODO: Make this not just a skeleton + def self.upload_file(file_path) + endpoint = "#{BASE_URL}/file" + headers = { + 'apikey' => OPSWAT_API_KEY + # Add other necessary CSR headers here + } + options = { + headers:, + body: { file: File.new(file_path, 'rb') } + } + HTTParty.post(endpoint, options) + end + + # Retrieves the scan result for a given data_id + def self.get_scan_result(data_id) + # TODO: make this more robust + endpoint = "#{BASE_URL}/file/#{data_id}" + headers = { + 'apikey' => OPSWAT_API_KEY + # Add other necessary CSR headers here + } + HTTParty.get(endpoint, headers:) + end +end diff --git a/lib/api_clients/virus_total_client.rb b/lib/api_clients/virus_total_client.rb index e69de29..f2c5d58 100644 --- a/lib/api_clients/virus_total_client.rb +++ b/lib/api_clients/virus_total_client.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'httparty' +require 'yaml' + +# Main class for handling communication with the Virus Total API v3 +class VirusTotalClient + API_KEY = YAML.load_file(File.join(__dir__, '../../config/api_keys.yml'))['virus_total'] + BASE_URL = 'https://www.virustotal.com/api/v3/' + + def self.scan_url(url) + response = HTTParty.post("#{BASE_URL}urls", + headers: { 'x-apikey' => API_KEY }, + body: { url: }) + # TODO: Implement proper response handling + { is_safe: response.ok? } # Placeholder: Implement based on actual API response + end +end diff --git a/lib/database_manager.rb b/lib/database_manager.rb index e33e8df..e8fa8e2 100644 --- a/lib/database_manager.rb +++ b/lib/database_manager.rb @@ -4,9 +4,23 @@ require 'sequel' DB = Sequel.sqlite(File.join(__dir__, '../db/diswarden.sqlite3')) -# Schema definition -DB.create_table? :scanned_files do - primary_key :id - String :file_hash, unique: true, null: false - Boolean :is_safe, default: true +# Main class for handling Database ORM +class DatabaseManager + # TODO: Implement this properly to save hashes + def self.file_hash_exists?(hash) + DB[:scanned_files].where(file_hash: hash).count.positive? + end + + def self.insert_file_hash(hash, is_safe) + DB[:scanned_files].insert(file_hash: hash, is_safe:) + end + + def create_table + # Schema definition + DB.create_table? :scanned_files do + primary_key :id + String :file_hash, unique: true, null: false + Boolean :is_safe, default: true + end + end end diff --git a/lib/message_processor.rb b/lib/message_processor.rb index e69de29..6d71311 100644 --- a/lib/message_processor.rb +++ b/lib/message_processor.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# this class is what actually processes the message, and extracts the file/link/image from it +class MessageProcessor + def self.process_message(event) + # Check if the message has attachments + return if event.message.attachments.empty? + + # Process each attachment + event.message.attachments.each do |attachment| + # TODO: logic to download the file and scan it (WIP) + puts "Found attachment: #{attachment.filename}" + # For now, just print the URL + puts "Attachment URL: #{attachment.url}" + end + # TODO: Add logic for links and images (WIP) + end +end diff --git a/lib/virus_scanner.rb b/lib/virus_scanner.rb index e69de29..236895e 100644 --- a/lib/virus_scanner.rb +++ b/lib/virus_scanner.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require 'httparty' +require_relative 'api_clients/virus_total_client' + +# Main class for sending files/links to the security vendors, and getting the response. +class VirusScanner + def self.scan_file(url) + response = VirusTotalClient.scan_url(url) + # TODO: Implement proper return + response['is_safe'] + end +end