当前位置:网站首页>Cross Site Request Forgery in PHP
Cross Site Request Forgery in PHP
2022-07-25 18:51:00 【allway2】
Cross-Site Request Forgery in PHP | SecureFlag Security Knowledge Base
The prevention of
PHP Not available for CSRF Built in protection against attacks ; Developers must implement it manually by checking session tokens or using one of many well tested libraries and frameworks .
PHP
The following steps show how action.php?do=logout Use Synchronizer Token Pattern Protect endpoints from CSRF attack .
- After successful verification , A random token will be generated in a secure manner and stored as a user's session variable .
<?php
$_SESSION["csrf_token"] = bin2hex(random_bytes(32));
?>- Now? , Any request for a state change endpoint
action.php?do=logoutThe token must be used as HTTP Parameter passing . The following code snippet shows how to write<a href>Mark the token as GET Parameter passing .
<a href="action.php?do=logout&csrf=<?php echo $_SESSION["csrf_token"]; ?>">
- The following code snippet shows how to protect a form by passing a token as a hidden field .
<form>
<input type="hidden" name="csrf" value="<?php echo $_SESSION["csrf_token"]; ?>">
</form>- This document
action.phpNow you must verify before executing the protected codecsrfParameters that match the token stored in the user session . Use a secure hash comparison function , for example 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 Protector
OWASP CSRF Protector It's an independent php library , be used for Web In the application CSRF relieve . according to Project page Install according to the instructions on . In order to use it , Just include the library and call init() function .
<?php
include_once __DIR__ .'/vendor/owasp/csrf-protector-php/libs/csrf/csrfprotector.php';
// Initialise CSRFGuard library
csrfProtector::init();Symfony frame
Symfony By default, the form component provides automatic CSRF Protect . Other types of resources , As usual HTML Form or any other state change route , Must be generated and checked manually CSRF Token to explicitly protect .
Consider using conventional HTML Form to delete resources . First , Use csrf_token()Twig Function generates a CSRF token , And store it as a hidden form field .
<form action="/delete" method="post">
<input type="hidden" name="token" value="{
{ csrf_token('action-delete') }}"/>
</form> then , Get in the controller action CSRF The value of the token , And use isCsrfTokenValid() To check its effectiveness .
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.
}Reference resources
OWASP - Cross Site Request Forgery memo MITRE - CWE 352 OWASP - CSRF Protector Symfony - How to achieve CSRF Protect
边栏推荐
- Ping 命令详解[通俗易懂]
- Circulaindicator component, which makes the indicator style more diversified
- 2022 robocom provincial competition solution
- 蓝牙协议详解(蓝牙是什么)
- rust多线程安全计数
- Analysis of the internet jam in IM development? Network disconnection?
- Virtual machine VMware installation steps (how to install software in virtual machine)
- With a financing of 200million yuan, the former online bookstore is now closed nationwide, with only 3 stores left in 60 stores
- 阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
- What is hpapaas platform?
猜你喜欢

Introduction notes of JVM foundation and problem analysis

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

曾拿2亿融资,昔日网红书店如今全国闭店,60家店仅剩3家

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

对迁移学习中域适应的理解和3种技术的介绍

Process communication (Systemv communication mode: shared memory, message queue, semaphore)

Twitter acquired a public opinion war, which was turned into a child quarrel by musk

给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享

什么是3DE体验平台

Project: serial port receiving RAM storage TFT display (complete design)
随机推荐
优维低代码:Use Resolves
F5: Six capabilities required for enterprise digital transformation
给生活加点惊喜,做创意生活的原型设计师丨编程挑战赛 x 选手分享
Yes, UDP protocol can also be used to request DNS server
Analysis of the internet jam in IM development? Network disconnection?
淦,为什么 ““ .length !== 3 ??
大厂云业务调整,新一轮战争转向
jmeter性能测试实战视频(常用性能测试工具有哪些)
The understanding of domain adaptation in transfer learning and the introduction of three technologies
Typescript reflection object reflection use
东北人,最懂性感
Osmosis extends its cross chain footprint to poca through integration with axelar and moonbeam
对迁移学习中域适应的理解和3种技术的介绍
F5:企业数字化转型所需六大能力
Process communication (Systemv communication mode: shared memory, message queue, semaphore)
15. Simple salary management system design
2022 robocom provincial competition solution
Typescript反射对象Reflect使用
CS615 System Administration, Week 02, Segment 5 - Partitions
ServletConfig class and ServletContext class