What is SQL Injection & What are steps to fix it ? – most common problems of e-commerce sites

SQL Injection (SQLi) refers to an injection attack wherein an attacker can execute malicious SQL statements (also commonly referred to as a malicious payload) that control a web application’s database server (also commonly referred to as a Relational Database Management System – RDBMS). Since an SQL Injection vulnerability could possibly affect any website or web application that makes use of an SQL-based database, the vulnerability is one of the oldest, most prevalent and most dangerous of web application vulnerabilities.

How SQL Injection works

In order to run malicious SQL queries against a database server, an attacker must first find an input within the web application that is included inside of an SQL query.

In order for an SQL Injection attack to take place, the vulnerable website needs to directly include user input within an SQL statement. An attacker can then insert a payload that will be included as part of the SQL query and run against the database server.

The following server-side pseudo-code is used to authenticate users to the web application.

app_user_name      = request.POST[‘username’]
app_user_passwd  = request.POST[‘password’]

# SQL query vulnerable to SQLi

sql = “SELECT id FROM users WHERE username=’” + uname + “’ AND password=’” + passwd + “’”

# Execute the SQL statement database.execute(sql)

 

The above script is a simple example of authenticating a user with a username and a password against a database with a table named users, and a username and password column.

The above script is vulnerable to SQL Injection because an attacker could submit malicious input in such a way that would alter the SQL statement being executed by the database server.

A simple example of an SQL Injection payload could be something as simple as setting the password field to password’ OR setting an inner query to (delete * from users) 

This would result in the query to be executed and data can be corrupted or manipulated.


What’s the worst an attacker can do with SQL?

SQL is a programming language designed for managing data stored in an RDBMS, therefore SQL can be used to access, modify and delete data. Furthermore, in specific cases, an RDBMS could also run commands on the operating system from an SQL statement.

Below are few things an attacker can inflict serious trouble to any organisation :

  • An attacker can use SQL Injection to bypass authentication or even impersonate specific users.
  • One of SQL’s primary functions is to select data based on a query and output the result of that query. An SQL Injection vulnerability could allow the complete disclosure of data residing on a database server.
  • Since web applications use SQL to alter data within a database, an attacker could use SQL Injection to alter data stored in a database. Altering data affects data integrity and could cause repudiation issues, for instance, issues such as voiding transactions, altering balances and other records.
  • SQL is used to delete records from a database. An attacker could use an SQL Injection vulnerability to delete data from a database. Even if an appropriate backup strategy is employed, deletion of data could affect an application’s availability until the database is restored.
  • Some database servers are configured (intentional or otherwise) to allow arbitrary execution of operating system commands on the database server. Given the right conditions, an attacker co

An SQL Injection needs just two conditions to exist – a relational database that uses SQL, and a user controllable input which is directly used in an SQL query.

In the example below, it shall be assumed that the attacker’s goal is to exfiltrate data from a database by exploiting an SQL Injection vulnerability present in a web application.

Example on how to avoid SQL Injections 

Default Example : 

String name = //user input 
int age = //user input 
Connection connection = DriverManager.getConnection(...); 
PreparedStatement statement = connection.prepareStatement( "SELECT * FROM people WHERE lastName = ? AND age > ?" ); 
statement.setString(1, name); //lastName is a VARCHAR 
statement.setInt(2, age); //age is an INT 
ResultSet rs = statement.executeQuery(); 
while (rs.next()){ //... }

Now the above code can be violated by executing like name = (update employee set password=’123′)   or name = ‘delete * from employee’   – this could completely put things at a toss

Safer Approach :

List<Person>; people = //user input

Connection connection = DriverManager.getConnection(…);
connection.setAutoCommit(false);

try

{

             PreparedStatement statement = connection.prepareStatement( “UPDATE people SET lastName = ?, age = ? WHERE id = ?”);
for (Person person : people)
{
statement.setString(1, person.getLastName());
statement.setInt(2, person.getAge());
statement.setInt(3, person.getId());
statement.execute(); }
connection.commit();
}

catch (SQLException e)
{
connection.rollback();
}

So these design and coding principles have  to applied during intial phase of the project so every sprint or every iteration code gets better and better.

-Tech Team from Techuva Solutions