当前位置:网站首页>PHP 中的跨站请求伪造
PHP 中的跨站请求伪造
2022-07-25 18:40:00 【allway2】
Cross-Site Request Forgery in PHP | SecureFlag Security Knowledge Base
预防
PHP 不提供针对 CSRF 攻击的内置保护;开发人员必须通过检查会话令牌或使用许多经过良好测试的库和框架之一来手动实现它。
PHP
以下步骤展示了如何action.php?do=logout使用 Synchronizer Token Pattern 保护端点免受 CSRF 攻击。
- 成功验证后,会以安全方式生成随机令牌并存储为用户的会话变量。
<?php
$_SESSION["csrf_token"] = bin2hex(random_bytes(32));
?>- 现在,对状态更改端点的任何请求都
action.php?do=logout必须将令牌作为 HTTP 参数传递。以下代码段显示了如何编写<a href>标记以将令牌作为 GET 参数传递。
<a href="action.php?do=logout&csrf=<?php echo $_SESSION["csrf_token"]; ?>">
- 以下代码段显示了如何通过将令牌作为隐藏字段传递来保护表单。
<form>
<input type="hidden" name="csrf" value="<?php echo $_SESSION["csrf_token"]; ?>">
</form>- 该文件
action.php现在必须在执行受保护的代码之前验证csrf与保存在用户会话中的令牌相匹配的参数。使用安全的哈希比较函数,例如 PHPhash_equals()。
if (!empty($_REQUEST["csrf"]) && hash_equals($_REQUEST["csrf_token"], $_SESSION["csrf_token"])) {
// The token is correct, execute the functionality.
}
else {
die("Anti CSRF token is missing or wrong.");
}OWASP CSRF 保护器
OWASP CSRF Protector 是一个独立的 php 库,用于 Web 应用程序中的 CSRF 缓解。按照项目页面上的说明进行安装。要使用它,只需包含库并调用init()函数。
<?php
include_once __DIR__ .'/vendor/owasp/csrf-protector-php/libs/csrf/csrfprotector.php';
// Initialise CSRFGuard library
csrfProtector::init();Symfony 框架
Symfony 表单组件默认提供自动 CSRF 保护。其他类型的资源,如常规 HTML 表单或任何其他状态更改路由,必须通过手动生成和检查 CSRF 令牌来明确保护。
考虑使用常规 HTML 表单来删除资源。首先,使用csrf_token()Twig 函数在模板中生成一个 CSRF 令牌,并将其存储为隐藏的表单字段。
<form action="/delete" method="post">
<input type="hidden" name="token" value="{
{ csrf_token('action-delete') }}"/>
</form>然后,在控制器动作中获取 CSRF 令牌的值,并使用isCsrfTokenValid()来检查其有效性。
public function delete(Request $request)
{
$submittedToken = $request->request->get('token');
if (!$this->isCsrfTokenValid('action-delete', $submittedToken)) {
// The token is not correct, redirect to the index.
return $this->redirectToRoute('index');
}
// The token is correct, execute the functionality.
}参考
OWASP -跨站点请求伪造备忘单MITRE - CWE 352 OWASP - CSRF 保护器Symfony -如何实现 CSRF 保护
边栏推荐
- RTC 性能自动化工具在内存优化场景下的实践
- pd.melt() vs reshape2::melt()
- How developers choose the right database for react native
- 2022 Robocom 省赛题解
- 上半年出货量已超去年全年,森思泰克毫米波雷达“夺食”国际巨头
- Safe operation instructions for oscilloscope probe that must be read by engineers
- 大厂云业务调整,新一轮战争转向
- Advanced software testing - test classification
- 【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
- 什么是hpaPaaS平台?
猜你喜欢

人人可参与开源活动正式上线,诚邀您来体验!

With a market value of 30billion yuan, the largest IPO in Europe in the past decade was re launched on the New York Stock Exchange

微软Azure和易观分析联合发布《企业级云原生平台驱动数字化转型》报告

Powell's function of Ceres

Care for front-line epidemic prevention workers, Haocheng JIAYE and Gaomidian sub district office jointly build the great wall of public welfare

Detailed explanation of super full mavan label

3DE reply

Flexible current probe selection guide

CircleIndicator组件,使指示器风格更加多样化

黄鹤楼超震撼视角,这样的VR全景你绝对没见过!
随机推荐
Software testing -- common testing tools
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
What is 3DE experience platform
动态内存管理
Project: serial port receiving RAM storage TFT display (complete design)
The auction house is a VC, and the first time it makes a move, it throws a Web3
Analysis of regression problem, modeling and prediction
You can change this value on the server by setting the 'Max_ allowed_ Packet 'variable error
F5:企业数字化转型所需六大能力
如何创建一个有效的帮助文档?
Paper revision reply 1
Dynamic memory management
遍历数组的方法有哪些?for循环 forEach for/in for/of map的性能又有什么差别 该如何选择?
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
这届年轻人,为“丑东西”有多上头?
Practice of RTC performance automation tool in memory optimization scenario
Register carefully! The number of applicants for these double non-governmental institutions exceeded 10000!
[QNX Hypervisor 2.2用户手册]9.4 dryrun
推特收购舆论战,被马斯克变成了小孩吵架
「跨链互连智能合约」解读