SlideShare a Scribd company logo
1 of 43
Understanding SQL
BY: JOHN PATRICK LITA
INFORMATION SECURITY CONSULTANT – GLOBE TELECOM
ONLINE BANK
Email Address
Password
Log in
Our website is secure, almost never get hacked
ONLINE BANK
tcon@email.com
password
Log in
Our website is secure, almost never get hacked
Lets assume the
attacker trying to guess
what is the password of
tcon user
Unknown username and password
An error message
prompted
ONLINE BANK
tcon@email.com
password’
Log in
Our website is secure, almost never get hacked
By adding a single
quote (‘) at the end of
the password, lets see
the behavior of the
application
An unexpected Error occured
An error message
prompted
The logs show a
SQL Syntax error.
This indicates that
The quote character
Messed something
up
In an unexpected
way
SELECT *
FROM users
WHERE email = ‘?’
AND pass = ‘?’ LIMIT 1
This is what the
application code looks
like behind the scenes.
Let’s see how the SQL
code gets built as you
enter your login details
ONLINE BANK
tcon@email.com
password’
Log in
Our website is secure, almost never get hacked
An unexpected Error occurred
"'password'' limit 1" LINE 1: ...ers where email
= ‘tcon@email.com' and password =
'password'... ^ : select * from users where
email = ‘tcon@email.com' and password =
'password'' limit 1.Unable to login this user
due to unexpected error.
Unexpected Error
SELECT *
FROM users
WHERE email = ‘tcon@email.com’
AND pass = ‘password’’ LIMIT 1
This is what the
application code looks
like behind the scenes.
Let’s see how the SQL
code gets built as you
enter your login details
Authentication Bypass 1
ONLINE BANK
Email Address
Password
Log in
Our website is secure, almost never get hacked
Enter the following credentials and click “Log
In”:
Email:
tcon@email.com
Password:
‘ or 1=1--
Authentication Bypass 1
ONLINE BANK
tcon@email.com
‘ or 1=1--
Log in
Our website is secure, almost never get hacked
Rendering login page.
Checking supplied authentication details for
tcon@email.com.
Finding user in database.
Authentication details confirmed,
establishing session for this user.
Authentication Bypass 1
ONLINE BANK
Bank Accounts
Account Available Balance Present Balance
Checking PHP 80,260.56 PHP 80,260.56
SavingsPHP 95,895.96 PHP 95.895.96
Transfer Funds!
Authentication Bypass 1
SELECT *
FROM users
WHERE email = ‘tcon@email.com’
AND pass = ‘1 or 1=1--’ LIMIT 1
The - - characters you entered cause the database
to ignore the rest of the SQL statement, allowing
you to be authenticated without having to supply
the real password
Code walkthrough
HACKTHENORTH.ORG
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
To validate user credentials, the
request.getParamenter() method is
first called to extract tcon’s email
(from HTTP request parameter)
which is assigned to the email
variable
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
Similarly, the
request.getParameter() method is
called to extract tcon’s password
value and assigned to password
variable
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
The string variable sql is then declared
which represents the SQL query used to
authenticate tcon’s credentials.
Note, the tcon’s email and password value
are concatenated to build the final query
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
The SQL query in defined in sql string variable is then executed by invoking
the executeQuery method. This method executes our query against the
backend SQL server and returns a result as ResultSet object which is on
result.next through the if/else block
Finally, should tcon’s credentials match the loggedIn variable is set to true
and tcon is redirected to its profile page.
Simplify the Code
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
Code walkthrough
//string email = request.getParameter("Email");
//String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
//Connection connection = pool.getConnection();
//Statement statement = connection.createStatement();
//ResultSet result = statement.executeQuery(sql);
//if (result.next()) {
//LoggedIn = true;
// #Successfully logged in and redirect to user profile page
//} else {
// #Authentication failure - Redirect to login page
//}
Now that we have simplified our authentication logic code, lets analyze the
SQL query generated by Bank to validate tcon’s authentication credentials
Understanding Injection
Step 1: Enter the password tcon@email.com and watch the code widow.
Notice how the single quote (‘) you appended is interpreted by the SQL server
as a string delimiter.
However, when the query is processed, the last quote does not have a
closing/matching (‘) character, which cause a SQL Syntax Error, resulting in
the HTTP 500 Internal server Error
Step 2: Now try logging with a password followed by two single quotes e.g.
password’’ Interestingly the application does nor error in this case
Action:1
ONLINE BANK
Email Address
Password
Log in
Our website is secure, almost never get hacked
Action:1
ONLINE BANK
tcon@email.com
password’
Log in
Our website is secure, almost never get hacked
Action:1
ONLINE BANK
Our website is secure, almost never get hacked
Action:1
ONLINE BANK
Our website is secure, almost never get hacked
500 Internal Server Error.
The server encounter an internal error or misconfiguration and was
unable to complete your request. Please contact your handsome
administrator and inform them of the time the error occurred, and
anything might have done that may have caused the error.
Also by them a coffee or burger so that they will fix your problem
Action:2
ONLINE BANK
tcon@email.com
password’’
Log in
Our website is secure, almost never get hacked
Action:2
ONLINE BANK
Email
password
Log in
Our website is secure, almost never get hacked
ERROR: Invalid username and password
Bypassing Authentication
At this point we know that injecting characters interpreted by the database
server is known as SQL injection
How ever, it’s not just (‘) characters that can be injected, entire strings can be
injected, entire strings can be injected. What if this could be used to alter the
purpose of the SQL Statement Entirely?
Bypassing Authentication
ONLINE BANK
tcon@email.com
Password ‘ 1 or 1=1)#
Log in
Our website is secure, almost never get hacked
Note in MySQL the (#) character is used for
code comments. Keep an eye on the code,
everything to the right of the (#) character is
commented our, including the extra (‘) and )
character.
Note: the provided SQL payload is the one
must use
Bypassing Authentication
ONLINE BANK
Our website is secure, almost never get hacked
Bypassing Authentication
ONLINE BANK
Bank Accounts
Account Available Balance Present Balance
Checking PHP 80,260.56 PHP 80,260.56
SavingsPHP 95,895.96 PHP 95.895.96
Transfer Funds!
Authentication was bypass,
but why?
The credentials we provide on the login resulted in the following SQL
statement:
SELECT *
WHERE (email = ‘tcon@email.com’)
AND password = ‘ ‘ OR 1=1)#
Because the statement is both syntactically valid and OR 1=1 always return
true, the authentication mechanism was bypassed. Let’s now analyze the
Vulnerability from a code perspective
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
To quickly recap, the
request.getParameter() method is
first called to extract tcon’s
username and password values
which are assigned to the email
and password variable
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
The string variable sql is then declared, which
represents the SQL query used to authenticate
tcon’s credentials
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
Connection connection = pool.getConnection();
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
Notice that tcon’s email and password input (retrieved using the
request.getParameter() method) are concatenated within the variabl sql ,
without any input validation checks.
Input Validation
Because there is no input validation (i.e. no checking of legal characters,
minimum/maximum string lengths, or removal of “malicious” characters)
TCON has the ability to inject raw SQL syntax within the username and
password input fields to alter the meaning of the underlying SQL query
responsible for authentication, resulting in a bypass of the application’s
authentication mechanism
A SQL Injection Attack!
Remediation
HACKTHENORTH.ORG
Remediation
Prepared Statements (A.K.A parameterized queries) are the best mechanism for
preventing SQL injection attacks
Prepared statements are used to abstract SQL statement syntax from input
parameters. Statement templates are first defined at the application layer, and the
parameters are then passed to them
In java this can be achieved using PreparedStatement class for sending SQL
statements to the backend database
Aside from a better security posture against SQL injection attacks, prepared
statements offer improved code quality from a legibility and maintainability
perspective due to separation of SQL logic from inputs
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
//String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
String sql = “select * from users where email = ? and password = ?”;
Connection connection = pool.getConnection();
//Statement statement = connection.createStatement();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//ResultSet result = statement.executeQuery(sql);
preparedStatement.setString(1, email);
preparedStatement.setString(2, password);
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
In our modified code example, we first declared the authentication query
string and assign it to the sql string variable. Notice that the email and
password variable have now been replace with (?) symbol which act as a
place holder for java’s PreparedStatement class.
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
//String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
String sql = “select * from users where email = ? and password = ?”;
Connection connection = pool.getConnection();
//Statement statement = connection.createStatement();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//ResultSet result = statement.executeQuery(sql);
preparedStatement.setString(1, email);
preparedStatement.setString(2, password);
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
The sql string variable is then passed as an argument to the
preparedStatement() method, which precompiles the SQL query and creates
a PreparedStatement object for sending parameteized SQL statements to
the back end SQL Server
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
//String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
String sql = “select * from users where email = ? and password = ?”;
Connection connection = pool.getConnection();
//Statement statement = connection.createStatement();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//ResultSet result = statement.executeQuery(sql);
preparedStatement.setString(1, email);
preparedStatement.setString(2, password);
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
Similarly, the setString() method is then called to pass the password
parameter value to our prepared statement.
Code walkthrough
string email = request.getParameter("Email");
String password = request.getParameter("password");
//String sql = "select * from users where (email='" + email +"' and password='" + password +"')";
String sql = “select * from users where email = ? and password = ?”;
Connection connection = pool.getConnection();
//Statement statement = connection.createStatement();
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//ResultSet result = statement.executeQuery(sql);
preparedStatement.setString(1, email);
preparedStatement.setString(2, password);
ResultSet result = preparedStatement.executeQuery();
if (result.next()) {
LoggedIn = true;
#Successfully logged in and redirect to user profile page
} else {
#Authentication failure - Redirect to login page
}
Finally, we execute our authentication query by invoking the
preparedStatement.executeQuery() method. The SQL used by prepareStatement is
precompiled ensuring that all parameters sent to underlying database are treated as literal
value and not SQL statement/query language, ensuring that no SQL code can be injected
using an untrusted parameter.
Ultimately, the security payoff with using prepared statements is that the database will
ensure that parameters are automatically escaped
END OF THE SLIDE
HACKTHENORTH.ORG
References:
Hackplaining
OWASP Foundation
CodeBashing

