IL Exercise 18: Advanced SQL Injection

This exercise follows on from Basic SQL Injection, so make sure you have completed that exercise before starting this one. Some additional SQL knowledge is needed for this exercise, specifically related to SQL’s UNION operator. UNION will be used to combine two SELECT statements: one baked into the application, which we cannot change, and another which we are injecting into the application in order to probe the database or extract data from it.

The exercise requires you to use the Immersive Labs platform, so make sure you have set up access before you begin.

Preparation

  1. Login to Immersive Labs and start the SQL Injection: UNION lab.

  2. Click on the Info button at the top-right of the screen to reveal the information panel and read through its contents. Refer back to this where needed as you work through the exercise.

  3. When the virtual desktop is visible, click on the Network button at the top-right of the screen and make a note of the target’s IP address. To access the web application, run the Chromium web browser and enter the aforementioned IP address in the browser address bar.

  4. Click on the Tasks button at the top-right of the screen to reveal the tasks panel. Review the nine questions. Return to the tasks panel as you work through the exercise, answering each question. You can click Submit after entering each answer, to check whether you have answered correctly.

Probing Database Structure

  1. Find the part of the application that allows user input and experiment with it. Try inputs such as james or ruby, for which you should see some output displayed by the application. Then answer Question 1. See the Hints section at the bottom of this page if you need help.

  2. Using the discussion of UNION on the information panel as a guide, inject SQL that will help you determine how many columns are vulnerable. Then answer Questions 2 & 3. See the Hints section at the bottom of this page if you need help.

  3. Now inject SQL that will help you to answer Questions 4 & 5. As before, use the information panel to guide you. See the Hints if you need help. Make a note of the table names and the column names for both tables, as you’ll need these later.

Extracting Data

  1. In probing the database structure, you will have determined that the customers table stores email addresses and credit card numbers. Questions 6 & 7 ask you to find the email address of one individual and the credit card number of a different individual. To answer these questions, inject some SQL that uses UNION with a SELECT on the customers table. See the Hints section if you need help.

  2. Questions 8 & 9 ask you to find the company of one individual and the balance owed by a different individual. Both of these tasks involve the other table, whose name and column names you determined when probing database structure. You can use the same approach here as for Questions 6 & 7, modifying the table name and column names as appropriate. See the Hints if you need help.

Optional Extra Work

If you want to investigate a more automated approach to probing a database and extracting data, try the lab on sqlmap.

Screengrab from an episode of Star Trek: Discovery

Hints