SQL injection (SQLi)
What is SQL injection (SQLi)?
SQL injection is a commonly employed attack by hackers to exploit SQL database-driven web applications. It involves inserting SQL code or statements into execution fields to alter database contents, extract valuable information, causing repudiation issues, spoofing identity, and more. Consider a straightforward scenario with a web application featuring a login form with username and password fields. If PHP is used for development, the code might appear as follows:
<?php
$query = "SELECT * FROM users WHERE username = '" . $_POST['username'] . "'";
$query .= " AND password = '" . $_POST['password'] . "'";
?>
?For instance, if a user named Karen with the password '12345' attempts to log in, the generated SQL query sent to the database would be:
SELECT * FROM users WHERE username='Karen' AND password='12345'
?However, if an attacker knows the username and aims to bypass the login window, they might input something like 'Karen;--' in the username field. The resulting SQL query would then look like:
SELECT * FROM users WHERE username='Karen'; -- ' AND password='1111'
?Here, the attacker adds '--' (double-dash) to comment out the remaining SQL statement, enabling them to retrieve information from the password field and bypass the login screen.
?Attacker: Exploits features of the database server to execute commands and gather results using the same communication channel.
Types of SQLi
In-band SQLi is a type of SQL injection where the attacker can gather results directly through the same communication channel used to launch the attack. This is the most common type of SQL injection as it provides a straightforward and efficient means for attackers to access the database server. In-band SQLi can be further categorized into Error-based SQLi and Union-based SQLi.
?Blind SQL Injection/Inferential SQLi occurs when attackers cannot directly retrieve data from the web application's response. Instead, they infer information about the database structure by sending malicious payloads and analyzing the application's response. Blind SQLi is categorized into Content-based SQLi and Time-based SQLi.
Out-of-Band SQLi
How Attackers Perform SQLi
?Commonly Known SQLi Attack Examples & Techniques
How to Prevent SQL Injection Attacks
Preventing SQL injection attacks requires a multifaceted approach, including secure coding practices, input validation, and implementing specific tools and techniques tailored to the programming language and database engine used.
Pre-requisites
This tutorial assumes that you have a functioning DVWA (Damn Vulnerable Web Application) setup. If DVWA needs to be installed on your Kali Linux system, please look at the step-by-step guide in the related article for a comprehensive installation process.
Step 1: Setup DVWA for SQL Injection
Step 2: Basic Injection
Step 3: Always True Scenario
Step 4: Display Database Version
Step 5: Display Database User
Step 6: Display Database Name
Step 7: Display all tables in information_schema
Step 8: Display all the user tables in information_schema
Step 9: Display all the columns fields in the information_schema user table
Step 10: Display Column field contents
Step 11: Decrypt the Hashed Password
Conclusion
This article demonstrates that SQL injection is a critical vulnerability that can exist in a system. Attackers can exploit it to reveal user or customer information and corrupt the entire database, potentially bringing down the entire system. As of the writing of this post (2021), Injection is listed as the number one vulnerability in the OWASP Top 10 Vulnerabilities summary. DVWA is a valuable resource for penetration testers aiming to enhance their skills and web developers aiming to develop systems with security in mind. Continuous awareness and proactive measures are essential for mitigating SQL injection risks and maintaining robust cybersecurity practices.
Commands Table
Basic Commands
Order By Command to find the number of Columns
You should display all the values that pass the surname condition, not check the username.
SQL syntax error to check the MySQL server version for the right syntax
URL Injection
Payload Injection
Display the name of the Version of the Database
Display Hostname of Database
?Could you display the database username name?
Could you display the name of the database from which the web app is running?
List all the tables in the information schema.
List all the Users tables in the information schema.
?List all the Column Name of the User's tables
Display all the Column Content of the User's tables
Extract Storage Password
John the Ripper
Hashcat
SQL Injection Lab Performance
Provide a Suitable Solution based on the Questions (In terms of Images, Words, or Reports)
1. What is the purpose of entering different User ID values and clicking Submit in the basic commands section?
2. Explain the significance of the 'order by' commands with numbers in the SQL injection context.
3. What does the SQL injection payload '% or 1=1#' accomplish in the context of displaying values based on the surname condition?
4. Why is there a deliberate SQL syntax error in the command 'select first_name, last_name from users where user_id ='%' or '1'='1';'?
5. How does the URL injection payload in the provided links demonstrate SQL injection in a web application?
6. Explain the purpose of the SQL injection payloads used for displaying database version, hostname, username, and database name.
7. What is the significance of the command '% and 1=0 union select null, table_name from information_schema.tables #' in listing tables in the information schema?
8. How does the command '% and 1=0 union select null, table_name from information_schema.tables where table_name like 'user%'#' list user-related tables in the information schema?
9. Describe the purpose of the command '% and 1=0 union select null, concat(table_name,0x0a,column_name) from information_schema.columns where table_name = 'users' #'.
10. Explain how the payload '% and 1=0 union select null, concat(first_name,0x0a,last_name,0x0a,user,0x0a,password) from users #' displays the content of user-related columns.
11. What is the purpose of the command '% and 1=0 union select load_file('/etc/passwd'), null #' in the context of extracting storage passwords?
12. Compare the usage of John the Ripper and Hashcat in the context of decrypting hashed passwords. How do they differ in their approaches??
?
Cyber Security | Data Structures & Algorithms | Java | Intern @IBM | Web Devolopment | SQL | NSS UPES | IEEE UPES
1 年helpful