More Related Content

Viewers also liked

IOT Security - ICCT College of Engineering
IOT Security - ICCT College of EngineeringIOT Security - ICCT College of Engineering
IOT Security - ICCT College of EngineeringPotato
 
Cyber Security - ICCT Colleges
Cyber Security - ICCT CollegesCyber Security - ICCT Colleges
Cyber Security - ICCT CollegesPotato
 
Understanding dom based xss
Understanding dom based xssUnderstanding dom based xss
Understanding dom based xssPotato
 
Understanding CSRF
Understanding CSRFUnderstanding CSRF
Understanding CSRFPotato
 
Web appsec and it’s 10 best SDLC practices
Web appsec and it’s 10 best SDLC practicesWeb appsec and it’s 10 best SDLC practices
Web appsec and it’s 10 best SDLC practicesPotato
 
CS101- Introduction to Computing- Lecture 42
CS101- Introduction to Computing- Lecture 42CS101- Introduction to Computing- Lecture 42
CS101- Introduction to Computing- Lecture 42Bilal Ahmed
 
Diskusi masalah regulator kuadratik untuk
Diskusi masalah regulator kuadratik untukDiskusi masalah regulator kuadratik untuk
Diskusi masalah regulator kuadratik untukrukmono budi utomo
 
Proyecto pedro zurita 7
Proyecto pedro zurita 7 Proyecto pedro zurita 7
Proyecto pedro zurita 7 negrita13hgft
 
