Ch05 (I) (SQL Injection)
Ch05 (I) (SQL Injection)
2
SQL injection attacks
• One of the most prevalent and dangerous network-based
security threats
• Sends malicious SQL commands to the database server
• Depending on the environment SQL injection can also be
exploited to:
– Modify or delete data
– Execute arbitrary operating system commands
– Launch denial-of-service (DoS) attacks
3
A typical
injection
attack
4
Sample SQL injection
• The SQLi attack typically works by prematurely
terminating a text string and appending a new command
SELECT fname
FROM student
where fname is ‘user prompt’;
5
SQLi attack avenues
• User input: Attackers inject SQL commands by providing suitably
crafted user inputs.
Typically comes from form submissions that are sent to the web application via
HTTP GET or POST
• Server variables: Collection of variables that contain HTTP headers,
n/w protocol headers and environmental variables. Attackers can
forge these values, which are then triggered during query.
• Second order injection: Occurs when incomplete prevention
mechanisms against SQLi attacks are in place. The attack occurs
from within the system.
• Cookies: Stores the client’s state information. An attacker can alter
cookies such that such that the structure and function of query is
modified.
6
In-band attacks
• Tautology: This form of attack injects code in one or
more conditional statements so that they always evaluate
to true
• End-of-line comment: After injecting code into a
particular field, legitimate code that follows are nullified
through usage of end of line comments
• Piggybacked queries: The attacker adds additional
queries beyond the intended query, piggy-backing the
attack on top of a legitimate request
7
Sample SQL injection: tautology
$query= “
SELECT info FROM user WHERE name =
`$_GET[“name”]’ AND pwd = `GET[“pwd”]`
”;
Resulting query:
SELECT info FROM users WHERE name = ` ` OR 1=1 – AND
pwd = ` `
8
Inferential attack
• There is no actual transfer of data. The attacker can
reconstruct information by an inference channel.
• Illegal/Logically incorrect queries: Attacker gathers info
about the type and structure of the backend database.
preliminary, info-gathering step for other attacks
default error page returned by app servers is often descriptive
• Blind SQL: Allows attackers to infer data when the system is
secure to not display any erroneous info back to the attacker.
Attacker asks TRUE/FALSE questions.
If TRUE, the site continues normal operation
If FALSE, the page differs significantly from the normal operations
https://owasp.org/www-community/attacks/Blind_SQL_Injection
9
Out-band attack
• This can be used when there are limitations on
information retrieval, but outbound connectivity from the
database server is lax
10
SQLi countermeasures
• Defensive coding: stronger data validation
• Detection
– Signature based
– Anomaly based
– Code analysis
• Runtime prevention: Check queries at runtime to see if
they conform to a model of expected queries
11