Tuesday, January 23, 2018

Securing APIs – Defence Controls for Developers

APIs have emerged as a solid backbone of applications in this modern world of mashups where applications are using one back-end with multiple front-ends. REST based APIs running over HTTP with JSON or XML are becoming preferred choices for developers. However, API end points are also becoming points of attack for attackers. Hence, developers need to implement proper security controls across APIs during design and development. Here are some important security controls for APIs :

Authentication control – APIs should have proper authentication control in place. APIs mostly run with access tokens/keys. OAuth is a popular building mechanism for tokens or keys. One should make sure that the tokens get validated with every API call/request. If session based authentication is defined, session management along with proper validation must be in place.

Authorization control
– APIs should be well guarded by a proper authorization layer on two fronts – token layer and HTTP method access. REST APIs use HTTP methods like GET for read, POST for create an entry, PUT for update and DELETE for remove. Hence, it is imperative to implement double locking with authorization both at the token layer and at the method layer. Also, in certain cases, resource locking is required at path level as well (example: /admin/ or /globalconfig/).

Injection Controls – API parameters need to be validated before consuming into business logic. All these parameters come over HTTP and can be easily tampered with. Hence, all traditional attacks like SQLi, XSS, CMDinjections etc. are possible on each of these parameters. One needs to have strong controls for injections and at the validation layer across API calls.

CSRF Control
– CSRF is a highly likely attack vector with session cookies in play. Hence, methods like PUT, DELETE or POST need to be validated with tokens to avoid CSRF attacks. Also, CORS can be used to strengthen the defence against CSRF attack vectors using preflight check with a set of rules.

Client Side Controls – Browsers are powerful points of abuse for attackers. Hence, critical vectors like XSS or ClickJacking can abuse APIs. It is important to apply appropriate types of HTTP headers like “nosniff” on X-Content, X-Frame options, Content-Type etc. to protect against these vectors. Also, it is critical to apply JSON and XML encoding as appropriate for the API context. Since DOM based XSS are also on the rise, using secure methods in the browser after consuming APIs is also a must (avoid .innerHTML call and use other pure text calls).

Third-Party-Integration Controls – At times, APIs are using various third-party components and it is imperative to use right coding practices before consuming them. Many applications leverage third party APIs for authentication purpose but fail to make calls to third party APIs when the user logs out of the application, keeping the session alive and exposed for a long duration.  Further, before serializing and de-serializing objects, it is important to make sure that the used libraries are secure and do not have intrinsic vulnerabilities like command injection or similar. Also, many times parsers introduce weaknesses into API calls. Hence, strong overall vetting and controls are required when using third-party components in the code base.

Information Leakage Controls
– HTTP status codes are important for context analysis of APIs. In many cases, these codes help in enumerating information. APIs should not spit out exceptions causing internal implementation or other sensitive information leakage. Hence, proper security controls around exception handling are a must for information leakage protection.

DoS Controls – REST APIs are easy to DoS by overloading with multiple requests in a short span of time. It is a good practice to keep “Rate Limit” on the basis of keys or users in many cases. Also, publicly open calls can be controlled by IP or on session basis to provide robust defence against API layer DoS.

Crypto & Sensitive Data Controls – Obviously, it is important to have SSL/TLS controls (HTTPS) in place for API communication over Internet. Apart from that, one should avoid having sensitive data in the API URL itself. This data can be harvested and misused from time to time at intermediate levels depending on the implementation. Data stored should be kept securely and must also have crypto applied to sensitive fields. If the API is using third party protocols or controls, then their implementation should be verified as well. (Example – JWT integrity).

Auditing Controls – APIs need strong audit and logging controls from security standpoint. API methods and its usage should be logged for various details like authentications, authorizations and injections. If any attempt of bypass or injection is happening across APIs, it should not only be logged but also flagged from time to time for protection of the APIs. One should analyse logs and extract security related incidents from them for improving defences.


Guarding and protecting APIs is becoming very critical for enterprises. We have seen many recent breaches where APIs are being leveraged to enter into corporate networks. API controls are different from regular web applications and need to be addressed at the design and development levels themselves. Controls covered in this article should help as a guideline for developers.

Article by Hemil Shah

Friday, December 22, 2017

Server Side Request Forgery (SSRF) Attack and Defence

Server Side Request Forgery (SSRF) is an interesting vulnerability, which can have potential impact on internal infrastructure residing behind firewall. In this case, HTTP server running over ports like 80/443 becomes an entry point for an attacker to enter into internal network over as legitimate HTTP/HTTPS tunnel.

To understand this vulnerability in detail, let’s assume we have a vulnerable application running on a hypothetical domain www.victim-example.com having a resource named “profile.php”. It is having a parameter named “propic” and it seems to be fetching picture from a resources as below.

