--- title: Cross Site Request Forgery localeTitle: 跨站点请求伪造 --- ## 跨站点请求伪造 跨站点请求伪造是应用程序中的一个漏洞,由程序员无法检查请求的发送位置 - 此攻击被发送给高权限级别用户以获得对应用程序的更高级别访问权限。 ### 示例跨站请求伪造攻击 在线博客允许用户提交评论并在评论中包含图像,博客的管理面板允许博客作者通过加载URL `/admin/deletecomment.php?id=123`删除评论。恶意用户可以制作加载删除评论网址的图片标记,例如``以便管理员在下次查看评论时,管理员的计算机将加载网址并删除评论编号123。 ### 在PHP中保护您的网站免受跨站点请求伪造攻击 要防止跨站点请求伪造攻击,您应该检查定期更改的令牌。 url `/admin/deletecomment.php?id=123`将更改为`/admin/deletecomment.php?id=123&csrf-token=random-per-user-unique-string-here` 。 ```PHP Delete Comment'; // Only the logged in user has access to the CSRF Token - the token isn't accessible to the attacker preventing their attack from being successful. ``` #### 更多信息: * [OWASP Wiki - 跨站请求伪造](https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)) * [php.net bin2hex()手册](https://secure.php.net/manual/en/function.bin2hex.php) * [php.net openssl\_random\_pseudo\_bytes()手册](https://secure.php.net/manual/en/function.openssl-random-pseudo-bytes.php)