Non-encrypted hard drives - Java

Non-encrypted hard drives - Java

Need

Implementation of full disk encryption for data protection

Context

  • Usage of Java for building scalable and robust applications
  • Usage of javax.servlet for building Java web applications with servlets
  • Usage of java.io for input and output operations in Java

Description

Non compliant code

        import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class UnencryptedDataServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String sensitiveData = "Sensitive Customer Data";
        File file = new File("unencrypted_data.txt");
        
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter writer = new FileWriter(file);
        writer.write(sensitiveData);
        writer.close();
    }
}
        
        

The above servlet, UnencryptedDataServlet, represents a serious vulnerability regarding non-encrypted hard drives. The servlet is designed to handle HTTP GET requests and writes sensitive customer data directly to a file named unencrypted_data.txt.

The main vulnerability lies in these lines:

                String sensitiveData = "Sensitive Customer Data";
File file = new File("unencrypted_data.txt");
FileWriter writer = new FileWriter(file);
writer.write(sensitiveData);
writer.close();

            


Here, sensitive data is being written directly to a file on the disk without any form of encryption. This means that if the hard drive is compromised, the sensitive data can be directly accessed by unauthorized individuals. This is a clear violation of data privacy and security guidelines, and poses a serious risk to the confidentiality and integrity of the stored data.

To exploit this vulnerability, an attacker would simply need to gain access to the file system where the data is stored. This could be achieved through various means such as physical access, malware, or through other vulnerabilities in the system. Once they have access to the file system, they can easily read the contents of unencrypted_data.txt and obtain the sensitive customer data stored within.

Steps

  • Implement full disk encryption on the computer or network storage system.
  • Avoid storing sensitive data on unencrypted disks or hard drives.
  • Use secure storage mechanisms that provide encryption for sensitive data.
  • Ensure that sensitive data is encrypted before being written to disk.
  • Implement access controls to restrict unauthorized access to sensitive data.
  • Regularly update and patch the operating system and software to address security vulnerabilities.
  • Implement secure coding practices to prevent data leakage or unauthorized access to sensitive data.
  • Train developers and system administrators on secure coding and data protection best practices.

Compliant code

        import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.security.Key;
import java.util.Base64;

public class EncryptedDataServlet extends HttpServlet {

    private static final String ALGORITHM = "AES";
    private static final byte[] KEY = "MySuperSecretKey".getBytes();

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String sensitiveData = "Sensitive Customer Data";
        File file = new File("encrypted_data.txt");

        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter writer = new FileWriter(file);
        writer.write(encrypt(sensitiveData));
        writer.close();
    }

    private String encrypt(String data) {
        try {
            Key key = new SecretKeySpec(KEY, ALGORITHM);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] encryptedDataBytes = cipher.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(encryptedDataBytes);
        } catch (Exception e) {
            throw new RuntimeException("Error occurred while encrypting data", e);
        }
    }
}
        
        

The above code is a revised version of the original servlet that now includes encryption for sensitive data. The sensitive data is encrypted using the AES (Advanced Encryption Standard) algorithm before being written to the disk.

The encrypt method is used to encrypt the sensitive data. It uses a secret key (in this case, "MySuperSecretKey") and the AES algorithm to create a Cipher instance. The Cipher instance is initialized in the encryption mode with the secret key, and then the sensitive data is encrypted. The encrypted data bytes are then encoded into a string using Base64 encoding before being written to the disk.

This approach ensures that even if the data is accessed directly from the disk, it cannot be understood without the decryption key. This is a basic level of encryption and in a real-world scenario, you would want to use a more secure method to store and retrieve your encryption keys, not hard-code them as shown in this example.

Remember, this code only encrypts the data at the application level. For full disk encryption, you would need to use system-level tools or solutions provided by your operating system or third-party software.

References