SlideShare a Scribd company logo
SQL INJECTION
IN ACTION
Pradeep Kumar
What is SQL Injection?
• SQL injection is a code injection technique, used to attack
data-driven applications, in which nefarious SQL
statements are inserted into an entry field for execution
(e.g. to dump the database contents to the attacker).
• SQL injection must exploit a security vulnerability in an
application's software, for example, when user input is
either incorrectly filtered for string literal escape
characters embedded in SQL statements or user input is
not strongly typed and unexpectedly executed.
Causes of SQL Injection
• Incorrectly filtered escape characters
Attacker sends following input in a text field and developer doesn’t filters it for
further computation.
myuser' or 'foo' = 'foo' --
• Incorrect type handling or passing wrong data to DB
Developer sends this unfiltered data to database.
<?php
$sql = "SELECT *
FROM users
WHERE username = 'myuser' or 'foo' = 'foo' --
AND password = 'a029d0df84eb5549c641e04a9ef389e5'";
?>
SQL Injection Example
HTML File –
[index.html]
<form action="injection.php" method="POST">
<p>
Username: <input type="text" name="username" />
</p>
<p>
Password: <input type="password" name="password" />
</p>
<p>
<input type="submit" value="Log In" />
</p>
</form>
SQL Injection Example
PHP File – [injection.php]
<?php
//connection to the database and select a DB to work with
$dbhandle = mysql_connect('localhost', 'root', '') or die('MySQL not connected');
mysql_select_db('php_security',$dbhandle) or die ( 'Could not select php_security' );
// execute the SQL query and return records
$username = $_POST["username"];
$password = $_POST["password"];
//uncomment these to fix SQL injection
//$username = mysql_real_escape_string( $_POST["username"] );
//$password = mysql_real_escape_string( $_POST["password"] );
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = mysql_query( $query , $dbhandle);
// fetch tha data from the database
$num = mysql_num_rows($result);
if ($num > 0) {
print 'got a matching user';
}
// close the connection
mysql_close ( $dbhandle );
What’s wrong with the code
//execute the SQL query and return records
$username = $_POST[‘username’];
$password = $_POST[‘password’];
$query = "SELECT * FROM users WHERE username = $username AND
password=$password";
In the above example, if we take $password as
myuser' or 'foo' = 'foo
$query becomes =
SELECT *
FROM users
WHERE username = ‘prady’
AND password = 'myuser' or 'foo' = 'foo'
Fixing the code
//execute the SQL query and return records
$username = mysql_real_escape_string( $_POST[‘username’] );
$password = mysql_real_escape_string( $_POST[‘password’] );
$query = "SELECT * FROM users WHERE username = $username
AND password=$password";
Complete code
A copy of complete code is available here
https://github.com/prady00/php-security-essentials
Need help?
Please connect via email
pradeep.online00@gmail.com
Thankyou


More Related Content

What's hot (20)

PPT
Web application attacks using Sql injection and countermasures
Cade Zvavanjanja
 
PPT
Sql injection
Nikunj Dhameliya
 
PPT
A Brief Introduction in SQL Injection
Sina Manavi
 
PDF
What is advanced SQL Injection? Infographic
JW CyberNerd
 
PPT
D:\Technical\Ppt\Sql Injection
avishkarm
 
PPTX
Sql Injection attacks and prevention
helloanand
 
PPT
Advanced SQL Injection
amiable_indian
 
PPT
Sql Injection Tutorial!
ralphmigcute
 
PDF
SQL Injection Tutorial
Magno Logan
 
PPT
Sql injection
Nitish Kumar
 
PPTX
Ppt on sql injection
ashish20012
 
PDF
SQL Injection: complete walkthrough (not only) for PHP developers
Krzysztof Kotowicz
 
PPTX
seminar report on Sql injection
Jawhar Ali
 
PDF
Advanced SQL Injection: Attacks
Nuno Loureiro
 
KEY
SQL Injection - Mozilla Security Learning Center
Michael Coates
 
PPTX
SQL Injection Attacks cs586
Stacy Watts
 
PPTX
Sql injection - security testing
Napendra Singh
 
PDF
SQL Injection
Abhinav Nair
 
PDF
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
ijtsrd
 
PPTX
SQL Injections (Part 1)
n|u - The Open Security Community
 
Web application attacks using Sql injection and countermasures
Cade Zvavanjanja
 
Sql injection
Nikunj Dhameliya
 
A Brief Introduction in SQL Injection
Sina Manavi
 
What is advanced SQL Injection? Infographic
JW CyberNerd
 
D:\Technical\Ppt\Sql Injection
avishkarm
 
Sql Injection attacks and prevention
helloanand
 
Advanced SQL Injection
amiable_indian
 
Sql Injection Tutorial!
ralphmigcute
 
SQL Injection Tutorial
Magno Logan
 
Sql injection
Nitish Kumar
 
Ppt on sql injection
ashish20012
 
SQL Injection: complete walkthrough (not only) for PHP developers
Krzysztof Kotowicz
 
seminar report on Sql injection
Jawhar Ali
 
Advanced SQL Injection: Attacks
Nuno Loureiro
 
SQL Injection - Mozilla Security Learning Center
Michael Coates
 
SQL Injection Attacks cs586
Stacy Watts
 
Sql injection - security testing
Napendra Singh
 
