SlideShare a Scribd company logo
Are You Sure Your Site Is Secure?

                                        Security 202




Confoo 2011 Edition
By Arne Blankerts, thePHP.cc
What is this talk about?

                  Myths in web security
                  Broken configurations
                  Typical implementation
                   issues
Session data




  “I can always trust my session data
      since I know what I did store”
Session data

[theseer@rikka ~] $ grep "session.save_path" /etc/php.ini | grep -v ";"

session.save_path = "/var/lib/php/session"



    Identical for all php instances unless
     specifically overwritten
        Read and write access from php code
    May be crafted in shared hosting
        Session-id takeover from vhost to vhost
        Session-Content can be modified
        Can even lead to code execution
Session hijacking




   “To protect my users from session
      hijacking, I did implement a
           validation check”
Session hijacking

    session.php
01   <?php
02   session_start();
03   $success = true;
04   if (($_SESSION['IP'] != $_SERVER['REMOTE_ADDR'])
05      or ($_SESSION['VIA'] != $_SERVER['HTTP_VIA'])
06      or ($_SESSION['FORWARD'] != $_SERVER['HTTP_X_FORWARDED_FOR'])
07      or ($_SESSION['AGENT'] != $_SERVER['HTTP_USER_AGENT'])) {
08   // ...
09   }
Session hijacking – what to do?

   Determine if hijacking is a problem
   Regenerate id on every request
       Doesn't block it but makes it harder to exploit
   Fully switch to https for transport
       Alternatively use a separate id in ssl context
Cross Site Request Forgery




 “I have an anti CSRF token in my forms
        – So I'm well protected”
CSRF

   csrftoken.php
    01   <?php
    02
    03   session_start();
    04   $_SESSION['CSRF']=md5(time());
    05
    06   //...


   validate.php
    01   <?php
    02
    03   session_start();
    04   if ($_SESSION['CSRF']==$_GET['CSRF']) {
    05      // ...
    06   }
CSRF

   Regenerate token for every form?
       Do you keep a backlog of tokens?


   Do you validate your session?
       Session fixation may violate CSRF tokens


   What do you base the token on?
CAPTCHA




  “I'm using a captcha to protect my
   forms from abuse – So I'm save.”
CAPTCHA

   Conceptual Problems
       Distortion often unreadable
       Not the least bit accessible


   Breaking can be “crowd sourced”


   Implementation issues
CAPTCHA

   captcha.php
     01   <?php
     02   session_start();
     03   require 'captchaHelper.php';
     04
     05   $code = generateCaptchaCode();
     06   $_SESSION['CAPTCHA'] = $code;
     07
     08   header('Content-type: image/jpeg');
     09   echo createCaptchaImage($code);

   validation.php
     01   <?php
     02   session_start();
     03
     04   if ($_SESSION['CAPTCHA'] != $_REQUEST['code']) {
     05      die('Captcha value wrong');
     06   }
     07   echo 'Welcome!';
Prepared Statements




    “I'm using prepared statements
 so I'm protected from sql injections”
Prepared Statements

01   <?php
02
03   $db = new PDO(....);
04   $query = $db->prepare('SELECT ... WHERE NAME=:name');
05   $query->bindParam(':name', $_GET['name']);
06
07   //...
Prepared Statements

   What about fieldnames?
   Variable table names?
   Do you sort your results?
   Any need for limits?


   Still use ext/mysql?
       Sprintf based implementations?
Drawbacks of sprintf

   Manual escaping needed
    
        mysql_escape_string vs. mysql_real_escape_string
   PDO::quote() does not work with ODBC
   No knowledge of fieldtype
       String vs. Integer exploits
       PDO::quote vs. mysql(i)_real_escape_string
Password storage



 “I know storing clear text passwords
    is a bad idea. That's why I'm only
    storing hashes of passwords to
            protect my users.”
Password storage

