SlideShare a Scribd company logo
1 of 4
Download to read offline
SQL Injection:

There are two types of SQL Injections :

1- Plain SQL Injection: The web page accepts data from a client and executes SQL
queries without first validating the client’s input. The attacker is then free to extract,
modify, add, or delete content from the database.

Hackers typically test for SQL injection vulnerabilities by sending the application
input that would cause the server to generate an invalid SQL query. If the server
then returns an error message to the client, the attacker will attempt to revers e-
engineer portions of the original SQL query using information gained from these
error messages. The typical administrative safeguard is simply to prohibit the display
of database server error messages.

2- Blind SQL Injection:

Consider the following SQL query in PHP:


$result=mysql_query('SELECT * FROM users WHERE
username="'.$_GET['username'].'"') ;

But what would happen if $_GET['username'] was the following:


" OR 1=1 OR username=”

The corresponding statement will be like this:


SELECT * FROM users WHERE username = "" OR 1=1 OR username = ""

This selects all rows from the users table.

Solution:

Never trust user provided data, process this data only after validation; as a rule, t his
is done by pattern matching:


if (preg_match("/^w{8,20}$/", $_GET['username'], $matches))
   $result = mysql_query("SELECT * FROM users WHERE
username=$matches[0] ");
else
   echo "username not accepted";

Every parameter of every script on the server should always be checked.
If attacker wants to Login without entering password:

Username: osama’ --


SELECT * FROM members WHERE username = 'osama'--' AND password = 'password'

In this way, attacker may execute any SQL command. If he enter a password as
follow ing:


osama’; DELETE FROM users;

and PHP code was:


$name = $_POST[„password‟];
mysql_query(“SELECT * FROM users WHERE name=‟ {$name}‟”);

then he can delete all users from users table !!!

Fortunately, if you use MySQL, the mysql_que ry() function does not permit query
stacking, or executing multiple queries in a single function call. If you try to stack
queries, the call fails. Unlike SQLite and PostgreSQL.

In PHP, there is many different ways to avoid such injections:

   Using magic_quotes_gpc() function which escapes all ' (single-quote), "
    (double quote),  (backslash) and NULL's with a backslash automatically.
    Although it escape undesirable characters, it is not good to rely on this feature:


                                       Warning

     This feature has been DEPRECATED as of PHP 5.3.0
     and REMOVED as of PHP 6.0.0. Relying on this feature is highly
     discouraged.
   Using customized escape mechanisms: mysql_real_escape _string() Which
      escapes special characters in a string for use in a SQL statement.


if (get_magic_quotes_gpc()) {
      $name = stripslashes($name);
}
$name = mysql_real_escape_string($name);
mysql_query(“SELECT * FROM users WHERE name=‟ {$name}‟”);

      Note: mysql_real_escape_string() does not escape % and _. These are wildcards in
      MySQL if combined with LIKE, GRANT, or REVOKE.
     Using addslashes() function which quote string with slashes


$str = "Is your name O'reilly?";

// Outputs: Is your name O'reilly?
echo addslashes($str);

      Or you can combine addslashes() w ith ma gic_quotes_gpc () :


$sub = addcslashes(mysql_real_escape_string(“%something_”),
“%_”);
// $sub == %something_
mysql_query(“SELECT * FROM messages WHERE subject LIKE
„{$sub}%‟”);

     Using urlencode() :This function is convenient when encoding a string to be
      used in a query part of a URL, as a convenient way to pass variables to the
      next page.




$url = urlencode('?page=10');

// Outputs: %3Fpage%3D10
echo $url;
Gene ral advise about Security:

      Never connect to the database as a superuser or as the database owner. Use
       always customized users w ith very limited privileges

      Check if the given input has the expected data type.

      If the application waits for numerical input, consider verifying data with
       is_nume ric(), or silently change its type using settype(), or use its numeric
       representation by sprintf()

      Quote each non numeric user supplied value that is passed to the database
       with the database-specific string escape function (e.g.
       mysql_real_escape_string(), sqlite _escape_string(), etc.). If a
       database-specific string escape mechanism is not available, the
       addslashes() and str_replace() functions may be useful (depending on
       database type).

Additionally, attacker may not reply on SQL Injection only, he may ask a millions of
request to the server, which cause down to the server or stop responding.
Extra danger will exists if site allow visitors to upload files, then user can upload a
script that execute some commands, and request this command from browser.
Admin should set folder permissions in safe way to prevent some one from Directory
Traversal.

More Related Content

What's hot

Practical Approach towards SQLi ppt
Practical Approach towards SQLi pptPractical Approach towards SQLi ppt
Practical Approach towards SQLi pptAhamed Saleem
 
seminar report on Sql injection
seminar report on Sql injectionseminar report on Sql injection
seminar report on Sql injectionJawhar Ali
 
XSS And SQL Injection Vulnerabilities
XSS And SQL Injection VulnerabilitiesXSS And SQL Injection Vulnerabilities
XSS And SQL Injection VulnerabilitiesMindfire Solutions
 
SQL Injections - A Powerpoint Presentation
SQL Injections - A Powerpoint PresentationSQL Injections - A Powerpoint Presentation
SQL Injections - A Powerpoint PresentationRapid Purple
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL InjectionVortana Say
 
Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)John Pham
 
Sql injection
Sql injectionSql injection
Sql injectionZidh
 
SQL Injection attack
SQL Injection attackSQL Injection attack
SQL Injection attackRayudu Babu
 
Appreciative Advanced Blind SQLI Attack
Appreciative Advanced Blind SQLI AttackAppreciative Advanced Blind SQLI Attack
Appreciative Advanced Blind SQLI Attackijtsrd
 
Union based sql injection by Urdu Tutorials Point
Union based sql injection by Urdu Tutorials PointUnion based sql injection by Urdu Tutorials Point
Union based sql injection by Urdu Tutorials PointAl Zarqali
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTIONAnoop T
 
Database presentation
Database presentationDatabase presentation
Database presentationwebhostingguy
 
PHP - Getting good with MySQL part II
 PHP - Getting good with MySQL part II PHP - Getting good with MySQL part II
PHP - Getting good with MySQL part IIFirdaus Adib
 
PyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialPyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialjbellis
 

What's hot (20)

Practical Approach towards SQLi ppt
Practical Approach towards SQLi pptPractical Approach towards SQLi ppt
Practical Approach towards SQLi ppt
 
SQL Injections (Part 1)
SQL Injections (Part 1)SQL Injections (Part 1)
SQL Injections (Part 1)
 
seminar report on Sql injection
seminar report on Sql injectionseminar report on Sql injection
seminar report on Sql injection
 
Sql injection
Sql injectionSql injection
Sql injection
 
XSS And SQL Injection Vulnerabilities
XSS And SQL Injection VulnerabilitiesXSS And SQL Injection Vulnerabilities
XSS And SQL Injection Vulnerabilities
 
Sql injection
Sql injectionSql injection
Sql injection
 
SQL Injections - A Powerpoint Presentation
SQL Injections - A Powerpoint PresentationSQL Injections - A Powerpoint Presentation
SQL Injections - A Powerpoint Presentation
 
Web Security: SQL Injection
Web Security: SQL InjectionWeb Security: SQL Injection
Web Security: SQL Injection
 
Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)Eureka - elegant iOs form builder in swift (johnp - mingle)
Eureka - elegant iOs form builder in swift (johnp - mingle)
 
