Home php教程 php手册 php漏洞之跨网站请求伪造与防止伪造方法

php漏洞之跨网站请求伪造与防止伪造方法

Jun 13, 2016 am 11:41 AM
php and introduce forgery harm attacker method Compare loopholes website ask prevent precaution difficult

伪造跨站请求介绍
伪造跨站请求比较难以防范,而且危害巨大,攻击者可以通过这种方式恶作剧,发spam信息,删除数据等等。这种攻击常见的表现形式有:
  伪造链接,引诱用户点击,或是让用户在不知情的情况下访问
  伪造表单,引诱用户提交。表单可以是隐藏的,用图片或链接的形式伪装。
  比较常见而且也很廉价的防范手段是在所有可能涉及用户写操作的表单中加入一个随机且变换频繁的字符串,然后在处理表单的时候对这个字符串进行检查。这个随机字符串如果和当前用户身份相关联的话,那么攻击者伪造请求会比较麻烦。
如果攻击者以隐藏的方式发送给目标用户链接
php漏洞之跨网站请求伪造与防止伪造方法,那么如果目标用户不小心访问以后,购买的数量就成了1000个
实例
随缘网络PHP留言板V1.0

复制代码 代码如下:


任意删除留言
//delbook.php 此页面用于删除留言
include_once("dlyz.php");    //dlyz.php用户验证权限,当权限是admin的时候方可删除留言
include_once("../conn.php");
$del=$_GET["del"];
$id=$_GET["id"];
if ($del=="data")
{
$ID_Dele= implode(",",$_POST['adid']);
$sql="delete from book where id in (".$ID_Dele.")";
mysql_query($sql);
}
else
{
$sql="delete from book where id=".$id; //传递要删除的留言ID
mysql_query($sql);
}
mysql_close($conn);
echo "";
?>


当我们具有admin权限,提交http://localhost/manage/delbook.php?id=2 时,就会删除id为2的留言
利用方法:
我们使用普通用户留言(源代码方式),内容为

复制代码 代码如下:


php漏洞之跨网站请求伪造与防止伪造方法
php漏洞之跨网站请求伪造与防止伪造方法
php漏洞之跨网站请求伪造与防止伪造方法
php漏洞之跨网站请求伪造与防止伪造方法


插入4张图片链接分别删除4个id留言,然后我们返回首页浏览看,没有什么变化。。图片显示不了
现在我们再用管理员账号登陆后,来刷新首页,会发现留言就剩一条,其他在图片链接中指定的ID号的留言,全部都被删除。
攻击者在留言中插入隐藏的图片链接,此链接具有删除留言的作用,而攻击者自己访问这些图片链接的时候,是不具有权限的,所以看不到任何效果,但是当管理员登陆后,查看此留言,就会执行隐藏的链接,而他的权限又是足够大的,从而这些留言就被删除了
修改管理员密码

复制代码 代码如下:


//pass.php
if($_GET["act"])
{
$username=$_POST["username"];
$sh=$_POST["sh"];
$gg=$_POST["gg"];
$title=$_POST["title"];
$copyright=$_POST["copyright"]."
网站:脚本之家";
$password=md5($_POST["password"]);
if(empty($_POST["password"]))
{
$sql="update gly set username='".$username."',sh=".$sh.",gg='".$gg."',title='".$title."',copyright='".$copyright."' where id=1";
}
else
{
$sql="update gly set username='".$username."',password='".$password."',sh=".$sh.",gg='".$gg."',title='".$title."',copyright='".$copyright."' where id=1";
}
mysql_query($sql);
mysql_close($conn);
echo "";
}
这个文件用于修改管理密码和网站设置的一些信息,我们可以直接构造如下表单:













存为attack.html,放到自己网站上http://www.jb51.net此页面访问后会自动向目标程序的pass.php提交参数,用户名修改为root,密码修改为root,然后我们去留言板发一条留言,隐藏这个链接,管理访问以后,他的用户名和密码全部修改成了root
防止伪造跨站请求
yahoo对付伪造跨站请求的办法是在表单里加入一个叫.crumb的随机串;而facebook也有类似的解决办法,它的表单里常常会有post_form_id和fb_dtsg。
随机串代码实现
咱们按照这个思路,山寨一个crumb的实现,代码如下:

复制代码 代码如下:


class Crumb {
CONST SALT = "your-secret-salt";
static $ttl = 7200;
static public function challenge($data) {
return hash_hmac('md5', $data, self::SALT);
}
static public function issueCrumb($uid, $action = -1) {
$i = ceil(time() / self::$ttl);
return substr(self::challenge($i . $action . $uid), -12, 10);
}
static public function verifyCrumb($uid, $crumb, $action = -1) {
$i = ceil(time() / self::$ttl);
if(substr(self::challenge($i . $action . $uid), -12, 10) == $crumb ||
substr(self::challenge(($i - 1) . $action . $uid), -12, 10) == $crumb)
return true;
return false;
}
}


代码中的$uid表示用户唯一标识,而$ttl表示这个随机串的有效时间。
  应用示例
  构造表单
  在表单中插入一个隐藏的随机串crumb

复制代码 代码如下:








处理表单 demo.php
  对crumb进行检查

复制代码 代码如下:


if(Crumb::verifyCrumb($uid, $_POST['crumb'])) {
//按照正常流程处理表单
} else {
//crumb校验失败,错误提示流程
}
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles