Skip to main content

Non-encrypted confidential information - Base 64

Need

Secure storage and transmission of confidential information

Context

  • Usage of PHP for server-side scripting and web development
  • Usage of base64 for encoding and decoding data in base64 format

Description

Non compliant code

<?php

// Credentials are stored in Base64 format
$username = base64_encode('admin');
$password = base64_encode('password123');

?>

In the above PHP code, the username and password credentials are stored in Base64 format. This is a significant security vulnerability as Base64 is not encryption, but a simple encoding scheme.

While it may seem that the credentials are obscured, in reality, they can be easily decoded by anyone who has access to the source code. This is because Base64 encoding is a publicly known scheme and can be easily reversed.

This means that if an attacker gains access to your source code, they can easily decode these Base64 strings to obtain the original credentials. This could potentially give them unauthorized access to sensitive parts of your application, leading to data breaches or other security incidents.

Storing credentials in the source code is a bad practice in general, whether they are encoded or not, as source code is often shared between developers and may be stored in source control systems which could be compromised.

In a Laravel application, credentials should be stored in environment variables and should be encrypted using Laravel's encryption functions to ensure they are secure.

Steps

  • Remove the Base64 encoded credentials from the source code.
  • Store the credentials in a secure and encrypted configuration file or a secure database.
  • Use secure methods to retrieve these credentials when needed.
  • Ensure that the configuration file or database is not accessible to unauthorized users or systems.
  • Consider using a secure password hashing algorithm instead of Base64 encoding for storing passwords.
  • Implement proper access controls and permissions to prevent unauthorized access to the credentials.

Compliant code

<?php

// Load the credentials from a secure and encrypted configuration file
$credentials = parse_ini_file('/path/to/secure/config.ini');

// Use secure methods to retrieve these credentials
$username = $credentials['username'];
$password = $credentials['password'];

?>

The original code was storing the username and password in Base64 format directly in the source code. This is a security vulnerability as anyone with access to the source code would be able to retrieve the credentials.

The updated code removes the Base64 encoded credentials from the source code. Instead, it loads the credentials from a secure and encrypted configuration file using the parse_ini_file function. This function parses a configuration (ini) file and returns the settings in it as an associative array.

The path to the configuration file is /path/to/secure/config.ini. This file should be stored in a secure location that is not accessible to unauthorized users or systems. The file should also be encrypted to add an additional layer of security.

The credentials are then retrieved from the array using the keys 'username' and 'password'. This is a more secure method of handling credentials as they are not exposed in the source code.

In addition to these changes, it is recommended to use a secure password hashing algorithm for storing passwords instead of Base64 encoding. Also, proper access controls and permissions should be implemented to prevent unauthorized access to the credentials.

References