Sql injection
Sql injectionSql injection
Sql injection
 
SQL Injection attack
SQL Injection attackSQL Injection attack
SQL Injection attack
 
Appreciative Advanced Blind SQLI Attack
Appreciative Advanced Blind SQLI AttackAppreciative Advanced Blind SQLI Attack
Appreciative Advanced Blind SQLI Attack
 
SQL Injection
SQL InjectionSQL Injection
SQL Injection
 
Union based sql injection by Urdu Tutorials Point
Union based sql injection by Urdu Tutorials PointUnion based sql injection by Urdu Tutorials Point
Union based sql injection by Urdu Tutorials Point
 
Jdbc tutorial
Jdbc tutorialJdbc tutorial
Jdbc tutorial
 
SQL INJECTION
SQL INJECTIONSQL INJECTION
SQL INJECTION
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
PHP - Getting good with MySQL part II
 PHP - Getting good with MySQL part II PHP - Getting good with MySQL part II
PHP - Getting good with MySQL part II
 
Jdbc ja
Jdbc jaJdbc ja
Jdbc ja
 
PyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialPyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorial
 

Viewers also liked

netgeneracio_2010_pek_HUN.
 netgeneracio_2010_pek_HUN. netgeneracio_2010_pek_HUN.
netgeneracio_2010_pek_HUN.juditmacska
 
Building a list of companies in one source bsm_israel
Building a list of companies in one source bsm_israelBuilding a list of companies in one source bsm_israel
Building a list of companies in one source bsm_israelrajenmunoo
 
netgeneracio_2010_pek_HUN.
 netgeneracio_2010_pek_HUN. netgeneracio_2010_pek_HUN.
netgeneracio_2010_pek_HUN.juditmacska
 
