跨站请求伪造 (csrf) 是一种网络安全漏洞,攻击者可以利用该漏洞诱骗经过身份验证的用户在他们当前登录的网站上执行不需要的操作。该攻击通过利用网站所拥有的信任来进行在用户的浏览器中。
这是最常用的方法。实现方法如下:
// in your session initialization (e.g., at login) session_start(); if (empty($_session['csrf_token'])) { $_session['csrf_token'] = bin2hex(random_bytes(32)); } // in your form function generateformwithcsrftoken() { return '<form method="post" action="/submit"> <input type="hidden" name="csrf_token" value="' . $_session['csrf_token'] . '"> <!-- rest of your form fields --> <input type="submit" value="submit"> </form>'; } // in your form processing function validatecsrftoken() { if (!isset($_post['csrf_token']) || !isset($_session['csrf_token']) || !hash_equals($_session['csrf_token'], $_post['csrf_token'])) { die('csrf token validation failed'); } return true; }
此方法使用带有自定义标头的 ajax 请求:
// php backend session_start(); if (empty($_session['csrf_token'])) { $_session['csrf_token'] = bin2hex(random_bytes(32)); } // validate the token if ($_server['request_method'] === 'post') { $headers = getallheaders(); if (!isset($headers['x-csrf-token']) || !hash_equals($_session['csrf_token'], $headers['x-csrf-token'])) { http_response_code(403); die('csrf token validation failed'); } } // javascript frontend const csrftoken = '<?php echo $_session["csrf_token"]; ?>'; fetch('/api/endpoint', { method: 'post', headers: { 'x-csrf-token': csrftoken, 'content-type': 'application/json' }, body: json.stringify(data) });
此方法涉及将令牌作为 cookie 和请求参数发送:
// set both cookie and session token session_start(); $token = bin2hex(random_bytes(32)); $_session['csrf_token'] = $token; setcookie('csrf_token', $token, [ 'httponly' => true, 'secure' => true, 'samesite' => 'strict' ]); // validation function function validatedoublesubmittoken() { if (!isset($_cookie['csrf_token']) || !isset($_post['csrf_token']) || !isset($_session['csrf_token'])) { return false; } return hash_equals($_cookie['csrf_token'], $_post['csrf_token']) && hash_equals($_session['csrf_token'], $_post['csrf_token']); }
现代应用程序还可以使用 samesite cookie 属性作为附加保护层:
// set cookie with samesite attribute session_start(); session_set_cookie_params([ 'lifetime' => 0, 'path' => '/', 'domain' => $_server['http_host'], 'secure' => true, 'httponly' => true, 'samesite' => 'strict' ]);
function generatesecuretoken($length = 32) { return bin2hex(random_bytes($length)); }
function validatetoken($usertoken, $storedtoken) { if (empty($usertoken) || empty($storedtoken)) { return false; } return hash_equals($storedtoken, $usertoken); }
class csrfprotection { public static function gettokenfield() { return sprintf( '<input type="hidden" name="csrf_token" value="%s">', htmlspecialchars($_session['csrf_token']) ); } }
许多 php 框架提供内置的 csrf 保护:
// in your form @csrf // manual token generation {{ csrf_field() }}
// In your form {{ csrf_token('form_name') }}
csrf 保护对于 web 应用程序安全至关重要。虽然实现 csrf 保护的方法有多种,但使用隐藏表单字段的基于令牌的方法仍然是使用最广泛、最可靠的方法。请记住结合不同的保护方法以增强安全性,并在 php 应用程序中实施 csrf 保护时始终遵循安全最佳实践。
请记住,csrf 保护应该是更广泛的安全策略的一部分,其中包括适当的会话管理、安全 cookie 处理和输入验证。