Ayurveda for fall health
Ayurveda for fall healthAyurveda for fall health
Ayurveda for fall healthNeelam Toprani
 
Tugas Metode Numerik Pendidikan Matematika UMT
Tugas Metode Numerik Pendidikan Matematika UMTTugas Metode Numerik Pendidikan Matematika UMT
Tugas Metode Numerik Pendidikan Matematika UMTrukmono budi utomo
 
From Problems to Preventive Care
From Problems to Preventive CareFrom Problems to Preventive Care
From Problems to Preventive Carefaultyadult7689
 
CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04Bilal Ahmed
 
Bab10 pengelolaan server_jaringan
Bab10 pengelolaan server_jaringanBab10 pengelolaan server_jaringan
Bab10 pengelolaan server_jaringanAgung Sakepris
 
CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 18CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 18Bilal Ahmed
 

Viewers also liked (20)

IOT Security - ICCT College of Engineering
IOT Security - ICCT College of EngineeringIOT Security - ICCT College of Engineering
IOT Security - ICCT College of Engineering
 
Cyber Security - ICCT Colleges
Cyber Security - ICCT CollegesCyber Security - ICCT Colleges
Cyber Security - ICCT Colleges
 
Understanding dom based xss
Understanding dom based xssUnderstanding dom based xss
Understanding dom based xss
 
