SlideShare a Scribd company logo
SQL Injection Attacks
CS 183 : Hypermedia and the Web
UC Santa Cruz
What is a SQL Injection Attack?
• Many web applications take user input from a
form
• Often this user input is used literally in the
construction of a SQL query submitted to a
database. For example:
– SELECT productdata FROM table WHERE
productname = „user input product name‟;
• A SQL injection attack involves placing SQL
statements in the user input
An Example SQL Injection Attack
Product Search:
• This input is put directly into the SQL statement
within the Web application:
– $query = “SELECT prodinfo FROM prodtable WHERE prodname = „” .
$_POST[„prod_search‟] . “‟”;
• Creates the following SQL:
– SELECT prodinfo FROM prodtable WHERE prodname = „blah‘ OR ‘x’ = ‘x‟
– Attacker has now successfully caused the entire database to be
returned.
blah‘ OR ‘x’ = ‘x
A More Malicious Example
• What if the attacker had instead entered:
– blah‘; DROP TABLE prodinfo; --
• Results in the following SQL:
– SELECT prodinfo FROM prodtable WHERE prodname = „blah’; DROP TABLE
prodinfo; --‟
– Note how comment (--) consumes the final quote
• Causes the entire database to be deleted
– Depends on knowledge of table name
– This is sometimes exposed to the user in debug code called during a
database error
– Use non-obvious table names, and never expose them to user
• Usually data destruction is not your worst fear, as there is low
economic motivation
Other injection possibilities
• Using SQL injections, attackers can:
– Add new data to the database
• Could be embarrassing to find yourself selling politically
incorrect items on an eCommerce site
• Perform an INSERT in the injected SQL
– Modify data currently in the database
• Could be very costly to have an expensive item suddenly be
deeply „discounted‟
• Perform an UPDATE in the injected SQL
– Often can gain access to other user‟s system
capabilities by obtaining their password
Defenses
• Use provided functions for escaping strings
– Many attacks can be thwarted by simply using the
SQL string escaping mechanism
• „  ‟ and “  ”
– mysql_real_escape_string() is the preferred function
for this
• Not a silver bullet!
– Consider:
• SELECT fields FROM table WHERE id = 23 OR 1=1
• No quotes here!
More Defenses
• Check syntax of input for validity
– Many classes of input have fixed languages
• Email addresses, dates, part numbers, etc.
• Verify that the input is a valid string in the language
• Sometime languages allow problematic characters (e.g., „*‟ in
email addresses); may decide to not allow these
• If you can exclude quotes and semicolons that‟s good
– Not always possible: consider the name Bill O‟Reilly
• Want to allow the use of single quotes in names
• Have length limits on input
– Many SQL injection attacks depend on entering long
strings
Even More Defenses
• Scan query string for undesirable word
combinations that indicate SQL statements
– INSERT, DROP, etc.
– If you see these, can check against SQL syntax to
see if they represent a statement or valid user input
• Limit database permissions and segregate users
– If you‟re only reading the database, connect to
database as a user that only has read permissions
– Never connect as a database administrator in your
web application
More Defenses
• Configure database error reporting
– Default error reporting often gives away information that is
valuable for attackers (table name, field name, etc.)
– Configure so that this information is never exposed to a user
• If possible, use bound variables
– Some libraries allow you to bind inputs to variables inside a SQL
statement
– PERL example (from http://www.unixwiz.net/techtips/sql-
injection.html)
$sth = $dbh->prepare("SELECT email, userid FROM members WHERE
email = ?;");
$sth->execute($email);
Be careful out there!

More Related Content

PPTX
SQL Injections - A Powerpoint Presentation
PPTX
Sql Injection attacks and prevention
PDF
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
PPTX
Sql injection - security testing
PPTX
SQL Injections (Part 1)
PPTX
SQL injection prevention techniques
PPTX
seminar report on Sql injection
PPTX
Ppt on sql injection
SQL Injections - A Powerpoint Presentation
Sql Injection attacks and prevention
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
Sql injection - security testing
SQL Injections (Part 1)
SQL injection prevention techniques
seminar report on Sql injection
Ppt on sql injection

What's hot (20)

PPT
D:\Technical\Ppt\Sql Injection
PPTX
Sql injections
PPT
A Brief Introduction in SQL Injection
PPT
Sql Injection Attacks Siddhesh
PPTX
Sql Injection and Entity Frameworks
PPT
SQL Injection
PDF
Sql Injection - Vulnerability and Security
PPT
Advanced SQL Injection
PPT
Sql injection attack
PPT
Sql injection
PPT
Sql injection
PPTX
Web Security: SQL Injection
PPTX
Sql injection
PPTX
Sql injection attack
KEY
SQL Injection - Mozilla Security Learning Center
PDF
Sql Injection and XSS
PPT
Sql injection attacks
PDF
Web Application Security II - SQL Injection
PPTX
SQL Injection Defense in Python
PPT
Web application attacks using Sql injection and countermasures
D:\Technical\Ppt\Sql Injection
Sql injections
A Brief Introduction in SQL Injection
Sql Injection Attacks Siddhesh
Sql Injection and Entity Frameworks
SQL Injection
Sql Injection - Vulnerability and Security
Advanced SQL Injection
Sql injection attack
Sql injection
Sql injection
Web Security: SQL Injection
Sql injection
Sql injection attack
SQL Injection - Mozilla Security Learning Center
Sql Injection and XSS
Sql injection attacks
Web Application Security II - SQL Injection
SQL Injection Defense in Python
Web application attacks using Sql injection and countermasures
Ad

Similar to Sql injection attacks (20)

PPT
Sql injection attacks
PPT
SQL Injection Attacks
PPTX
Greensql2007
PPTX
Code injection and green sql
PPTX
SQL Injection Stegnography in Pen Testing
PPTX
Google Dorks and SQL Injection
PDF
Hack your db before the hackers do
PPTX
SQLi for Security Champions
PPTX
Web application security part 01
PPTX
SQL INJECTION
PPTX
Unique Features of SQL Injection in PHP Assignment
PPT
SQLSecurity.ppt
PPT
SQLSecurity.ppt
PDF
[Kerference] Nefarious SQL - 김동호(KERT)
PPT
SQL injection and buffer overflows are hacking techniques used to exploit wea...
PDF
Ch 9 Attacking Data Stores (Part 2)
PPTX
Sql Injection
PPT
Sql security
PPTX
Sql injection
PPTX
SQL Injection in JAVA
Sql injection attacks
SQL Injection Attacks
Greensql2007
Code injection and green sql
SQL Injection Stegnography in Pen Testing
Google Dorks and SQL Injection
Hack your db before the hackers do
SQLi for Security Champions
Web application security part 01
SQL INJECTION
Unique Features of SQL Injection in PHP Assignment
SQLSecurity.ppt
SQLSecurity.ppt
[Kerference] Nefarious SQL - 김동호(KERT)
SQL injection and buffer overflows are hacking techniques used to exploit wea...
Ch 9 Attacking Data Stores (Part 2)
Sql Injection
Sql security
Sql injection
SQL Injection in JAVA
Ad

More from Kumar (20)

PPT
Graphics devices
PPT
Fill area algorithms
PDF
region-filling
PDF
Bresenham derivation
PPT
Bresenham circles and polygons derication
PPTX
Introductionto xslt
PPTX
Extracting data from xml
PPTX
Xml basics
PPTX
XML Schema
PPTX
Publishing xml
PPTX
DTD
PPTX
Applying xml
PPTX
Introduction to XML
PDF
How to deploy a j2ee application
PDF
JNDI, JMS, JPA, XML
PDF
EJB Fundmentals
PDF
JSP and struts programming
PDF
java servlet and servlet programming
PDF
Introduction to JDBC and JDBC Drivers
PDF
Introduction to J2EE
Graphics devices
Fill area algorithms
region-filling
Bresenham derivation
Bresenham circles and polygons derication
Introductionto xslt
Extracting data from xml
Xml basics
XML Schema
Publishing xml
DTD
Applying xml
Introduction to XML
How to deploy a j2ee application
JNDI, JMS, JPA, XML
EJB Fundmentals
JSP and struts programming
java servlet and servlet programming
Introduction to JDBC and JDBC Drivers
Introduction to J2EE

Sql injection attacks

  • 1. SQL Injection Attacks CS 183 : Hypermedia and the Web UC Santa Cruz
  • 2. What is a SQL Injection Attack? • Many web applications take user input from a form • Often this user input is used literally in the construction of a SQL query submitted to a database. For example: – SELECT productdata FROM table WHERE productname = „user input product name‟; • A SQL injection attack involves placing SQL statements in the user input
  • 3. An Example SQL Injection Attack Product Search: • This input is put directly into the SQL statement within the Web application: – $query = “SELECT prodinfo FROM prodtable WHERE prodname = „” . $_POST[„prod_search‟] . “‟”; • Creates the following SQL: – SELECT prodinfo FROM prodtable WHERE prodname = „blah‘ OR ‘x’ = ‘x‟ – Attacker has now successfully caused the entire database to be returned. blah‘ OR ‘x’ = ‘x
  • 4. A More Malicious Example • What if the attacker had instead entered: – blah‘; DROP TABLE prodinfo; -- • Results in the following SQL: – SELECT prodinfo FROM prodtable WHERE prodname = „blah’; DROP TABLE prodinfo; --‟ – Note how comment (--) consumes the final quote • Causes the entire database to be deleted – Depends on knowledge of table name – This is sometimes exposed to the user in debug code called during a database error – Use non-obvious table names, and never expose them to user • Usually data destruction is not your worst fear, as there is low economic motivation
  • 5. Other injection possibilities • Using SQL injections, attackers can: – Add new data to the database • Could be embarrassing to find yourself selling politically incorrect items on an eCommerce site • Perform an INSERT in the injected SQL – Modify data currently in the database • Could be very costly to have an expensive item suddenly be deeply „discounted‟ • Perform an UPDATE in the injected SQL – Often can gain access to other user‟s system capabilities by obtaining their password
  • 6. Defenses • Use provided functions for escaping strings – Many attacks can be thwarted by simply using the SQL string escaping mechanism • „  ‟ and “  ” – mysql_real_escape_string() is the preferred function for this • Not a silver bullet! – Consider: • SELECT fields FROM table WHERE id = 23 OR 1=1 • No quotes here!
  • 7. More Defenses • Check syntax of input for validity – Many classes of input have fixed languages • Email addresses, dates, part numbers, etc. • Verify that the input is a valid string in the language • Sometime languages allow problematic characters (e.g., „*‟ in email addresses); may decide to not allow these • If you can exclude quotes and semicolons that‟s good – Not always possible: consider the name Bill O‟Reilly • Want to allow the use of single quotes in names • Have length limits on input – Many SQL injection attacks depend on entering long strings
  • 8. Even More Defenses • Scan query string for undesirable word combinations that indicate SQL statements – INSERT, DROP, etc. – If you see these, can check against SQL syntax to see if they represent a statement or valid user input • Limit database permissions and segregate users – If you‟re only reading the database, connect to database as a user that only has read permissions – Never connect as a database administrator in your web application
  • 9. More Defenses • Configure database error reporting – Default error reporting often gives away information that is valuable for attackers (table name, field name, etc.) – Configure so that this information is never exposed to a user • If possible, use bound variables – Some libraries allow you to bind inputs to variables inside a SQL statement – PERL example (from http://www.unixwiz.net/techtips/sql- injection.html) $sth = $dbh->prepare("SELECT email, userid FROM members WHERE email = ?;"); $sth->execute($email);
  • 10. Be careful out there!