Is it possible to read and extract HTTP request headers via JavaScript?
i.e. something like;
var req = new XMLHttpRequest();
req.open(‘GET’, document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders();
console.log(headers);
But instead of reading HTTP response headers (as per snippet above), it reads the HTTP request headers?
The reason I'm asking about this is that the above snippet (altered to read request HTTP headers and not response HTTP headers) will be an external JavaScript file that will successfully be called through an XSS vulnerability. External JS files are permitted here due to the lack of a CORS policy. The above code snippet (again, altered to read request headers) would then allow me to extract the HTTP request header value via scraping the http request headers from a null (or mock) HTTP request to then have the specific request header (X-CSRF-TOKEN
) included within a specially crafted HTTP request which is accepted by the server and therefore bypasses the flawed CSRF protection mechanism in order to execute CSRF.
I have created a functional exploit within a localhost demo I spun up where this works for reading response headers, but I'm needing some form of capability whereby JavaScript can read HTTP request headers. Is this something that is possible in this context?
I have elaborated on this in slightly further detail below (please excuse any duplicated details);
The CSRF target request contains a CSRF-Token (anti-CSRF protection mechanism). This is a static value that is only unique per user session (and not dynamic upon individual requests) - hence the flaw here. I am trying to create a JavaScript file which I will call remotely via a Cross-site Scripting (XSS) flaw (works due to lack of CORS policy) where this JavaScript will execute within the context of the victim user. As a result, HTTP request headers are automatically added to the HTTP request by the application as I am now performing client-side requests within the context of the victim user under this localhost application (basically like being able to navigate the website while authenticated as the victim user). This is why there is no need for me to supply HTTP request headers other than the request header I need to extract (X-CSRF-TOKEN
) via the XmlHttpRequest (or whatever). I just need to be able to read the X-CSRF-TOKEN
HTTP request header that is set in the HTTP request (not response), extract this value, and include within a XmlHttpRequest (or whatever) to craft a request that will be accepted by the server in order to bypass the faulty anti-CSRF mechanism and successfully perform CSRF.
I have written an exploit for this but it pertains to HTTP response header cookie values within the HTTP response headers, but this obviously doesn't work as the XSRF-TOKEN
cookies are not used in this context for anti-CSRF. It's the X-CSRF-TOKEN
HTTP Request header that is used for the anti-CSRF mechanism. I was thinking I could do something like req.open('GET', document.location, false);
Again since this will be executing under the context of the victim user, shouldn't it be trivial to scrape the X-CSRF-TOKEN
HTTP request header here while submitting something like req.open('GET', document.location, false);
?
Any help will be much appreciated. I hope I have explained this well enough for you to understand what it is I'm trying to do (it's late here).
Thank you for your time, /J
window.XMLHttpRequest
(orfetch()
if you want to be modern) class if needed, or just the.setRequestHeader()
method to one that logs as it does the original thing as well alacachedMethodRef.apply(this, arguments)
setRequestHeader
should be malleable just the same.