01   <?php
02
03   $db = new PDO(....);
04   $query = $db->prepare(
05      'UPDATE user SET PASSWD=:pwd WHERE UID=:uid'
06   );
07   $query->bindParam(':uid', $_SESSION['uid']);
08   $query->bindParam(':pwd', sha1($_POST['pwd']));
09
10   //...
Most favorite passwords

   123456           Abc123
   12345            Qwertz / Qwerty
   123456789        Dragon
   Password         Sexgod
   iloveyou         Football
   princess         1234
   rockyou          Pussy
   1234567          Letmein
   12345678         admin
Password storage

   Always salt hashes
       Prepend and/or append additional values


   Stretch your passwords
       Re-apply and calculate the hash
       400.000 iterations take <1sec on my laptop


   Do a quality check on user supplied codes
Validation




  “I know using blacklists is pointless.
That's why I use regular expressions to
   check for valid chars in a string”
Validation

01   <?php
02
03   $name = isset($_GET['name']) ? $_GET['name'] : 'Anonymous User';
04
05   if (ereg("^[a-zA-Z0-9 +-]*$", $name)) {
06       echo "Welcome, $name";
07   } else {
08       echo "Sorry, that name contains invalid chars";
09   }
10
11   ?>
Clickjacking



   “To make sure my site cannot be a
    victim of clickjacking, I have a
     Javascript to Break out from
          frames or iframes”
Clickjacking

   Old style frame busting code
    01   <script type=”text/javascript”>
    02   if (top != self) { top.location.replace(self.location.href); }
    03   </script>
Clickjacking

   Old style frame busting code
    01   <script type=”text/javascript”>
    02   if (top != self) { top.location.replace(self.location.href); }
    03   </script>



   Frame buster busting code
    01   <script type=”text/javascript”>
    02   var prevent_bust = 0
    03   window.onbeforeunload = function() { prevent_bust++ }
    04   setInterval(function() {
    05       if (prevent_bust > 0) {
    06           prevent_bust -= 2
    07           window.top.location = 'http://attacker/204.php';
    08       }
    09     }, 1);
    10   </script>
Clickjacking – what works

   JavaScript & CSS
       Hide content by use display:none
       Switch to visible if frametest succeeds


   Use X-FRAME-OPTIONS header
       Set to DENY for no iframe embedding
       Set to SAMEORIGIN to allow from same host
Lessons learned?

   Tiny problems add up
       Some attacks are only effective if various
        vectors get combined
       Combinations of attack vectors may render
        your solution useless


   Security requires a fully secure eco system
Q & A
Congrats!
Contact
   Slides will be available
        http://talks.thephp.cc

   Please rate this talk
        http://joind.in/talk/view/2785

   Contact options
        Email: team@thePHP.cc / arne@thePHP.cc

   Follow us on twitter:
        @arneblankerts / @thePHPcc

More Related Content

PDF
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
PDF
Php tips-and-tricks4128
PPT
Corephpcomponentpresentation 1211425966721657-8
ODP
My app is secure... I think
ODP
My app is secure... I think
PDF
Dependency Injection with PHP 5.3
PDF
Symfony2 - OSIDays 2010
PPTX
Electrify your code with PHP Generators
Marrow: A Meta-Framework for Python 2.6+ and 3.1+
Php tips-and-tricks4128
Corephpcomponentpresentation 1211425966721657-8
My app is secure... I think
My app is secure... I think
Dependency Injection with PHP 5.3
Symfony2 - OSIDays 2010
Electrify your code with PHP Generators

What's hot (20)

