SlideShare a Scribd company logo
SQL Injection What it is, how it happens, and how to prevent it.
What is SQL Injection? According to WIKIPEDIA: SQL injection  is a  code injection  technique that exploits a  security vulnerability  occurring in the  database  layer of an  application .  The vulnerability is present when user input is either incorrectly filtered for  string literalescape characters  embedded in  SQL  statements or user input is not  strongly typed  and thereby unexpectedly executed.  It is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another. SQL injection attacks are also known as SQL insertion attacks.
What can it do? Allow logging in as another use, such as the administrator Cause extra DB records to be displayed Allow vandalism of the database Allow destruction of the database Ruin your day... or night... or weekend... The recent theft of 130 million credit card numbers involved SQL Injection.
How does it work? By passing un-filtered user data into a SQL query, a user can cause un-intended SQL queries to be executed. $SQL = "SELECT * FROM users  WHERE name = '{$_GET['name']}'" $_GET['name']  =  " a' or 't'='t " ; SELECT   *   FROM  users  WHERE  name  =   'a'   OR   't' = 't' ;
How do you stop SQL Injection? Do not allow direct user input into the database. Parameterized statements Use a library that uses parameters Use a built in functionality that supports Parametization such as MySQLi in PHP5 $db   =   new  mysqli ( "localhost" ,   "user" ,   "pass" ,   "database" ) ;   $stmt   =   $db   ->   prepare ( "SELECT * FROM Usrs WHERE usrnm=? AND psswrd=?" ) ;   $stmt   ->   bind_param ( "ss" ,   $user ,   $pass ) ;   $stmt   ->   execute () ; Escaping Escape the query using a built in or library function $ Password =  mysql_real_escape_string ( $Password ) ;
Why does this work? $ name =  mysql_real_escape_string ( $_GET['name'] ) ; $SQL = "SELECT * FROM users  WHERE name = '{$name}'" $_GET['name']  =  " a' or 't'='t " ; $name  =  " a\' or \'t\'=\'t " ; SELECT   *   FROM  users  WHERE  name  =   'a\' OR \'t\'=\'t' ;
Preventing SQL injection in CMSes ALWAYS keep all your modules, themes, components etc. current. The CMS communities stay on top of security issues with their software. ALWAYS keep all your modules, themes, components etc. current. When a compromise is announced, the 'bad people' get just as busy as the 'good people'. ALWAYS keep all your modules, themes, components etc. current.
Preventing SQL Injection in JOOMLA! Use JOOMLA!'s JRequest functionality to get typed data from the user.   JRequest:: get(), getBool(), getCmd(), getFloat(), getInt(), getString(), getVar() Use JOOMLA!'s database quoting and escaping functions. $sql   =   'UPDATE #__mytable SET `string` = '   .   $db -> quote (   $db -> getEscaped (   $string   ),   false   );   JOOMLA!'s database quoting will escape for you automatically $sql   =   'UPDATE #__mytable SET `string` = '   .   $db -> quote (   $string   );   JOOMLA!'s database escaping for 'LIKE' escapes '%' $sql   =   'UPDATE #__mytable SET .... WHERE `string` LIKE ' .   $db -> quote (   $db -> getEscaped (   $string ,   true   ),   false   );
Preventing SQL Injection in Drupal Drupal has a database layer as well which uses and escapes placeholders:  %s, %d, %f, %b, %% So, instead of: db_query("SELECT * FROM {users} WHERE name = '$username' ");  You would want to write this: db_query("SELECT * FROM {users} WHERE name = '%s' ", $username); Also, use db_rewrite_sql to respect node access restrictions: $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n"));
Prevent SQL Injection in Wordpress Wordpress has a database object that supports prepared statements and will escape the values for you. $sql  =  $wpdb -> prepare (  'query' [,  value_parameter ,  value_parameter  ... ] );     Additionally,  the $wpdb object will escape data for you. $wpdb -> insert (  $table, (array) $data  ); $wpdb -> update (  $table, (array) $data, (array) $where ); $data should be unescaped. Keys are columns, Values are values.  $where should be unescaped. Multiple WHERE conditions are ANDed together.
Any Questions?

