SQL Injection
SQL Injection
SQL
SQL, or Structured Query Language, is a standard programming language used for
managing and manipulating relational databases. It allows you to perform various
operations like querying data, updating records, inserting new data, and deleting
data.
Imagine you have a database with a table named Employees that stores
information about employees. The table might look something like this:
Querying Data
To retrieve all records from the Employees table, you use a SELECT statement:
This query fetches all columns (*) and all rows from the Employees table.
Inserting Data
To add a new employee to the table, you use an INSERT statement:
Updating Data
To change the department of an existing employee, you use an UPDATE
statement:
UPDATE Employees SET Department = 'Finance' WHERE EmployeeID = 1;
This query updates the Department for the employee with EmployeeID 1 to
'Finance'.
Deleting Data
To remove an employee from the table, you use a DELETE statement:
Filtering Data
To get all employees in the 'IT' department, you use a SELECT statement with a
WHERE clause:
This query fetches only the rows where the Department is 'IT'.
DELETE Remove rows from a table. DELETE FROM table_name WHERE condition;
CREATE Define a new table and its CREATE TABLE table_name (column1 datatype
TABLE columns. constraints, column2 datatype constraints, ...);
Sort the result set by one or `SELECT column1, column2, ... FROM table_name
ORDER BY more columns. ORDER BY column1 ASC
Remove duplicate rows from SELECT DISTINCT column1, column2, ... FROM
DISTINCT the result set. table_name;
SQL Injection
A structured query language (SQL) injection is defined as a cybersecurity attack
technique or vulnerability, where malicious types of SQL statements are placed
inside entry fields in backend databases, either deliberately or inadvertently,
which facilitates attacks on data-driven applications.
Types of SQL injection
1. In-band SQL Injection
This type exploits vulnerabilities in a web application's query formation to
manipulate SQL commands directly. It typically involves entering malicious SQL
code into input fields.
Types of in-band SQL Injection:
• Error-Based SQL Injection: This type of injection exploits database errors
thrown by the server. The attacker crafts input to cause SQL errors and
extract information from the error messages.
1' OR '1'='1
The database might return an error message containing information about the
database schema.
This injection type uses the UNION SQL operator to combine the results of two or
more queries. The attacker can use this to retrieve data from other tables within
the same database.
This query returns results from the Users table along with the original Products
data.
-- Always true
Example: An attacker might use SQL commands to make the database send HTTP
requests to a server they control, revealing sensitive data.
Practices:
1. https://tryhackme.com/r/room/sqlinjectionlm
2. https://portswigger.net/web-security/all-labs#sql_injection
Reference:
• https://www.geeksforgeeks.org/what-is-sql/
• https://www.w3schools.com/sql/sql_syntax.asp
• https://www.spiceworks.com/it-security/application-
security/articles/what-is-sql-injection/
• https://portswigger.net/web-security/sql-injection
• https://www.cybercrowd.co.uk/news/impact-of-a-sql-injection/
• https://brightsec.com/blog/sql-injection-attack/
• https://tryhackme.com/r/room/sqlinjectionlm
• https://portswigger.net/web-security/all-labs#sql_injection