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

What's hot

D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injection
avishkarm
 
Sql injections
Sql injectionsSql injections
Sql injections
KK004
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionSina Manavi
 
Sql Injection Attacks Siddhesh
Sql Injection Attacks SiddheshSql Injection Attacks Siddhesh
Sql Injection Attacks Siddhesh
Siddhesh Bhobe
 
Sql Injection and Entity Frameworks
Sql Injection and Entity FrameworksSql Injection and Entity Frameworks
Sql Injection and Entity FrameworksRich Helton
 
SQL Injection
SQL Injection SQL Injection
SQL Injection
Adhoura Academy
 
Sql Injection - Vulnerability and Security
Sql Injection - Vulnerability and SecuritySql Injection - Vulnerability and Security
Sql Injection - Vulnerability and Security
Sandip Chaudhari
 
Advanced SQL Injection
Advanced SQL InjectionAdvanced SQL Injection
Advanced SQL Injection
amiable_indian
 
Sql injection attack
Sql injection attackSql injection attack
Sql injection attack
RajKumar Rampelli
 
Sql injection
Sql injectionSql injection
Sql injection
Nikunj Dhameliya
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL Injection
Vortana Say
 
Sql injection attack
Sql injection attackSql injection attack
Sql injection attack
Raghav Bisht
 
SQL Injection - Mozilla Security Learning Center
SQL Injection - Mozilla Security Learning CenterSQL Injection - Mozilla Security Learning Center
SQL Injection - Mozilla Security Learning CenterMichael Coates
 
Sql Injection and XSS
Sql Injection and XSSSql Injection and XSS
Sql Injection and XSS
Mike Crabb
 
Sql injection attacks
Sql injection attacksSql injection attacks
Sql injection attacks
chaitanya Lotankar
 
Web Application Security II - SQL Injection
Web Application Security II - SQL InjectionWeb Application Security II - SQL Injection
Web Application Security II - SQL Injection
Md Syed Ahamad
 
SQL Injection Defense in Python
SQL Injection Defense in PythonSQL Injection Defense in Python
SQL Injection Defense in Python
Public Broadcasting Service
 
Web application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasuresWeb application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasures
Cade Zvavanjanja
 

What's hot (20)

D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injection
 
Sql injections
Sql injectionsSql injections
Sql injections
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL Injection
 
Sql Injection Attacks Siddhesh
Sql Injection Attacks SiddheshSql Injection Attacks Siddhesh
Sql Injection Attacks Siddhesh
 
Sql Injection and Entity Frameworks
Sql Injection and Entity FrameworksSql Injection and Entity Frameworks
Sql Injection and Entity Frameworks
 
SQL Injection
SQL Injection SQL Injection
SQL Injection
 
Sql Injection - Vulnerability and Security
Sql Injection - Vulnerability and SecuritySql Injection - Vulnerability and Security
Sql Injection - Vulnerability and Security
 
Advanced SQL Injection
Advanced SQL InjectionAdvanced SQL Injection
Advanced SQL Injection
 
Sql injection attack
Sql injection attackSql injection attack
Sql injection attack
 
Sql injection
Sql injectionSql injection
Sql injection
 
Sql injection
Sql injectionSql injection
Sql injection
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL Injection
 
Sql injection
Sql injectionSql injection
Sql injection
 
Sql injection attack
Sql injection attackSql injection attack
Sql injection attack
 
SQL Injection - Mozilla Security Learning Center
SQL Injection - Mozilla Security Learning CenterSQL Injection - Mozilla Security Learning Center
SQL Injection - Mozilla Security Learning Center
 
Sql Injection and XSS
Sql Injection and XSSSql Injection and XSS
Sql Injection and XSS
 
Sql injection attacks
Sql injection attacksSql injection attacks
Sql injection attacks
 
Web Application Security II - SQL Injection
Web Application Security II - SQL InjectionWeb Application Security II - SQL Injection
Web Application Security II - SQL Injection
 
SQL Injection Defense in Python
SQL Injection Defense in PythonSQL Injection Defense in Python
SQL Injection Defense in Python
 
Web application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasuresWeb application attacks using Sql injection and countermasures
Web application attacks using Sql injection and countermasures
 

Similar to Sql injection attacks

Sql injection attacks
Sql injection attacksSql injection attacks
Sql injection attacksNitish Kumar
 
Code injection and green sql
Code injection and green sqlCode injection and green sql
Code injection and green sql
Kaustav Sengupta
 
Google Dorks and SQL Injection
Google Dorks and SQL InjectionGoogle Dorks and SQL Injection
Google Dorks and SQL Injection
Mudassir Hassan Khan
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers dofangjiafu
 
SQLi for Security Champions
SQLi for Security ChampionsSQLi for Security Champions
SQLi for Security Champions
PetraVukmirovic
 
Web application security part 01
Web application security part 01Web application security part 01
Web application security part 01
G Prachi
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTION
Anoop T
 