Understanding CSRF
Understanding CSRFUnderstanding CSRF
Understanding CSRF
 
Web appsec and it’s 10 best SDLC practices
Web appsec and it’s 10 best SDLC practicesWeb appsec and it’s 10 best SDLC practices
Web appsec and it’s 10 best SDLC practices
 
Writing clinic itb
Writing clinic itbWriting clinic itb
Writing clinic itb
 
CS101- Introduction to Computing- Lecture 42
CS101- Introduction to Computing- Lecture 42CS101- Introduction to Computing- Lecture 42
CS101- Introduction to Computing- Lecture 42
 
Diskusi masalah regulator kuadratik untuk
Diskusi masalah regulator kuadratik untukDiskusi masalah regulator kuadratik untuk
Diskusi masalah regulator kuadratik untuk
 
Ketaatan
KetaatanKetaatan
Ketaatan
 
Proyecto pedro zurita 7
Proyecto pedro zurita 7 Proyecto pedro zurita 7
Proyecto pedro zurita 7
 
Ayurveda for fall health
Ayurveda for fall healthAyurveda for fall health
Ayurveda for fall health
 
Marco,cabrera;proyectos;primer,parcial
Marco,cabrera;proyectos;primer,parcialMarco,cabrera;proyectos;primer,parcial
Marco,cabrera;proyectos;primer,parcial
 
Tugas Metode Numerik Pendidikan Matematika UMT
Tugas Metode Numerik Pendidikan Matematika UMTTugas Metode Numerik Pendidikan Matematika UMT
Tugas Metode Numerik Pendidikan Matematika UMT
 
Bab9 wan
Bab9 wanBab9 wan
Bab9 wan
 
From Problems to Preventive Care
From Problems to Preventive CareFrom Problems to Preventive Care
From Problems to Preventive Care
 
Uniformes oficial
Uniformes oficialUniformes oficial
Uniformes oficial
 
QCL-14-v3_[5S]_[SIIB]_[Sandeep Majumder]
QCL-14-v3_[5S]_[SIIB]_[Sandeep Majumder]QCL-14-v3_[5S]_[SIIB]_[Sandeep Majumder]
QCL-14-v3_[5S]_[SIIB]_[Sandeep Majumder]
 
CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04CS201- Introduction to Programming- Lecture 04
CS201- Introduction to Programming- Lecture 04
 
Bab10 pengelolaan server_jaringan
Bab10 pengelolaan server_jaringanBab10 pengelolaan server_jaringan
Bab10 pengelolaan server_jaringan
 
CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 18CS201- Introduction to Programming- Lecture 18
CS201- Introduction to Programming- Lecture 18
 

Similar to SQL Understanding