GET /profile.php?propic=http://www.someserver.com/mypicture.png&id=9826739HTTP/1.1
Host: www.victim-example.com

Now, looking at the parameter propic, it is taking URL and fetching file from third party server. What if we can tamper with it and try to fetch something else instead? To try it out, we make a following request.

GET /profile.php?propic=http://www.myblueserver.com/foobar.html HTTP/1.1
Host: www.victim-example.com

This example server www.myblueserver.com belongs to us so we can see the logs of it. We can see this entry in it.

<IP of www.victim-example.com> - - [14/Dec/2017:00:06:28 -0700] "GET /foobar.html HTTP/1.1" 404 113 "-" "<Client type>"

Hence, clearly we got out-of-band call from the application. Application is processing propic (URL)parameter and making call to external app or resources. Application page may have lines like below.

$getpicurl = $_GET['propic'];
$profilepic = fopen($getpicurl, 'rb');

Now, this opens up various exploitation opportunities for an attacker. One of them is shown in figure below.

An attacker can start scanning internal hosts and network with reverse HTTP calls like below -
GET /profile.php?propic=http://192.168.100.X HTTP/1.1
Host: www.victim-example.com

It may get access to internal files and resources. This type of vulnerability is known as SSRF. It is like abusing HTTP calls with request forgery.

Exploit scenarios:

This functionality can be abused and following types of exploitation can be done.

•    Internal resource scanning and stealing.
•    Using file:// schema to access sensitive resources/files like /etc/passwd or other resources from the same server.
•    One can use other schemas like "ftp://", “dict://” or "gopher://" to abuse vulnerability further.
•    Attacking some other ports as well with cross protocol requests if possible and given scenario.


SSRF is relatively easy to detect by DAST specially using manual pentesting. One can fuzz requests and look for 200 OK coming from the same server with content coming from different server (internal/external). It may require out-of-band testing setup in some cases if there is a blind SSRF. If vulnerability is identified then it brings major threat to internal resources and should be fixed immediately by proper validations, whitelisting and other secure coding practices.

Wednesday, December 13, 2017

XXE Attack – A4 of OWASP Top 10

XML External Entities (XXE) issue is added to newly listed OWASP Top 10 vulnerabilities list. In current world, number of applications is using XML streams or Web Services (SOAP) for back-end processing. Hence, browser or client can make this simple XML call over HTTP POST and fetch XML response from the server as shown in the figure -

Once XML stream hits to the application server, it needs to process through XML parser. This opens up an opportunity for attacker to inject malicious payload. XML processing or parser may be weakly designed (DTD or XSD parsing) or not properly configured; it allows external entity attributes to get processed. This can lead to access to file system, network drives, network resources or other OS level access.

As shown in the above diagram, one can craft following XML document where XXE is defined to read “/etc/passwd”.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE test [
<!ELEMENT test ANY >
<!ENTITY xxe SYSTEM "file:///etc/passwd" >]>

Hence, if an attacker can successfully execute the above payload then application end up serving /etc/passwd as HTTP response (XML stream). It is also possible to inject internal URL or causing DoS (/dev/random file call). In certain cases, parser breaks with error while sending data back into XML stream. One can overcome that by (i) an attacker can call content by using “CDATA” wrapper in case getting error from the parser while displaying content. (ii) by sending data to external system (out-of-band) injection.


Though XXE is not very common issue but the nature of vulnerability is very serious and can have devastating impact on the target application/infrastructure. By leveraging this issue, an attacker can launch SSRF, steal file/source-code/confidential-information from the system or DoS the system. Hence, it is imperative to provide mitigation by proper patching, verifying XML streams and validations.

Monday, December 4, 2017

Insecure Deserialization Attacks – OWASP A8 2017

HTML5 and Web 2.0 applications are using modern architecture and modelling for web programming. It has opened up relatively different ways of attacking and exploiting. One of the issues, which are easy to detect and exploit by manual analysis, is called insecure deserialization. This is not new attack category but OWASP Top 10 came up with this vulnerability into their current list and started to get noticed.

Web programming language uses objects extensively and at some point in time, it is required to store these objects or transfer them over network. Hence, simple mechanism is developed; it is called serialization and deserialization.

Serialization – Converting an object instance into simpler form like XML, JSON, text or binary.
Deserialization – Converting XML, JSON, text or binary back into Object for consumption.

In web applications, these converted streams go back and forth between browser and servers. It goes over HTTP(S) and can be dissected at client end. It opens up opportunities for attacker to understand and tamper these object streams to exploit programming weaknesses. Here is a quick look at possible attacks and exploit scenarios.

1. Command Injections
- Object streams get handled at two layers – framework and programming (developers). At framework layer, in some cases, it gets parsed by using underlying operating systems resources to make some modifications. This opens up an opportunity to inject OS commands.