Unique Features of SQL Injection in PHP Assignment
Unique Features of SQL Injection in PHP AssignmentUnique Features of SQL Injection in PHP Assignment
Unique Features of SQL Injection in PHP Assignment
Lesa Cote
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.ppt
CNSHacking
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.ppt
LokeshK66
 
[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)
NAVER D2
 
Ch 9 Attacking Data Stores (Part 2)
Ch 9 Attacking Data Stores (Part 2)Ch 9 Attacking Data Stores (Part 2)
Ch 9 Attacking Data Stores (Part 2)
Sam Bowne
 
Sql Injection
Sql InjectionSql Injection
Sql Injection
penetration Tester
 
Sql security
Sql securitySql security
Sql security
Safwan Hashmi
 
Sql injection
Sql injectionSql injection
Sql injection
MathewHarrison3
 
SQL Injection in JAVA
SQL Injection in JAVASQL Injection in JAVA
SQL Injection in JAVA
Hossein Yavari
 
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
Sam Bowne
 
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
Sam Bowne
 

Similar to Sql injection attacks (20)

Sql injection attacks
Sql injection attacksSql injection attacks
Sql injection attacks
 
SQL Injection Attacks
SQL Injection AttacksSQL Injection Attacks
SQL Injection Attacks
 
Greensql2007
Greensql2007Greensql2007
Greensql2007
 
Code injection and green sql
Code injection and green sqlCode injection and green sql
Code injection and green sql
 
Google Dorks and SQL Injection
Google Dorks and SQL InjectionGoogle Dorks and SQL Injection
Google Dorks and SQL Injection
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers do
 
SQLi for Security Champions
SQLi for Security ChampionsSQLi for Security Champions
SQLi for Security Champions
 
Web application security part 01
Web application security part 01Web application security part 01
Web application security part 01
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTION
 
Unique Features of SQL Injection in PHP Assignment
Unique Features of SQL Injection in PHP AssignmentUnique Features of SQL Injection in PHP Assignment
Unique Features of SQL Injection in PHP Assignment
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.ppt
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.ppt
 
[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)[Kerference] Nefarious SQL - 김동호(KERT)
[Kerference] Nefarious SQL - 김동호(KERT)
 
Ch 9 Attacking Data Stores (Part 2)
Ch 9 Attacking Data Stores (Part 2)Ch 9 Attacking Data Stores (Part 2)
Ch 9 Attacking Data Stores (Part 2)
 
Sql Injection
Sql InjectionSql Injection
Sql Injection
 
Sql security
Sql securitySql security
Sql security
 
Sql injection
Sql injectionSql injection
Sql injection
 
SQL Injection in JAVA
SQL Injection in JAVASQL Injection in JAVA
SQL Injection in JAVA
 
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S Ch 9: Attacking Data Stores (Part 2 of 2)
 
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
CNIT 129S: 9: Attacking Data Stores (Part 2 of 2)
 

More from Kumar

Graphics devices
Graphics devicesGraphics devices
Graphics devices
Kumar
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
Kumar
 
region-filling
region-fillingregion-filling
region-filling
Kumar
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
Kumar
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
Kumar
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
Kumar
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
Kumar
 
Xml basics
Xml basicsXml basics
Xml basics
Kumar
 
XML Schema
XML SchemaXML Schema
XML Schema
Kumar
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
Kumar
 
DTD
DTDDTD
DTD
Kumar
 
Applying xml
Applying xmlApplying xml
Applying xml
Kumar
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
Kumar
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
Kumar
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
Kumar
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
Kumar
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
Kumar
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
Kumar
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
Kumar
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
Introduction to J2EE
Kumar
 

More from Kumar (20)

Graphics devices
Graphics devicesGraphics devices
Graphics devices
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
region-filling
region-fillingregion-filling
region-filling
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Bresenham circles and polygons derication
Bresenham circles and polygons dericationBresenham circles and polygons derication
Bresenham circles and polygons derication
 
Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
Extracting data from xml
Extracting data from xmlExtracting data from xml
Extracting data from xml
 
Xml basics
Xml basicsXml basics
Xml basics
 
XML Schema
XML SchemaXML Schema
XML Schema
 
Publishing xml
Publishing xmlPublishing xml
Publishing xml
 
DTD
DTDDTD
DTD
 
Applying xml
Applying xmlApplying xml
Applying xml
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
How to deploy a j2ee application
How to deploy a j2ee applicationHow to deploy a j2ee application
How to deploy a j2ee application
 
JNDI, JMS, JPA, XML
JNDI, JMS, JPA, XMLJNDI, JMS, JPA, XML
JNDI, JMS, JPA, XML
 
EJB Fundmentals
EJB FundmentalsEJB Fundmentals
EJB Fundmentals
 
JSP and struts programming
JSP and struts programmingJSP and struts programming
JSP and struts programming
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 
Introduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC DriversIntroduction to JDBC and JDBC Drivers
Introduction to JDBC and JDBC Drivers
 
Introduction to J2EE
Introduction to J2EEIntroduction to J2EE
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!