Skip to main content

Non-encrypted confidential information - Hexadecimal

Need

Encryption of confidential information to ensure its confidentiality

Context

  • Usage of Python 3.0+ for developing Python applications
  • Usage of Django for building web applications

Description

Non compliant code

from django.http import HttpResponse
from django.views import View

class ConfidentialView(View):
def get(self, request):
confidential_info = "736563726574696e666f" # "secretinfo" in hexadecimal
return HttpResponse(confidential_info)

This code represents a Django view that returns confidential information as a response to a GET request. The confidential information is "secretinfo" which is encoded in hexadecimal as "736563726574696e666f". The issue here is that the confidential information is not encrypted, it's just encoded in hexadecimal.

Even though it's not in plain text, hexadecimal is not a secure way to hide information because it can be easily decoded. An attacker can intercept the HTTP response and decode the hexadecimal string to get the confidential information in plain text.

This is a serious vulnerability because it can lead to information disclosure, which can further lead to more serious attacks like identity theft, financial fraud, etc.

It's important to always encrypt confidential information using secure encryption algorithms before transmitting it over the network. Encoding is not encryption and should not be used as a substitute for encryption.

Steps

  • Use a secure encryption algorithm to encrypt the confidential information before sending it. Algorithms such as AES (Advanced Encryption Standard) are commonly used.
  • Store the encryption key securely. The key should not be hard-coded in the application, but instead stored in a secure and encrypted location that only the application can access.
  • Ensure that the encrypted information is decrypted only when necessary and only in a secure manner. The decryption process should also be done in a secure environment to prevent leakage of the confidential information.
  • Consider using HTTPS for communication to ensure that the data is encrypted during transmission. This will protect the data from being intercepted during transmission.

Compliant code

from django.http import HttpResponse
from django.views import View
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import base64

class ConfidentialView(View):
def get(self, request):
confidential_info = "736563726574696e666f" # "secretinfo" in hexadecimal
cipher = AES.new(get_random_bytes(16), AES.MODE_EAX)
nonce = cipher.nonce
ciphertext, tag = cipher.encrypt_and_digest(confidential_info.encode())
return HttpResponse(base64.b64encode(nonce + ciphertext).decode())

The above code fixes the vulnerability by encrypting the confidential information using the AES encryption algorithm before sending it.

The Crypto.Cipher.AES module is used to create a new AES cipher object. The get_random_bytes(16) function is used to generate a random 16-byte key for the AES cipher. The cipher.nonce is used to get a value that should never be repeated for the same key, providing an additional layer of security.

The cipher.encrypt_and_digest() function is used to encrypt the confidential information and return the encrypted data along with a tag that can be used to verify the authenticity of the data.

Finally, the encrypted data is encoded using base64 before being sent in the HTTP response. This ensures that the data remains intact without modification during transport.

Please note that the encryption key is generated randomly for each request in this example. In a real-world application, you would want to use a consistent key stored securely and use it for both encryption and decryption.

Also, it's important to use HTTPS for communication to ensure that the data is encrypted during transmission, protecting it from being intercepted.

References