More Related Content

What's hot (20)

PPT
PHP Security
Mindfire Solutions
 
PDF
Ben Bridts - $ aws help
AWSCOMSUM
 
PDF
Php101
Ömer Taşkın
 
PDF
Check username availability with vue.js and PHP
Yogesh singh
 
PDF
PHP Secure Programming
Balavignesh Kasinathan
 
KEY
[Coscup 2012] JavascriptMVC
Alive Kuo
 
PPT
General Principles of Web Security
jemond
 
PPT
Quebec pdo
Valentine Dianov
 
PDF
OWASP Top 10 - DrupalCon Amsterdam 2019
Ayesh Karunaratne
 
PDF
Agile database access with CakePHP 3
José Lorenzo Rodríguez Urdaneta
 
PDF
Eric Redmond – Distributed Search on Riak 2.0 - NoSQL matters Barcelona 2014
NoSQLmatters
 
PDF
Future of HTTP in CakePHP
markstory
 
PDF
Hidden in plain site – joomla! hidden secrets for code monkeys
Nicholas Dionysopoulos
 
PPTX
Security: Odoo Code Hardening
Odoo
 
PDF
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
 
PDF
Security Meetup 22 октября. «Реверс-инжиниринг в Enterprise». Алексей Секрето...
Mail.ru Group
 
PDF
Teaching Your Machine To Find Fraudsters
Ian Barber
 
PDF
Php unit the-mostunknownparts
Bastian Feder
 
PPTX
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
DevOps_Fest
 
ODP
When dynamic becomes static: the next step in web caching techniques
Wim Godden
 
PHP Security
Mindfire Solutions
 
Ben Bridts - $ aws help
AWSCOMSUM
 
Check username availability with vue.js and PHP
Yogesh singh
 
PHP Secure Programming
Balavignesh Kasinathan
 
[Coscup 2012] JavascriptMVC
Alive Kuo
 
General Principles of Web Security
jemond
 
Quebec pdo
Valentine Dianov
 
OWASP Top 10 - DrupalCon Amsterdam 2019
Ayesh Karunaratne
 
Agile database access with CakePHP 3
José Lorenzo Rodríguez Urdaneta
 
Eric Redmond – Distributed Search on Riak 2.0 - NoSQL matters Barcelona 2014
NoSQLmatters
 
Future of HTTP in CakePHP
markstory
 
Hidden in plain site – joomla! hidden secrets for code monkeys
Nicholas Dionysopoulos
 
Security: Odoo Code Hardening
Odoo
 
Debugging: Rules And Tools - PHPTek 11 Version
Ian Barber
 
Security Meetup 22 октября. «Реверс-инжиниринг в Enterprise». Алексей Секрето...
Mail.ru Group
 
Teaching Your Machine To Find Fraudsters
Ian Barber
 
Php unit the-mostunknownparts
Bastian Feder
 
DevOps Fest 2019. Сергей Марченко. Terraform: a novel about modules, provider...
DevOps_Fest
 
When dynamic becomes static: the next step in web caching techniques
Wim Godden
 

Viewers also liked (20)

PPT
Sql Injection Tutorial!
ralphmigcute
 
PDF
Neutralizing SQL Injection in PostgreSQL
Juliano Atanazio
 
PDF
SQL Injection - The Unknown Story
Imperva
 
PDF
Defcon 17-joseph mccray-adv-sql_injection
Ahmed AbdelSatar
 
PPT
Blind SQL Injection - Optimization Techniques
guest54de52
 
PDF
Sql Injection and XSS
Mike Crabb
 
PPT
Sql Injection Attacks Siddhesh
Siddhesh Bhobe
 
PDF
Advanced SQL Injection: Attacks
Nuno Loureiro
 
