SlideShare a Scribd company logo
SQL Injection Attacks By Komal Arora
How a dynamic website works... Programming Language in the Front-end and DATABASE in the backend. Queries are used to store or retrieve data
How do we make a secure Dynamic website? Javascript Validations.... Server side validations..... No script tags should be allowed.... And Avoid SQL injections....
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:  blah‘ OR ‘1’ = ‘1' •  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 1x1 = 1x1 –  Attacker has now successfully caused the entire database to be returned.
Another 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
Other injection possibilities Using SQL injections, attackers can: –  Add new data to the database •  Selling someone else's 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 Check syntax of input for validity Do not allow problematic characters (e.g., ‘*’ ,'=' in user input)‏ •  If you can exclude quotes and semicolons that’s good –  Not always possible: consider the name Bill O’Reilly •  Have length limits on input –  Many SQL injection attacks depend on entering long strings
More... 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
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 $sth = $dbh->prepare("SELECT email, userid FROM members WHERE email = ?;"); $sth->execute($email);
How we can do it in CodeIgniter? Escaping Queries It's a very good security practice to escape your data before submitting it into your database.  mysql_real_escape_string() calls MySQL’s library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ‘, ” .
Examples... $this->db->escape()  This function determines the data type so that it can escape only string data. It also automatically adds single quotes around the data so you don't have to: $sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")"; $this->db->escape_str()  This function escapes the data passed to it, regardless of type. Most of the time you'll use the above function rather than this one. Use the function like this: $sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')"; $this->db->escape_like_str()  This method should be used when strings are to be used in LIKE conditions so that LIKE wildcards ('%', '_') in the string are also properly escaped. $search = '20% raise'; $sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'";
Query Bindings Bindings enable you to simplify your query syntax by letting the system put the queries together for you. Consider the following example: $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?";  $this->db->query($sql, array(3, 'live', 'Rick')); The question marks in the query are automatically replaced with the values in the array in the second parameter of the query function. The secondary benefit of using binds is that the values are automatically escaped, producing safer queries. You don't have to remember to manually escape data; the engine does it automatically for you.

More Related Content

PPT
Web services - REST and SOAP
PPTX
Day02 a pi.
PPTX
Restful webservices
PDF
RESTful Web Services
PPTX
REST-API introduction for developers
PPTX
Web API with ASP.NET MVC by Software development company in india
PDF
Making Sense of APEX Security by Christoph Ruepprich
PDF
Best Practices in Web Service Design
Web services - REST and SOAP
Day02 a pi.
Restful webservices
RESTful Web Services
REST-API introduction for developers
Web API with ASP.NET MVC by Software development company in india
Making Sense of APEX Security by Christoph Ruepprich
Best Practices in Web Service Design

What's hot (20)

PDF
Understanding and testing restful web services
PPTX
PPTX
QA Lab: тестирование ПО. Владимир Гарбуз: "Application Security 101"
PDF
Object Oriented Programming with Laravel - Session 4
PPTX
ASP.NET Web API and HTTP Fundamentals
PPT
External Data Access with jQuery
PDF
Input validation slides of web application workshop
PPT
AJAX
PDF
The never-ending REST API design debate
PPT
Introduction to the Web API
PDF
Object Oriented Programming with Laravel - Session 2
PPT
ASP.NET 12 - State Management
PPT
Understanding REST
PPT
Entity frameworks101
PDF
Object Oriented Programming with Laravel - Session 6
PDF
Object Oriented Programming with Laravel - Session 5
PDF
<img src="../i/r_14.png" />
PPT
Securing Your Web Server
ODP
The Internet as Web Services: introduction to ReST
PDF
Ibm tivoli access manager for e business junctions and links redp4621
Understanding and testing restful web services
QA Lab: тестирование ПО. Владимир Гарбуз: "Application Security 101"
Object Oriented Programming with Laravel - Session 4
ASP.NET Web API and HTTP Fundamentals
External Data Access with jQuery
Input validation slides of web application workshop
AJAX
The never-ending REST API design debate
Introduction to the Web API
Object Oriented Programming with Laravel - Session 2
ASP.NET 12 - State Management
Understanding REST
Entity frameworks101
Object Oriented Programming with Laravel - Session 6
Object Oriented Programming with Laravel - Session 5
<img src="../i/r_14.png" />
Securing Your Web Server
The Internet as Web Services: introduction to ReST
Ibm tivoli access manager for e business junctions and links redp4621
Ad

