在PHP中没有Cookie的CSRF令牌

编程入门 行业动态 更新时间:2024-10-12 03:24:50
本文介绍了在PHP中没有Cookie的CSRF令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种方法来添加CSRF令牌到我正在做的应用程序。注意,应用程序目前不使用Cookie或会话。

I am looking for a way to add a CSRF token to an application I'm making. The caveat is, the application does not currently use cookies or sessions.

我想找到一种方法来引入CSRF令牌,而不必:

I would love to find a way to introduce a CSRF token without having to:

  • 在我的申请中介绍状态。
  • 使用工作阶段或Cookie( $ _ SESSION / $ _ COOKIE )存储
  • Introduce state in my application.
  • Use session or cookie ($_SESSION / $_COOKIE) storage
  • 可能性,或者我在我的应用程序中卡住了创建新状态。

    Is this at all a possibility, or am I stuck creating new state in my application.

    推荐答案

    你可以,但不会很安全。

    You can, but it's not going to be very secure.

    这里有一个例子但是不要使用它,这是一个很糟糕的想法:

    // Do this somewhere define('CSRF_SALT', '4dedMj4CWgBMNhRsoTpJlokwB5wTz7UsmF8Mq4uzFIbv'); $token = base64_encode( hash_hmac( 'sha256', date('Ymd') . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT'], CSRF_SALT, true ) ); if (\hash_equals($_POST['security_token'], $token)) { // Form passes validation }

    缺点是这些令牌本质上是可重用的,所以如果一个泄漏的攻击者可以简单地重用(或重新计算)。您也可以尝试在哈希计算中添加表单action =。

    The downside is that these tokens are inherently reusable, so if one leaks an attacker can simply reuse (or recalculate) it. You can also try adding the form action="" value in the hash calculation.

    function getToken($action) { return base64_encode( hash_hmac( 'sha256', hash_hmac( 'sha256', date('Ymd') . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT'], hash('sha256', $action, true), true ), CSRF_SALT, true ) ); } echo "<form action='register.php' method='post'>\n"; echo "<input type='hidden' name='security_token' value='".getToken('register.php')."' />\n"; // ...

    你们对会话的厌恶是什么?

    What's your anathema for sessions anyway?

    更多推荐

    在PHP中没有Cookie的CSRF令牌

    本文发布于:2023-05-29 03:39:47,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/335911.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!