W02_Web_CSRF
W02_Web_CSRF
Forgery
(CSRF)
Outline
● Cross-Site Requests and Its Problems
● Cross-Site Request Forgery Attack
● CSRF Attacks on HTTP GET
● CSRF Attacks on HTTP POST
● Countermeasures
Cross-Site Requests and Its Problems
● When a page from a website
sends an HTTP request back to
the website, it is called same-site
request.
● If a request is sent to a different
website, it is called cross-site
request because the where the
page comes from and where the
request goes are different.
Steps:
● The attacker crafts a webpage that can forge a cross-site request to be
sent to the targeted website.
● The attacker needs to attract the victim user to visit the malicious
website.
● The victim is logged into the targeted website.
Environment Setup
● Elgg: open-source web application for social networking
● Countermeasures for CSRF is disabled by us in the VM
● Target website: http://www.csrflabelgg.com
● Attacker’s website: http://www.csrflabattacker.com
● These websites are hosted on localhost via Apache’s Virtual Hosting
CSRF Attacks on HTTP Get Services
HTTP GET requests: data (foo and bar) are attached in the URL.
HTTP POST requests: data (foo and bar) are placed inside the data field of
the HTTP request.
CSRF Attack on GET Requests - Basic
Idea
● Consider an online banking web application www.bank32.com which
allows users to transfer money from their accounts to other people’s
accounts.
● An user is logged in into the web application and has a session cookie
which uniquely identifies the authenticated user.
● HTTP request to transfer $500 from his/her account to account 3220:
http://www.bank32.com/transfer.php?to=3220&amount=500
● In order to perform the attack, the attacker needs to send out the forged
request from the victim’s machine so that the browsers will attach the
victim’s session cookies with the requests.
CSRF Attack on GET Requests - Basic
Idea
● The attacker can place the piece of code (to trigger request) in the form of
Javascript code in the attacker’s web page.
● HTML tags like img and iframe can trigger GET requests to the URL
specified in src attribute. Response for this request will be an
image/webpage.
Attack on Elgg’s Add-Friend Service
Goal : Add yourself to the victim’s friend list without his/her consent.
Line ② : Elgg’s
countermeasure
Line ③ : Session cookie which is unique for each user. It is against CSRF attacks
automatically sent by browsers. which are disabled.
Create the malicious web page
1. The img tag will
trigger an HTTP
GET request.
When browsers
render a web
page and sees an
2. The attacker use add-friend URL along with friend img tag, it sends
parameter. The size of the image is very small so an HTTP GET
that the victim is not suspicious. request to the
URL specified in
3. The crafted web page is placed in the malicious the src attribute.
website www.csrflabattacker.com (inside the
/var/www/CSRF/Attacker folder).
Attract Victim to Visit Your Malicious
Page
● Samy can send a private message to Alice with the link to the malicious
web page.
● If Alice clicks the link, Samy’s malicious web page will be loaded into
Alice’s browser and a forged add-friend request will be sent to the Elgg
server.
● On success, Samy will be added to Alice’s friend list.
CSRF Attacks on HTTP POST Services
Constructing a POST Request Using JavaScript
● POST requests can be generated using HTML forms. The above form has
two text fields and a Submit button.
● When the user clicks on the Submit button, POST request will be sent
out to the URL specified in the action field with to and amount fields
included in the body.
● Attacker’s job is to click on the button without the help from the user.
CSRF Attacks on HTTP POST Services
Line ①: Creates a form
dynamically; request type
is set to “POST”
Line ②: Session
cookie (unique for
each user). It is
automatically set by
browsers.
Line ③: CSRF
countermeasures,
which are disabled
Attack on Elgg’s Edit-Profile Service
● The two hidden parameters are added to the form so that when the form is
submitted via an HTTP request, these two values are included in the request.
● These two hidden values are generated by the server and added as a hidden
field in each page.
Elgg’s Countermeasure
JavaScript variables
to access using
JavaScript code.