Viewers also liked (20)

ODP
Nuclear cemeteries
PPT
Software Development Life Cycle Part II
PPT
Social Media Integration
ODP
Presentación inglés
PPTX
An Analysis of the Privacy and Security Risks of Android VPN Permission-enabl...
PPTX
Mobile app privacy
PDF
UCB Corporate Compliance Training - Module 5 Gramm-Leach-Bliley-Act (GLBA)-Ti...
PPTX
Sql injection ( http://etabz.blogspot.com/2014/11/sql-injection.html )
PDF
Apps and Privacy
PPTX
VPN Types, Vulnerabilities & Solutions - Tareq Hanaysha
PPTX
Storage in the Windows Azure Platform - ericnel
PPTX
Information Security and Privacy
PPTX
App Privacy
PPT
Secure Data Sharing in Cloud (SDSC)
PPTX
Sql Injection attacks and prevention
PPT
Cloud Computing Security Issues
PPTX
Cloud Computing Security
PPTX
Cloud security ppt
PPTX
Privacy and mobile apps - status 2013 Belgium
PPTX
Data security in cloud computing
Nuclear cemeteries
Software Development Life Cycle Part II
Social Media Integration
Presentación inglés
An Analysis of the Privacy and Security Risks of Android VPN Permission-enabl...
Mobile app privacy
UCB Corporate Compliance Training - Module 5 Gramm-Leach-Bliley-Act (GLBA)-Ti...
Sql injection ( http://etabz.blogspot.com/2014/11/sql-injection.html )
Apps and Privacy
VPN Types, Vulnerabilities & Solutions - Tareq Hanaysha
Storage in the Windows Azure Platform - ericnel
Information Security and Privacy
App Privacy
Secure Data Sharing in Cloud (SDSC)
Sql Injection attacks and prevention
Cloud Computing Security Issues
Cloud Computing Security
Cloud security ppt
Privacy and mobile apps - status 2013 Belgium
Data security in cloud computing
Ad

Similar to SQL Injection Attacks (20)

PPT
Sql injection attacks
PPT
Sql injection attacks
PPT
Sql injection attacks
PPT
Sql Injection Adv Owasp
PPT
Advanced SQL Injection
PPT
SQLSecurity.ppt
PPT
SQLSecurity.ppt
PPTX
Sql injection
PDF
A Brief Introduction About Sql Injection in PHP and MYSQL
PPT
Sql security
PPSX
Web application security
PPT
PHP - Introduction to Advanced SQL
PPT
Sql injection attack
PPTX
SQL Injections - A Powerpoint Presentation
PPTX
SQL Injection Sql Injection Typesagdsgdsgdsgbdshfdshbfdshbfdshbfdhsh
PPT
A Brief Introduction in SQL Injection
PPT
SQL Injection in PHP
PPTX
Sql injection
PDF
business articles
Sql injection attacks
Sql injection attacks
Sql injection attacks
Sql Injection Adv Owasp
Advanced SQL Injection
SQLSecurity.ppt
SQLSecurity.ppt
Sql injection
A Brief Introduction About Sql Injection in PHP and MYSQL
Sql security
Web application security
PHP - Introduction to Advanced SQL
Sql injection attack
SQL Injections - A Powerpoint Presentation
SQL Injection Sql Injection Typesagdsgdsgdsgbdshfdshbfdshbfdshbfdhsh
A Brief Introduction in SQL Injection
SQL Injection in PHP
Sql injection
business articles

More from Compare Infobase Limited (20)

PPT
Dos and Don't during Monsoon!
PPT
Intellectual Property Rights : A Primer
PPT
CIL initiative against Corruption
PPT
Cloud Computing
PPT
PPT
Storage and Storage Devices
PPT
World No Tobacco Day
PPT
Tips for Effective Online Marketing
PPT
iOS Application Development
PPT
Have a safe Summer!
PPT
Introduction to Android Environment
PPT
MySQL Functions
PPT
Excel with Excel
PPT
Software Development Life Cycle (SDLC)
PPT
How to increase effective CTR, CPC and e CPM of website?
PPT
How do speed up web pages? CSS & HTML Tricks
PPT
Steps for Effective Keyword Research
PPT
50 Social Media Breakfasts - An Amazing Journey!
Dos and Don't during Monsoon!
Intellectual Property Rights : A Primer
CIL initiative against Corruption
Cloud Computing
Storage and Storage Devices
World No Tobacco Day
Tips for Effective Online Marketing
iOS Application Development
Have a safe Summer!
Introduction to Android Environment
MySQL Functions
Excel with Excel
Software Development Life Cycle (SDLC)
How to increase effective CTR, CPC and e CPM of website?
How do speed up web pages? CSS & HTML Tricks
Steps for Effective Keyword Research
50 Social Media Breakfasts - An Amazing Journey!

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
cuic standard and advanced reporting.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Teaching material agriculture food technology
Machine learning based COVID-19 study performance prediction
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
Dropbox Q2 2025 Financial Results & Investor Presentation
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Chapter 3 Spatial Domain Image Processing.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Unlocking AI with Model Context Protocol (MCP)
cuic standard and advanced reporting.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

SQL Injection Attacks

  • 1. SQL Injection Attacks By Komal Arora
  • 2. How a dynamic website works... Programming Language in the Front-end and DATABASE in the backend. Queries are used to store or retrieve data
  • 3. How do we make a secure Dynamic website? Javascript Validations.... Server side validations..... No script tags should be allowed.... And Avoid SQL injections....
  • 4. 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
  • 5. An Example SQL Injection Attack Product Search: blah‘ OR ‘1’ = ‘1' • 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 1x1 = 1x1 – Attacker has now successfully caused the entire database to be returned.
  • 6. Another 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
  • 7. Other injection possibilities Using SQL injections, attackers can: – Add new data to the database • Selling someone else's 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
  • 8. Defenses Check syntax of input for validity Do not allow problematic characters (e.g., ‘*’ ,'=' in user input)‏ • If you can exclude quotes and semicolons that’s good – Not always possible: consider the name Bill O’Reilly • Have length limits on input – Many SQL injection attacks depend on entering long strings
  • 9. More... 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
  • 10. 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 $sth = $dbh->prepare("SELECT email, userid FROM members WHERE email = ?;"); $sth->execute($email);
  • 11. How we can do it in CodeIgniter? Escaping Queries It's a very good security practice to escape your data before submitting it into your database. mysql_real_escape_string() calls MySQL’s library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ‘, ” .
  • 12. Examples... $this->db->escape() This function determines the data type so that it can escape only string data. It also automatically adds single quotes around the data so you don't have to: $sql = "INSERT INTO table (title) VALUES(".$this->db->escape($title).")"; $this->db->escape_str() This function escapes the data passed to it, regardless of type. Most of the time you'll use the above function rather than this one. Use the function like this: $sql = "INSERT INTO table (title) VALUES('".$this->db->escape_str($title)."')"; $this->db->escape_like_str() This method should be used when strings are to be used in LIKE conditions so that LIKE wildcards ('%', '_') in the string are also properly escaped. $search = '20% raise'; $sql = "SELECT id FROM table WHERE column LIKE '%".$this->db->escape_like_str($search)."%'";
  • 13. Query Bindings Bindings enable you to simplify your query syntax by letting the system put the queries together for you. Consider the following example: $sql = "SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?"; $this->db->query($sql, array(3, 'live', 'Rick')); The question marks in the query are automatically replaced with the values in the array in the second parameter of the query function. The secondary benefit of using binds is that the values are automatically escaped, producing safer queries. You don't have to remember to manually escape data; the engine does it automatically for you.