Implementing Virus and Malware Scanning with Java

In today’s interconnected digital landscape, ensuring the security of your application and protecting your users from malicious content is paramount. Two common scenarios where implementing robust virus scanning is crucial are user file uploads and importing data from external sources. Let’s explore why these situations demand attention and how you can implement a simple yet effective virus scanning solution using a third-party API like attachmentAV.

Implementing Virus and Malware Scanning with Java

Scanning files for viruses and malware is crucial in the following scenarios:

  1. User Uploads: Many applications allow users to upload files, whether they’re profile pictures, documents, or media files. While this feature enhances user experience and functionality, it also opens a potential avenue for malware to enter your system. A single infected file could compromise your server or spread to other users who download or interact with the uploaded content.
  2. Processing External Data: Businesses often need to import data from external sources, such as partner APIs, data feeds, or bulk imports from clients. These external data sources, while valuable, can also be vectors for malware. Scanning imported data helps maintain the integrity of your system and protects your internal network from potential threats.

Implementing a Virus and Malware Scanning with Java

To address these security concerns, let’s walk through a Java implementation that leverages the attachmentAV API. This solution can be easily integrated into your file upload process or data import pipeline.

Here’s a code example that demonstrates how to use the API:

  1. The scanFile method takes a file path as input.
  2. It reads the file content into a byte array.
  3. An HTTP POST request is created with the necessary headers, including the API key for authentication.
  4. The file content is sent as the request body.
  5. The method returns the API response, which includes the scan result.
package com.example.demo;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;

import org.json.JSONException;
import org.json.JSONObject;

public class FileScannerAPI {
  private static final String API_URL = "https://eu.developer.attachmentav.com/v1/scan/sync/binary";
  private static final String API_KEY = "XXX";

  public static void main(String[] args) {
    String filePath = "./infected.exe";

    try {
      JSONObject result = scanFile(filePath);
      System.out.println("Scan status: " + result.getString("status"));
      System.out.println("File size: " + result.getInt("size"));
    } catch (IOException | InterruptedException | JSONException e) {
      e.printStackTrace();
    }
  }

  public static JSONObject scanFile(String filePath) throws IOException, InterruptedException, JSONException {
    HttpClient client = HttpClient.newHttpClient();

    byte[] fileContent = Files.readAllBytes(Path.of(filePath));

    HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create(API_URL))
        .header("Content-Type", "application/octet-stream")
        .header("x-api-key", API_KEY)
        .POST(HttpRequest.BodyPublishers.ofByteArray(fileContent))
        .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

    if (response.statusCode() == 200) {
      return new JSONObject(response.body());
    } else {
      throw new IOException("API request failed with status code: " + response.statusCode());
    }
  }
}

About attachmentAV API for Virus and Malware Scanning

To implement our virus and malware scanning solution, we are using the attachmentAV API. This robust API offers a straightforward way to protect your application from malicious content. Let’s explore its features and how it works:

  1. As a developer, you integrate your application with the attachmentAV API.
  2. Your application calls the API to scan a file whenever needed.
  3. The file is scanned by Sophos, a trusted name in cybersecurity.
  4. Your application then evaluates the scan result and takes appropriate action.

The key benefits of attachmentAV are:

  • Simple: The API is designed for ease of use, allowing you to integrate virus and malware scanning with minimal effort.
  • Highly Available: You can rely on attachmentAV’s highly available API, which is deployed on a fully managed fleet of machines, ensuring your scanning needs are met 24/7.
  • Trusted Solution: Powered by Sophos, attachmentAV protects your application using an up-to-date threat database and advanced detection algorithms.

To use the attachmentAV API, you’ll need an API key. This key serves as both your license and authentication method for API calls. You can obtain an API key by subscribing to one of the following plans:

PlanSmallMediumLarge
Requests per month10.00050.000100.000
Requests per second123
Maximum File Size (Synchronous API call)10 MB10 MB10 MB
Maximum File Size (Asynchronous API call)5 GB5 GB5 GB
EngineSophosSophosSophos
High Availability
Support via E-Mail

Get API key by creating a subscription!

As shown in the plan details, attachmentAV offers both synchronous and asynchronous API calls:

  • Synchronous calls are suitable for smaller files up to 10 MB, providing quick results for immediate processing.
  • Asynchronous calls can handle larger files up to 5 GB, making them ideal for scanning more substantial data imports or large user uploads.

By leveraging the attachmentAV API, you’re not just implementing a simple scanning solution – you’re employing a trusted, highly available service backed by Sophos’ advanced threat detection capabilities. This ensures that your application remains protected against the latest malware threats, providing peace of mind for both you and your users.

Conclusion

By implementing virus scanning for user uploads and external data imports, you significantly enhance the security of your application and protect your users from potential threats. The provided Java code example offers a starting point for integrating a virus scanning API into your workflow. Remember, security is an ongoing process, and staying vigilant against new threats is key to maintaining a robust and secure application.


Published on July 10, 2024 | Written by Andreas

Stay up-to-date

Monthly digest of security updates, new capabilities, and best practices.