PDF
Dependency injection in PHP 5.3/5.4
PDF
Symfony2 - WebExpo 2010
PDF
PhpBB meets Symfony2
PDF
Learning Dtrace
KEY
PDF
4069180 Caching Performance Lessons From Facebook
KEY
Can't Miss Features of PHP 5.3 and 5.4
PPTX
Speed up your developments with Symfony2
PDF
Dependency injection - phpday 2010
PDF
Dependency Injection IPC 201
ODP
My app is secure... I think
PDF
Beyond symfony 1.2 (Symfony Camp 2008)
PDF
Advanced php testing in action
PPTX
New in php 7
PDF
Node.js API 서버 성능 개선기
PDF
The symfony platform: Create your very own framework (PHP Quebec 2008)
ODP
My app is secure... I think
PDF
Unit and Functional Testing with Symfony2
PDF
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
PDF
The state of Symfony2 - SymfonyDay 2010
Dependency injection in PHP 5.3/5.4
Symfony2 - WebExpo 2010
PhpBB meets Symfony2
Learning Dtrace
4069180 Caching Performance Lessons From Facebook
Can't Miss Features of PHP 5.3 and 5.4
Speed up your developments with Symfony2
Dependency injection - phpday 2010
Dependency Injection IPC 201
My app is secure... I think
Beyond symfony 1.2 (Symfony Camp 2008)
Advanced php testing in action
New in php 7
Node.js API 서버 성능 개선기
The symfony platform: Create your very own framework (PHP Quebec 2008)
My app is secure... I think
Unit and Functional Testing with Symfony2
Scaling Symfony2 apps with RabbitMQ - Symfony UK Meetup
The state of Symfony2 - SymfonyDay 2010
Ad

Viewers also liked (6)

PDF
Metaprogramming in Ruby
PDF
Scalable Architecture 101
PDF
The business behind open source
PDF
Writing a Ruby Gem for beginners
PDF
Anatomy of a large Django site
PDF
Opensource Authentication and Authorization
Metaprogramming in Ruby
Scalable Architecture 101
The business behind open source
Writing a Ruby Gem for beginners
Anatomy of a large Django site
Opensource Authentication and Authorization
Ad

Similar to Security 202 - Are you sure your site is secure? (20)

PPTX
Php security common 2011
PPTX
Securing your web apps now
PPT
Php Security By Mugdha And Anish
PDF
Evolution Of Web Security
PDF
Dip Your Toes in the Sea of Security (PHP UK 2016)
PDF
Security in php
PDF
PHP Secure Programming
ODP
Security In PHP Applications
PDF
Intro to Php Security
ODP
My app is secure... I think
PDF
Top 10 Web Application vulnerabilities
PDF
Php web app security (eng)
PDF
Security in PHP Applications: An absolute must!
PDF
Dip Your Toes in the Sea of Security (CoderCruise 2017)
PPTX
Secure programming with php
PDF
Php Security
PPT
Php & Web Security - PHPXperts 2009
PPTX
Application and Website Security -- Fundamental Edition
PDF
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
PDF
Dip Your Toes in the Sea of Security (IPC Fall 2017)
Php security common 2011
Securing your web apps now
Php Security By Mugdha And Anish
Evolution Of Web Security
Dip Your Toes in the Sea of Security (PHP UK 2016)
Security in php
PHP Secure Programming
Security In PHP Applications
Intro to Php Security
My app is secure... I think
Top 10 Web Application vulnerabilities
Php web app security (eng)
Security in PHP Applications: An absolute must!
Dip Your Toes in the Sea of Security (CoderCruise 2017)
Secure programming with php
Php Security
Php & Web Security - PHPXperts 2009
Application and Website Security -- Fundamental Edition
Dip Your Toes in the Sea of Security (PHP South Africa 2017)
Dip Your Toes in the Sea of Security (IPC Fall 2017)

More from ConFoo (17)