Pedagogy Logistics Mangement School
Pedagogy Logistics Mangement SchoolPedagogy Logistics Mangement School
Pedagogy Logistics Mangement Schoolrafiqul1969
 
World Studies Unit 11 Game
World Studies Unit 11 GameWorld Studies Unit 11 Game
World Studies Unit 11 Gameguestf484e2
 

Viewers also liked (6)

netgeneracio_2010_pek_HUN.
 netgeneracio_2010_pek_HUN. netgeneracio_2010_pek_HUN.
netgeneracio_2010_pek_HUN.
 
Building a list of companies in one source bsm_israel
Building a list of companies in one source bsm_israelBuilding a list of companies in one source bsm_israel
Building a list of companies in one source bsm_israel
 
netgeneracio_2010_pek_HUN.
 netgeneracio_2010_pek_HUN. netgeneracio_2010_pek_HUN.
netgeneracio_2010_pek_HUN.
 
Pedagogy Logistics Mangement School
Pedagogy Logistics Mangement SchoolPedagogy Logistics Mangement School
Pedagogy Logistics Mangement School
 
World Studies Unit 11 Game
World Studies Unit 11 GameWorld Studies Unit 11 Game
World Studies Unit 11 Game
 
Zuzi Bab Ii
Zuzi Bab IiZuzi Bab Ii
Zuzi Bab Ii
 

Similar to A Brief Introduction About Sql Injection in PHP and MYSQL

SQL Injection in PHP
SQL Injection in PHPSQL Injection in PHP
SQL Injection in PHPDave Ross
 
SQL Injection in action with PHP and MySQL
SQL Injection in action with PHP and MySQLSQL Injection in action with PHP and MySQL
SQL Injection in action with PHP and MySQLPradeep Kumar
 
Php Security - OWASP
Php  Security - OWASPPhp  Security - OWASP
Php Security - OWASPMizno Kruge
 
Advanced Sql Injection ENG
Advanced Sql Injection ENGAdvanced Sql Injection ENG
Advanced Sql Injection ENGDmitry Evteev
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injectionavishkarm
 
Sql Injection Attacks(Part1 4)
Sql Injection Attacks(Part1 4)Sql Injection Attacks(Part1 4)
Sql Injection Attacks(Part1 4)Hongyang Wang
 
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)Grand Parade Poland
 
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)SqliChema Alonso
 
Types of sql injection attacks
Types of sql injection attacksTypes of sql injection attacks
Types of sql injection attacksRespa Peter
 
Understanding and preventing sql injection attacks
Understanding and preventing sql injection attacksUnderstanding and preventing sql injection attacks
Understanding and preventing sql injection attacksKevin Kline
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionSina Manavi
 
Web app development_crud_13
Web app development_crud_13Web app development_crud_13
Web app development_crud_13Hassen Poreya
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erickokelloerick
 
Advanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection ProtectionAdvanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection Protectionamiable_indian
 

Similar to A Brief Introduction About Sql Injection in PHP and MYSQL (20)

SQL Injection in PHP
SQL Injection in PHPSQL Injection in PHP
SQL Injection in PHP
 
SQL Injection in action with PHP and MySQL
SQL Injection in action with PHP and MySQLSQL Injection in action with PHP and MySQL
SQL Injection in action with PHP and MySQL
 
Php Security - OWASP
Php  Security - OWASPPhp  Security - OWASP
Php Security - OWASP
 
SQL Injection Attacks
SQL Injection AttacksSQL Injection Attacks
SQL Injection Attacks
 
Advanced Sql Injection ENG
Advanced Sql Injection ENGAdvanced Sql Injection ENG
Advanced Sql Injection ENG
 
D:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql InjectionD:\Technical\Ppt\Sql Injection
D:\Technical\Ppt\Sql Injection
 
Sql Injection Attacks(Part1 4)
Sql Injection Attacks(Part1 4)Sql Injection Attacks(Part1 4)
Sql Injection Attacks(Part1 4)
 
Sql injection
Sql injectionSql injection
Sql injection
 
Sql Injection Myths and Fallacies
Sql Injection Myths and FallaciesSql Injection Myths and Fallacies
Sql Injection Myths and Fallacies
 
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)
Pawel Cygal - SQL Injection and XSS - Basics (Quality Questions Conference)
 
Playing With (B)Sqli
Playing With (B)SqliPlaying With (B)Sqli
Playing With (B)Sqli
 
Types of sql injection attacks
Types of sql injection attacksTypes of sql injection attacks
Types of sql injection attacks
 
Web Security 101
Web Security 101Web Security 101
Web Security 101
 
Understanding and preventing sql injection attacks
Understanding and preventing sql injection attacksUnderstanding and preventing sql injection attacks
Understanding and preventing sql injection attacks
 
