Prevent CSRF Attacks PDF
Prevent CSRF Attacks PDF
Prevent CSRF Attacks PDF
Abstract
The web has become an indispensable part of our lives.
Unfortunately, as our dependency on the web increases, so
does the interest of attackers in exploiting web applications
and web-based information systems. Previous work in the
field of web application security has mainly focused on the
mitigation of Cross Site Scripting (XSS) and SQL injection
attacks. In contrast, Cross Site Request Forgery (XSRF) attacks have not received much attention. In an XSRF attack,
the trust of a web application in its authenticated users is
exploited by letting the attacker make arbitrary HTTP requests on behalf of a victim user. The problem is that web
applications typically act upon such requests without verifying that the performed actions are indeed intentional.
Because XSRF is a relatively new security problem, it is
largely unknown by web application developers. As a result, there exist many web applications that are vulnerable
to XSRF. Unfortunately, existing mitigation approaches are
time-consuming and error-prone, as they require manual effort to integrate defense techniques into existing systems. In
this paper, we present a solution that provides a completely
automatic protection from XSRF attacks. More precisely,
our approach is based on a server-side proxy that detects
and prevents XSRF attacks in a way that is transparent to
users as well as to the web application itself. We provide
experimental results that demonstrate that we can use our
prototype to secure a number of popular open-source web
applications, without negatively affecting their behavior.
2.1
1 Introduction
Cross site request forgery [18, 20, 23] (abbreviated
XSRF or CSRF, sometimes also called Session Riding),
denotes a relatively new class of attack against web application users. By launching a successful XSRF attack against a
user, an adversary is able to initiate arbitrary HTTP requests
from that user to the vulnerable web application. Thus, if
the victim is authenticated, a successful XSRF attack effectively bypasses the underlying authentication mechanism.
Depending on the web application, the attacker could, for
create a cookie with a given name and value. In all subsequent requests to the server, the browser automatically includes this cookie information, using the Cookie HTTP
header. Based on this cookie information, a web application can then associate requests with certain clients.
Of course, using cookies to store information is only
suitable for data that may be freely modified by the client.
Because data is stored on the clients machine, it is under the
users direct control. For some web applications, information must not be modified between requests. In other cases,
the amount of data that is associated with a certain user is
too large to constantly exchange it between the client and
the server. To address these issues, web applications typically make use of sessions.
determine whether that user is already logged in by consulting this boolean value. As a result, the user is able to perform privileged actions without the need to explicitly submit a password each time. Instead, the authentication occurs implicitly by the underlying session mechanism. That
is, the session ID serves as an implicit authentication token.
2.2
4 A Proxy-Based Solution
To be useful in practice, a mitigation technique for XSRF
attacks has to satisfy two properties. First, it has to be effective in detecting and preventing XSRF attacks with a
very low false negative and false positive rate. Second, it
should be generic and spare web site administrators and programmers from application-specific modifications. Unfortunately, all existing approaches presented in the previous
section fail in at least one of the two aspects.
Our solution to the XSRF problem is to decouple the
necessary security mechanisms from the application and to
provide a separate module that can be plugged into existing systems with minimal effort. More precisely, we propose a proxy that is placed on the server side between the
web server and the target application. This proxy is able
to inspect and modify client requests as well as the applications replies (output) to automatically and transparently
extend applications with the previously sketched shared secret technique. In particular, the proxy has to
ensure that replies to an authenticated user are modified in such a way that future requests originating from
this document (i.e, through hyperlinks and forms) will
contain a valid token, and
take countermeasures against requests from authenticated users that do not contain a valid token.
An essential prerequisite for this mechanism is the
proxys ability to associate a users session with a valid token. To this end, the proxy maintains a token table with
entries that map session IDs to tokens.
By decoupling the proxy from the actual application, the
XSRF protection can be offered transparently for (virtually)
all applications. Note that, alternatively, our proxy could
also be located between the client and the web server. However, this case could lead to problems in combination with
SSL connections. With our proposed architecture, SSL issues are directly handled by the web server, which eases the
tasks that are to be performed by the proxy.
In the following sections, we present a more detailed description of how requests to and replies from the web application are handled, along with illustrative examples.
4.1
XSRF attacks are still relatively unknown to web developers and attackers. Nevertheless, we believe that the attention paid to this class of attacks will reach that of more traditional XSS attacks in the near future as the attack becomes
better known and understood. Unfortunately, current mitigation techniques have shortcomings that limit their general
applicability. To address this problem, the following section
presents a novel and automatic approach for XSRF protection.
Request Processing
4.2
Reply Processing
URL such that it contains the token associated with this session ID:
<a href=index.php?action=logout
&token=99>LogOut
</a>
When the user follows this link, the mechanism has ensured
that the proper token is transmitted.
The name of the parameter that stores the token (token
in this example) can be chosen arbitrarily, but must not interfere with the names of other parameters used by the target
application. The tokens value (99) is retrieved from the
token table that the proxy maintains.
At this point, an important question is the following:
How can the proxy determine whether a client is authenticated or not? For our purposes, we treat the state a client
is authenticated as equal to a client has an active session.
This is a safe assumption, because XSRF attacks cannot
succeed when there is no session information that can be
exploited to force the victim into performing privileged actions (that is, actions which require previous authentication)
on behalf of the attacker.
The next question is how to determine whether a user has
an active session or not. Programming languages such as
PHP provide a built-in session infrastructure that could be
consulted about whether there exists such a session. However, many applications make use of custom session management techniques. Sometimes, session information is
even stored in a back-end database. In such cases, the target application could be instrumented with functions that
enable the proxy to issue appropriate queries about the session state. Unfortunately, this would lead to the undesirable
necessity to perform application-specific modifications.
We solve the problem of determining whether a session
exists in the following way. Basically, there are two cases
that have to be distinguished, depending on whether the application sets a session cookie while processing a clients
request or not. We can check this by searching the applications reply for an HTTP Set-Cookie header. Of
course, this approach requires our system to distinguish between session cookies (i.e., cookies that store session information) and cookies that are set for other purposes. While
it might be possible to use heuristics to automatically identify session cookies, we currently require the administrator
of the system to manually specify their names. Typically,
this is straightforward, as many applications make use of
4.3
The token table should be freed from stale entries regularly to save memory and CPU time. To this end, we ex-
4.4
4.5
Eliminating State
As mentioned previously, the token table associates session IDs with tokens. This explicit, stateful mapping can
be replaced by a mapping that requires no state at all to be
stored by the proxy. To this end, a token is computed by
applying a hash function based on a secret server key to a
session ID. As this approach does not require the explicit
storage of session ID to token mappings (the token value
can always be computed from the session ID), it eliminates
the DoS attack vector sketched in Section 4.4. Another major difference compared to the token table approach is that
the mappings remain constant over time. When using a token table, mappings change as they are introduced with randomly generated tokens and removed when they expire.
At first glance, static mappings might appear dangerous
from a security point of view. An attacker could, in theory, construct an explicit representation of the hash function
that is used by sending all possible session IDs to the server
and writing down the observed tokens in return. By inverting this mapping, the attacker could deduce the session ID
from the users token (modulo hash collisions). This would
turn the token into a piece of information worth stealing, increasing the attackers opportunities. However, reconstructing even small parts of the hash function is not feasible in
practice due to the vast number of possible session IDs. In
PHP, for example, the space for session IDs comprises 6232
entries (32 digits or case-sensitive characters from a to z).
For harvesting just 0.01% of all possible mappings in one
year, an attacker would have to issue 7 1045 requests per
second. An even higher level of security could be achieved
by changing the hash function each year, invalidating an attackers previous harvesting efforts. The resulting convenience penalty for users with active sessions would be min-
imal, as users with incorrect tokens are immediately provided with a link containing the correct token.
4.6
Limitations
5 Implementation
To demonstrate the feasibility of our concepts, we implemented NoForge, a server-side proxy that is able to defend
PHP applications against XSRF attacks. As explained previously, this proxy is located between the web server and the
protected web applications. To realize this in a straightforward fashion, we decided to implement the proxy as wrapper functions around those PHP applications that we intend
to protect. These wrapper functions check the input and
output of the application and perform the necessary request
and reply processing.
Application
phpBB
2.0.19
10,483,075
Delete postings.
Send postings.
phpMyAdmin
2.8.0.2
9,494,550
Delete databases.
Create databases.
Gallery
2.0.4
3,937,352
None found.
XOOPS
2.0.13.2
3,448,408
None found.
phpNuke
7.0
2,727,943
Delete messages.
Add messages.
1.4.4
1,981,777
Squirrelmail
1.4.6
1,905,277
its simplicity. Alternatively, the proxy could also be implemented on the network level, where it has full access to the
complete HTTP stream.
Another implementation issue we encountered was that
if the target application halted program execution using
the exit() or die() function, execution of our proxy
stopped as well. As a result, no reply processing was performed. In this case, we successfully applied the same solution as before. By providing additional wrappers to the
offending functions, we were able to catch such calls and
initiate proper processing of the reply.
To summarize, here are the steps that are necessary for
protecting a web application with our prototype implementation:
1. Add an appropriate alias to the Apache configuration.
2. Execute the sed script on the target application to enable the proxys wrapper functions.
3. Specify the cookie names that the target application
uses to store session IDs (typically, this defaults to
PHPSESSID).
4. Specify the page that the user shall be redirected to in
case of an XSRF attack.
These steps typically take less that five minutes for each
application. Clearly, this process is far less time-consuming
than manually adapting the whole target application to prevent XSRF attacks.
6 Experimental Results
To test our implementation, we chose the current stable
releases of the seven largest (in terms of all-time down-
loads) open-source PHP web applications from SourceForge [22]. The high number of downloads (see Table 1) indicates that these applications are popular and wide-spread.
Despite the fact that these applications are popular and
well-maintained, we quickly discovered XSRF vulnerabilities in five of the applications. The two remaining ones,
Gallery 2.0.4 and XOOPS 2.0.13.2, appeared to be immune
to XSRF attacks, although we did not conduct exhaustive
tests or source code reviews in pursue of XSRF vulnerabilities.
For the five vulnerable applications, we constructed a
number of XSRF exploits that modify important data by
abusing the privileges of an authenticated user. For instance,
we managed to post and delete messages from a forum in
the name of the victim, send mail, or even change the user
name and password of the current user, resulting in identity theft. A comprehensive list of the created exploits is
provided in Table 1.
Our first test evaluated the proxys ability to protect vulnerable applications. After verifying that our exploits were
working properly, we installed the proxy and repeated the
attacks. This time, every XSRF attempt was correctly detected.
Apart from protecting applications, a central requirement
that the proxy has to fulfill is to not interfere with the normal application behavior (both regular behavior as well as
behavior in case of errors). To test this property, we can
observe and compare the applications behavior without the
proxys protection to the behavior with enabled XSRF protection. If the two results are identical, the proxy succeeded
in performing its task transparently. Hence, we applied the
following test procedure for each application:
1. Log into the application.
2. On the following page, if there is a request trigger (e.g.,
a hyperlink) that was not activated yet:
(a) Activate the next unvisited request trigger. If the
trigger is a form, test it with correct as well as
with incorrect input.
(b) Hit the browsers back button.
(c) Continue with Step 2.
3. Log out.
Note that these tests also cover the correct behavior of
the browsers back button, which is a widely used convenience feature that must not be broken by XSRF countermeasures. Also note that the use cases that were targets of
our demonstration exploits have already been tested in the
previous stage (i.e., while verifying that the XSRF protection works). In addition to this systematic procedure, we
also chose some random work flows typical for the application in question. Altogether, we are confident that the
coverage of our tests is large enough to give representative
7 Related Work
The detection of web-based attacks has received considerable attention because of the increasingly critical role that
web-based services are playing on the Internet. This includes web application firewalls [19] to protect applications
from malicious requests as well as intrusion detection systems that attempt to identify attacks against web servers and
their applications [1, 9]. Also, code analysis tools were proposed that check applications for the existence of bugs that
can lead to security vulnerabilities [4, 7].
In particular, cross site scripting (XSS) attacks have received much interest, and both server-side and client-side
solutions were proposed. For example, in [3], the use
of a variety of software-testing techniques (including dynamic analysis, black-box testing, fault injection and behavior monitoring) are suggested to identify XSS vulnerabilities. Alternatively, dynamic techniques on the server
side [16, 24] can be used to track non-validated user input
while it is processed by the application. This can help to detect and mitigate XSS flaws. Finally, in previous work, we
implemented a client-side solution [8] to protect users from
XSS attempts. Unfortunately, these solutions cannot be applied to the problem of cross site request forgery, because
XSRF attacks are not due to input validation problems.
The general class of cross site request forgery (XSRF)
attacks was first introduced by Peter W. in a posting [23]
to the BugTraq mailing list, and has since been picked up
by web application developers [21]. However, it appears to
be a little known problem in the academic community and,
as a result, has only received little attention (which is one
of the reasons why we believe that this paper might be interesting). The mitigation mechanisms for XSRF that were
proposed so far (discussed in more detail in Section 3) either provide only partial protection (such as replacing GET
requests by POST requests, or relying on the information in
the Referer header of HTTP requests) or require significant
modifications to each individual web application that should
be protected (when embedding shared secrets into the applications output). Our solution, on the other hand, attempts
to retain the advantage of a solution based on shared secrets
8 Conclusion
In a cross site request forgery (XSRF) attack, the trust
of a web application in its authenticated users is exploited,
allowing an attacker to make arbitrary HTTP requests in
the victims name. Unfortunately, current XSRF mitigation techniques have shortcomings that limit their general
applicability. To address this problem, this paper presents
a solution that provides a completely automatic protection
from XSRF attacks. Our approach is based on a server-side
proxy that detects and prevents XSRF attacks in a way that
is transparent to users as well as to the web application itself.
We have successfully used our prototype to secure a
number of popular open-source web applications that were
vulnerable to XSRF. Our experimental results demonstrate
that the solution is viable, and that we can secure existing
web applications without adversely affecting their behavior.
Currently, XSRF attacks are relatively unknown to both
web developers and attackers that are on the hunt for easy
targets. However, we expect the attention paid to this class
of attacks to soon reach that of more traditional web security
problems (such as XSS or SQL injections), and we hope
that our solution will prove useful in protecting vulnerable
web applications.
Acknowledgments
This work was supported by the Austrian Science Foundation (FWF) under grants P18368 (Omnis) and P18764
(Web-Defense), and by the Secure Business Austria competence center.
References
[1] M. Almgren, H. Debar, and M. Dacier. A lightweight tool
for detecting web server attacks. In ISOC Symposium on
Network and Distributed Systems Security (NDSS), 2000.