PPTX
Understanding and preventing sql injection attacks
Kevin Kline
 
PDF
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
Ivan Ortega
 
PPTX
Sql Injection and Entity Frameworks
Rich Helton
 
PPT
D:\Technical\Ppt\Sql Injection
avishkarm
 
PPT
ShmooCON 2009 : Re-playing with (Blind) SQL Injection
Chema Alonso
 
PPT
Web application attacks using Sql injection and countermasures
Cade Zvavanjanja
 
PPTX
Google Dorks and SQL Injection
Mudassir Hassan Khan
 
PPTX
SQL INJECTION
Anoop T
 
PDF
Database security issues
n|u - The Open Security Community
 
PPT
SQL Injection
Adhoura Academy
 
PPT
Sql injection
Pallavi Biswas
 
PDF
Ataques a-bases-de-datos
alan moreno
 
Sql Injection Tutorial!
ralphmigcute
 
Neutralizing SQL Injection in PostgreSQL
Juliano Atanazio
 
SQL Injection - The Unknown Story
Imperva
 
Defcon 17-joseph mccray-adv-sql_injection
Ahmed AbdelSatar
 
Blind SQL Injection - Optimization Techniques
guest54de52
 
Sql Injection and XSS
Mike Crabb
 
Sql Injection Attacks Siddhesh
Siddhesh Bhobe
 
Advanced SQL Injection: Attacks
Nuno Loureiro
 
Understanding and preventing sql injection attacks
Kevin Kline
 
Web Security - OWASP - SQL injection & Cross Site Scripting XSS
Ivan Ortega
 
Sql Injection and Entity Frameworks
Rich Helton
 
D:\Technical\Ppt\Sql Injection
avishkarm
 
ShmooCON 2009 : Re-playing with (Blind) SQL Injection
Chema Alonso
 
Web application attacks using Sql injection and countermasures
Cade Zvavanjanja
 
Google Dorks and SQL Injection
Mudassir Hassan Khan
 
SQL INJECTION
Anoop T
 
Database security issues
n|u - The Open Security Community
 
SQL Injection
Adhoura Academy
 
Sql injection
Pallavi Biswas
 
Ataques a-bases-de-datos
alan moreno
 
Ad

Similar to SQL Injection in PHP (20)

PPT
Sql Injection Adv Owasp
Aung Khant
 
PPT
Advanced SQL Injection
amiable_indian
 
PPT
SQL Injection Attacks
Compare Infobase Limited
 
PPT
PHP - Introduction to Advanced SQL
Vibrant Technologies & Computers
 
PPTX
03. sql and other injection module v17
Eoin Keary
 
PPTX
How did i steal your database CSCamp2011
Mostafa Siraj
 
PPT
Sql injection
Nitish Kumar
 
PDF
A Brief Introduction About Sql Injection in PHP and MYSQL
kobaitari
 
PDF
Hacking Your Way To Better Security
Colin O'Dell
 
PPTX
Hacking Your Way to Better Security - PHP South Africa 2016
Colin O'Dell
 
PPTX
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Colin O'Dell
 
PPTX
Sql injection
Mehul Boghra
 
PDF
How to lose your database and your job
Ryan Gooler
 
PDF
business articles
client001competitors
 
PPT
Sql injection
Nikunj Dhameliya
 
PPTX
SQL Injection Sql Injection Typesagdsgdsgdsgbdshfdshbfdshbfdshbfdhsh
RAKIBULISLAM529074
 
PPTX
Sql injection
Hemendra Kumar
 
PDF
Chapter 14 sql injection
newbie2019
 
PPTX
SQL Injection in action with PHP and MySQL
Pradeep Kumar
 
Sql Injection Adv Owasp
Aung Khant
 
Advanced SQL Injection
amiable_indian
 
SQL Injection Attacks
Compare Infobase Limited
 
PHP - Introduction to Advanced SQL
Vibrant Technologies & Computers
 
03. sql and other injection module v17
Eoin Keary
 
