gpt4 book ai didi

CSRF TEST and mismatched value trouble(CSRF测试和值不匹配故障)

转载 作者:bug小助手 更新时间:2023-10-25 20:36:02 31 4
gpt4 key购买 nike



<!doctype html> 

<meta charset="utf-8">


<title> T CSRF_TEST.php </title>

<style> html * { font-size:1.75rem; font-family:monospace; } </style>

<?php
// Generate a CSRF token and store it in a cookie
$csrfToken = bin2hex(random_bytes(32));
setcookie("csrfToken", $csrfToken, time() + 3600);


?>


<form action="CSRF_TEST.php" method="POST">
<input type="hidden" name="csrfToken" value="<?php echo $_COOKIE['csrfToken']; ?>">

<input type="submit" value="Submit" onclick="refreshPage()">

</form>

<?php
echo "<br>";

$TOKEN = $_POST["csrfToken"];
echo "<br>TOKEN= " . $TOKEN;

$TOKEN2 = $_COOKIE['csrfToken'];
echo "<br>TOKEN2= " . $TOKEN2;

// On the server side, verify the CSRF token
if ($_POST['csrfToken'] !== $_COOKIE['csrfToken']) {
// CSRF token mismatch
// Reject the request
echo "<br>";
echo "Reject the request";
}
else
{
echo "APPROVED REQUEST";
}

?>

<script>
function refreshPage() {
window.location.replace("CSRF_TEST.php");
}
</script>

I am running this program and obtain two different value in the conditional ($_POST['csrfToken'] !== $_COOKIE['csrfToken']) test . As I know the generative CSRF token is stored in cookie then the same is POST to php and i cannot realize why the values are different.

我正在运行这个程序,并在条件($_POST[‘csrfToken’]!==$_cookie[‘csrfToken’])测试中获得了两个不同的值。据我所知,生成性CSRF令牌存储在cookie中,然后POST到php中也是如此,我不明白为什么值会不同。


更多回答

Every time you reload the page, after a submit, a new cookie value is created and set.

每次重新加载页面时,在提交之后,都会创建并设置一个新的Cookie值。

KIKO i asked the question because i am trying to test if my small program is a bit secure against CSRF attack. it is more or less didactic. in your comment you suggest that is impossibile a match between $_POST['csrfToken'] and $_COOKIE['csrfToken'] , is it right?

Kiko我问这个问题是因为我想测试我的小程序对CSRF攻击是否有点安全。这或多或少带有说教意味。在您的评论中,您建议$_POST[‘csrfToken’]和$_cookie[‘csrfToken’]之间不可能匹配,对吗?

Yes, that is right. I think what you want is to only create a new cookie value when it isn't set. So: if (!isset($_COOKIE['csrfToken'])) { .... create cookie .... }. That way it can be the same value as $_POST['csrfToken'] because you don't overwrite it every time.

是的,就是这样。我认为您想要的是仅在未设置时创建新的Cookie值。所以:如果(!isset($_cookie[‘csrfToken’])){...创建Cookie...}。这样,它可以与$_POST[‘csrfToken’]相同的值,因为您不会每次都覆盖它。

KIKO thanks for the suggestion, you mean if (!isset($_COOKIE['csrfToken'])) { $csrfToken = bin2hex(random_bytes(32)); setcookie("csrfToken", $csrfToken, time() + 3600); }

Kiko感谢您的建议,您的意思是if(!isset($_cookie[‘csrfToken’])){$csrfToken=bin2hex(随机字节(32));setcookie(“csrfToken”,$csrfToken,time()+3600);}

Yes, that's what I meant. Create and set the cookie once for new visitors to your page.

是的,我就是这个意思。为页面的新访问者创建并设置一次Cookie。

优秀答案推荐
更多回答

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com