From 4c2f190bed2ef8c772caca04c37fd644b5136e8b Mon Sep 17 00:00:00 2001 From: VetheonGames Date: Sun, 11 Feb 2024 11:05:39 -0700 Subject: [PATCH] Push 3 Remove HTTPParty from depends, gonna use the NET/HTTP library from the STDLib, even though it needs more coding to make work. Replace HTTPParty code with NET/HTTP code. Move opswat_client and virus_total_client into their own directories for organization --- Gemfile | 2 - lib/api_clients/{ => opswat}/opswat_client.rb | 0 .../virus_total/virus_total_client.rb | 47 +++++++++++++++++++ lib/api_clients/virus_total_client.rb | 18 ------- lib/virus_scanner.rb | 13 +++-- 5 files changed, 55 insertions(+), 25 deletions(-) rename lib/api_clients/{ => opswat}/opswat_client.rb (100%) create mode 100644 lib/api_clients/virus_total/virus_total_client.rb delete mode 100644 lib/api_clients/virus_total_client.rb diff --git a/Gemfile b/Gemfile index baec483..ea3bad2 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,4 @@ gem 'sequel', '~> 5.77' gem 'sqlite3', '~> 1.7' -gem 'httpparty', '~> 0.2.0' - gem 'dotenv', '~> 2.8' diff --git a/lib/api_clients/opswat_client.rb b/lib/api_clients/opswat/opswat_client.rb similarity index 100% rename from lib/api_clients/opswat_client.rb rename to lib/api_clients/opswat/opswat_client.rb diff --git a/lib/api_clients/virus_total/virus_total_client.rb b/lib/api_clients/virus_total/virus_total_client.rb new file mode 100644 index 0000000..386621d --- /dev/null +++ b/lib/api_clients/virus_total/virus_total_client.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'net/http' +require 'uri' +require 'json' +require 'yaml' + +# main class for communication with the VirusTotal 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/' + + # Method to scan URLs + def self.scan_url(url) + uri = URI("#{BASE_URL}urls") + request = Net::HTTP::Post.new(uri) + request['x-apikey'] = API_KEY + request['Content-Type'] = 'application/json' + request.body = { url: }.to_json + + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| + http.request(request) + end + + # TODO: Implement proper response handling + JSON.parse(response.body) + end + + # method to upload files to scan + def self.scan_file(file_path) + uri = URI("#{BASE_URL}files") + request = Net::HTTP::Post.new(uri) + request['x-apikey'] = API_KEY + request['accept'] = 'application/json' + request['Content-Type'] = 'multipart/form-data' + + form_data = [['file', File.open(file_path)]] + request.set_form form_data, 'multipart/form-data' + + response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| + http.request(request) + end + + # TODO: Implement proper response handling + JSON.parse(response.body) # Simplified example; adjust based on actual needs + end +end diff --git a/lib/api_clients/virus_total_client.rb b/lib/api_clients/virus_total_client.rb deleted file mode 100644 index f2c5d58..0000000 --- a/lib/api_clients/virus_total_client.rb +++ /dev/null @@ -1,18 +0,0 @@ -# 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/virus_scanner.rb b/lib/virus_scanner.rb index 236895e..cafcb95 100644 --- a/lib/virus_scanner.rb +++ b/lib/virus_scanner.rb @@ -1,13 +1,16 @@ # frozen_string_literal: true require 'httparty' -require_relative 'api_clients/virus_total_client' +require_relative 'api_clients/virus_total/virus_total_client' +require_relative 'api_clients/opswat/opswat_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'] + def self.scan_file_with_virustotal(file_path) + VirusTotalClient.scan_file(file_path) + end + + def self.scan_file_with_opswat(file_path) + OpswatClient.scan_file(file_path) end end