PDF
Debugging applications with network security tools
PDF
OWASP Enterprise Security API
PDF
Introduction à la sécurité des WebServices
PDF
Le bon, la brute et le truand dans les nuages
PDF
The Solar Framework for PHP
PDF
Décrire un projet PHP dans des rapports
PDF
Server Administration in Python with Fabric, Cuisine and Watchdog
PDF
Think Mobile First, Then Enhance
PDF
As-t-on encore besoin d'un framework web ?
PDF
Pragmatic Guide to Git
PDF
Building servers with Node.js
PDF
An Overview of Flash Storage for Databases
PDF
Android Jump Start
PDF
Develop mobile applications with Flex
PDF
WordPress pour le développement d'aplications web
PDF
Graphs, Edges & Nodes: Untangling the Social Web
PDF
Rendre son CMS conforme au SGQRI 008 en 20 étapes
Debugging applications with network security tools
OWASP Enterprise Security API
Introduction à la sécurité des WebServices
Le bon, la brute et le truand dans les nuages
The Solar Framework for PHP
Décrire un projet PHP dans des rapports
Server Administration in Python with Fabric, Cuisine and Watchdog
Think Mobile First, Then Enhance
As-t-on encore besoin d'un framework web ?
Pragmatic Guide to Git
Building servers with Node.js
An Overview of Flash Storage for Databases
Android Jump Start
Develop mobile applications with Flex
WordPress pour le développement d'aplications web
Graphs, Edges & Nodes: Untangling the Social Web
Rendre son CMS conforme au SGQRI 008 en 20 étapes

