×

What is SQL Injection?

Introduction to SQL Injection

  • SQL injection is a vulnerability or a technique that might destroy the database of a website or a web application. It is one of the most widely used web-based attacks.  
  • The major objective of an SQL injection is to get access to the database of a website. So, for an SQL injection to work, one needs a web application that is connected to a database.
  •  Since, the database has all the data including the administrator password i.e., the login password of the owner, the attacker of the website can access the admin page to edit the website by filling a web form.
  • The web-form includes user name and passwords. When the user inputs something in the input field of the web form, the SQL SELECT query is implemented on the database.
  • The system tries to match the inputs i.e., the username and the password entered by the user with the username and password which is already stored in the database. If both the inputs are matched, then user gets the access else the access will be denied to the user.
  • There are certain websites which do not have the mechanism to block any other input. Such websites are vulnerable to SQL injection. In this case, any SQL query can be fed as input and the system will execute it.
  • For example, the attacker can input a query to download the entire database, delete the database, modify the database and make the condition always true. If a password condition is made always true, so no matters if any password is entered. For the password, system access the ones which are stored in the database, but if a query is made to drop the password table in the database, the system will start to take any password. In this way, the attacker can get access to the database easily. Now he can know all the passwords of the people logged in the website, the various tables and their contents, the internal structure of the website and any other information related to the website.
  • This method is called SQL injection in which you inject a query into the database to manipulate it and gain unauthorized access to it. This is the most dangerous type of website attack for an SQL based database.
  • One way to prevent such attacks is to block the unnecessary inputs other than the usernames and passwords.
  • SQL injection commands are made at run time. So avoid dynamic input commands. Prevent the database with web application firewall. Also, do not reveal any confidential information regarding the website to anyone.
  • Websites can suffer huge data and financial losses due to such SQL attacks.

Example:

Suppose, there is a web application with a database connected to it. This web application might be taking input from the user and storing the information onto the database or fetching the data from the database and displaying it to the user.

In either cases, there is an SQL query or database query that is generated on the web application which is send to the database and this query is executed on the database and relevant information is returned back to the web application. This is how the normal scenario works.

So, when the attacker uses SQL injection, he tries to manipulate this database query in order to make it do something that it is ideally not supposed to do. So, the attacker changes the SQL query, manipulates it, he injects some malicious string in the SQL query and then make it do something in unauthorized way. So now the database query is manipulated by the attacker, then this malicious query is sent to the database, it is executed there and the relevant results are returned.

This is known as SQL injection. SQL injection is a code injection technique used to execute malicious and dynamic SQL statements. SQL attacks are something that the attacker uses to take control over database servers.

How to prevent SQL injection

  1. Avoid using dynamic SQL

The inputs provided by the user should not be directly placed into the SQL query that will be operated on the database. Instead of using dynamic SQL, one should make use of stored procedures, prepared statements and parameterized queries as they are safer as compared with dynamic SQL queries.

  •  User provided inputs must be sanitized

The type of data which will be provided by the user must be properly matched and verified with the expected type.

  • Sensitive data should not be in plaintext

Before storing the confidential data such as passwords into the database, it should be properly encrypted with hashes. Salting must be applied to the encrypted hashes to provide an extra layer of security to the confidential data.

  • Database errors should not be displayed directly to the user

Error information displayed to the attacker might help him to get information about the database.


Related Topics

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL Data Control Language

Data Control Language decides to whom should (which user) permit access privileges. GRANT and REVOKE are the commands of DCL. GRANT: It gives privileges to user. REVOKE: It takes back privileges from granted...

1 minute read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.