SQL Injection
Abhinav Nair
 
SQL Injection Attack Detection and Prevention Techniques to Secure Web-Site
ijtsrd
 
SQL Injections (Part 1)
n|u - The Open Security Community
 

Similar to SQL Injection in action with PHP and MySQL (20)

PDF
business articles
client001competitors
 
PDF
5-databasevuln.pdf
SalmanAlfarizhi2
 
PPT
Sql Injection Adv Owasp
Aung Khant
 
PDF
A Brief Introduction About Sql Injection in PHP and MYSQL
kobaitari
 
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
03. sql and other injection module v17
Eoin Keary
 
PDF
Hacking Your Way To Better Security
Colin O'Dell
 
PDF
POWER OF VISUALIZATION
sihleGumede3
 
PDF
Chapter 5 - SQL-Injection-NK.pdf
Waad Waad
 
PPT
PHP - Introduction to Advanced SQL
Vibrant Technologies & Computers
 
PPT
SQL Injection Attacks
Compare Infobase Limited
 
PDF
Sql Injection
Tayyip Gören
 
PDF
Chapter 14 sql injection
newbie2019
 
PPT
SQL Injection in PHP
Dave Ross
 
PPTX
Hacking Your Way to Better Security - ZendCon 2016
Colin O'Dell
 
PDF
Hacking Your Way To Better Security - php[tek] 2016
Colin O'Dell
 
PPTX
Hacking Your Way To Better Security - DrupalCon Baltimore 2017
Colin O'Dell
 
PDF
SQL Injection
Magno Logan
 
business articles
client001competitors
 
5-databasevuln.pdf
SalmanAlfarizhi2
 
Sql Injection Adv Owasp
Aung Khant
 
A Brief Introduction About Sql Injection in PHP and MYSQL
kobaitari
 
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
 
03. sql and other injection module v17
Eoin Keary
 
Hacking Your Way To Better Security
Colin O'Dell
 
POWER OF VISUALIZATION
sihleGumede3
 
Chapter 5 - SQL-Injection-NK.pdf
Waad Waad
 
PHP - Introduction to Advanced SQL
Vibrant Technologies & Computers
 
SQL Injection Attacks
Compare Infobase Limited
 
Sql Injection
Tayyip Gören
 
Chapter 14 sql injection
newbie2019
 
SQL Injection in PHP
Dave Ross
 
Hacking Your Way to Better Security - ZendCon 2016
Colin O'Dell
 
Hacking Your Way To Better Security - php[tek] 2016
Colin O'Dell
 
Hacking Your Way To Better Security - DrupalCon Baltimore 2017
Colin O'Dell
 
SQL Injection
Magno Logan
 
Ad

Recently uploaded (20)

PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Ad

SQL Injection in action with PHP and MySQL

  • 2. What is SQL Injection? • SQL injection is a code injection technique, used to attack data-driven applications, in which nefarious SQL statements are inserted into an entry field for execution (e.g. to dump the database contents to the attacker). • SQL injection must exploit a security vulnerability in an application's software, for example, when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and unexpectedly executed.
  • 3. Causes of SQL Injection • Incorrectly filtered escape characters Attacker sends following input in a text field and developer doesn’t filters it for further computation. myuser' or 'foo' = 'foo' -- • Incorrect type handling or passing wrong data to DB Developer sends this unfiltered data to database. <?php $sql = "SELECT * FROM users WHERE username = 'myuser' or 'foo' = 'foo' -- AND password = 'a029d0df84eb5549c641e04a9ef389e5'"; ?>
  • 4. SQL Injection Example HTML File – [index.html] <form action="injection.php" method="POST"> <p> Username: <input type="text" name="username" /> </p> <p> Password: <input type="password" name="password" /> </p> <p> <input type="submit" value="Log In" /> </p> </form>
  • 5. SQL Injection Example PHP File – [injection.php] <?php //connection to the database and select a DB to work with $dbhandle = mysql_connect('localhost', 'root', '') or die('MySQL not connected'); mysql_select_db('php_security',$dbhandle) or die ( 'Could not select php_security' ); // execute the SQL query and return records $username = $_POST["username"]; $password = $_POST["password"]; //uncomment these to fix SQL injection //$username = mysql_real_escape_string( $_POST["username"] ); //$password = mysql_real_escape_string( $_POST["password"] ); $query = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysql_query( $query , $dbhandle); // fetch tha data from the database $num = mysql_num_rows($result); if ($num > 0) { print 'got a matching user'; } // close the connection mysql_close ( $dbhandle );
  • 6. What’s wrong with the code //execute the SQL query and return records $username = $_POST[‘username’]; $password = $_POST[‘password’]; $query = "SELECT * FROM users WHERE username = $username AND password=$password"; In the above example, if we take $password as myuser' or 'foo' = 'foo $query becomes = SELECT * FROM users WHERE username = ‘prady’ AND password = 'myuser' or 'foo' = 'foo'
  • 7. Fixing the code //execute the SQL query and return records $username = mysql_real_escape_string( $_POST[‘username’] ); $password = mysql_real_escape_string( $_POST[‘password’] ); $query = "SELECT * FROM users WHERE username = $username AND password=$password";
  • 8. Complete code A copy of complete code is available here https://github.com/prady00/php-security-essentials