A Brief Introduction in SQL Injection
A Brief Introduction in SQL InjectionA Brief Introduction in SQL Injection
A Brief Introduction in SQL Injection
 
Sql injection
Sql injectionSql injection
Sql injection
 
Web app development_crud_13
Web app development_crud_13Web app development_crud_13
Web app development_crud_13
 
Sql injection
Sql injectionSql injection
Sql injection
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
 
Advanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection ProtectionAdvanced Topics On Sql Injection Protection
Advanced Topics On Sql Injection Protection
 

Recently uploaded

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Recently uploaded (20)

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

A Brief Introduction About Sql Injection in PHP and MYSQL

  • 1. SQL Injection: There are two types of SQL Injections : 1- Plain SQL Injection: The web page accepts data from a client and executes SQL queries without first validating the client’s input. The attacker is then free to extract, modify, add, or delete content from the database. Hackers typically test for SQL injection vulnerabilities by sending the application input that would cause the server to generate an invalid SQL query. If the server then returns an error message to the client, the attacker will attempt to revers e- engineer portions of the original SQL query using information gained from these error messages. The typical administrative safeguard is simply to prohibit the display of database server error messages. 2- Blind SQL Injection: Consider the following SQL query in PHP: $result=mysql_query('SELECT * FROM users WHERE username="'.$_GET['username'].'"') ; But what would happen if $_GET['username'] was the following: " OR 1=1 OR username=” The corresponding statement will be like this: SELECT * FROM users WHERE username = "" OR 1=1 OR username = "" This selects all rows from the users table. Solution: Never trust user provided data, process this data only after validation; as a rule, t his is done by pattern matching: if (preg_match("/^w{8,20}$/", $_GET['username'], $matches)) $result = mysql_query("SELECT * FROM users WHERE username=$matches[0] "); else echo "username not accepted"; Every parameter of every script on the server should always be checked.
  • 2. If attacker wants to Login without entering password: Username: osama’ -- SELECT * FROM members WHERE username = 'osama'--' AND password = 'password' In this way, attacker may execute any SQL command. If he enter a password as follow ing: osama’; DELETE FROM users; and PHP code was: $name = $_POST[„password‟]; mysql_query(“SELECT * FROM users WHERE name=‟ {$name}‟”); then he can delete all users from users table !!! Fortunately, if you use MySQL, the mysql_que ry() function does not permit query stacking, or executing multiple queries in a single function call. If you try to stack queries, the call fails. Unlike SQLite and PostgreSQL. In PHP, there is many different ways to avoid such injections:  Using magic_quotes_gpc() function which escapes all ' (single-quote), " (double quote), (backslash) and NULL's with a backslash automatically. Although it escape undesirable characters, it is not good to rely on this feature: Warning This feature has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.
  • 3. Using customized escape mechanisms: mysql_real_escape _string() Which escapes special characters in a string for use in a SQL statement. if (get_magic_quotes_gpc()) { $name = stripslashes($name); } $name = mysql_real_escape_string($name); mysql_query(“SELECT * FROM users WHERE name=‟ {$name}‟”); Note: mysql_real_escape_string() does not escape % and _. These are wildcards in MySQL if combined with LIKE, GRANT, or REVOKE.  Using addslashes() function which quote string with slashes $str = "Is your name O'reilly?"; // Outputs: Is your name O'reilly? echo addslashes($str); Or you can combine addslashes() w ith ma gic_quotes_gpc () : $sub = addcslashes(mysql_real_escape_string(“%something_”), “%_”); // $sub == %something_ mysql_query(“SELECT * FROM messages WHERE subject LIKE „{$sub}%‟”);  Using urlencode() :This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page. $url = urlencode('?page=10'); // Outputs: %3Fpage%3D10 echo $url;
  • 4. Gene ral advise about Security:  Never connect to the database as a superuser or as the database owner. Use always customized users w ith very limited privileges  Check if the given input has the expected data type.  If the application waits for numerical input, consider verifying data with is_nume ric(), or silently change its type using settype(), or use its numeric representation by sprintf()  Quote each non numeric user supplied value that is passed to the database with the database-specific string escape function (e.g. mysql_real_escape_string(), sqlite _escape_string(), etc.). If a database-specific string escape mechanism is not available, the addslashes() and str_replace() functions may be useful (depending on database type). Additionally, attacker may not reply on SQL Injection only, he may ask a millions of request to the server, which cause down to the server or stop responding. Extra danger will exists if site allow visitors to upload files, then user can upload a script that execute some commands, and request this command from browser. Admin should set folder permissions in safe way to prevent some one from Directory Traversal.