网站建设资讯

NEWS

网站建设资讯

php防注入数据过滤 phpsql防注入函数

如何防止代码注入攻击在PHP

一,HTML防注入。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名与空间、网页空间、营销软件、网站建设、仁和网站维护、网站推广。

一般的html注入都是在字符串中加入了html标签,用下JAVA代码可以去掉这部分代码。

代码如下,自己封装成方法即可。

String msge = "asdasdasdasd div id=\"f\"asdfsdf";

System.out.println(msge);

msge = msge.replace("", "");

msge = msge.replace("", "");

msge = msge.replace(" ", " ");

msge = msge.replace("", "");

msge = msge.replace("\"", """);

msge = msge.replace("'", "qpos;");

System.out.println(msge);

二、防SQL注入

最简单最容易的是限制用户输入。

简单点的就是不允许用户输入单引号 和 --,因为单引号号--在SQL中都是影响执行的。

但SQL注入是多方面的,防止的方法也有很多种。

1、地址栏禁止特殊字符防SQL注入

把特殊字符(如and、or、'、")都禁止提交就可以防止注入了。

2、php过滤html字符串,防止SQL注入

批量过滤post,get敏感数据

$_GET = stripslashes_array($_GET);

$_POST = stripslashes_array($_POST);

数据过滤函数

function stripslashes_array($array) {

while(list($key,$var) = each($array)) {

if ($key != 'argc' $key != 'argv' (strtoupper($key) != $key || ''.intval($key) == "$key")) {

if (is_string($var)) {

$array[$key] = stripslashes($var);

}

if (is_array($var)) {

$array[$key] = stripslashes_array($var);

}

}

}

return $array;

}

3、替换HTML尾标签

function lib_replace_end_tag($str)

{

if (empty($str)) return false;

$str = htmlspecialchars($str);

$str = str_replace( '/', "", $str);

$str = str_replace("\\", "", $str);

$str = str_replace("", "", $str);

$str = str_replace("", "", $str);

$str = str_replace("SCRIPT", "", $str);

$str = str_replace("/SCRIPT", "", $str);

$str = str_replace("script", "", $str);

$str = str_replace("/script", "", $str);

$str=str_replace("select","select",$str);

$str=str_replace("join","join",$str);

$str=str_replace("union","union",$str);

$str=str_replace("where","where",$str);

$str=str_replace("insert","insert",$str);

$str=str_replace("delete","delete",$str);

$str=str_replace("update","update",$str);

$str=str_replace("like","like",$str);

$str=str_replace("drop","drop",$str);

$str=str_replace("create","create",$str);

$str=str_replace("modify","modify",$str);

$str=str_replace("rename","rename",$str);

$str=str_replace("alter","alter",$str);

$str=str_replace("cas","cast",$str);

$str=str_replace("","",$str);

$str=str_replace("","",$str);

$str=str_replace("","",$str);

$str=str_replace(" ",chr(32),$str);

$str=str_replace(" ",chr(9),$str);

$str=str_replace(" ",chr(9),$str);

$str=str_replace("",chr(34),$str);

$str=str_replace("'",chr(39),$str);

$str=str_replace("br /",chr(13),$str);

$str=str_replace("''","'",$str);

$str=str_replace("css","'",$str);

$str=str_replace("CSS","'",$str);

return $str;

}

三、专业的事情交给专业的工具去做。

安装安全软件。例如,在服务器中安装“服务器安全狗”,可以设置防注入,防攻击的设置,只要设置好安全规则,就可以屏蔽大多数攻击入侵。

php 关于thinkphp的防sql注入跟过滤问题

防止SQL注入

opensns

对于WEB应用来说,SQL注入攻击无疑是首要防范的安全问题,系统底层对于数据安全方面本身进行了很多的处理和相应的防范机制,例如:

$User = M("User"); // 实例化User对象

$User-find($_GET["id"]);

即便用户输入了一些恶意的id参数,系统也会强制转换成整型,避免恶意注入。这是因为,系统会对数据进行强制的数据类型检测,并且对数据来源进行数据格式转换。而且,对于字符串类型的数据,ThinkPHP都会进行escape_string处理(real_escape_string,mysql_escape_string)。

通常的安全隐患在于你的查询条件使用了字符串参数,然后其中一些变量又依赖由客户端的用户输入,要有效的防止SQL注入问题,我们建议:

查询条件尽量使用数组方式,这是更为安全的方式;

如果不得已必须使用字符串查询条件,使用预处理机制(3.1版本新增特性);

开启数据字段类型验证,可以对数值数据类型做强制转换;(3.1版本开始已经强制进行字段类型验证了)

使用自动验证和自动完成机制进行针对应用的自定义过滤;

字段类型检查、自动验证和自动完成机制我们在相关部分已经有详细的描述。

查询条件预处理

where方法使用字符串条件的时候,支持预处理(安全过滤),并支持两种方式传入预处理参数,例如:

$Model-where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))-select();

或者

$Model-where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)-select();

模型的query和execute方法 同样支持预处理机制,例如:

$model-query('select * from user where id=%d and status=%d',$id,$status);

或者

$model-query('select * from user where id=%d and status=%d',array($id,$status));

execute方法用法同query方法。

ThinkPHP如何防止SQL注入?

(1)查询条件尽量使用数组方式,这是更为安全的方式;

(2)如果不得已必须使用字符串查询条件,使用预处理机制;

(3)使用绑定参数;

(4)强制进行字段类型验证,可以对数值数据类型做强制转换;

(5)使用自动验证和自动完成机制进行针对应用的自定义过滤;

(6)使用字段类型检查、自动验证和自动完成机制等避免恶意数据的输入;

(7)做一些过滤。

求php防止被sql 注入攻击的过滤用户输入内容的函数

function clean($v) { 

//判断magic_quotes_gpc是否为打开

if (!get_magic_quotes_gpc()) {

//进行magic_quotes_gpc没有打开的情况对提交数据的过滤

$v = addslashes($v);

}

//把'_'过滤掉

$v = str_replace("_", "\_", $v);

//把'%'过滤掉 

$v = str_replace("%", "\%", $v);

//把'*'过滤掉 

$v = str_replace("*", "\*", $v);

//回车转换

$v = nl2br($v);

//html标记转换

$v = htmlspecialchars($v);

return $v; 

}

如果需要,还可以屏蔽一下危险字符,例如insert, update, delete等

//将update去掉

$v = str_replace("update", "", $v);

最后,在拼装sql语句时,用户输入的东西,全括在单引号内


网页名称:php防注入数据过滤 phpsql防注入函数
文章来源:http://cdweb.net/article/dodeepg.html