php ci csrf,CI的CSRF的改造
CI的CSRF是有缺陷的。
只要同时开俩个不同的涉及csrf的页面,
http://host/csrf1
http://host/csrf2
就会发现页面直接互相影响(问题1)。
即使同一页面也涉及这样的问题。
http://host/csrf1
http://host/csrf1
也会发现这样的问题(问题2)。
先解决简单问题2
只需要将配置 csrf_regenerate 设置为 false; 即是cookie过期或者被清掉。$config['csrf_regenerate'] = FALSE;
解决复杂问题1:
一. 将system/core/Input 中的 验证代码过滤。// CSRF Protection check
if ($this->_enable_csrf === TRUE && ! is_cli())
{
//$this->security->csrf_verify();
}
二. 改造Security类class CI_Security {
/**
* Class constructor
*
* @returnvoid
*/
public function __construct()
{
$this->charset = strtoupper(config_item('charset'));
log_message('info', 'Security Class Initialized');
}
public function start_csrf($class, $function)
{
// Is CSRF protection enabled?
if (config_item('csrf_protection'))
{
if(!$class || !$function){
return ;
}
$this->_csrf_cookie_name = md5($class.'_'.$function);
// CSRF config
foreach (array(
'csrf_expire',
'csrf_token_name',
//'csrf_cookie_name',
) as $key)
{
if (NULL !== ($val = config_item($key)))
{
$this->{'_'.$key} = $val;
}
}
// Append application specific cookie prefix
if ($cookie_prefix = config_item('cookie_prefix'))
{
//$this->_csrf_cookie_name = $cookie_prefix.$this->_csrf_cookie_name;
}
// Set the CSRF hash
$this->_csrf_set_hash();
$this->csrf_set_cookie();
}
}
// --------------------------------------------------------------------
/**
* CSRF Verify
*
* @returnCI_Security
*/
public function csrf_verify($class, $function)
{
if (!config_item('csrf_protection')){
return ;
}
if(!$class || !$function){
return ;
}
$this->_csrf_cookie_name = md5($class.'_'.$function);
// CSRF config
foreach (array(
'csrf_expire',
'csrf_token_name',
//'csrf_cookie_name',
) as $key)
{
if (NULL !== ($val = config_item($key)))
{
$this->{'_'.$key} = $val;
}
}
// If it's not a POST request we will set the CSRF cookie
if (strtoupper($_SERVER['REQUEST_METHOD']) !== 'POST')
{
//return $this->csrf_set_cookie();
$this->csrf_show_error();
}
// Check if URI has been whitelisted from CSRF checks
if ($exclude_uris = config_item('csrf_exclude_uris'))
{
$uri = load_class('URI', 'core');
foreach ($exclude_uris as $excluded)
{
if (preg_match('#^'.$excluded.'$#i'.(UTF8_ENABLED ? 'u' : ''), $uri->uri_string()))
{
return $this;
}
}
}
// Check CSRF token validity, but don't error on mismatch just yet - we'll want to regenerate
$valid = isset($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name])
&& hash_equals($_POST[$this->_csrf_token_name], $_COOKIE[$this->_csrf_cookie_name]);
// We kill this since we're done and we don't want to pollute the _POST array
unset($_POST[$this->_csrf_token_name]);
// Regenerate on every submission?
if (config_item('csrf_regenerate'))
{
// Nothing should last forever
unset($_COOKIE[$this->_csrf_cookie_name]);
$this->_csrf_hash = NULL;
}
$this->_csrf_set_hash();
$this->csrf_set_cookie();
if ($valid !== TRUE)
{
$this->csrf_show_error();
}
log_message('info', 'CSRF token verified');
return $this;
}
// --------------------------------------------------------------------
/**
* CSRF Set Cookie
*
* @codeCoverageIgnore
* @returnCI_Security
*/
public function csrf_set_cookie()
{
if (!config_item('csrf_protection')){
return ;
}
$expire = time() + $this->_csrf_expire;
$secure_cookie = (bool) config_item('cookie_secure');
if ($secure_cookie && ! is_https())
{
return FALSE;
}
setcookie(
$this->_csrf_cookie_name,
$this->_csrf_hash,
$expire,
config_item('cookie_path'),
config_item('cookie_domain'),
$secure_cookie,
config_item('cookie_httponly')
);
log_message('info', 'CSRF cookie sent');
return $this;
}
// --------------------------------------------------------------------
/**
* Set CSRF Hash and Cookie
*
* @returnstring
*/
protected function _csrf_set_hash()
{
if (!config_item('csrf_protection')){
return ;
}
if ($this->_csrf_hash === NULL)
{
// If the cookie exists we will use its value.
// We don't necessarily want to regenerate it with
// each page load since a page could contain embedded
// sub-pages causing this feature to fail
if (isset($_COOKIE[$this->_csrf_cookie_name]) && is_string($_COOKIE[$this->_csrf_cookie_name])
&& preg_match('#^[0-9a-f]{32}$#iS', $_COOKIE[$this->_csrf_cookie_name]) === 1)
{
return $this->_csrf_hash = $_COOKIE[$this->_csrf_cookie_name];
}
$rand = $this->get_random_bytes(16);
$this->_csrf_hash = ($rand === FALSE)
? md5(uniqid(mt_rand(), TRUE))
: bin2hex($rand);
}
return $this->_csrf_hash;
}
}
三.使用实例
controller<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function csrf_test1()
{
if($_POST){
$this->security->csrf_verify(__CLASS__, __FUNCTION__);
var_dump($_POST);
exit;
}
$this->security->start_csrf(__CLASS__, __FUNCTION__);
$csrf = array(
'name' => $this->security->get_csrf_token_name(),
'hash' => $this->security->get_csrf_hash()
);
$data['csrf'] = $csrf;
$this->load->view('csrf1', $data);
}
}
view<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
html>
Welcome to CodeIgniter
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
Welcome to CodeIgniter!
用户名: | ||
密 码: | ||
" value="=$csrf['hash'];?>" /> |
---|
php ci csrf,CI的CSRF的改造相关推荐
- 个人踩坑记录Lighthouse ci Lighthouse CI Server
Lighthouse 是一款检测网页质量的自动化工具,Google 提供的开源软件,任何网站都可以免费使用.可以检查的网站性能.可访问性.SEO 等,并给出优化建议. 安装 Lighthouse 作为 ...
- python middleware模块_详解利用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击...
一.在django后台处理 1.将django的setting中的加入django.contrib.messages.middleware.MessageMiddleware,一般新建的django项 ...
- php ci hooks,CI框架 -- 核心文件 之 Hooks.php
1 /**2 * 钩子嘛,就是在不修改系统核心文件的基础上来改变或增加系统的核心运行功能3 */ 4 classCI_Hooks {5 6 /**7 * 检测hook是否开启8 */ 9 var $e ...
- spring security CSRF 问题 Invalid CSRF Token 'null' was found on ......
1. 问题 前面几篇博客 spring security在集成spring boot的微服务框架后,实现了cas认证和权限控制.但是在使用 postman 进行调用的时候出现这个问题 HTTP Sta ...
- php ci 参数,CI如何写一个控制器,并传递参数正常解析访问?
控制器 控制器是你整个应用的核心,因为它们决定了 HTTP 请求将被如何处理. 什么是控制器? 简而言之,一个控制器就是一个类文件,是以一种能够和 URI 关联在一起的方式来命名的. 考虑下面的 UR ...
- php ci oracle,CI连接Oracle 11G数据库
CI框架算是个人最喜欢的PHP框架之一,易用性上没的说,还有完备的中文文档,不过大多数时候是搭配MySQL一起使用. 不过最近接触的一个项目使用的是Oracle 11G数据库,开发前给大家搭环境的时候 ...
- php ci框架结构,CI框架目录结构分析
application:具体项目开发目录: system:CI框架代码: user_guide:用户手册,和实现无关,删掉也不影响 index.php:唯一的入口文件,除了这个文件外其他php文件都不 ...
- Spring Security4 CSRF 如何关闭CSRF功能
什么是CSRF? csrf又称跨域请求伪造,攻击方通过伪造用户请求访问受信任站点.CSRF这种攻击方式在2000年已经被国外的安全人员提出,但在国内,直到06年才开始被关注,08年,国内外的多个大型社 ...
- django种表单post出现CSRF verification failed( CSRF验证失败 ) 的两种解决方案
现象 表单界面如下: 在点击提交之后,出现如下错误页面: HTML的代码如下: contact_form.html <!DOCTYPE HTML PUBLIC ><html> ...
最新文章
- opencv-python之机器视觉
- java 静态与非静态之间的访问规则简述
- 超适合小白的python新手教程
- Windows学习总结(20)——Win10 子系统Linux(Ubuntu 18.04)的安装与卸载
- 一个 TypeScript keyof 泛型用法
- DeFi一体化平台Parsec获125万美元种子轮融资并正式启动
- EDB*Plus的当前路径问题
- 1.4.1 启动与销毁Activity
- Windroy Lets Android run on Windows systems-- 国外androids 虚拟系统分享
- C++中容器的使用(二)
- 秒杀/抢购系统设计优化
- Panabit应用层流量管理系统
- 6.3 交通工具类
- L1-039古风排版
- Kubernetes存储Longhorn
- 微信app支付功能-服务端的实现-python3版
- 怎么检测计算机硬件好坏,鲁大师如何检测硬件好坏?硬件好坏检测方法介绍
- 考研/嵌入式/我的所思所想及其他
- HTML + js 播放amr音频文件
- Scikit-learn学习系列 | 4. sklearn特征降维方法汇总(方差过滤,卡方,F过滤,互信息,嵌入法)
热门文章
- 风格:原理图中电容电阻电感的取值命名风格
- Microsoft JET Engine的完美替代: SQL Server Compact Version 3.5和完美的Synchronizer库
- Airtest清除文本框内容
- Pixel-安卓系统刷机指南
- 同步助手怎么用?同步推(助手)使用教程图解
- golang设计哲学
- pp助手不能关联ipa 提示用管理员身份运行
- css怎么动画中该透明度,CSS如何实现透明度变化的动画
- 【爬虫】使用beautifulsoup、requests爬取网页上的图片;循环爬取上市公司高管信息
- google chrome:版本列表