如何通过cookie安全地记住用户?

发布于 2024-11-07 12:55:26 字数 223 浏览 0 评论 0原文

假设我有一个会员基础网站,当用户登录时,我放置一个带有键值对的 cookie(或会话)来记住用户是谁。但我刚刚注意到我应该使用哪些信息来记住用户以确保其安全。我不能使用 username=username 或 user_id = user_id (因为我的 user_id 将为 1),因为人们可以简单地猜测 cookie 值是什么并以该用户身份登录。那么我应该使用什么键/值对来识别用户并仍然安全地将他们的信息连接到数据库?谢谢。

So lets say i have a member base website and when the user signs in i put put a cookie (or a session) with a key value pair remembering who the user is. But its just come to my attention which information i should use to remember the user so that its secure. I cant use username=username or user_id = user_id (because my user_id will be 1), because people then can just simply guess what the cookie values are and logged in as that user. So what key/value pair should i use to be able to identify users and still connect their information to the database securely? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤独患者 2024-11-14 12:55:26

Ben,您需要关注几种不同类型的攻击。例如,简单地使用私钥加密标识符并不能阻止能够拦截加密值的人将其简单地重播到您的服务器(并且看起来是用户)。此处详细介绍了一些常见的安全风险(以及本页底部的相关链接):

https:// www.owasp.org/index.php/Session_hijacking_attack

会话管理可能非常复杂,并且根据您需要的安全级别,这不是您想要自己解决的问题,因为您的开发环境/框架可能已经有一个解决方案有比自制解决方案经过更多审查。这里有一个链接,详细说明了一些需要考虑的事项,不幸的是,这个主题不仅仅是一个简单的 Stack Overflow 帖子:

https://www.owasp.org/index.php/Session_Management

Ben, there are a few different types of attacks you need to be concerned with. For example simply encrypting the identifier with a private key doesn't prevent someone who can intercept the encrypted value from simply replaying it to your server (and appear to be the user). Some common security risks are detailed here (and in associated links at bottom of this page):

https://www.owasp.org/index.php/Session_hijacking_attack

Session management can be quite complex and depending on the level of security you require, it is not something you want to tackle yourself, because likely your development environment / framework already has a solution that has been vetted moreso than a homebrew solution. Here is a link detailing some things to consider, unfortunately this topic has more to it than a simple Stack Overflow post:

https://www.owasp.org/index.php/Session_Management

源来凯始玺欢你 2024-11-14 12:55:26

如果您出于某种原因不喜欢加密,那么更简单的解决方案可能是使用 GUID 来识别用户。这样,黑客就必须对您的应用程序发起拒绝服务类型的攻击,才能运行 GUID 的一小部分。

如果您想正确执行此操作,那么您还应该看看 http://jaspan.com/improved_persistent_login_cookie_best_practice

If you dont prefer encryption for whatever reason, then a simpler solution could be to use a GUID to identify the user. This way, a hacker would have to launch a denial of service kind-of attack on your application to be able to run through even a very small fraction of the GUIDs.

If you want to do this properly, then you should have a look at http://jaspan.com/improved_persistent_login_cookie_best_practice also.

人生百味 2024-11-14 12:55:26

我绝对不是安全方面的专家,但我最近实现了用户管理工具,并且做了以下工作。

  • 不要使用加密,它很慢,而且大多数时候对于简单的实现来说只是浪费时间。

以下是您需要在服务器上存储的内容 - 为了验证每个请求。

  • UserId(明显)
  • CookieHash(由 userId、一些秘密私钥和加密随机生成的数字组成)
  • LastLogin
  • SessionRenewed(对于何时取消某人的会话很有用,例如每 10 分钟更新一次 cookieHash,否则注销用户)
  • LastIP

我在 cookie 中存储的内容遵循

  • UserId
  • CookieHash

如何使用此基本安全性

只需在用户登录时检查用户名/密码等(只是通常)如果一切正常,则登录用户并生成新的cookiehash 并填写上面给出的值。

每个请求都会根据其哈希值检查 UserId。如果有人提供 UserId = 4 但哈希不匹配,则会自动删除会话并将用户转发到登录屏幕。可能的日志可以很好地了解人们尝试玩弄你的辛勤工作的频率。

我希望这有帮助。

I'm definitely not an expert in security, but I have recently implemented user management tool and I have done the following.

  • Don't use encryption, its slow and most of the time for simple implementation its just a waste of time.

Here is what you do need to store on the server - in order to authenticate each request.

  • UserId (obvious)
  • CookieHash (made out of userId, some secret private key and crypto randomly generated number)
  • LastLogin
  • SessionRenewed (useful for when to cancel someone's session eg. renew cookieHash every 10 min, otherwise log out user)
  • LastIP

What I store in cookie is following

  • UserId
  • CookieHash

How to use this basic security

Simply when user logs in you check username/password etc. (just the usual) If everything is fine then log in user and generate new cookiehash and fill those values given above.

Every request check UserId against its hash. If someone gave UserId = 4 but hash didnt match then automatically drop a session and forward user to login screen. Possible log is good to see how often people try to play around with your hard work.

I hope this helps.

自此以后,行同陌路 2024-11-14 12:55:26

您可以使用保存在服务器上的私有加密密钥来加密用户 ID。使用这种方法需要注意以下几点:

  • 每次调用服务器都需要解密 cookie 以获取用户的 ID。这会增加每个请求的开销。
  • 如果密钥被泄露,您将被迫放弃当前使用的 cookie 名称,并在分配新的 cookie 名称时使用另一个加密密钥;当然,这将导致用户必须重新登录。

虽然我不认为这些是主要障碍,但它们可能对您来说是,并且您必须自己评估对您网站的影响。

You can just encrypt the user id with a private encryption key that you keep on the server. There are a few things to watch out for with this approach:

  • Every call to the server will require you to decrypt the cookie to get the id of the user. This will add overhead to each request.
  • If the key is ever compromised, you will be forced to abandon the current name for the cookie you use and use another encryption key when assigning to the new cookie name; this will cause the user to have to re-login, of course.

While I don't think that these are major hurdles, they might be to you, and you would have to evaluate the impact on your site for yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文