PHP漏洞全解-跨网站请求伪造
时间:2016年08月10日 人气:...


防范方法
防范CSRF要比防范其他攻击更加困难,因为CSRF的HTTP请求虽然是攻击者伪造的,但是却是由目标用户发出的,一般常见的防范方法有下面几种:
1、检查网页的来源
2、检查内置的隐藏变量
3、使用POST,不要使用GET
检查网页来源
在//pass.php头部加入以下红色字体代码,验证数据提交
if($_GET[“act”])
{
if(isset($_SERVER[“HTTP_REFERER”]))
{
$serverhost = $_SERVER[“SERVER_NAME”];
$strurl = str_replace(“http://”,””,$_SERVER[“HTTP_REFERER”]);
$strdomain = explode(“/”,$strurl);
$sourcehost = $strdomain[0];
if(strncmp($sourcehost, $serverhost, strlen($serverhost)))
{
unset($_POST);
echo “”;
echo “alert(‘数据来源异常!’);”;
echo ” location=’index.php’;”;
echo “”;
}
}
$username=$_POST[“username”];
$sh=$_POST[“sh”];
$gg=$_POST[“gg”];
$title=$_POST[“title”];
$copyright=$_POST[“copyright”].”
设计制作:厦门随缘网络科技”;
$password=md5($_POST[“password”]);
if(emptyempty($_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 “”;
echo “alert(‘修改成功!’);”;
echo ” location=’pass.php’;”;
echo “”;
}
检查内置隐藏变量
我们在表单中内置一个隐藏变量和一个session变量,然后检查这个隐藏变量和session变量是否相等,以此来判断是否同一个网页所调用
php
include_once("dlyz.php");include_once("../conn.php");if($_GET["act"]){if (!isset($_SESSION["post_id"])){// 生成唯一的ID,并使用MD5来加密$post_id = md5(uniqid(rand(), true));// 创建Session变量$_SESSION["post_id"] = $post_id;}// 检查是否相等if (isset($_SESSION["post_id"])){// 不相等if ($_SESSION["post_id"] != $_POST["post_id"]){// 清除POST变量unset($_POST);echo "<script language=’javascript’>";echo "alert(‘数据来源异常!’);";echo " location=’index.php’;";echo "script>";}}……<input type="reset" name="Submit2" value="重 置"><input type="hidden" name="post_id" value="php echo $_SESSION["post_id"];?>">td>tr>table>form>php}mysql_close($conn);?></body>
</html>
使用POST,不要使用GET
传递表单字段时,一定要是用POST,不要使用GET,处理变量也不要直接使用$_REQUEST

热门评论