©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
外部链接:» 会话固定
HTTP 会话管理是 web 应用安全的核心内容, 要采用尽可能的手段来确保会话安全。 开发人员应该合理的启用或使用可接受的设置。
会话模块无法保证存储到会话中的数据仅能被会话创建者可见。 如果要保护会话的完整性, 你需要进行一些额外的工作。 至于如何保护会话,取决于你在会话中所存储的数据。
要保护会话中的数据,通常有额外的成本, 并且有可能影响用户使用的便利性。 例如,要想防范简单的社会工程学方式的攻击, 你需要启用 session.use_only_cookies。 这样一来, 用户需要无条件的接受 cookie, 否则会话功能将无法正常工作。
有几种方式可以将会话 ID 泄露给第三方。 会话 ID 的泄露会导致 第三方可以访问该会话 ID 所关联的所有资源。 如果从你的站点链接到外部站点, 在 URL 中所携带的会话 ID 可能会被外部站点的 “referrer”日志记录。 其次,更加主动的攻击者会尝试监听你的网络通信, 明文传输的会话 ID 可能会被攻击者从网络层面窃取。 对于这种场景的解决方案是在服务器使用 SSL 通信, 并且强制用户使用 SSL 方式访问你的服务。
自 PHP 5.5.2 开始,可以使用 session.use_strict_mode 设置项。当启用该设置项,并且会话保存管理器能够支持此设置项的话, 它会拒绝未经初始化的会话 ID 并且返回一个新创建的会话 ID。 通过强制用户使用新的会话 ID,可以避免遭受攻击。 如果启用了 session.use_trans_sid 设置项,攻击者可以发送包含会话 ID 的 URL 给受害者, 例如:http://example.com/page.php?PHPSESSID=123456789, 那么受害者将会使用攻击者提供的会话 ID 开始会话。 session.use_strict_mode 设置项可以降低这样的风险。
虽然 session.use_strict_mode 设置项可以降低风险, 攻击者依然可以使用自己创建的、已经经过初始化的会话 ID 来欺骗受害人。 只是攻击者需要在发起攻击之前初始化会话 ID 并且要保持这个会话处于活跃状态。
可以对会话 ID cookie 设置 domain,path,httponly,secure 等属性, 但是浏览器中有优先级定义, 攻击者可以利用这个特点设置可以永久使用的会话 ID。 使用 session.use_only_cookies 设置项无法解决此问题。session.use_strict_mode 设置项可以降低风险,当 session.use_strict_mode 为 “On”时,服务器将拒绝未经初始化的会话 ID, 而是返回一个新创建的会话 ID。 这可能会导致受害者面临 DoS 攻击,但是总好过账号被盗取。
session.use_strict_mode 设置项可以缓解这种情况, 但是对于已经经过认证的会话,它就有些力不从心了。 开发人员必须使用 session_regenerate_id() 来进行认证, 并且还需要在向 $_SESSION 中存储认证信息之前调用 session_regenerate_id() 函数。 session_regenerate_id() 可以确保仅在新会话中存储认证后的信息。 例如,如果在认证过程中发生错误, 可能会在旧的会话中保存认证通过的标识。
和 use_strict_mode=On 类似,使用 session_regenerate_id() 函数 也可能导致 DoS 攻击(从个人角度来看),但是总好过账号被盗取。 至少应该在用户通过认证之后为其重新生成新的会话 ID。 重新生成会话 ID 可以降低被窃取的风险, 所以应该定期重新生成会话 ID。 开发人员不应依赖会话过期, 因为攻击者可以通过定期使用受害者会话 ID 发起访问以保持会话活跃。 开发人员必须自行实现针对过期会话的处理。
需要提醒的是,默认情况下, session_regenerate_id()
函数并不会删除旧的会话。
对用户而言,原有的已认证的会话可能仍然可用。
如果开发人员需要阻止其他人使用原有的已认证会话,
需要设置 delete_old_session
参数为 TRUE
。
但是立即删除旧的会话可能会带来其他影响,
比如在并发访问或者网络不稳定的情况下,
可能会导致会话无效。(译注:意指浏览器携带旧的会话 ID 发起了并发的请求,如果在第一个被服务器接受和处理的请求中删除了旧的会话数据,那么后续的请求将会产生会话无效的问题)
可以在 $_SESSION 中设置一个很短的过期时间来代替直接删除旧的会话,
并且拒绝用户访问旧的会话(过期的会话)。
session.use_only_cookies 和 session_regenerate_id() 联合使用, 在某些情况下可能导致针对个人的 DoS 攻击。 如果发生这种情况, 你可以告知用户清除浏览器的 cookie 并且警告用户可能存在安全隐患。 攻击者可以利用 web 应用的弱点(例如,JavaScript 注入) 或恶意的浏览器插件生成恶意的 cookie。
开发人员一定不要使用长生命周期会话 ID 来实现自动登录的功能, 因为这样会使会话很容易被窃取。 开发人员需要自行实现自动登录的功能, 可以使用比 SHA-2 更安全的散列算法, 例如 SHA-256 或者更高, 并且包含从 /dev/urandom 产生随机数据。 如果用户尚未认证通过,就检查一次性自动登录密钥是否有效。 如果密钥有效,那么对用户进行认证,并且设置新的、安全的散列值。 自动登录密钥的生存周期比认证密钥的更长, 所以要竭尽所能来保护自动登录密钥的安全。 至于 cookie,可以使用 path/httponly/secure 等属性来进行保护。 同时,开发人员还必须为用户提供禁用自动登录以及移除不再使用的自动登录密钥 cookie 的功能。
[#1] bmearns at ieee dot org [2009-01-27 11:41:17]
In addition to ip-address binding not always being effective, it can also prevent users connecting through a proxy-pool from even being able to use your site. In such a scenario, a person's IP address may very well change with every access.
If you're handling anything remotely secure, the only safe option is HTTPS. If the data doesn't need to be that secure, than you should not allow a high-jacked session to do too much damage. Basically, don't assume that a person really is who they pretend to be just because the session says a person authenticated with a username and password: it may have been that person who logged in, but that doesn't mean it's still that person. So if you're going to do something like change passwords or something, require them to authenticate again on the same form.
Of course this needs to be done in some secure way, as well, so that the password is not just floating over the network. A good way to do this is sending a nonce (number-used-once) along with the form and some javascript to concatenate the nonce to the password, then perform a cryptographic hash of the combined string. The resulting valid-once "password" should then be sent instead of the plain text password. To be effective, you also need to prevent people from using the form if they don't have JavaScript enabled. You can do this by disabling the form fields in HTML, then re-enabling them in JavaScript.
[#2] justin at fatbird dot ca [2009-01-18 18:56:49]
IP checking is a sometimes useful feature with two limitations that are important to be aware of:
1. Anyone surfing behind a proxy (e.g., someone at work) will provide the proxy's IP, not his own. Session ID replay attacks will not be prevented by IP checking for an attacker on the user's side of the proxy.
2. If the PHP application is behind a reverse proxy, the reverse proxy's IP address will be the only request IP seen by PHP, so IP checking is useless.
[#3] Anurag Jain [2008-12-12 09:28:04]
Websites which have sensitive information need to be patched to ensure its not exploited because of session issues.
In earlier versions of apache cookie reliability was not assumed and hence the default method was always using url-rewrite which meant every url link, every form submission etc would have a PHPSESSID=<sessionid> passed along to inform the server about the active session. New versions have turned this off using
session.use_trans_sid = 0
in the /etc/php5/apache2/php.ini file.
Reasons?
Well one might safe the offline page as a bookmark or pass the link across to others not realizing that the session id information is also sent. So someone who quickly accesses these pages could possible get logged on, this was also true wrt search engines, and I guess in some cases it being seen as duplicate content as the same page will have a different session id every time the robots scan the website.
But having this set does not mean you are protected. Let me explain.
What prevents me from presetting the session id! Assume there is a banking site www.example.com which has a login screen at www.example.com/login.php
I can send you can email with a link to the bank site as http://www.example.com/login.php?PHPSESSID=12345
When you click on the link it presents the session id as 12345 rather then asking the server to generate a new one. This is called session fixation. Keep in mind even with session.use_trans_sid = 0 this will work as this sets it only not to use url-rewrite. To prevent this altogether set session.use_only_cookies = 1 which ensures that only cookies will be used, but this could cause problems when dealing with transaction which involve switch sites, i.e. siteA forwards to site B for payment which forwards to siteA for thank you, in which case a phpsessid inform might be used to revive the old session.
A good approach would always be to at the login screen and immediately post login to force a new session id generated using random numbers
session_start();
$newsessid = somerandomnumberfunction();
session_id($newsessid);
you can also use session_regenerate_id() function to generate a new id
session_start();
session_regenerate_id();
Also its always good to ensure every valid session is checked against an ip. One good method is to store the session id and remote ip information in a table, or better store the ip as a session variable itself, once the user logs in and ensure that this is continued for remaining pages for security. This ofcourse wont work when users use the same office or shared network as the ip to the outside world is the same.
https is always a good idea for sensitive sites, but keeping it persistent for all pages which use session is important if you really want a foolproof system else anyone can always sniff your packets.
So to quickly go through the bits
- set session.use_trans_sid = 0 in /etc/php5/apache2/php.ini file.
- Ensure you always use a new self generated session id on successful login attempt.
- Try setting session.use_only_cookies = 1 and check if all works fine.
- Use https throughout to ensure no one can sniff your session id.
- Store session id, remote IP information and compare for successive pages
[#4] JonathanFeller at NOSPAMgmx dot ch [2008-10-27 12:22:48]
Perhaps, you would also like to timeout a session after some idle time. I noticed that session.gc_maxlifetime is not suitable for this. So I used this code to do the job:
<?php
if (!isset($_SESSION['timeout_idle'])) {
$_SESSION['timeout_idle'] = time() + MAX_IDLE_TIME;
} else {
if ($_SESSION['timeout_idle'] < time()) {
//destroy session
} else {
$_SESSION['timeout_idle'] = time() + MAX_IDLE_TIME;
}
}
?>
[#5] Olle Bergkvist [2008-10-21 15:47:13]
It is also quite important to (somehow) make sure that the cookies you're setting (including the session cookie) is only visible to the site that created it (or to other trusted sites only).
If the cookie's path is set to '/' (the whole domain), then any website on the same domain (might be lots of websites) _will_ get the cookie through HTTP headers and could possibly hijack your session.
One slightly acceptable protection would be to lock a session to one IP adress.