php记住我,如何在PHP中安全地实现“记住我”按钮(持久登录)

首先是一些背景,我还在学习

PHP,我正在尝试建立一个CMS(免责声明:仅供我自己使用).

我还在测试一些东西,我已经成功创建了一个管理员登录系统.

所以,现在这是它的工作原理:

>用户登录后,在验证和清理输入后,我检查用户是否存在,并使用PHPs password_verify将密码与存储在数据库中的散列密码进行比较.

>如果登录成功,我将管理员重定向到dashboard.PHP并创建一个名为adminId的会话变量,并将用户ID存储在该变量中.

这是我的第一个问题:

攻击者可以更改$_SESSION [‘adminId’]的值吗?例如,从1更改为12,因此以不同的管理员身份登录?

无论如何,我读过this article引入攻击者可以使用的多个漏洞.

所以,如果我没有错,我应该使用cookie进行持久登录吗?

好的,首先是永远不要将用户ID存储在cookie中并使用它来检查登录,因为攻击者可以轻松地改变它吗?

很好,所以我可以创建一个随机令牌(使用random_bytes然后转换为十六进制)并将其与数据库中登录表中的用户ID配对.

例如,我有这个:

token: 2413e99262bfa13d5bf349b7f4c665ae2e79e357,userId: 2

因此,用户登录时,会创建令牌,并使用userId存储在数据库中.

假设用户关闭了浏览器.

然后他再次打开它,它所拥有的是带有令牌的cookie:2413e99262bfa13d5bf349b7f4c665ae2e79e357.所以,它会自动以userId:2登录用户吗?

如果他想以userId:10为用户登录,他需要知道与该用户配对的随机令牌.

很好,但是那就是这个问题:时间泄漏.所以他们提出了一个使用两个东西的解决方案,一个选择器和一个验证器这是我不理解的时候,这就是文章所说的:

What follows is our proposed strategy for handling “remember me” cookies in a web application without leaking any useful information (even timing information) to an attacker,while still being fast and efficient (to prevent denial of service attacks).

Our proposed strategy deviates from the above simple token-based automatic login system in one crucial way: Instead of only storing a random token in a cookie,we store selector:validator.

selector is a unique ID to facilitate database look-ups,while preventing the unavoidable timing information from impacting security. (This is preferable to simply using the database id field,which leaks the number of active users on the application.)

07001

On the database side of things,the validator is not stored wholesale; instead,the SHA-256 hash of validator is stored in the database,while the plaintext is stored (with the selector) in the user’s cookie. With this fail-safe in place,if somehow the auth_tokens table is leaked,immediate widespread user impersonation is prevented.

The automatic login algorithm looks something like:

Separate selector from validator.

Grab the row in auth_tokens for the given selector. If none is found,abort.

Hash the validator provided by the user’s cookie with SHA-256.

Compare the SHA-256 hash we generated with the hash stored in the database,using hash_equals().

If step 4 passes,associate the current session with the appropriate user ID.

所以这就是我不明白的地方:

1)选择器和验证器应包含哪些内容?

2)为什么这个系统是安全的以及它如何防止定时泄漏?

在此先感谢,对于长篇文章感到抱歉,但我真的想了解这是如何工作的,我该如何实现它.

我知道我可以使用一些框架或库但我想从scracth制作它,通过反复试验来学习!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值