2. Privilege escalation
- Object stream may contain tokens or reference about ACLs (Access Control List). In some cases, an attacker can decode the information and manipulate it to gain higher level of access.

3. Reverse engineering
- In modern day applications, developers are using these objects on the fly and in some cases, they store very sensitive information. Data contained in these objects become a rich source of information for reverse engineering. By dissecting and decoding these objects, one can build a set of possible attack vectors. Hence, these streams coming to browser opens up bundle of opportunities for reverse engineering.

4. Authorization bypass -
Objects coming to the browser ends are having some tokens to access information time to time. These tokens can be guessed, reverse engineered or tampered, it can be sent back to server with mutation. It certainly opens up opportunities for authorization bypass and access to other user’s information.

5. Object stealing from browser space
- Objects are serialized and get stored in browser memory space. Libraries and developers are using HTML5 features like localStorage and file APIs to store these objects. If application is having XSS or DOM Injections then it can lead to information extraction from the browser. Also, developers build object maps via JavaScript within browser, one can enumerate entire object map and extract information from it via XSS. Hence, it is imperative not to have XSS across application to avoid this type of exploitation.


In this article, we have a brief overview of this attack vector (A8), which is newly added to OWASP. Top 10. There is more research going on in the area and we may end up seeing innovative way of both detecting and exploiting this mechanism. You can read more about it over here.

OWASP top 10 (A8) - https://www.owasp.org/images/7/72/OWASP_Top_10-2017_%28en%29.pdf.pdf

Untrusted streams - https://www.owasp.org/index.php/Deserialization_of_untrusted_data

Cheatsheet - https://www.owasp.org/index.php/Deserialization_Cheat_Sheet

Monday, November 27, 2017

New OWASP Top 10 (2017) - What is NEW and final state?

Here is the view on top 10 from OWASP

Resource on OWASP - Get it from HERE
  • Injections are still on the top. It makes sense since lot of attack vectors are leveraging poor validation controls. It leads to injections like SQLi, Command injections etc, and are still very common across applications.
  • Broken authentication is still a very common issue, it is very easy to discover by human intelligence and sometimes difficult from automated scanner standpoint. But this control is still holding second position on the list.
  • XSS used to be higher in the list but now moved to seventh position. There are several frameworks in place, which gives protection against it. Also, browsers are having controls to defend against XSS. But still XSS is very common and easy to discover across applications.
  • NEW (XXE) – XXE attack is added to the list, it is not very common but still XML streaming can be injected and can cause potential danger for the application. It allows lower level access on the box. Hence, it is added and important to detect and protect.
  • MERGED - Insecure Object and Missing Access Control are merged and added as a “Broken Access Control”. Again, it is very common at truly application layer. It is relatively easy to detect by humans and get missed by crawlers/scanners at large.
  • Security Misconfiguration and Using Known Vulnerable components are major issues from deployment/framework standpoint. It allows possible exploitations by default before developers build up code on top of it. Hence, both of them are having place in the top stack.
  • NEW (Insecure Deserialization) – HTTP is now supporting object level of streaming and it is imperative to serialize/de-serialize objects. It opens up a set of vulnerabilities and very effective attacks can be created using streams like JSON.
  • NEW (Insufficient Monitoring/Logging) – Application layer doesn’t have logging and it allows attacks to go unnoticed. Hence, even though this is not an attack category but still added so developers start providing mechanism for logging across applications.
  • DROPPED - CSRF and Unvalidated Redirects are dropped from the list. We still see them very commonly around applications but some protections are coming at framework layer and seems they are moved out.

Tuesday, November 7, 2017

HTML5 Drag and Drop abuse with ClickJacking

We came across interesting observation/article over here


It is possible to Hijack content by click jacking by loading two frames coming from the same domains. If domain is the same then it is possible to drag and drop API to function between two frames. Hence, it is possible to force victim to do “drag-and-drop” followed by a Click. It can lead to “Click Content Jacking” as concept outlined in the article.

Friday, September 8, 2017

(Advisory) Patch your Apache Struts

Researchers discovered a critical and easily exploitable vulnerability in Apache Struts framework  recently. Exploitation can lead to remote command execution and complete control of the machine via web server. Web Server running on port 80/443 is not blocked by firewall and can be exploited at ease.

Here is the original note from research group -

Here is the CVE entry –

Technically speaking, vulnerability was discovered in REST plugin, REST allows processing of XML streams via XStreamHandler. It is imperative to do some filtering at the code layer before deserialization. Defect and missing protection against these types of payload must have caused this exploitable opening.

Fix your Struts (CVE-2017-9805) by upgrading to 2.5.13 or 2.3.34 along with upgrading the plugin.