Security 202 - Are you sure your site is secure?

  • 1. Are You Sure Your Site Is Secure? Security 202 Confoo 2011 Edition By Arne Blankerts, thePHP.cc
  • 2. What is this talk about?  Myths in web security  Broken configurations  Typical implementation issues
  • 3. Session data “I can always trust my session data since I know what I did store”
  • 4. Session data [theseer@rikka ~] $ grep "session.save_path" /etc/php.ini | grep -v ";" session.save_path = "/var/lib/php/session"  Identical for all php instances unless specifically overwritten  Read and write access from php code  May be crafted in shared hosting  Session-id takeover from vhost to vhost  Session-Content can be modified  Can even lead to code execution
  • 5. Session hijacking “To protect my users from session hijacking, I did implement a validation check”
  • 6. Session hijacking  session.php 01 <?php 02 session_start(); 03 $success = true; 04 if (($_SESSION['IP'] != $_SERVER['REMOTE_ADDR']) 05 or ($_SESSION['VIA'] != $_SERVER['HTTP_VIA']) 06 or ($_SESSION['FORWARD'] != $_SERVER['HTTP_X_FORWARDED_FOR']) 07 or ($_SESSION['AGENT'] != $_SERVER['HTTP_USER_AGENT'])) { 08 // ... 09 }
  • 7. Session hijacking – what to do?  Determine if hijacking is a problem  Regenerate id on every request  Doesn't block it but makes it harder to exploit  Fully switch to https for transport  Alternatively use a separate id in ssl context
  • 8. Cross Site Request Forgery “I have an anti CSRF token in my forms – So I'm well protected”
  • 9. CSRF  csrftoken.php 01 <?php 02 03 session_start(); 04 $_SESSION['CSRF']=md5(time()); 05 06 //...  validate.php 01 <?php 02 03 session_start(); 04 if ($_SESSION['CSRF']==$_GET['CSRF']) { 05 // ... 06 }
  • 10. CSRF  Regenerate token for every form?  Do you keep a backlog of tokens?  Do you validate your session?  Session fixation may violate CSRF tokens  What do you base the token on?
  • 11. CAPTCHA “I'm using a captcha to protect my forms from abuse – So I'm save.”
  • 12. CAPTCHA  Conceptual Problems  Distortion often unreadable  Not the least bit accessible  Breaking can be “crowd sourced”  Implementation issues
  • 13. CAPTCHA  captcha.php 01 <?php 02 session_start(); 03 require 'captchaHelper.php'; 04 05 $code = generateCaptchaCode(); 06 $_SESSION['CAPTCHA'] = $code; 07 08 header('Content-type: image/jpeg'); 09 echo createCaptchaImage($code);  validation.php 01 <?php 02 session_start(); 03 04 if ($_SESSION['CAPTCHA'] != $_REQUEST['code']) { 05 die('Captcha value wrong'); 06 } 07 echo 'Welcome!';
  • 14. Prepared Statements “I'm using prepared statements so I'm protected from sql injections”
  • 15. Prepared Statements 01 <?php 02 03 $db = new PDO(....); 04 $query = $db->prepare('SELECT ... WHERE NAME=:name'); 05 $query->bindParam(':name', $_GET['name']); 06 07 //...
  • 16. Prepared Statements  What about fieldnames?  Variable table names?  Do you sort your results?  Any need for limits?  Still use ext/mysql?  Sprintf based implementations?
  • 17. Drawbacks of sprintf  Manual escaping needed  mysql_escape_string vs. mysql_real_escape_string  PDO::quote() does not work with ODBC  No knowledge of fieldtype  String vs. Integer exploits  PDO::quote vs. mysql(i)_real_escape_string
  • 18. Password storage “I know storing clear text passwords is a bad idea. That's why I'm only storing hashes of passwords to protect my users.”
  • 19. Password storage 01 <?php 02 03 $db = new PDO(....); 04 $query = $db->prepare( 05 'UPDATE user SET PASSWD=:pwd WHERE UID=:uid' 06 ); 07 $query->bindParam(':uid', $_SESSION['uid']); 08 $query->bindParam(':pwd', sha1($_POST['pwd'])); 09 10 //...
  • 20. Most favorite passwords  123456  Abc123  12345  Qwertz / Qwerty  123456789  Dragon  Password  Sexgod  iloveyou  Football  princess  1234  rockyou  Pussy  1234567  Letmein  12345678  admin
  • 21. Password storage  Always salt hashes  Prepend and/or append additional values  Stretch your passwords  Re-apply and calculate the hash  400.000 iterations take <1sec on my laptop  Do a quality check on user supplied codes
  • 22. Validation “I know using blacklists is pointless. That's why I use regular expressions to check for valid chars in a string”
  • 23. Validation 01 <?php 02 03 $name = isset($_GET['name']) ? $_GET['name'] : 'Anonymous User'; 04 05 if (ereg("^[a-zA-Z0-9 +-]*$", $name)) { 06 echo "Welcome, $name"; 07 } else { 08 echo "Sorry, that name contains invalid chars"; 09 } 10 11 ?>
  • 24. Clickjacking “To make sure my site cannot be a victim of clickjacking, I have a Javascript to Break out from frames or iframes”
  • 25. Clickjacking  Old style frame busting code 01 <script type=”text/javascript”> 02 if (top != self) { top.location.replace(self.location.href); } 03 </script>
  • 26. Clickjacking  Old style frame busting code 01 <script type=”text/javascript”> 02 if (top != self) { top.location.replace(self.location.href); } 03 </script>  Frame buster busting code 01 <script type=”text/javascript”> 02 var prevent_bust = 0 03 window.onbeforeunload = function() { prevent_bust++ } 04 setInterval(function() { 05 if (prevent_bust > 0) { 06 prevent_bust -= 2 07 window.top.location = 'http://attacker/204.php'; 08 } 09 }, 1); 10 </script>
  • 27. Clickjacking – what works  JavaScript & CSS  Hide content by use display:none  Switch to visible if frametest succeeds  Use X-FRAME-OPTIONS header  Set to DENY for no iframe embedding  Set to SAMEORIGIN to allow from same host
  • 28. Lessons learned?  Tiny problems add up  Some attacks are only effective if various vectors get combined  Combinations of attack vectors may render your solution useless  Security requires a fully secure eco system
  • 31. Contact  Slides will be available  http://talks.thephp.cc  Please rate this talk  http://joind.in/talk/view/2785  Contact options  Email: team@thePHP.cc / arne@thePHP.cc  Follow us on twitter:  @arneblankerts / @thePHPcc