{"@ID": "185", "@Name": "Incorrect Regular Expression", "@Abstraction": "Class", "@Structure": "Simple", "@Status": "Draft", "Description": "The product specifies a regular expression in a way that causes data to be improperly matched or compared.", "Extended_Description": "When the regular expression is used in protection mechanisms such as filtering or validation, this may allow an attacker to bypass the intended restrictions on the incoming data.", "Related_Weaknesses": {"Related_Weakness": [{"@Nature": "ChildOf", "@CWE_ID": "697", "@View_ID": "1000", "@Ordinal": "Primary"}, {"@Nature": "CanPrecede", "@CWE_ID": "187", "@View_ID": "1000"}, {"@Nature": "CanPrecede", "@CWE_ID": "182", "@View_ID": "1000"}]}, "Weakness_Ordinalities": {"Weakness_Ordinality": {"Ordinality": "Primary"}}, "Applicable_Platforms": {"Language": {"@Class": "Not Language-Specific", "@Prevalence": "Undetermined"}}, "Modes_Of_Introduction": {"Introduction": {"Phase": "Implementation"}}, "Common_Consequences": {"Consequence": [{"Scope": "Other", "Impact": ["Unexpected State", "Varies by Context"], "Note": "When the regular expression is not correctly specified, data might have a different format or type than the rest of the program expects, producing resultant weaknesses or errors."}, {"Scope": "Access Control", "Impact": "Bypass Protection Mechanism", "Note": "In PHP, regular expression checks can sometimes be bypassed with a null byte, leading to any number of weaknesses."}]}, "Detection_Methods": {"Detection_Method": {"@Detection_Method_ID": "DM-14", "Method": "Automated Static Analysis", "Description": "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 searching for potentially-vulnerable patterns that connect \"sources\" (origins of input) with \"sinks\" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)", "Effectiveness": "High"}}, "Potential_Mitigations": {"Mitigation": {"@Mitigation_ID": "MIT-45", "Phase": "Implementation", "Strategy": "Refactoring", "Description": "Regular expressions can become error prone when defining a complex language even for those experienced in writing grammars. Determine if several smaller regular expressions simplify one large regular expression. Also, subject the regular expression to thorough testing techniques such as equivalence partitioning, boundary value analysis, and robustness. After testing and a reasonable confidence level is achieved, a regular expression may not be foolproof. If an exploit is allowed to slip through, then record the exploit and refactor the regular expression."}}, "Demonstrative_Examples": {"Demonstrative_Example": [{"@Demonstrative_Example_ID": "DX-37", "Intro_Text": "The following code takes phone numbers as input, and uses a regular expression to reject invalid phone numbers.", "Example_Code": {"@Nature": "Bad", "@Language": "Perl", "xhtml:div": {"xhtml:br": [null, null], "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:i": "# looks like it only has hyphens and digits", "xhtml:br": null, "#text": "system(\"lookup-phone $phone\");"}, {"@style": "margin-left:1em;", "#text": "error(\"malformed number!\");"}], "#text": "$phone = GetPhoneNumber();if ($phone =~ /\\d+-\\d+/) {}\n\t\t\t\t  else {}"}}, "Body_Text": "An attacker could provide an argument such as: \"; ls -l ; echo 123-456\" This would pass the check, since \"123-456\" is sufficient to match the \"\\d+-\\d+\" portion of the regular expression."}, {"@Demonstrative_Example_ID": "DX-154", "Intro_Text": "This code uses a regular expression to validate an IP string prior to using it in a call to the \"ping\" command.", "Example_Code": {"@Nature": "Bad", "@Language": "Python", "xhtml:div": {"xhtml:br": [null, null, null, null, null, null], "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": [null, null, null], "xhtml:div": [{"@style": "margin-left:1em;", "xhtml:br": null, "#text": "return ip"}, {"@style": "margin-left:1em;", "xhtml:br": null, "#text": "raise ValueError(\"IP address does not match valid pattern.\")"}], "#text": "ip_validator = re.compile(r\"((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}\")\n\t\t    if ip_validator.match(ip):\n\t\t    \n\t\t    else:"}, {"@style": "margin-left:1em;", "xhtml:br": [null, null, null, null], "xhtml:i": "# The ping command treats zero-prepended IP addresses as octal", "#text": "validated = validate_ip_regex(ip)\n\t\t    \n\t\t    result = subprocess.call([\"ping\", validated])\n\t\t    print(result)"}], "#text": "import subprocess\n\t\t  import re\n\t\t  \n\t\t  def validate_ip_regex(ip: str):\n\t\t  \n\t\t  \n\t\t  def run_ping_regex(ip: str):"}}, "Body_Text": "Since the regular expression does not have anchors (CWE-777), i.e. is unbounded without ^ or $ characters, then prepending a 0 or 0x to the beginning of the IP address will still result in a matched regex pattern. Since the ping command supports octal and hex prepended IP addresses, it will use the unexpectedly valid IP address (CWE-1389). For example, \"0x63.63.63.63\" would be considered equivalent to \"99.63.63.63\". As a result, the attacker could potentially ping systems that the attacker cannot reach directly."}]}, "Observed_Examples": {"Observed_Example": [{"Reference": "CVE-2002-2109", "Description": "Regexp isn't \"anchored\" to the beginning or end, which allows spoofed values that have trusted values as substrings.", "Link": "https://www.cve.org/CVERecord?id=CVE-2002-2109"}, {"Reference": "CVE-2005-1949", "Description": "Regexp for IP address isn't anchored at the end, allowing appending of shell metacharacters.", "Link": "https://www.cve.org/CVERecord?id=CVE-2005-1949"}, {"Reference": "CVE-2001-1072", "Description": "Bypass access restrictions via multiple leading slash, which causes a regular expression to fail.", "Link": "https://www.cve.org/CVERecord?id=CVE-2001-1072"}, {"Reference": "CVE-2000-0115", "Description": "Local user DoS via invalid regular expressions.", "Link": "https://www.cve.org/CVERecord?id=CVE-2000-0115"}, {"Reference": "CVE-2002-1527", "Description": "chain: Malformed input generates a regular expression error that leads to information exposure.", "Link": "https://www.cve.org/CVERecord?id=CVE-2002-1527"}, {"Reference": "CVE-2005-1061", "Description": "Certain strings are later used in a regexp, leading to a resultant crash.", "Link": "https://www.cve.org/CVERecord?id=CVE-2005-1061"}, {"Reference": "CVE-2005-2169", "Description": "MFV. Regular expression intended to protect against directory traversal reduces \".../...//\" to \"../\".", "Link": "https://www.cve.org/CVERecord?id=CVE-2005-2169"}, {"Reference": "CVE-2005-0603", "Description": "Malformed regexp syntax leads to information exposure in error message.", "Link": "https://www.cve.org/CVERecord?id=CVE-2005-0603"}, {"Reference": "CVE-2005-1820", "Description": "Code injection due to improper quoting of regular expression.", "Link": "https://www.cve.org/CVERecord?id=CVE-2005-1820"}, {"Reference": "CVE-2005-3153", "Description": "Null byte bypasses PHP regexp check.", "Link": "https://www.cve.org/CVERecord?id=CVE-2005-3153"}, {"Reference": "CVE-2005-4155", "Description": "Null byte bypasses PHP regexp check.", "Link": "https://www.cve.org/CVERecord?id=CVE-2005-4155"}]}, "Taxonomy_Mappings": {"Taxonomy_Mapping": {"@Taxonomy_Name": "PLOVER", "Entry_Name": "Regular Expression Error"}}, "Related_Attack_Patterns": {"Related_Attack_Pattern": [{"@CAPEC_ID": "15"}, {"@CAPEC_ID": "6"}, {"@CAPEC_ID": "79"}]}, "References": {"Reference": {"@External_Reference_ID": "REF-7", "@Section": "Chapter 10, \"Using Regular Expressions for Checking Input\" Page 350"}}, "Mapping_Notes": {"Usage": "Allowed-with-Review", "Rationale": "This CWE entry is a Class and might have Base-level children that would be more appropriate", "Comments": "Examine children of this entry to see if there is a better fit", "Reasons": {"Reason": {"@Type": "Abstraction"}}}, "Notes": {"Note": [{"@Type": "Relationship", "#text": "While there is some overlap with allowlist/denylist problems, this entry is intended to deal with incorrectly written regular expressions, regardless of their intended use. Not every regular expression is intended for use as an allowlist or denylist. In addition, allowlists and denylists can be implemented using other mechanisms besides regular expressions."}, {"@Type": "Research Gap", "#text": "Regexp errors are likely a primary factor in many MFVs, especially those that require multiple manipulations to exploit. However, they are rarely diagnosed at this level of detail."}]}, "Content_History": {"Submission": {"Submission_Name": "PLOVER", "Submission_Date": "2006-07-19", "Submission_Version": "Draft 3", "Submission_ReleaseDate": "2006-07-19"}, "Modification": [{"Modification_Name": "Eric Dalci", "Modification_Organization": "Cigital", "Modification_Date": "2008-07-01", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Time_of_Introduction"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2008-09-08", "Modification_Version": "1.0", "Modification_ReleaseDate": "2008-09-09", "Modification_Comment": "updated Description, Name, Relationships, Observed_Example, Other_Notes, Taxonomy_Mappings"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2009-12-28", "Modification_Version": "1.7", "Modification_ReleaseDate": "2009-12-28", "Modification_Comment": "updated Common_Consequences, Other_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2010-02-16", "Modification_Version": "1.8", "Modification_ReleaseDate": "2010-02-16", "Modification_Comment": "updated References"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2010-04-05", "Modification_Version": "1.8.1", "Modification_ReleaseDate": "2010-04-05", "Modification_Comment": "updated Description"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-03-29", "Modification_Version": "1.12", "Modification_ReleaseDate": "2011-03-30", "Modification_Comment": "updated Observed_Examples"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2011-06-01", "Modification_Version": "1.13", "Modification_ReleaseDate": "2011-06-01", "Modification_Comment": "updated Common_Consequences"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-05-11", "Modification_Version": "2.2", "Modification_ReleaseDate": "2012-05-15", "Modification_Comment": "updated Demonstrative_Examples, Related_Attack_Patterns, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2012-10-30", "Modification_Version": "2.3", "Modification_ReleaseDate": "2012-10-30", "Modification_Comment": "updated Potential_Mitigations"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2014-06-23", "Modification_Version": "2.7", "Modification_ReleaseDate": "2014-06-23", "Modification_Comment": "updated Applicable_Platforms, Common_Consequences, Other_Notes, Relationship_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2014-07-30", "Modification_Version": "2.8", "Modification_ReleaseDate": "2014-07-31", "Modification_Comment": "updated Demonstrative_Examples, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2015-12-07", "Modification_Version": "2.9", "Modification_ReleaseDate": "2015-12-07", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2017-11-08", "Modification_Version": "3.0", "Modification_ReleaseDate": "2017-11-08", "Modification_Comment": "updated References"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2018-03-27", "Modification_Version": "3.1", "Modification_ReleaseDate": "2018-03-27", "Modification_Comment": "updated References"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2019-06-20", "Modification_Version": "3.3", "Modification_ReleaseDate": "2019-06-20", "Modification_Comment": "updated Related_Attack_Patterns, Relationships, Type"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-02-24", "Modification_Version": "4.0", "Modification_ReleaseDate": "2020-02-24", "Modification_Comment": "updated Relationships, Type"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2020-06-25", "Modification_Version": "4.1", "Modification_ReleaseDate": "2020-06-25", "Modification_Comment": "updated Relationship_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2021-03-15", "Modification_Version": "4.4", "Modification_ReleaseDate": "2021-03-15", "Modification_Comment": "updated Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2022-10-13", "Modification_Version": "4.9", "Modification_ReleaseDate": "2022-10-13", "Modification_Comment": "updated Demonstrative_Examples, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-01-31", "Modification_Version": "4.10", "Modification_ReleaseDate": "2023-01-31", "Modification_Comment": "updated Description"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-04-27", "Modification_Version": "4.11", "Modification_ReleaseDate": "2023-04-27", "Modification_Comment": "updated Detection_Factors, Relationships"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2023-06-29", "Modification_Version": "4.12", "Modification_ReleaseDate": "2023-06-29", "Modification_Comment": "updated Mapping_Notes"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2025-12-11", "Modification_Version": "4.19", "Modification_ReleaseDate": "2025-12-11", "Modification_Comment": "updated Weakness_Ordinalities"}, {"Modification_Name": "CWE Content Team", "Modification_Organization": "MITRE", "Modification_Date": "2026-04-30", "Modification_Version": "4.20", "Modification_ReleaseDate": "2026-04-30", "Modification_Comment": "updated Potential_Mitigations"}], "Previous_Entry_Name": {"@Date": "2008-09-09", "#text": "Regular Expression Error"}}}
