> For the complete documentation index, see [llms.txt](https://dev-angelist.gitbook.io/writeups-and-walkthroughs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev-angelist.gitbook.io/writeups-and-walkthroughs/portswigger-web-security-academy/essential-skills/obfuscating-attacks-using-encodings/sql-injection-with-filter-bypass-via-xml-encoding.md).

# SQL injection with filter bypass via XML encoding

https\://portswigger.net/web-security/sql-injection/lab-sql-injection-with-filter-bypass-via-xml-encoding

## Description

This lab contains a SQL injection vulnerability in its stock check feature. The results from the query are returned in the application's response, so you can use a UNION attack to retrieve data from other tables.

The database contains a `users` table, which contains the usernames and passwords of registered users. To solve the lab, perform a SQL injection attack to retrieve the admin user's credentials, then log in to their account.

## Solution

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FcVOcMZ22SeFDAM8LP7n4%2Fimage.png?alt=media&amp;token=1b87357a-c525-4bdd-bb1a-205faf6b8619" alt=""><figcaption></figcaption></figure>

Click to one of products shop: <https://0a5b002b0394c64382a61f0e00eb00c9.web-security-academy.net/product?productId=1>

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FggENtTkMUD3Ly68LQaAv%2Fimage.png?alt=media&amp;token=fa77049c-3119-402d-9c3b-f26daec5ffde" alt=""><figcaption></figcaption></figure>

Analyzing HTTP request of this request with BurpSuite there're not XML, so this is the wrong via.

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Fp3r95dd6SXtslIEFcNdT%2Fimage.png?alt=media&amp;token=db448f97-f8b0-47b5-a3e4-28cbb37732ad" alt=""><figcaption></figcaption></figure>

See well the page, there's a form with a method POST that permits to display the stock value of relative product in three various store place.

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FxaGBLorVuMyRCyotSJnH%2Fimage.png?alt=media&amp;token=4ed5617f-20f1-47ea-88f0-e5814afd6f94" alt=""><figcaption></figcaption></figure>

Capturing it, we can see the XML that we're searching!

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FZFYXu4uPkXRSdn3ist9t%2Fimage.png?alt=media&amp;token=05b2ae59-bfef-40bd-a730-f76660775489" alt=""><figcaption></figcaption></figure>

We can start trying to injecting this payload: `1 UNION SELECT NULL` for understand if the print of 'NULL' value will be executed

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FCdDgUcBWDURiBbvcin16%2Fimage.png?alt=media&amp;token=5015c1d0-5c9a-4d6a-8eb1-87a71330d64a" alt=""><figcaption></figcaption></figure>

WAF identify a malicious payload, so we can try to encode our payload using tools

Install HackVector (Optional), we can use other web tools

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FyK2uQqf7KRbsT4aUjPfF%2Fimage.png?alt=media&amp;token=dfcac27b-f479-4cfe-b2f3-e052ba74b2a8" alt=""><figcaption></figcaption></figure>

Encoding the payload to HEX\_EntitIes: `1 <@hex_entities>UNION SELECT NULL`\
`</@hex_entities>` we're able to evade WAF and obtain 'NULL' as result:

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2Fy9hc2uJTVthaZgB8MjWc%2Fimage.png?alt=media&amp;token=ace90b42-5aa9-4cb8-a79f-e407594806fd" alt=""><figcaption></figcaption></figure>

So, adding a new one 'NULL' value we can see that the 2nd new column doesn't exists, than the table's column are only one,&#x20;

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FaIWsgzuPMNbNZ3lLcJia%2Fimage.png?alt=media&amp;token=249e8971-6930-46e8-a116-704b09f93638" alt=""><figcaption></figcaption></figure>

remembering the task there're not problem "The database contains a `users` table, which contains the usernames and passwords of registered users. ", column requested are two, we can ovviate to this thing concatening the two parameters:

1 <@hex\_entities>UNION SELECT username || '\~' || password FROM USERS\
\</@hex\_entities>

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2FZlLd135xsc6krEGHs5y2%2Fimage.png?alt=media&amp;token=dc80dacf-fc63-4d63-b911-c761e678b353" alt=""><figcaption></figcaption></figure>

and obtaining credentials for login and complete the lab.

<figure><img src="https://677614291-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrRWtuMw6xkkeDjZfkcWC%2Fuploads%2F3e4fjgbE7bYXdjVeYSzv%2Fimage.png?alt=media&amp;token=29d3703e-bd46-4d66-9377-80fbc089528b" alt=""><figcaption></figcaption></figure>
