Understand XPath Injection And How To Prevent It

We will discuss here the issue which arises each and every day. I will introduce you patiently to it, describe it, show you the examples to understand it totally, and later I will teach you how to behave once it arrives. But, we all say, hopefully, it won’t. Let’s go to learn some new things together!

XPath Injection Description

Most of the times, vulnerabilities of this type arise when the user’s data which is controllable becomes incorporated in the XPath queries. I think that it’s clear that those are incorporated in an unsafe manner, but I would mention it anyways. So, what the attacker does here? He is supplying the input that is crafted with a purpose to break out all the data’s context. In such a case, the input always appears and later on it also interferes with the whole structure of the query which is surrounded.

That vulnerability query can be used for different purposes. Let’s say the attacker wants to exploit the XPath injection flaw with a purpose to read the sensitive app’s data. He or she may also use it in a way to interfere with the app’s logic itself.

XPath Injection Impact

I would like to start this part with an explanation what XPath actually is. It is a language which is always used for querying the certain parts of some document which is in XML form. For your better understanding, I would compare it to SQL language. That one is used to query the whole databases. What this injection does is that it allows the attacker to modify the XPath query in the way he wants or needs. Of course, that can be done only if the user’s input is not validated properly. What would be the worst case scenario here? Definitely, the ability of the attacker to read out the whole XML document that is queried.

Example Of XPath Injection

We will take a look now how it works when for the example a SOAP message delivers a customer’s ID. It sends it to the app’s logic and in that case, it is querying the XML doc with all of the customer’s information. At first, it looks like this:

//users/custid[123]

After that, it may happen that the user has already supplied the customer’s ID and then the app will return that information about every and each customer in the total XML doc. It happens because every customer has an age which is described as >0. Let’s see!

//users/custid[./age>0]

Recent Attacks Of XPath Injection And Impact

As I have already mentioned, these attacks are arising these days. But, you need to know that only people who have a great knowledge about hacking can complete successfully the XPath injection. So, what are the characteristics of the attacker? First of all, he must know what are the endpoints of a web service. How else could he/she reach the web service, right? Second thing is that he or she knows the metadata totally. At least, an attacker must be very well educated about the WSDL file metadata. And the third thing is the location. The attacker easily reaches the endpoint and all that from its location. Wow! Why is that so important? Because, otherwise, the access to the web server which has to be attacked couldn’t be possible for the attacker. That is why the cybersecurity tips always recommend that it is a must for the web services to allow the access only to the users which are within that certain network.

How To Fix XPath Injection

We have learned so many information about this high-risk vulnerability which’s severity is also on a high level. How to prevent it? How to fix it? The first thing to talk about would definitely be the user’s input. It must (I will say must again) be totally validated before the incorporation process into the XPath queries start. What happens in most cases is that it may happen that accepting the input which contains only the short alphanumeric strings may be appropriate. But, don’t be so relaxed when you hear that. When you recognize the input which contains meta-characteristics (of the XPath) such as ” ‘ / @ *, reject it!

A little bit complicated issue to describe, but I think that we have learned so much about it. It behaves strangely so people can often miss it. I would recommend you to remember by the head the last sentence in the ”How to Fix” part of this article.