CWE 798 - Use of Hard-Coded Credentials
Disclaimer
The code provided is for
**educational purposes**only and should not be used in a**production environment**without proper review and testing.The provided code should be regarded as
**best practice**. There are also several ways to remediate the Vulnerabilities.The code provided
**may contain vulnerabilities**that could be exploited by attackers, and is intended to be used as a learning tool to help improve security awareness and best practices.The mitigations provided are intended to address
**specific vulnerabilities**in the code, but may not be effective against all potential attack vectors or scenarios.The code provided is not
**guaranteed to be secure or free**from all vulnerabilities, and should be reviewed and tested thoroughly before being used in a production environment.The code provided is
**not a substitute for professional security**advice or guidance, and users should consult with a qualified security professional before implementing any security measures.The authors and contributors of the code provided
**cannot be held responsible**for any damages or losses resulting from the use of this code.The code provided is provided "as is" without any warranties, express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose.
We do not have any sponsors or financial interests in any specific products or services, and the information provided is based solely on our own knowledge and experience.
The vulnerable and mitigated code samples provided on our platform are generated with the help of AI and sourced from the internet. We have made every effort to ensure that the code is accurate and up-to-date, but we cannot guarantee its correctness or completeness. If you notice that any of the code samples belong to you and you wish for it to be removed, please contact us and we will take appropriate action. We also apologize for any inconvenience caused and are committed to giving appropriate credit to the respective authors.
About CWE 798
Use of Hard-coded Credentials
This Vulnerability occurs when software systems that involve the use of credentials (such as usernames and passwords) that are hard-coded directly into the source code or configuration files of an application.
What is Inbound/Outbound Variant?
Inbound use of hard-coded credentials involves using hard-coded credentials to authenticate incoming requests or connections to the application.
For example, let's say you have a web application that requires users to enter a username and password to log in. In this scenario, if the application's source code contains hard-coded credentials that are used to authenticate the incoming user requests, it would be an example of inbound use of hard-coded credentials.
Outbound use of hard-coded credentials involves using hard-coded credentials to authenticate the application itself when it interacts with external systems or services.
For Example, consider a software application that needs to connect to a database for retrieving or storing data. If the application's code contains hard-coded credentials that are used to authenticate the application when establishing the connection to the database, it would be an example of outbound use of hard-coded credentials.
Impact
Unauthorized Access
Credential Exposure
Lack of Accountability
Difficulty in Credential Management
Compliance Violations
Example with Code Explanation:
Let us consider an example case and understand the CWE 798 with context of Vulnerable code and Mitigated code.
C
CVulnerable Code
In this code, the authenticate function compares the provided username and password with hard-coded credentials (validUsername and validPassword). If the comparison succeeds, it prints Authentication successful, indicating a successful login. Otherwise, it prints Authentication failed. This approach is vulnerable because the valid credentials are hard-coded directly into the source code. If an attacker gains access to the code, they can easily extract the credentials and potentially gain unauthorized access to the system.
Some of the ways the Vulnerable code can be mitigated is:
Use Credential Storage Mechanisms:Instead of hard-coding credentials directly in the code or configuration files, use secure storage mechanisms such as encrypted files, secure key stores, or dedicated credential management systems. This helps protect the credentials from unauthorized access and reduces the risk of exposure.Externalize Credentials:Store credentials separately from the application code. Use external configuration files or environment variables to provide credentials at runtime. This separation allows for easier credential management, reduces the risk of accidental exposure, and facilitates secure deployment and configuration management.Employ Encryption or Hashing:If credentials need to be stored within the application, consider using strong encryption or hashing algorithms to protect them. This adds an additional layer of security and makes it harder for attackers to extract the original credentials even if they gain access to the code.Implement Credential Rotation:Regularly rotate credentials, especially for sensitive accounts or high-value systems. Periodic rotation of credentials reduces the window of opportunity for attackers and limits the potential impact of a compromised credential.Use Token-based Authentication:Instead of using traditional username/password combinations, consider implementing token-based authentication mechanisms such as JSON Web Tokens (JWT) or OAuth. These mechanisms eliminate the need for hard-coded credentials and provide more secure and flexible authentication options.Employ Secure Key Management:If using API keys, access tokens, or other forms of credentials, employ secure key management practices. This includes securely generating, distributing, and revoking keys, as well as implementing appropriate access controls and auditing mechanisms.
Mitigated Code
The Mitigated code does the following:
Retrieval of credentials:The mitigated code simulates retrieving credentials from asecure configuration file or external source. It uses SSL/TLS to establish a secure connection, loads a trust store, verifies the server certificate, and reads the encrypted credentials from the configuration file. The actual retrieval logic may vary depending on the implementation.Encryption and decryption:The mitigated code includes encryption and decryption mechanisms to protect the credentials stored in the configuration file. It assumes a symmetric key for decryption, although the actual implementation may use a more secure encryption scheme.Hashing and constant-time comparison:The mitigated code hashes the input password using a cryptographic hash function (SHA-256) and a salt value. It then compares the hashed password and username with the retrieved values using a constant-time comparison function. This prevents attackers from exploiting timing attacks to gain information about the credentials.Secure memory handling:The mitigated code clears the credentials from memory usingmemsetafter authentication to minimize the risk of the sensitive data being accessed by other processes or attackers.
Java
JavaVulnerable Code
The below Vulnerable and Mitgated code focuses on Inbound Authentication.
The code is Vulnerable, because it uses hard-coded default credentials for first time logins. The default username and password are stored in plain text in the DEFAULT_USERNAME and DEFAULT_PASSWORD constants and can be easily extracted by an attacker. This can allow an attacker to bypass the authentication mechanism and access the system.
Some of the ways the Vulnerable code can be mitigated is
For Inbound Authentication,
Implement a "first login" mode:Instead of hard-coding default credentials, introduce a mechanism where users are required to enter a unique strong password or key during their initial login. This ensures that each user sets their own credentials. (as per CWE listed Potential Mitigations).Use secure storage for credentials:Store the user's credentials in a secure manner, such as using cryptographic techniques to protect sensitive information. Avoid storing plaintext passwords or keys.Implement multi-factor authentication (MFA):Consider implementing MFA, where users are required to provide additional verification factors (such as a one-time password or biometric data) along with their credentials. This adds an extra layer of security to the authentication process.
Mitigated Code
The Mitigated code does the following:
The code demonstrates mitigation for CWE-798 by implementing a
first loginmode that requires the user to enter a unique strong password, for inbound Authentication.
Python
PythonVulnerable Code
The above code is Vulnerable due to:
Hard-coded credentials: The username and password are directly embedded in the code, making them easily accessible to anyone who has access to the source code.
Outbound communication: The code sends the username and password as part of the HTTP request to the external API using the requests.get() function. This means that the credentials are transmitted over the network in plain text, which is highly insecure.
Inbound authentication: The login() function compares the input credentials directly with the hard-coded credentials.
The Vulnerable code can be mitigated by:
Store credentials in a
secure configuration fileor database with appropriateaccess control.Apply
strong one-way hash functionswith random salts to passwords before storing them.Use
secure protocolssuch as HTTPS for transmitting sensitive information over the network.Implement a
secure authentication mechanismthat compares the hashed input password with the stored hash, rather than comparing plain-text passwords directly.
Mitigated Code
The Mitigated code does the following:
It applies a
strong one-way hashwith a salt to the incoming password during authentication.The secure loading of credentials from a
configuration file or database. It reads the username, password hash, and salt from the "config.txt" file, assuming they are stored securely.
References
Use of hard-coded password | OWASP Foundation
A07 Identification and Authentication Failures - OWASP Top 10:2021
How to Prevent Hardcoded Passwords?
GO Code Review #1 : Hard-coded credentials are security-sensitive
Last updated