LUIDIBLU / tech blog
PT EN
PT EN

How does a basic SQL Injection attack work?

13 posts · 8,990 words · 46 min of reading · since 2016

0% read

How does a basic SQL Injection attack work?

An introduction to the risks of SQL queries built with untreated input.

TL;DR Too lazy to read? Click here.

First, we need to know what SQL and databases are. A database is a collection of information organized into columns and rows, forming a table. SQL is a very powerful language, and we need to know about it to understand the attack. In the example below, we see a common scenario: a page where users enter their login and password to access their accounts. Below, we can see a simple SQL query:

sqlselect * from login where usuario = 'Luke' and senha = 'skyw@lk3r!'

The SQL query above shows the table where there is a user named Luke and his corresponding password. But what would happen if you only had access to the username? You might have thought we would not be able to access the table. However, we can comment out the part that asks for the user’s password by commenting out this small section of code using: ‘ — ‘.

By injecting this comment, everything written after the symbols will be disregarded and ignored.

In this case, we can see that even if we entered a password different from the one in the database, we could still access the table, because the section that requests the password would be ignored due to the code acting as a comment.

sqlselect * from login where usuario = 'Luke' -- and senha = 'skyw@lk3r!'

Another way to gain access through SQL Injection is when we have neither a username nor a password. We then use the following string in the code, in both the name field and the password field: ‘or’ 1 = 1 —

The or instruction tells the database that if one condition cannot be met, another can be.

Using this code, our query would look like this:

sqlselect * from login where usuario = '' or 1 = 1 -- and senha = 'teste senha'

We can see that because the name is empty, it will move on to the or instruction, which will ask whether 1 equals 1. (Of course it does.) It will then access the first registered user and ignore the password.

ATTACK STRINGS TO ENTER IN THE FIELDS:
1) — ‘
2) ‘ — ‘
3) ‘or’ 1=1
4) ‘or’ 1=1 —

Nowadays, software companies and their programmers already handle this type of vulnerability. However, flaws like these are still common in applications created by beginners or people who focus on usability without thinking about security.

Visitor dossier

Read only in your browser. The edge answers and keeps no log. Nothing is stored or sent to third parties.

Shortcuts

/⌘K
search and commands
?
this table
p
visitor dossier
t
light / dark theme
.
12-column grid
jk
next / previous post
enter
open the focused post
g hg pg tg sg r
go to home, posts, tags, about, rss
esc
close dialogs