当前位置:首页 > 建站教程 > 正文内容

php防止sql注入和xss攻击代码

小宝1年前 (2022-12-08)建站教程589

过滤方法1

写一个过滤函数,可以防止sql注入的函数,过滤掉那些非法的字符,提高sql安全性,同时也可以过滤XSS的攻击。

function filter($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( 'CR', "", $str);
    $str = str_replace( 'ASCII', "", $str);
    $str = str_replace( 'ASCII 0x0d', "", $str);
    $str = str_replace( 'LF', "", $str);
    $str = str_replace( 'ASCII 0x0a', "", $str);
    $str = str_replace( ',', "", $str);
    $str = str_replace( '%', "", $str);
    $str = str_replace( ';', "", $str);
    $str = str_replace( 'eval', "", $str);
    $str = str_replace( 'open', "", $str);
    $str = str_replace( 'sysopen', "", $str);
    $str = str_replace( 'system', "", $str);
    $str = str_replace( '$', "", $str);
    $str = str_replace( "'", "", $str);
    $str = str_replace( "'", "", $str);
    $str = str_replace( 'ASCII 0x08', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace( '"', "", $str);
    $str = str_replace("", "", $str);
    $str = str_replace("&gt", "", $str);
    $str = str_replace("&lt", "", $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","",$str);
    $str = str_replace("join","",$str);
    $str = str_replace("union","",$str);
    $str = str_replace("where","",$str);
    $str = str_replace("insert","",$str);
    $str = str_replace("delete","",$str);
    $str = str_replace("update","",$str);
    $str = str_replace("like","",$str);
    $str = str_replace("drop","",$str);
    $str = str_replace("DROP","",$str);
    $str = str_replace("create","",$str);
    $str = str_replace("modify","",$str);
    $str = str_replace("rename","",$str);
    $str = str_replace("alter","",$str);
    $str = str_replace("cas","",$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);
    $str = str_replace("<!--","",$str);
    $str = str_replace("convert","",$str);
    $str = str_replace("md5","",$str);
    $str = str_replace("passwd","",$str);
    $str = str_replace("password","",$str);
    $str = str_replace("../","",$str);
    $str = str_replace("./","",$str);
    $str = str_replace("Array","",$str);
    $str = str_replace("or 1='1'","",$str);
    $str = str_replace(";set|set&set;","",$str);
    $str = str_replace("`set|set&set`","",$str);
    $str = str_replace("--","",$str);
    $str = str_replace("OR","",$str);
    $str = str_replace('"',"",$str);
    $str = str_replace("*","",$str);
    $str = str_replace("-","",$str);
    $str = str_replace("+","",$str);
    $str = str_replace("/","",$str);
    $str = str_replace("=","",$str);
    $str = str_replace("'/","",$str);
    $str = str_replace("-- ","",$str);
    $str = str_replace(" -- ","",$str);
    $str = str_replace(" --","",$str);
    $str = str_replace("(","",$str);
    $str = str_replace(")","",$str);
    $str = str_replace("{","",$str);
    $str = str_replace("}","",$str);
    $str = str_replace("-1","",$str);
    $str = str_replace("1","",$str);
    $str = str_replace(".","",$str);
    $str = str_replace("response","",$str);
    $str = str_replace("write","",$str);
    $str = str_replace("|","",$str);
    $str = str_replace("`","",$str);
    $str = str_replace(";","",$str);
    $str = str_replace("etc","",$str);
    $str = str_replace("root","",$str);
    $str = str_replace("//","",$str);
    $str = str_replace("!=","",$str);
    $str = str_replace("$","",$str);
    $str = str_replace("&","",$str);
    $str = str_replace("&&","",$str);
    $str = str_replace("==","",$str);
    $str = str_replace("#","",$str);
    $str = str_replace("@","",$str);
    $str = str_replace("mailto:","",$str);
    $str = str_replace("CHAR","",$str);
    $str = str_replace("char","",$str);    return $str;
}

过滤方法2

更加简便的防止sql注入的方法,我个人极力推荐使用这个。

if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否为打开    
 {    
    $post = addslashes($name); // magic_quotes_gpc没有打开的时候把数据过滤    
 }    
  
 $name = str_replace("_", "\_", $name); // 把 '_'过滤掉   
 $name = str_replace("%", "\%", $name); // 把' % '过滤掉    
 $name = nl2br($name); // 回车转换    
 $name= htmlspecialchars($name); // html标记转换     
 return $name;

过滤方法3

这是我自己平常所用的过滤方法,专用于php防XSS、防SQL注入的代码。

/**
 * 过滤参数
 * @param string $str 接受的参数
 * @return string
 */static public function filterWords($str){
    $farr = array(            "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",            "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",            "/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|dump/is"
    );
    $str = preg_replace($farr,'',$str);    return $str;
}   
/**
 * 过滤接受的参数或者数组,如$_GET,$_POST
 * @param array|string $arr 接受的参数或者数组
 * @return array|string
 */static public function filterArr($arr){    if(is_array($arr)){        foreach($arr as $k => $v){
            $arr[$k] = self::filterWords($v);
        }
    }else{
        $arr = self::filterWords($v);
    }    return $arr;
}

总结:

在防止被注入攻击时,常会用到两个函数:htmlspecialchars()和addslashes() 、trim()函数。这两个函数都是对特殊字符进行转义。

 

1、addslashes()作用及使用

addslashes() 通常用于防止sql注入,它可对通过get,post和cookie传递过来的参数的单引号和双引号已经null前加“”进行转义

如:如变量 $str=$_POST["str"];的值为:bb' or 1='1。通过addslashes()函数过滤后会变为:bb' or 1='1;

 

2、htmlspecialchars()作用及使用

htmlspecialchars()也是对字符进行转义,与addslashes()不同的是htmlspecialchars()是将特殊字符用引用实体替换。

 

3、addslashes()与htmlspecialchars()的区别

除了两个函数的转义方式不同外,它们的使用也不同。

addslashes()通过用于防止sql语句注入,在执行sql语句前对通过get、post和cookie传递来的参数中的单引号,双引号, 和null进行转义。

但sql执行成功后,插入到数据库中的数据是不带有转义字符的。这是如果插入到数据库中的是一些js脚本,当这些脚本被读取出来时还是会被执行。

这是我们可对读取出来的数据使用htmlspecialchars()进行过滤,避免执行被注入的脚本。


来源:fujieace

扫描二维码推送至手机访问。

版权声明:本文由 菠萝博客 发布,如需转载请注明出处。

本文链接:https://www.boluobk.cn/post/223.html

标签: phpsqlxss
分享给朋友:

相关文章

【第一期】虚拟主机的购买和基础功能

虚拟主机的购买和基础功能讲解,后续将持续更新虚拟主机的各种功能及微型/小型网站的搭建教程...

CentOS7配置yum镜像源 教程

1、进入yum镜像源配置文件夹[root@localhost /]# cd /etc/yum.repos.d/2、备份系统原来的镜像文件[root@localhost yum.repos.d]#&nb...

Nginx开启Gzip压缩 教程(提高网站打开速度及整体性能)

Nginx开启Gzip压缩 教程(提高网站打开速度及整体性能)

要知道,网站的打开速度取决于浏览器打开下载的网页文件大小。如果传输的页面内容文件减少,那你网站的打开速度一定会加快。特别是手机端的用户,打开网站速度受限于移动端网络,所以压缩网站页面内容显得至关重要。gzip是一种非常流行的数据压缩方式。你...

如何查看服务器带宽网络实时使用情况?

如何查看服务器带宽网络实时使用情况?

服务器CPU 、内存、磁盘都是正常的,PHP+Nginx+Mysql+Memcached也是正常的,但就是进不了网站后台,请求一直是报504、524超时错误。 最终才发现是服务器被CC攻击了,大量的请求占用了服务器的带宽。之所以最...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。