How did i steal your database CSCamp2011
Mostafa Siraj
 
Sql injection
Nitish Kumar
 
A Brief Introduction About Sql Injection in PHP and MYSQL
kobaitari
 
Hacking Your Way To Better Security
Colin O'Dell
 
Hacking Your Way to Better Security - PHP South Africa 2016
Colin O'Dell
 
Hacking Your Way To Better Security - Dutch PHP Conference 2016
Colin O'Dell
 
Sql injection
Mehul Boghra
 
How to lose your database and your job
Ryan Gooler
 
business articles
client001competitors
 
Sql injection
Nikunj Dhameliya
 
SQL Injection Sql Injection Typesagdsgdsgdsgbdshfdshbfdshbfdshbfdhsh
RAKIBULISLAM529074
 
Sql injection
Hemendra Kumar
 
Chapter 14 sql injection
newbie2019
 
SQL Injection in action with PHP and MySQL
Pradeep Kumar
 
Ad

More from Dave Ross (20)

KEY
Stylesheets of the future with Sass and Compass
Dave Ross
 
KEY
HTML5 History & Features
Dave Ross
 
PPT
A geek's guide to getting hired
Dave Ross
 
KEY
NoSQL & MongoDB
Dave Ross
 
PDF
Date and Time programming in PHP & Javascript
Dave Ross
 
KEY
Simulated Eye Tracking with Attention Wizard
Dave Ross
 
KEY
What's new in HTML5?
Dave Ross
 
KEY
The Canvas Tag
Dave Ross
 
KEY
Wordpress
Dave Ross
 
PPT
Lamp Stack Optimization
Dave Ross
 
PPT
The FPDF Library
Dave Ross
 
PPT
FirePHP
Dave Ross
 
PPT
Bayesian Inference using b8
Dave Ross
 
KEY
Web App Security: XSS and CSRF
Dave Ross
 
KEY
The Mobile Web: A developer's perspective
Dave Ross
 
KEY
Balsamiq Mockups
Dave Ross
 
KEY
LAMP Optimization
Dave Ross
 
KEY
Lint - PHP & Javascript Code Checking
Dave Ross
 
KEY
Cufon - Javascript Font Replacement
Dave Ross
 
KEY
PHP Output Buffering
Dave Ross
 
Stylesheets of the future with Sass and Compass
Dave Ross
 
HTML5 History & Features
Dave Ross
 
A geek's guide to getting hired
Dave Ross
 
NoSQL & MongoDB
Dave Ross
 
Date and Time programming in PHP & Javascript
Dave Ross
 
Simulated Eye Tracking with Attention Wizard
Dave Ross
 
What's new in HTML5?
Dave Ross
 
The Canvas Tag
Dave Ross
 
Wordpress
Dave Ross
 
Lamp Stack Optimization
Dave Ross
 
The FPDF Library
Dave Ross
 
FirePHP
Dave Ross
 
Bayesian Inference using b8
Dave Ross
 
Web App Security: XSS and CSRF
Dave Ross
 
The Mobile Web: A developer's perspective
Dave Ross
 
Balsamiq Mockups
Dave Ross
 
LAMP Optimization
Dave Ross
 
Lint - PHP & Javascript Code Checking
Dave Ross
 
Cufon - Javascript Font Replacement
Dave Ross
 
PHP Output Buffering
Dave Ross
 

Recently uploaded (20)

PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
July Patch Tuesday
Ivanti
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 

