Pillar · Foundational

CWE-697: Incorrect Comparison

The product compares two entities in a security-relevant context, but the comparison is incorrect.

CWE-697 · Pillar Level ·3 CVEs

Description

The product compares two entities in a security-relevant context, but the comparison is incorrect.

This Pillar covers several possibilities:

Potential Impact

Other

Varies by Context

Demonstrative Examples

Consider an application in which Truck objects are defined to be the same if they have the same make, the same model, and were manufactured in the same year.
Bad
public class Truck {
                        private String make;private String model;private int year;
                           public boolean equals(Object o) {
                              if (o == null) return false;if (o == this) return true;if (!(o instanceof Truck)) return false;
                                 Truck t = (Truck) o;
                                 return (this.make.equals(t.getMake()) && this.model.equals(t.getModel()));
                           }
                     }
Here, the equals() method only checks the make and model of the Truck objects, but the year of manufacture is not included.
This example defines a fixed username and password. The AuthenticateUser() function is intended to accept a username and a password from an untrusted user, and check to ensure that it matches the username and password. If the username and password match, AuthenticateUser() is intended to indicate that authentication succeeded.
Bad
/* Ignore CWE-259 (hard-coded password) and CWE-309 (use of password system for authentication) for this example. */
                     
                     char *username = "admin";char *pass = "password";
                     int AuthenticateUser(char *inUser, char *inPass) {if (strncmp(username, inUser, strlen(inUser))) {logEvent("Auth failure of username using strlen of inUser");return(AUTH_FAIL);}if (! strncmp(pass, inPass, strlen(inPass))) {logEvent("Auth success of password using strlen of inUser");return(AUTH_SUCCESS);}else {logEvent("Auth fail of password using sizeof");return(AUTH_FAIL);}}
                     int main (int argc, char **argv) {
                     int authResult;if (argc < 3) {ExitError("Usage: Provide a username and password");}authResult = AuthenticateUser(argv[1], argv[2]);if (authResult == AUTH_SUCCESS) {DoAuthenticatedTask(argv[1]);}else {ExitError("Authentication failed");}}
In AuthenticateUser(), the strncmp() call uses the string length of an attacker-provided inPass parameter in order to determine how many characters to check in the password. So, if the attacker only provides a password of length 1, the check will only examine the first byte of the application's password before determining success.
As a result, this partial comparison leads to improper authentication (CWE-287).
Any of these passwords would still cause authentication to succeed for the "admin" user:
Attack
ppapaspass
This significantly reduces the search space for an attacker, making brute force attacks more feasible.
The same problem also applies to the username, so values such as "a" and "adm" will succeed for the username.
While this demonstrative example may not seem realistic, see the Observed Examples for CVE entries that effectively reflect this same weakness.

Detection Methods

  • Automated Static Analysis — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea

Real-World CVE Examples

CVE IDDescription
CVE-2021-3116Chain: Python-based HTTP Proxy server uses the wrong boolean operators (CWE-480) causing an incorrect comparison (CWE-697) that identifies an authN failure if all three conditions are met instead of
CVE-2020-15811Chain: Proxy uses a substring search instead of parsing the Transfer-Encoding header (CWE-697), allowing request splitting (CWE-113) and cache poisoning
CVE-2016-10003Proxy performs incorrect comparison of request headers, leading to infoleak

Frequently Asked Questions

What is CWE-697?

CWE-697 (Incorrect Comparison) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Pillar-level weakness. The product compares two entities in a security-relevant context, but the comparison is incorrect.

How can CWE-697 be exploited?

Attackers can exploit CWE-697 (Incorrect Comparison) to varies by context. This weakness is typically introduced during the Implementation phase of software development.

How do I prevent CWE-697?

Follow secure coding practices, conduct code reviews, and use automated security testing tools (SAST/DAST) to detect this weakness early in the development lifecycle.

What is the severity of CWE-697?

CWE-697 is classified as a Pillar-level weakness (Foundational abstraction). It has been observed in 3 real-world CVEs.