Compound Element ID: 352 (Compound Element Variant: Composite)
Status: Draft
Description
Description Summary
The web application does not, or can not, sufficiently verify
whether a well-formed, valid, consistent request was intentionally provided by
the user who submitted the request.
Extended Description
When a web server is designed to receive a request from a client without
any mechanism for verifying that it was intentionally sent, then it might be
possible for an attacker to trick a client into making an unintentional
request to the web server which will be treated as an authentic request.
This can be done via a URL, image load, XMLHttpRequest, etc. and can result
in data disclosure or unintended code execution.
Alternate Terms
Session Riding
Cross Site Reference Forgery
XSRF
Time of Introduction
Architecture and Design
Implementation
Applicable Platforms
Languages
All
Technology Classes
Web-Server
Likelihood of Exploit
High
Demonstrative Examples
Example 1
This example PHP code attempts to secure the form submission process
by validating that the user submitting the form has a valid session. A CSRF
attack would not be prevented by this countermeasure because the attacker
forges a request through the user's web browser in which a valid session
already exists.
The following HTML is intended to allow a user to update a
profile.
echo "Your profile has been successfully updated.";
}
This code may look protected since it checks for a valid session.
However, CSRF attacks can be staged from virtually any tag or HTML
construct, including image tags, links, embed or object tags, or other
attributes that load background images.
The attacker can then host code that will silently change the username
and email address of any user that visits the page while remaining
logged in to the target web application. The code might be an
innocent-looking web page such as:
Notice how the form contains hidden fields, so when it is loaded into
the browser, the user will not notice it. Because SendAttack() is
defined in the body's onload attribute, it will be automatically called
when the victim loads the web page.
Assuming that the user is already logged in to victim.example.com,
profile.php will see that a valid user session has been established,
then update the email address to the attacker's own address. At this
stage, the user's identity has been compromised, and messages sent
through this profile could be sent to the attacker's address.
Perform actions as administrator via a URL or an
img tag
Potential Mitigations
Phase
Description
Architecture and Design
Use anti-CSRF packages such as the OWASP CSRFGuard.
Implementation
Ensure that your application is free of cross-site scripting issues
(CWE-79), because most CSRF defenses can be bypassed using
attacker-controlled script.
Architecture and Design
Generate a unique nonce for each form, place the nonce into the form,
and verify the nonce upon receipt of the form. Be sure that the nonce is
not predictable (CWE-330).
Note that this can be bypassed using XSS (CWE-79).
Architecture and Design
Identify especially dangerous operations. When the user performs a
dangerous operation, send a separate confirmation request to ensure that
the user intended to perform that operation.
Note that this can be bypassed using XSS (CWE-79).
Architecture and Design
Use the "double-submitted cookie" method as described by Felten and
Zeller.
Note that this can probably be bypassed using XSS (CWE-79).
Architecture and Design
Use the ESAPI Session Management control.
This control includes a component for CSRF.
Architecture and Design
Do not use the GET method for any request that triggers a state
change.
Implementation
Check the HTTP Referer header to see if the request originated from an
expected page. This could break legitimate functionality, because users
or proxies may have disabled sending the Referer for privacy
reasons.
Note that this can be bypassed using XSS (CWE-79). An attacker could
use XSS to generate a spoofed Referer, or to generate a malicious
request from a page whose Referer would be allowed.
Testing
Use tools and techniques that require manual (human) analysis, such as
penetration testing, threat modeling, and interactive tools that allow
the tester to record and modify an active session. These may be more
effective than strictly automated techniques. This is especially the
case with weaknesses that are related to design and business
rules.
Use OWASP CSRFTester to identify potential issues.
The software stores security-critical state information about
its users, or the software itself, in a location that is accessible to
unauthorized actors.
Extended Description
If an attacker can modify the state information without detection, then it
could be used to perform unauthorized actions or access unexpected
resources, since the application programmer does not expect that the state
can be changed.
State information can be stored in various locations such as a cookie, in
a hidden web form field, input parameter or argument, an environment
variable, a database record, within a settings file, etc. All of these
locations have the potential to be modified by an attacker. When this state
information is used to control security or determine resource usage, then it
may create a vulnerability. For example, an application may perform
authentication, then save the state in an "authenticated=true" cookie. An
attacker may simply create this cookie in order to bypass the
authentication.
Time of Introduction
Architecture and Design
Implementation
Applicable Platforms
Languages
All
Technology Classes
Web-Server: (Often)
Common Consequences
Scope
Effect
Integrity
An attacker could potentially modify the state in malicious ways. If
the state is related to the privileges or level of authentication that
the user has, then state modification might allow the user to bypass
authentication or elevate privileges.
Confidentiality
The state variables may contain sensitive information that should not
be known by the client.
Availability
By modifying state variables, the attacker could violate the
application's expectations for the contents of the state, leading to a
denial of service due to an unexpected error condition.
Likelihood of Exploit
High
Enabling Factors for Exploitation
An application maintains its own state and/or user state (i.e. application
is stateful).
State information can be affected by the user of an application through
some means other than the legitimate state transitions (e.g. logging into
the system, purchasing an item, making a payment, etc.)
An application does not have means to detect state tampering and behave in
a fail safe manner.
Demonstrative Examples
Example 1
In the following example, an authentication flag is read from a
browser cookie, thus allowing for external control of user state
data.
(Bad Code)
Java
Cookie[] cookies = request.getCookies();
for (int i =0; i< cookies.length; i++) {
Cookie c = cookies[i];
if (c.getName().equals("authenticated") &&
Boolean.TRUE.equals(c.getValue())) {
authenticated = true;
}
}
Example 2
The following code segment implements a basic server that uses the
"ls" program to perform a directory listing of the directory that is listed
in the "HOMEDIR" environment variable. The code intends to allow the user to
specify an alternate "LANG" environment variable. This causes "ls" to
customize its output based on a given language, which is an important
capability when supporting internationalization.
(Bad Code)
Perl
$ENV{"HOMEDIR"} = "/home/mydir/public/";
my $stream = AcceptUntrustedInputStream();
while (<$stream>) {
chomp;
if (/^ENV ([\w\_]+) (.*)/) {
$ENV{$1} = $2;
}
elsif (/^QUIT/) { ... }
elsif (/^LIST/) {
open($fh, "/bin/ls -l $ENV{HOMEDIR}|");
while (<$fh>) {
SendOutput($stream, "FILEINFO: $_");
}
close($fh);
}
}
The programmer takes care to call a specific "ls" program and sets the
HOMEDIR to a fixed value. However, an attacker can use a command such as
"ENV HOMEDIR /secret/directory" to specify an alternate directory,
enabling a path traversal attack (CWE-22). At the same time, other
attacks are enabled as well, such as OS command injection (CWE-78) by
setting HOMEDIR to a value such as "/tmp; rm -rf /". In this case, the
programmer never intends for HOMEDIR to be modified, so input validation
for HOMEDIR is not the solution. A partial solution would be a whitelist
that only allows the LANG variable to be specified in the ENV command.
Alternately, assuming this is an authenticated user, the language could
be stored in a local file so that no ENV command at all would be
needed.
While this example may not appear realistic, this type of problem
shows up in code fairly frequently. See CVE-1999-0073 in the observed
examples for a real-world example with similar behaviors.
Server allows client to specify the search path,
which can be modified to point to a program that the client has
uploaded.
Potential Mitigations
Phase
Description
Architecture and Design
Understand all the potential locations that are accessible to
attackers. For example, some programmers assume that cookies and hidden
form fields cannot be modified by an attacker, or they may not consider
that environment variables can be modified before a privileged program
is invoked.
Architecture and Design
Do not keep state information on the client without using encryption
and integrity checking, or otherwise having a mechanism on the server
side to catch state tampering. Use a message authentication code (MAC)
algorithm, such as Hash Message Authentication Code (HMAC). Apply this
against the state data that you have to expose, which can guarantee the
integrity of the data - i.e., that the data has not been modified.
Ensure that you use an algorithm with a strong hash function
(CWE-328).
Architecture and Design
Store state information on the server side only. Ensure that the
system definitively and unambiguously keeps track of its own state and
user state and has rules defined for legitimate state transitions. Do
not allow any application user to affect state directly in any way other
than through legitimate actions leading to state transitions.
Architecture and Design
With a stateless protocol such as HTTP, use a framework that maintains
the state for you.
Examples include ASP.NET View State and the OWASP ESAPI Session
Management feature.
Be careful of language features that provide state support, since
these might be provided as a convenience to the programmer and may not
be considering security.
Architecture and Design
For any security checks that are performed on the client side, ensure
that these checks are duplicated on the server side, in order to avoid
CWE-602. Attackers can bypass the client-side checks by modifying values
after the checks have been performed, or by changing the client to
remove the client-side checks entirely. Then, these modified values
would be submitted to the server.
Operation
Implementation
If you are using PHP, configure your application so that it does not
use register_globals. During implementation, develop your application so
that it does not rely on this feature, but be wary of implementing a
register_globals emulation that is subject to weaknesses such as CWE-95,
CWE-621, and similar issues.
Testing
Use automated static analysis tools that target this type of weakness.
Many modern techniques use data flow analysis to minimize the number of
false positives. This is not a perfect solution, since 100% accuracy and
coverage are not feasible.
Testing
Use dynamic tools and techniques that interact with the software using
large test suites with many diverse inputs, such as fuzz testing
(fuzzing), robustness testing, and fault injection. The software's
operation may slow down, but it should not become unstable, crash, or
generate incorrect results.
Testing
Use tools and techniques that require manual (human) analysis, such as
penetration testing, threat modeling, and interactive tools that allow
the tester to record and modify an active session. These may be more
effective than strictly automated techniques. This is especially the
case with weaknesses that are related to design and business
rules.
According to WASC, "Insufficient Session Expiration is when a
web site permits an attacker to reuse old session credentials or session IDs for
authorization."
Time of Introduction
Architecture and Design
Implementation
Demonstrative Examples
Example 1
The following snippet was taken from a J2EE web.xml deployment
descriptor in which the session-timeout parameter is explicitly defined (the
default value depends on the container). In this case the value is set to
-1, which means that a session will never expire.
(Bad Code)
Java
<web-app>
[...snipped...]
<session-config>
<session-timeout>-1</session-timeout>
</session-config>
</web-app>
Potential Mitigations
Phase
Description
Set sessions/credentials expiration date.
Other Notes
The lack of proper session expiration may improve the likely success of
certain attacks. For example, an attacker may intercept a session ID,
possibly via a network sniffer or Cross-site Scripting attack. Although
short session expiration times do not help if a stolen token is immediately
used, they will protect against ongoing replaying of the session ID. In
another scenario, a user might access a web site from a shared computer
(such as at a library, Internet cafe, or open work environment).
Insufficient Session Expiration could allow an attacker to use the browser's
back button to access web pages previously accessed by the victim.
product does not sufficiently distinguish external
HTML from internal, potentially dangerous HTML, allowing bypass using
special strings in the page title. Overlaps special
elements.
A product can be used as an intermediary or proxy between an
attacker and the ultimate target, so that the attacker can either bypass access
controls or hide activities.
FTP bounce attack. Protocol allows attacker to
modify the PORT command to cause the FTP server to connect to other machines
besides the attacker's. Similar to proxied trusted
channel.
Potential Mitigations
Phase
Description
Enforce the use of strong mutual authentication mechanism between the
two parties.
This entry is currently a child of CWE-610 under view 1000, however there
is also a relationship with CWE-668 because the resulting proxy effectively
exposes the victims control sphere to the attacker. This should possibly be
considered as an emergent resource.
Content History
Submissions
Submission Date
Submitter
Organization
Source
PLOVER
Externally Mined
Modifications
Modification Date
Modifier
Organization
Source
2008-07-01
Eric Dalci
Cigital
External
updated Potential Mitigations,
Time of Introduction
2008-09-08
CWE Content Team
MITRE
Internal
updated Relationships, Observed Example, Other Notes,
Taxonomy Mappings
2008-11-24
CWE Content Team
MITRE
Internal
updated Maintenance Notes, Relationships,
Taxonomy Mappings, Time of Introduction