跨站请求伪造(英语:cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 csrf 或者 xsrf, 是一种挟制用户在当前已登录的web应用程序上执行非本意的操作的攻击方法。
跨站请求攻击,简单地说,是攻击者通过一些技术手段欺骗用户的浏览器去访问一个自己曾经认证过的网站并运行一些操作(如发邮件,发消息,甚至财产操作如转账和购买商品)。 (推荐学习:yii框架)
由于浏览器曾经认证过,所以被访问的网站会认为是真正的用户操作而去运行。
这利用了web中用户身份验证的一个漏洞:简单的身份验证只能保证请求发自某个用户的浏览器,却不能保证请求本身是用户自愿发出的。
yii2的csrf,这里简单介绍一下它的验证机制。
取用于csrf验证的token值;判断用于csrf的token是否存在,如果不存在则使用generatecsrftoken()生成。
验证web\controller中的beforeaction()方法中有yii::$app->getrequest()->validatecsrftoken()判断,用于验证csrf。
一般我的认识yii2的csrf都是从yii::$app->request->getcsrftoken()开始;好的,我们就从getcsrftoken()说起。 此方法在yii\web\request.php中:
/** * returns the token used to perform csrf validation. * 返回用于执行csrf验证的token * this token is a masked version of [[rawcsrftoken]] to prevent [breach attacks](http://breachattack.com/). * this token may be passed along via a hidden field of an html form or an http header value * to support csrf validation. * @param boolean $regenerate whether to regenerate csrf token. when this parameter is true, each time * this method is called, a new csrf token will be generated and persisted (in session or cookie). * @return string the token used to perform csrf validation. */public function getcsrftoken($regenerate = false){ if ($this->_csrftoken === null || $regenerate) { if ($regenerate || ($token = $this->loadcsrftoken()) === null) { //loadcsrftoken()就是在cookie或者session中获取token值 $token = $this->generatecsrftoken(); //如果token为空则调用generatecsrftoken()去生成 } // the mask doesn't need to be very random $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789_-.'; $mask = substr(str_shuffle(str_repeat($chars, 5)), 0, static::csrf_mask_length); // the + sign may be decoded as blank space later, which will fail the validation $this->_csrftoken = str_replace('+', '.', base64_encode($mask . $this->xortokens($token, $mask))); } return $this->_csrftoken;}/** * loads the csrf token from cookie or session. * @return string the csrf token loaded from cookie or session. null is returned if the cookie or session * does not have csrf token. */protected function loadcsrftoken(){ if ($this->enablecsrfcookie) { return $this->getcookies()->getvalue($this->csrfparam); //cookie中获取csrf的token } else { return yii::$app->getsession()->get($this->csrfparam); //session中获取csrf的token }}/** * creates a cookie with a randomly generated csrf token. * initial values specified in [[csrfcookie]] will be applied to the generated cookie. * @param string $token the csrf token * @return cookie the generated cookie * @see enablecsrfvalidation */protected function createcsrfcookie($token){ $options = $this->csrfcookie; $options['name'] = $this->csrfparam; $options['value'] = $token; return new cookie($options);}/** * generates an unmasked random token used to perform csrf validation. * @return string the random token for csrf validation. */protected function generatecsrftoken(){ $token = yii::$app->getsecurity()->generaterandomstring(); //生成随机的安全字符串 if ($this->enablecsrfcookie) { $cookie = $this->createcsrfcookie($token); //createcsrfcookie()用于生成csrf的key=>value形式的token yii::$app->getresponse()->getcookies()->add($cookie); //将生成key=>value保存到cookies } else { yii::$app->getsession()->set($this->csrfparam, $token); //将csrf的token存在session中 } return $token;}/** * 每次调用控制器中的方法的时候都会调用下面的yii::$app->getrequest()->validatecsrftoken()验证 * @inheritdoc */public function beforeaction($action){ if (parent::beforeaction($action)) { if ($this->enablecsrfvalidation && yii::$app->geterrorhandler()->exception === null && !yii::$app->getrequest()->validatecsrftoken()) { throw new badrequesthttpexception(yii::t('yii', 'unable to verify your data submission.')); } return true; } else { return false; }}/** * 校验方法 * performs the csrf validation. * * this method will validate the user-provided csrf token by comparing it with the one stored in cookie or session. * this method is mainly called in [[controller::beforeaction()]]. * * note that the method will not perform csrf validation if [[enablecsrfvalidation]] is false or the http method * is among get, head or options. * * @param string $token the user-provided csrf token to be validated. if null, the token will be retrieved from * the [[csrfparam]] post field or http header. * this parameter is available since version 2.0.4. * @return boolean whether csrf token is valid. if [[enablecsrfvalidation]] is false, this method will return true. */public function validatecsrftoken($token = null){ $method = $this->getmethod(); // only validate csrf token on non-"safe" methods http://www.w3.org/protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 if (!$this->enablecsrfvalidation || in_array($method, ['get', 'head', 'options'], true)) { return true; } $truetoken = $this->loadcsrftoken(); if ($token !== null) { return $this->validatecsrftokeninternal($token, $truetoken); } else { return $this->validatecsrftokeninternal($this->getbodyparam($this->csrfparam), $truetoken) || $this->validatecsrftokeninternal($this->getcsrftokenfromheader(), $truetoken); //getcsrftokenfromheader()这个我也不太理解,还请指点一下 }}/** * @return string the csrf token sent via [[csrf_header]] by browser. null is returned if no such header is sent. */public function getcsrftokenfromheader(){ $key = 'http_' . str_replace('-', '_', strtoupper(static::csrf_header)); return isset($_server[$key]) ? $_server[$key] : null;}/** * validates csrf token * * @param string $token * @param string $truetoken * @return boolean */private function validatecsrftokeninternal($token, $truetoken){ $token = base64_decode(str_replace('.', '+', $token)); //解码从客户端获取的csrf的token $n = stringhelper::bytelength($token); if ($n <= static::csrf_mask_length) { return false; } $mask = stringhelper::bytesubstr($token, 0, static::csrf_mask_length); $token = stringhelper::bytesubstr($token, static::csrf_mask_length, $n - static::csrf_mask_length); $token = $this->xortokens($mask, $token); return $token === $truetoken; //验证从客户端获取的csrf的token和真实的token是否相等}
以上就是yii csrf是什么的详细内容。