简体   繁体   English

PHP的检查会议足够安全?

[英]php checking session secure enough?

I have two possibly elementary questions about php SESSIONs and cookies 我有两个关于php会话和cookie的基本问题

1) How does the server know when a session terminates? 1)服务器如何知道会话何时终止? Or when to get rid of the session_id and info etc. (that is, if session_destroy isn't called) 或何时删除session_id和info等(即,如果未调用session_destroy的话)

2) Being that on the client side a cookie is stored containing a unique session id that the server uses to identify the individual, if someone were to gain access to that session id they could access all of the same information right? 2)是否在客户端存储了一个cookie,其中包含服务器用来标识个人的唯一会话ID,如果有人要访问该会话ID,他们可以访问所有相同的信息吗? Is there an extra level of security necessary then to identify a user other than simply checking the session information? 除了简单地检查会话信息之外,还有其他必要的安全级别来标识用户吗?

How does the server know when a session terminates? 服务器如何知道会话何时终止?

Session is basically a bunch of data, stored on the server. 会话基本上是一堆存储在服务器上的数据。 The client is recognized and matched to a specific session by the session ID, stored in the session and on the client side in a cookie with the default name of PHPSESSID . 客户端通过会话ID识别并匹配到特定会话,该会话ID存储在会话中以及客户端的cookie中,其默认名称为PHPSESSID (You can find its value in your browser or using session_id() function in PHP.) If the browser deletes this cookie, the session ID is lost on the client side, and there is no way the client can get back other data stored in the session on the server. (您可以在浏览器中找到其值,也可以在PHP中使用session_id()函数找到它的值。)如果浏览器删除此cookie,则会话ID在客户端丢失,并且客户端无法取回存储在其中的其他数据。服务器上的会话。 This data is eventually deleted by the garbage collector. 该数据最终将被垃圾收集器删除。

The garbage collector is called with some probability each time when session_start() is called. 每次调用session_start()时,垃圾回收器都会以一定的概率被调用。 The probability equals to gc_probability / gc_divisor . 概率等于gc_probability / gc_divisor Garbage collector deletes the session data if the session was around for more than gc_maxlifetime seconds. 如果会话持续时间超过gc_maxlifetime秒,则垃圾收集器将删除会话数据。 You can check all these values in phpinfo() output or using eg ini_get('session.gc_maxlifetime') . 您可以在phpinfo()输出中或使用ini_get('session.gc_maxlifetime')检查所有这些值。 That's about the session data on the server. 那是关于服务器上的会话数据。 Now about the client. 现在关于客户。

Session configuration contains a value session.cookie_lifetime . 会话配置包含一个值session.cookie_lifetime Quoting PHP manual : 引用PHP手册

session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. session.cookie_lifetime指定发送到浏览器的cookie的生存时间(以秒为单位)。 The value 0 means "until the browser is closed." 值0表示“直到浏览器关闭”。 Defaults to 0. 预设为0。

From PHP Sessions and security : PHP会话和安全性

0 have special meaning. 0有特殊含义。 It tells browsers not to store cookie to permanent storage. 它告诉浏览器不要将cookie存储到永久存储中。 Therefore, when browser is terminated, session ID cookie is deleted immediately. 因此,当浏览器终止时,会话ID cookie将立即删除。 If developer set other than 0, it may allow other users to use the session ID. 如果开发人员设置的值不是0,则可能允许其他用户使用会话ID。 Most applications should use "0" for this. 大多数应用程序应为此使用“ 0”。 If auto login feature is required, implement your own secure auto login feature. 如果需要自动登录功能,请实施自己的安全自动登录功能。 Do not use session ID for it. 不要为此使用会话ID。

You can check the setting using any of the following: 您可以使用以下任何一种方法来检查设置:

  1. phpinfo() , look for session.cookie_lifetime in the session section. phpinfo() ,在session部分中查找session.cookie_lifetime
  2. session_get_cookie_params()['lifetime']
  3. ini_get('session.cookie_lifetime')

So basically, if the session cookie lifetime is 0, the browser deletes it when it is closed and the client loses access to the session data. 因此,基本上,如果会话cookie生存期为0,则浏览器在关闭时将其删除,并且客户端将无法访问会话数据。

If someone were to gain access to that session ID, they could access all of the same information? 如果有人要访问该会话ID,他们可以访问所有相同的信息吗?

Yes. 是。 As long as the ID is known to (any) client and the session data is not deleted on the server, the client can access it. 只要(任何)客户端知道该ID并且在服务器上不删除会话数据,客户端就可以访问它。

Is there an extra level of security necessary then to identify a user other than simply checking the session information? 除了简单地检查会话信息之外,还有其他必要的安全级别来标识用户吗?

It depends on your application. 这取决于您的应用程序。 Getting the session ID is not trivial, one needs either to intercept the communication or have direct access to the client data. 获取会话ID并非易事,需要拦截通信或直接访问客户端数据。 Interception can be prevented using TLS -based encrypted connection. 使用基于TLS的加密连接可以防止拦截。 Getting the client data requires some malicious software. 获取客户端数据需要一些恶意软件。

This article describes a safer cookie implementation for user authentication. 本文介绍了一种用于用户身份验证的更安全的cookie实现。 In short, while it still doesn't prevent cookie hijacking , it gives you a way to detect it, notify the user and prevent the stolen cookie from being re-used. 简而言之,尽管它仍然不能阻止cookie劫持 ,但它为您提供了一种检测它,通知用户并防止再次使用被盗cookie的方法。

Another article lists gives a more complete overview of good practices for user authentication. 另一篇文章列表更完整地概述了用户身份验证的良好做法。 As usual, security requires that the whole project is implemented in a secure way, not just one, seemingly critical part of it. 与往常一样,安全性要求整个项目以安全的方式实施,而不仅仅是其中一个看似关键的部分。 But if my suggest, don't overdo it. 但是,如果我的建议是,请不要过度使用。 The effort should be reasonable for the particular requirements of each project. 对于每个项目的特定要求,工作应该是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM