{% extends "introduction/base.html" %} {% load static %} {% block content %} {% block title %} SQL Injection {% endblock %}

CWE-89: SQL Injection

Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data. This can be used to alter query logic to bypass security checks, or to insert additional statements that modify the back-end database, possibly including execution of system commands. SQL injection has become a common issue with database-driven web sites. The flaw is easily detected, and easily exploited, and as such, any site or software package with even a minimal user base is likely to be subject to an attempted attack of this kind. This flaw depends on the fact that SQL makes no real distinction between the control and data planes.


SQL injection errors occur when: Data enters a program from an untrusted source. The data used to dynamically construct a SQL query The main consequences are:

This lab helps you to exploit the common type of sql injection vulnerability, caused due to the lack of input validation and directly exposing input into the query.
The user on accessing the lab is given a log in page . The user has to try to login in as admin. SQL Injection vulnerability can be identified by injecting a ' in any of the fields. If it results in an SQL error, SQL injection vulnerability is identified
Exploiting SQL Injection Vulnerability


Understanding the Exploit

The website logs a user in by checking the entered username and password against the ones stored in the database. If they match, the user is logged in. Lets first analyse the sql query used to compare the username and password in the database.
"SELECT * FROM introduction_login WHERE user='"+name+"'AND password='"+password+"'"
The name and password parameters are the ones you give as input, which is directly inserted into the query.

Why the error?

When we inserted a ' in the input it threw an error , this is because the sql query was not balanced and it threw an error.
SELECT * FROM introduction_login WHERE user='admin' AND password='''
The query quotes in the password field are unbalanced, this can be balanced by adding another quote to it.

Lets just plug our payload into the query and see what it looks like.
SELECT * FROM introduction_login WHERE user='admin' AND password='anything' OR '1' ='1'
Now the query means select username = admin where password is anything OR '1'='1' .
'1'='1' will always result in TRUE and the query fetches the user with name admin and password=TRUE.
Thus allowing us to login in as admin.



Mitigation

{% endblock %}