PHP Security Crash Course - 6 - PHP Code Inclusion / Evaluation
PHP Security Crash Course - 6 - PHP Code Inclusion / Evaluation
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 1
PHP Code Inclusion
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 2
Static PHP Code Inclusion (I)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 3
Static PHP Code Inclusion (II)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 4
Dynamic PHP Code Inclusion (I)
• Dynamc inclusion
• include $_GET[‘module‘].“.php“
• include “./modules/“. $_GET[‘module‘].“.php“
• Path to include can be influenced
➡ Security problem because path can be changed to
malicious PHP code
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 5
Dynamic PHP Code Inclusion - URLs (I)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 6
Dynamic PHP Code Inclusion - URLs (II)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 7
Dynamic PHP Code Inclusion - Files (I)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 8
Dynamic PHP Code Inclusion - Files (II)
if (!in_array($module, $allowedModules)) {
$module = $allowedModules[0];
}
include “./modules/$module.php“;
?>
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 9
Part VII
PHP Code Evaluation
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 10
PHP Code Evaluation (I)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 11
PHP Code Evaluation (II)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 12
eval() (I)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 13
eval() (II)
• Example:
<?php
eval(‘$s = “‘ . addslashes($_GET[‘val‘]) . ‘“;‘);
?>
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 14
Complex Curly Syntax
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 15
eval () Whitelist Protection Approach
<?php
if (preg_match(“/^[0-9a-z]*$/iD“, $value)) {
?>
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 16
create_function()
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 17
create_function() - Internal Wrapper Function
/* Implementation similar */
return $name;
}
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 18
preg_replace() (I)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 19
preg_replace() (II)
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 20
Secure Usage of the /e Modifier
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 21
preg_replace_callback()
<?php
/* Callback function */
function pr_callback($match)
{
return chr($match[0]);
}
preg_replace_callback(‘/&#([0-9]+);/e‘,
‘pr_callback‘,
$source);
?>
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 22
Questions ?
Stefan Esser • PHP Security Crash Course at Dutch PHP Conference 2009 • June 2009 • 23