Php Security - OWASP
Php  Security - OWASPPhp  Security - OWASP
Php Security - OWASPMizno Kruge
 
Secure Code Warrior - Insufficient data encoding
Secure Code Warrior - Insufficient data encodingSecure Code Warrior - Insufficient data encoding
Secure Code Warrior - Insufficient data encodingSecure Code Warrior
 
Advanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection ProtectionAdvanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection Protectionamiable_indian
 
SQL Injections - 2016 - Huntington Beach
SQL Injections - 2016 - Huntington BeachSQL Injections - 2016 - Huntington Beach
SQL Injections - 2016 - Huntington BeachJeff Prom
 
Web security with Eng Ahmed Galal and Eng Ramy saeid
Web security with Eng Ahmed Galal and Eng Ramy saeid Web security with Eng Ahmed Galal and Eng Ramy saeid
Web security with Eng Ahmed Galal and Eng Ramy saeid Ahmed Ghazey
 
Chapter 14 sql injection
Chapter 14 sql injectionChapter 14 sql injection
Chapter 14 sql injectionnewbie2019
 
03. sql and other injection module v17
03. sql and other injection module v1703. sql and other injection module v17
03. sql and other injection module v17Eoin Keary
 
Code injection and green sql
Code injection and green sqlCode injection and green sql
Code injection and green sqlKaustav Sengupta
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injectionavishkarm
 
Top 10 Web Security Vulnerabilities
Top 10 Web Security VulnerabilitiesTop 10 Web Security Vulnerabilities
Top 10 Web Security VulnerabilitiesCarol McDonald
 
How to implement golang jwt authentication and authorization
How to implement golang jwt authentication and authorizationHow to implement golang jwt authentication and authorization
How to implement golang jwt authentication and authorizationKaty Slemon
 
Ppt on sql injection
Ppt on sql injectionPpt on sql injection
Ppt on sql injectionashish20012
 
How did i steal your database
How did i steal your databaseHow did i steal your database
How did i steal your databaseMostafa Siraj
 

Similar to SQL Understanding (20)

Php Security - OWASP
Php  Security - OWASPPhp  Security - OWASP
Php Security - OWASP
 
Secure Code Warrior - Insufficient data encoding
Secure Code Warrior - Insufficient data encodingSecure Code Warrior - Insufficient data encoding
Secure Code Warrior - Insufficient data encoding
 
Advanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection ProtectionAdvanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection Protection
 
Sql injection
Sql injectionSql injection
Sql injection
 
SQL Injections - 2016 - Huntington Beach
SQL Injections - 2016 - Huntington BeachSQL Injections - 2016 - Huntington Beach
SQL Injections - 2016 - Huntington Beach
 
Sql injection
Sql injectionSql injection
Sql injection
 
Sql injection
Sql injectionSql injection
Sql injection
 
Web security with Eng Ahmed Galal and Eng Ramy saeid
Web security with Eng Ahmed Galal and Eng Ramy saeid Web security with Eng Ahmed Galal and Eng Ramy saeid
Web security with Eng Ahmed Galal and Eng Ramy saeid
 
Chapter 14 sql injection
Chapter 14 sql injectionChapter 14 sql injection
Chapter 14 sql injection
 
03. sql and other injection module v17
03. sql and other injection module v1703. sql and other injection module v17
03. sql and other injection module v17
 
Code injection and green sql
Code injection and green sqlCode injection and green sql
Code injection and green sql
 
Greensql2007
Greensql2007Greensql2007
Greensql2007
 
Web Security
Web SecurityWeb Security
Web Security
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injection
 
Code injection
Code injectionCode injection
Code injection
 
Top 10 Web Security Vulnerabilities
Top 10 Web Security VulnerabilitiesTop 10 Web Security Vulnerabilities
Top 10 Web Security Vulnerabilities
 
How to implement golang jwt authentication and authorization
How to implement golang jwt authentication and authorizationHow to implement golang jwt authentication and authorization
How to implement golang jwt authentication and authorization
 
