A SQL injection is a security attack that is as dangerous
as it is ingenious. By abusing the data input mechanisms of
an application, an attacker can manipulate the generated SQL query to their
advantage, which can cause catastrophic events.
Many
people today might consider SQL injections a thing of the past. In truth,
they’re anything but. For instance, 70% of the
security exploit attempts on Rails apps analyzed in Sqreen’s State of Application Security Report were SQL injections. This finding
shows that even a robust framework such as Ruby on Rails—which counts as a
tried and true ORM in the form of Active Record—is not a silver bullet when it
comes to SQL injections. SQL injection is still top of the list of most common security threats to web apps, and it
has been there for quite a while.
What
are organizations supposed to do to avoid this problem, then? Education is the
key. And that’s what this post is about: educating people about not only what
SQL injections are and how they work, but most importantly how to avoid them.
Let’s dig in.
What is SQL Injection Attack?
Structured
Query Language (SQL) is a language designed to manipulate and manage data in a
database. SQL injection (SQLi) is a web security vulnerability that
allows an attacker to interfere with the queries that an application makes to
its database. It generally allows an attacker to view data that they are not
normally able to retrieve.This might include data belonging to other users, or
any other data that the application itself is able to access. In many cases, an
attacker can modify or delete this data, causing persistent changes to the
application's content or behavior. Since
its inception, SQL has steadily found its way into many commercial and
open-source databases. SQL injection (SQLi) is a type of cybersecurity attack that targets these
databases using specifically crafted SQL statements to trick the
systems into doing unexpected and undesired things.
Why are SQL injections so dangerous?
Some might think that a SQL injection
is only really damaging when the attacker gets write privileges to the
database.
That couldn’t be further from the
truth.
An attacker who reads unauthorized
information alone can give you a lot of headaches. The attacker could access
and expose sensitive data like financial data and personal information. Besides
tarnishing your organization’s reputation, such a leak would cause consequences
to your users/customers as the data is shared and sold. It could also put you
in trouble with privacy regulations such as Europe’s GDPR or
California’s CCPA, which could lead to severe legal
and financial consequences.
How do SQL injection attacks work?
Most applications allow their users to
input data somehow, and web applications are no different. Malicious
individuals can abuse those data-entering mechanisms in ways that interfere
with the generation of SQL queries.
The things I call “data entering
mechanisms” are the ways by which users input data into the application. Most
of the time, these would be form fields and URL parameters. By tinkering with
those elements in just the right way, attackers can inject—hence the name—additional
SQL commands, which get executed.
Types of SQL Injection Attack
SQL injection
attacks can be carried out in a number of ways. Attackers may
observe a system’s behavior before selecting a particular attack vector/method.
Unsanitized
Input
Unsanitized
input is a common type of SQLi attack in which the attacker provides user input
that isn’t properly sanitized for characters that should be escaped, and/or the
input isn’t validated to be the type that is correct/expected.
For
example, a website used to pay bills online might request the user’s account
number in a web form and then send that to the database to pull up the
associated account information. If the web application is building a SQL query
string dynamically with the account number the user provided, it might look
something like this:
“SELECT * FROM customers WHERE account = ‘“+ userProvidedAccountNumber +”’;”
While
this works for users who are properly entering their account number, it leaves
the door open for attackers. For example, if someone decided to provide an
account number of “‘ or ‘1’ = ‘1”, that would result in a query string of:
“SELECT * FROM customers WHERE account = ‘’ or ‘1’ = ‘1’;”
Due
to the ‘1’ = ‘1’ always evaluating to TRUE, sending this statement to the
database will result in the data for all customers being
returned instead of just a single customer.
Blind
SQL Injection
Also
referred to as Inferential SQL Injection, a Blind SQL injection attack doesn’t
reveal data directly from the database being targeted. Rather, the attacker
closely examines indirect clues in behavior. Details within HTTP responses,
blank web pages for certain user input, and how long it takes the database to
respond to certain user input are all things that can be clues depending on the
goal of the attacker. They could also point to another SQLi attack avenue for
the attacker to try.
Out-of-Band
Injection
This
attack is a bit more complex and may be used by an attacker when they cannot
achieve their goal in a single, direct query-response attack. Typically, an
attacker will craft SQL statements that, when presented to the database, will
trigger the database system to create a connection to an external server the
attacker controls. In this fashion, the attacker can harvest data or
potentially control behavior of the database.
A
Second Order Injection is a type of Out-of-Band Injection attack. In this case,
the attacker will provide an SQL injection that will get stored and executed by
a separate behavior of the database system. When the secondary system behavior
occurs (it could be something like a time-based job or something triggered by
other typical admin or user use of the database) and the attacker’s SQL
injection is executed, that’s when the “reach out” to a system the attacker
controls happens.
A
SQL injection example
Imagine
a real estate agency website. After accessing the site, you can search for
different kinds of properties and further filter by “renting” or “buying.”
Typical stuff. After selecting your search parameters and clicking on the
Search button, the selected parameters get added as URL parameters. So, let’s
say you’re searching for apartments. Your URL would look something like
this:
https://fakerealestate.com/properties?category=Apartments
And
the resulting SQL query that gets sent to the database would be something like
this:
SELECT
* FROM properties WHERE category = 'Apartments'
The
query above is pretty straightforward. We want to retrieve the rows from
the properties table in which the value of the category column
is equal to Apartments.
For
the sake of the argument, let’s say this site is vulnerable to SQL injections.
Let’s see what would happen if someone changed the URL to this:
https://fakerealestate.com/properties?category=Apartments’+OR+1=1–
That
would effectively result in the following query:
SELECT
* FROM properties WHERE category = 'Apartments' OR 1 = 1
Since
one always equals one, the query above successfully asks the database for
properties from all categories. This is a harmless example, but the general
modus operandi is the same: The attacker wants to conclude the legitimate query
and inject more commands after it in order to gain access to something they’re
not supposed to be able to access, such as a list of all users and
passwords.
How
to Prevent SQL Injection Attacks?
Preventing
or mitigating SQL injection attacks is a lot about ensuring that none of the
fields are vulnerable to invalid inputs and application execution. yours is
manually impossible to actually to check every page and every application on
the website, especially when updates are frequent and user-friendliness is the
top priority. Nonetheless, security analysts and seasoned
developers recommend a number of the
subsequent points guarantee your database square
measure well protected inside the confinement of the server.
1) Continuous
Scanning and Penetration Testing
The
automated web application scanner has been the best choice to point
out vulnerabilities within the web applications for quite some time now. Now,
with SQL injections getting smarter in exploiting logical flaws, website
security professionals should explore manual testing with the help of
a security vendor.
They
can authenticate user inputs against a set of rules for syntax, type, and
length. It helps to audit application vulnerabilities discreetly so that you
can patch the code before hackers exploit it to their advantage.
2) Restrict
Privileges
It
is more of a database management function, but enforcing specific privileges to
specific accounts helps prevent blind SQL injection attacks. Begin with no
privileges account and move on to ‘read-only’, ‘edit’, ‘delete’ and similar
privilege levels.
Minimizing
privileges to the application will ensure that the attacker, who gets into the
database through the application, cannot make unauthorized use of specific
data.
3) Use
Query Parameters
Dynamic
queries create a lot of troubles for security professionals. They have to deal
with variable vulnerabilities in each application, which only gets graver with
updates and changes. It is recommended that you prepare parameterized queries.
These
queries are simple, easy to write, and only pass when each parameter in SQL
code is clearly defined. This way, your info is supplied with
weapons to differentiate between code
and information inputs.
4) Instant
Protection
A
majority of organizations fail the problems like outdated code, scarcity of
resources to test and make changes, no knowledge of application security, and
frequent updates in the application. For these, web application protection is
the best solution.
A managed web
application firewall can be deployed for immediate mitigation of such
attacks. It contains custom policies to block any suspicious input and
deny information breaches instantly. This way, you do not have to
manually look for loopholes and mend problems afterward.
Published
By:
Yashashree Shastri
Syeda
Zarah Aiman
Rutuja Rathi
Vaishnav
Suryawanshi
Nice information content
ReplyDeleteGreat Work
ReplyDeleteVery Informative!
ReplyDeleteGood explanation!!!
ReplyDeleteWell writing
ReplyDeleteGood job
ReplyDelete