停止机器人 [SO] - PHP

发布于 2024-11-05 18:12:47 字数 826 浏览 0 评论 0原文

我将此代码保存在index.php中,填写后单击“提交”按钮,然后向我显示一个验证页面!
我的问题是它如何检测到它不是来自原始服务器
[我希望他们不要使用引荐来源网址,因为它很容易被禁用]

我的假代码

<html>
 <form id="post-form" action="http://stackoverflow.com/questions/ask/submit" method="post">
  <input id="title" name="title" type="text" maxlength="300" tabindex="100" class="ask-title-field" value="">                        
  <textarea id="wmd-input" name="post-text" cols="92" rows="15" tabindex="101"></textarea>
  <input id="tagnames" name="tagnames" type="text" size="60" value="" tabindex="103">
  <input id="submit-button" type="submit" value="Post Your Question" tabindex="120">  
 </form>
</html>  

任何人都可以指导我创建这样的安全页面[如果用户尝试从一个乏味的页面发帖,他会被要求验证]

I saved this code in index.php and after filling i clicked Submit button, and then a verification page was shown to me !
My question is how did it detected that it was not from the original server ?
[i hope they do not use the referrer as it can be disabled easily]

My Fake Code

<html>
 <form id="post-form" action="http://stackoverflow.com/questions/ask/submit" method="post">
  <input id="title" name="title" type="text" maxlength="300" tabindex="100" class="ask-title-field" value="">                        
  <textarea id="wmd-input" name="post-text" cols="92" rows="15" tabindex="101"></textarea>
  <input id="tagnames" name="tagnames" type="text" size="60" value="" tabindex="103">
  <input id="submit-button" type="submit" value="Post Your Question" tabindex="120">  
 </form>
</html>  

Can anyone guide me creating such secure page [where if a user tries to post from a dull page, he will be asked for verification] ?

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

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

发布评论

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

评论(2

夜巴黎 2024-11-12 18:12:47
  1. 在服务器上生成令牌。
  2. 将该标记放入隐藏的输入元素中。
  3. 将该令牌保存在服务器上生成的表单列表中。
  4. 当表单提交进来时
    1. 检查令牌是否已通过且有效,
    2. 从生成的表单列表中删除令牌。

我怀疑这就是问题提交表单上的 的用途。

  1. Generate a token on the server.
  2. Put that token into a hidden input element.
  3. Save that token on the server in a list of generated forms.
  4. When a form submission comes in
    1. check whether the token was passed and is valid,
    2. remove the token from the list of generated forms.

I suspect that this is what <input id="fkey" type="hidden" value="..." name="fkey"> on the question submission form is for.

好主意,但它也给数据库带来了压力:(

您可以创建一个反 XSRF 令牌,而不必在服务器上记住它们。

您可以通过将要验证的信息放入令牌中来做到这一点,通常包括:

  • 用户 ID,因此一个用户无法创建为另一个用户提交
  • 到期时间戳的表单,因此令牌不会永远持续,

然后将此信息与密钥一起散列并将其吐出到隐藏字段中。当令牌返回时,您可以查看给定的信息,再次使用密钥对其进行散列,然后查看散列是否与用户的提交匹配,

例如,对于用户 ID 18936,到期时间戳为 1304861680(大约现在的 Unix 时间)。 ,您可以创建一个如下令牌:

18936.1304861680.2A956E39.11E859E44B9308B812257BEE660330D9D0566189

其中 2A956E39 是一些随机盐,末尾的位是使用不太好的密钥的 18936.1304861680.2A956E39 的十六进制编码的 HMAC-SHA1 哈希secretkey

这达到了反XSRF的目的,但有时一次性服务器存储的令牌也用于防止表单重复提交。按原样,哈希方法对此没有帮助。但在创建新实体的具体情况下,这是部署双重提交预防的常见地方,您可以使用令牌作为唯一值插入数据库作为新实体的一部分,然后拒绝创建如果已经存在带有令牌的实体,则为新实体。

good idea, but it puts pressure on the DB too :(

You can create an anti-XSRF token without the element of having to remember them all on the server.

You can do this by putting the information you want to verify inside the token, typically including:

  • user ID, so one user can't create a form that submits for another user
  • an expiry timestamp, so that tokens don't last forever

then hash this information together with a secret key and spit it out in the hidden field. When the token comes back in, you can look at the information given, hash it with the key again and see if the hash matches the user's submission.

So for example, for user ID 18936 with expiry timestamp 1304861680 (Unix time for about now), you could create a token like:

18936.1304861680.2A956E39.11E859E44B9308B812257BEE660330D9D0566189

where 2A956E39 is some random salt, and the bit at the end is the hex-encoded HMAC-SHA1 hash of 18936.1304861680.2A956E39 using the not-very-good secret key secretkey.

This achieves the anti-XSRF purpose, but sometimes the one-time-server-stored-token is also used to prevent a form double-submission. As-is, the hash method doesn't help with that bit. But in the specific case of creating a new entity, which is a common place double-submission-prevention is deployed, you can use the token as a unique value inserted into the DB as part of the new entity, and then refuse to create a new entity if there's already one with the token in it.

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