Ppt on sql injection
Ppt on sql injectionPpt on sql injection
Ppt on sql injection
 
How did i steal your database
How did i steal your databaseHow did i steal your database
How did i steal your database
 
Sql injection
Sql injectionSql injection
Sql injection
 

Recently uploaded

OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingShane Coughlan
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slidesvaideheekore1
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shardsChristopher Curtin
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 

Recently uploaded (20)

OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full RecordingOpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
OpenChain Education Work Group Monthly Meeting - 2024-04-10 - Full Recording
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 
Introduction to Firebase Workshop Slides
Introduction to Firebase Workshop SlidesIntroduction to Firebase Workshop Slides
Introduction to Firebase Workshop Slides
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards2024 DevNexus Patterns for Resiliency: Shuffle shards
2024 DevNexus Patterns for Resiliency: Shuffle shards
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 

SQL Understanding

  • 1. Understanding SQL BY: JOHN PATRICK LITA INFORMATION SECURITY CONSULTANT – GLOBE TELECOM
  • 2. ONLINE BANK Email Address Password Log in Our website is secure, almost never get hacked
  • 3. ONLINE BANK tcon@email.com password Log in Our website is secure, almost never get hacked Lets assume the attacker trying to guess what is the password of tcon user Unknown username and password An error message prompted
  • 4. ONLINE BANK tcon@email.com password’ Log in Our website is secure, almost never get hacked By adding a single quote (‘) at the end of the password, lets see the behavior of the application An unexpected Error occured An error message prompted The logs show a SQL Syntax error. This indicates that The quote character Messed something up In an unexpected way
  • 5. SELECT * FROM users WHERE email = ‘?’ AND pass = ‘?’ LIMIT 1 This is what the application code looks like behind the scenes. Let’s see how the SQL code gets built as you enter your login details
  • 6. ONLINE BANK tcon@email.com password’ Log in Our website is secure, almost never get hacked An unexpected Error occurred "'password'' limit 1" LINE 1: ...ers where email = ‘tcon@email.com' and password = 'password'... ^ : select * from users where email = ‘tcon@email.com' and password = 'password'' limit 1.Unable to login this user due to unexpected error.
  • 7. Unexpected Error SELECT * FROM users WHERE email = ‘tcon@email.com’ AND pass = ‘password’’ LIMIT 1 This is what the application code looks like behind the scenes. Let’s see how the SQL code gets built as you enter your login details
  • 8. Authentication Bypass 1 ONLINE BANK Email Address Password Log in Our website is secure, almost never get hacked Enter the following credentials and click “Log In”: Email: tcon@email.com Password: ‘ or 1=1--
  • 9. Authentication Bypass 1 ONLINE BANK tcon@email.com ‘ or 1=1-- Log in Our website is secure, almost never get hacked Rendering login page. Checking supplied authentication details for tcon@email.com. Finding user in database. Authentication details confirmed, establishing session for this user.
  • 10. Authentication Bypass 1 ONLINE BANK Bank Accounts Account Available Balance Present Balance Checking PHP 80,260.56 PHP 80,260.56 SavingsPHP 95,895.96 PHP 95.895.96 Transfer Funds!
  • 11. Authentication Bypass 1 SELECT * FROM users WHERE email = ‘tcon@email.com’ AND pass = ‘1 or 1=1--’ LIMIT 1 The - - characters you entered cause the database to ignore the rest of the SQL statement, allowing you to be authenticated without having to supply the real password
  • 13. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page }
  • 14. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } To validate user credentials, the request.getParamenter() method is first called to extract tcon’s email (from HTTP request parameter) which is assigned to the email variable
  • 15. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } Similarly, the request.getParameter() method is called to extract tcon’s password value and assigned to password variable
  • 16. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } The string variable sql is then declared which represents the SQL query used to authenticate tcon’s credentials. Note, the tcon’s email and password value are concatenated to build the final query
  • 17. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } The SQL query in defined in sql string variable is then executed by invoking the executeQuery method. This method executes our query against the backend SQL server and returns a result as ResultSet object which is on result.next through the if/else block Finally, should tcon’s credentials match the loggedIn variable is set to true and tcon is redirected to its profile page.
  • 19. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page }
  • 20. Code walkthrough //string email = request.getParameter("Email"); //String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; //Connection connection = pool.getConnection(); //Statement statement = connection.createStatement(); //ResultSet result = statement.executeQuery(sql); //if (result.next()) { //LoggedIn = true; // #Successfully logged in and redirect to user profile page //} else { // #Authentication failure - Redirect to login page //} Now that we have simplified our authentication logic code, lets analyze the SQL query generated by Bank to validate tcon’s authentication credentials
  • 21. Understanding Injection Step 1: Enter the password tcon@email.com and watch the code widow. Notice how the single quote (‘) you appended is interpreted by the SQL server as a string delimiter. However, when the query is processed, the last quote does not have a closing/matching (‘) character, which cause a SQL Syntax Error, resulting in the HTTP 500 Internal server Error Step 2: Now try logging with a password followed by two single quotes e.g. password’’ Interestingly the application does nor error in this case
  • 22. Action:1 ONLINE BANK Email Address Password Log in Our website is secure, almost never get hacked
  • 23. Action:1 ONLINE BANK tcon@email.com password’ Log in Our website is secure, almost never get hacked
  • 24. Action:1 ONLINE BANK Our website is secure, almost never get hacked
  • 25. Action:1 ONLINE BANK Our website is secure, almost never get hacked 500 Internal Server Error. The server encounter an internal error or misconfiguration and was unable to complete your request. Please contact your handsome administrator and inform them of the time the error occurred, and anything might have done that may have caused the error. Also by them a coffee or burger so that they will fix your problem
  • 26. Action:2 ONLINE BANK tcon@email.com password’’ Log in Our website is secure, almost never get hacked
  • 27. Action:2 ONLINE BANK Email password Log in Our website is secure, almost never get hacked ERROR: Invalid username and password
  • 28. Bypassing Authentication At this point we know that injecting characters interpreted by the database server is known as SQL injection How ever, it’s not just (‘) characters that can be injected, entire strings can be injected, entire strings can be injected. What if this could be used to alter the purpose of the SQL Statement Entirely?
  • 29. Bypassing Authentication ONLINE BANK tcon@email.com Password ‘ 1 or 1=1)# Log in Our website is secure, almost never get hacked Note in MySQL the (#) character is used for code comments. Keep an eye on the code, everything to the right of the (#) character is commented our, including the extra (‘) and ) character. Note: the provided SQL payload is the one must use
  • 30. Bypassing Authentication ONLINE BANK Our website is secure, almost never get hacked
  • 31. Bypassing Authentication ONLINE BANK Bank Accounts Account Available Balance Present Balance Checking PHP 80,260.56 PHP 80,260.56 SavingsPHP 95,895.96 PHP 95.895.96 Transfer Funds!
  • 32. Authentication was bypass, but why? The credentials we provide on the login resulted in the following SQL statement: SELECT * WHERE (email = ‘tcon@email.com’) AND password = ‘ ‘ OR 1=1)# Because the statement is both syntactically valid and OR 1=1 always return true, the authentication mechanism was bypassed. Let’s now analyze the Vulnerability from a code perspective
  • 33. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } To quickly recap, the request.getParameter() method is first called to extract tcon’s username and password values which are assigned to the email and password variable
  • 34. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } The string variable sql is then declared, which represents the SQL query used to authenticate tcon’s credentials
  • 35. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; Connection connection = pool.getConnection(); Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(sql); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } Notice that tcon’s email and password input (retrieved using the request.getParameter() method) are concatenated within the variabl sql , without any input validation checks.
  • 36. Input Validation Because there is no input validation (i.e. no checking of legal characters, minimum/maximum string lengths, or removal of “malicious” characters) TCON has the ability to inject raw SQL syntax within the username and password input fields to alter the meaning of the underlying SQL query responsible for authentication, resulting in a bypass of the application’s authentication mechanism A SQL Injection Attack!
  • 38. Remediation Prepared Statements (A.K.A parameterized queries) are the best mechanism for preventing SQL injection attacks Prepared statements are used to abstract SQL statement syntax from input parameters. Statement templates are first defined at the application layer, and the parameters are then passed to them In java this can be achieved using PreparedStatement class for sending SQL statements to the backend database Aside from a better security posture against SQL injection attacks, prepared statements offer improved code quality from a legibility and maintainability perspective due to separation of SQL logic from inputs
  • 39. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); //String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; String sql = “select * from users where email = ? and password = ?”; Connection connection = pool.getConnection(); //Statement statement = connection.createStatement(); PreparedStatement preparedStatement = connection.prepareStatement(sql); //ResultSet result = statement.executeQuery(sql); preparedStatement.setString(1, email); preparedStatement.setString(2, password); ResultSet result = preparedStatement.executeQuery(); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } In our modified code example, we first declared the authentication query string and assign it to the sql string variable. Notice that the email and password variable have now been replace with (?) symbol which act as a place holder for java’s PreparedStatement class.
  • 40. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); //String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; String sql = “select * from users where email = ? and password = ?”; Connection connection = pool.getConnection(); //Statement statement = connection.createStatement(); PreparedStatement preparedStatement = connection.prepareStatement(sql); //ResultSet result = statement.executeQuery(sql); preparedStatement.setString(1, email); preparedStatement.setString(2, password); ResultSet result = preparedStatement.executeQuery(); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } The sql string variable is then passed as an argument to the preparedStatement() method, which precompiles the SQL query and creates a PreparedStatement object for sending parameteized SQL statements to the back end SQL Server
  • 41. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); //String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; String sql = “select * from users where email = ? and password = ?”; Connection connection = pool.getConnection(); //Statement statement = connection.createStatement(); PreparedStatement preparedStatement = connection.prepareStatement(sql); //ResultSet result = statement.executeQuery(sql); preparedStatement.setString(1, email); preparedStatement.setString(2, password); ResultSet result = preparedStatement.executeQuery(); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } Similarly, the setString() method is then called to pass the password parameter value to our prepared statement.
  • 42. Code walkthrough string email = request.getParameter("Email"); String password = request.getParameter("password"); //String sql = "select * from users where (email='" + email +"' and password='" + password +"')"; String sql = “select * from users where email = ? and password = ?”; Connection connection = pool.getConnection(); //Statement statement = connection.createStatement(); PreparedStatement preparedStatement = connection.prepareStatement(sql); //ResultSet result = statement.executeQuery(sql); preparedStatement.setString(1, email); preparedStatement.setString(2, password); ResultSet result = preparedStatement.executeQuery(); if (result.next()) { LoggedIn = true; #Successfully logged in and redirect to user profile page } else { #Authentication failure - Redirect to login page } Finally, we execute our authentication query by invoking the preparedStatement.executeQuery() method. The SQL used by prepareStatement is precompiled ensuring that all parameters sent to underlying database are treated as literal value and not SQL statement/query language, ensuring that no SQL code can be injected using an untrusted parameter. Ultimately, the security payoff with using prepared statements is that the database will ensure that parameters are automatically escaped
  • 43. END OF THE SLIDE HACKTHENORTH.ORG References: Hackplaining OWASP Foundation CodeBashing

Editor's Notes

  1. The Application crash unexpected! What does it mean?
  2. The quote is inserted directly into the SQL string , and terminates the query early, this is what caused The syntax error we saw in the logs. This behavior indicates that the application might be vulnerable To SQL injection
  3. And we are in! we successfully gained access to the application having to guess the password, using SQL Injection.
  4. And we are in! we successfully gained access to the application having to guess the password, using SQL Injection.