What is CSRF and How to Exploit?
Last updated
Was this helpful?
Last updated
Was this helpful?
CSRF Attack: attacker forces authenticated victim (cookie-based) to execute unwanted action that changes the state such as updating the profile, resetting the password, etc.
Hey hackers, my name is `mohammad amin` and you may know me as on the internet. today I wanna share my `CSRF Attack` knowledge base with you guys.
If you are not familiar with the basics of the web, the CSRF attack is more complicated than you think, so I suggest reading the section first and trying to fully understand the reason for using any payload and method.
🌟 I wanna thank who helped me to wrapped-up this awesome knowledge base.
an http request that doesn't fire the cors preflight request security check.
a cors preflight request is an OPTIONS
http request that checks to see if the cors protocol is understood and a server is aware using specific methods and headers.
same-origin policy (sop) is another security mechanism that prevents the current dom from being read by another origin.
There are three solutions to remove sop restrictions:
postMessage → sending and receiving messages between two different origins.
JSONP → using <script>
tag to transfer javascript object.
Cross-Origin Resource Sharing → modifying sop by some special response header.
window.postMessage()
is a browser object model (bom) method that helps developers to circumvent same-origin restrictions and it safely enables cross-origin communication between window objects.
cross-origin resource sharing (cors) is an http header based browser mechanism that enables controlled access to resources located outside of a given domain.
origin is a combination of a scheme, hostname, and port (if specified) and those websites that have the combination of the same scheme, hostname, and port are considered as same-origin.
site is a combination of a scheme and eTLD+1. those websites that have the combination of the same scheme and eTLD+1 are considered as same-site.
a cookie is a data block limited to ~4kb and sets on the client's browser from the server and it's automatically attached to each client http header request by the browser. there is sth important to us that is named as SameSite field:
None → will be sent in all cross-origin requests it will be treated as normal (old) cookies.
Lax → will be sent only through GET
requests in top window navigations such as <a> tag
, window.open()
...
Strict → will be sent only when the user types the website in the url bar and presses enter.
dashboard (crud any item or any access)
profile, client area, reset password (update profile data)
e-commerce (adding items to card)
tickets, support (request to transfer items, modify data, or deactivate the account)
admin panel (manipulate response and check admin panel functions in spa apps)
integration section (connect the user profile to the attacker’s oauth account)
developer section (generate a new token or revoke the previous one to make some damage)
as we know, custom headers and some content types fire the cors preflight request security check. remove custom headers and change the content types then fire the request and see what is the result.
in content types and headers worlds, an attacker can provide two different values based on a mismatch in the checker's function:
programmers generate the csrf token but do not check if its actually present or not
Null
or undefined
it helps us to bypass badly implemented checker function
hold your token and check if it's valid to another user (canceling action)
used unauthorized endpoint to get csrf token from response
generate a new token with the same length
put random chars at the beginning or at the end of the token
check if there are some static chars in the token and brute force dynamic ones or only using static part
take leverage of rainbow attacks or online databases to decode/decrypt the csrf token
change the http request method and dismiss the csrf token parameter
as a lack of bypasses, we need to chain bugs. in the following poc, we extract the csrf token with GET
request and put it into a POST
request:
extract csrf token with html/css injection:
remove the header from the request:
don’t be disappointed, just crack the context:
set attacker.tld
to the origin header and set the request.
you can change the origin into null with an iframe:
In Nov 2019, a new canary release with lax-by-default cookies is out and this feature makes some difficulty for hackers. based-on our knowledge base, lax cookies will be sent only through GET
requests in top window navigations but there is an exception for backward compatibility which help us to exploit outdated chrome browsers within 2 minutes.
at this point, you need to find a way to renew your cookie.
duplicate csrf token in cookie header:
run nuclei or manually checks their technologies to find a valid cve.
vulnerable application does not check the origin of the website that it receives the message from and incorporates the received data into its own application without proper validation.
auth endpoints