SQL Injection in PHP

  • 1. SQL Injection What it is, how it happens, and how to prevent it.
  • 2. What is SQL Injection? According to WIKIPEDIA: SQL injection  is a  code injection  technique that exploits a  security vulnerability  occurring in the  database  layer of an  application .  The vulnerability is present when user input is either incorrectly filtered for  string literalescape characters  embedded in  SQL  statements or user input is not  strongly typed  and thereby unexpectedly executed.  It is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another. SQL injection attacks are also known as SQL insertion attacks.
  • 3. What can it do? Allow logging in as another use, such as the administrator Cause extra DB records to be displayed Allow vandalism of the database Allow destruction of the database Ruin your day... or night... or weekend... The recent theft of 130 million credit card numbers involved SQL Injection.
  • 4. How does it work? By passing un-filtered user data into a SQL query, a user can cause un-intended SQL queries to be executed. $SQL = "SELECT * FROM users  WHERE name = '{$_GET['name']}'" $_GET['name']  =  " a' or 't'='t " ; SELECT * FROM users  WHERE name = 'a' OR 't' = 't' ;
  • 5. How do you stop SQL Injection? Do not allow direct user input into the database. Parameterized statements Use a library that uses parameters Use a built in functionality that supports Parametization such as MySQLi in PHP5 $db = new mysqli ( "localhost" , "user" , "pass" , "database" ) ; $stmt = $db -> prepare ( "SELECT * FROM Usrs WHERE usrnm=? AND psswrd=?" ) ; $stmt -> bind_param ( "ss" , $user , $pass ) ; $stmt -> execute () ; Escaping Escape the query using a built in or library function $ Password = mysql_real_escape_string ( $Password ) ;
  • 6. Why does this work? $ name = mysql_real_escape_string ( $_GET['name'] ) ; $SQL = "SELECT * FROM users  WHERE name = '{$name}'" $_GET['name']  =  " a' or 't'='t " ; $name  =  " a\' or \'t\'=\'t " ; SELECT   *   FROM  users  WHERE  name  =   'a\' OR \'t\'=\'t' ;
  • 7. Preventing SQL injection in CMSes ALWAYS keep all your modules, themes, components etc. current. The CMS communities stay on top of security issues with their software. ALWAYS keep all your modules, themes, components etc. current. When a compromise is announced, the 'bad people' get just as busy as the 'good people'. ALWAYS keep all your modules, themes, components etc. current.
  • 8. Preventing SQL Injection in JOOMLA! Use JOOMLA!'s JRequest functionality to get typed data from the user.   JRequest:: get(), getBool(), getCmd(), getFloat(), getInt(), getString(), getVar() Use JOOMLA!'s database quoting and escaping functions. $sql = 'UPDATE #__mytable SET `string` = ' . $db -> quote ( $db -> getEscaped ( $string ), false   ); JOOMLA!'s database quoting will escape for you automatically $sql = 'UPDATE #__mytable SET `string` = ' . $db -> quote ( $string   ); JOOMLA!'s database escaping for 'LIKE' escapes '%' $sql = 'UPDATE #__mytable SET .... WHERE `string` LIKE ' . $db -> quote ( $db -> getEscaped ( $string , true ), false );
  • 9. Preventing SQL Injection in Drupal Drupal has a database layer as well which uses and escapes placeholders:  %s, %d, %f, %b, %% So, instead of: db_query("SELECT * FROM {users} WHERE name = '$username' "); You would want to write this: db_query("SELECT * FROM {users} WHERE name = '%s' ", $username); Also, use db_rewrite_sql to respect node access restrictions: $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n"));
  • 10. Prevent SQL Injection in Wordpress Wordpress has a database object that supports prepared statements and will escape the values for you. $sql  =  $wpdb -> prepare (  'query' [,  value_parameter ,  value_parameter  ... ] );   Additionally,  the $wpdb object will escape data for you. $wpdb -> insert ( $table, (array) $data ); $wpdb -> update (  $table, (array) $data, (array) $where ); $data should be unescaped. Keys are columns, Values are values.  $where should be unescaped. Multiple WHERE conditions are ANDed together.

Editor's Notes

  • #3: http://en.wikipedia.org/wiki/SQL_injection
  • #4: http://en.wikipedia.org/wiki/SQL_injection#Real-world_examples
  • #9: http://developer.joomla.org/tutorials/181-preventing-sql-injections.html http://api.joomla.org/Joomla-Framework/Environment/JRequest.html