PHP Security Crash Course - 5 - Session Management
PHP Security Crash Course - 5 - Session Management
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 1
Why Session Management Security?
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 2
Session ID
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 3
Secure Session ID Generation
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 4
Session ID Generation in PHP
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 5
Session ID Transport
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 6
Session ID Transport - Cookie Security
• session name
• to stop applications to influence each other
- session_name(‘myApplicationX‘);
• httpOnly cookies
• to stop JavaScript from accessing the cookie
- ini_set(‘session.cookie_httponly‘, true);
• secure Flag important for SSL sites
• to stop cookie from leaking on port 80
- ini_set(‘session.cookie_secure‘, true);
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 7
Session Lifetime
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 8
Permissive vs. strict Session-Systems
• Permissive session-systems
• accept arbitrary session ids
• only refuses session ids containing illegal characters
• creates a new session, if none exists with the choosen id
• strict session-systems
• accept only session id created by themself
• will refuse a session id if it does not belong to a started session
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 9
Strict Session System in PHP
session_start();
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 10
Session Storage
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 11
Session Storage - Data Mixup (I)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 12
Session Storage - Data Mixup (II)
• Example 1 - Setup
• Customer runs two applications on own server
• both applications consist of multi-step forms
• both application store previous steps in the session
• application 1 copies all user input in the session - validation/
filtering occurs after the last step
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 13
Session Storage - Data Mixup (III)
• Example 1 - Exploit
• Attacker enters malicious data into application 1
• Attacker copies session id from cookie of application 1 into the
cookie of application 2
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 14
Session Storage - Data Mixup (IV)
• Example 2 - Setup
• Customer runs two applications on his own server
• both applications are for separate user groups
• both applications are developed by the same developers
• both applications share parts of their implementation
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 15
Session Storage - Data Mixup (V)
• Example 2 - Exploit
• Attacker is a user of application 1
(maybe even a moderator / admin)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 16
Session Storage - Data Mixup Prevention
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 17
Session Storage - Applicationmarker
session_start();
if (!isset($_SESSION[‘application‘])
|| ((string)$_SESSION[‘application‘] !== ‘application_1‘)) {
session_regenerate_id();
$_SESSION = array(‘application‘ => ‘application_1‘);
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 18
Session Storage - Userspace Session Storage
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 19
Session Storage - Insecure Transactions (I)
• usual implementation
• open - gets ignored
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 20
Session Storage - Insecure Transactions (II)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 21
Session Hijacking
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 22
Session Hijacking - Countermeasures
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 23
Session Hijacking - One Time URL Tokens
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 24
Session Fixation
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 25
Session Fixation - Invalid Countermeasures
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 26
Session Fixation - Working Countermeasures
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 27
Questions ?
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 28