PHP防御防御永久型和反射XSS
2015-06-01 11:44
393 查看
本代码只用于防御永久型和反射型XSS 攻击,有更好的意见请留言,thank you !!
<?php class Safe { function basic($c) { $ord = ord($c); if($this->isnumber($ord)) { if(mb_check_encoding($c,"UTF-8")) { return $c; } return $c; } if($this->isalphabet($ord)) { return $c; } return ""; } function basics($s) { $str = ""; for($i = 0;$i < mb_strlen($s);$i++) { $str .= $this->basic(mb_substr($s,$i,1)); } return $str; } function basic_arr($arr) { foreach($arr as $k => $v) { $arr[$k] = $this->basics($v); } return $arr; } function css($c) { $ord = ord($c); $hex = bin2hex($c); if(strlen($c) > 1) { if(mb_check_encoding($c,"UTF-8")) { return $c; } return "\\".$hex." "; } if($this->isnumber($ord)) { return $c; } if($this->isalphabet($ord)) { return $c; } return "\\".$hex." "; } function csss($css) { $str = ""; for($i = 0;$i < mb_strlen($css);$i++) { $str .= $this->css(mb_substr($css,$i,1)); } return $str; } function javascript($c) { $ord = ord($c); $hex = bin2hex($c); if(strlen($c) > 1) { if(mb_check_encoding($c,"UTF-8")) { return $c; } return "\\u".str_pad($hex,4,"0"); } if($this->isnumber($ord)) { return $c; } if($this->isalphabet($ord)) { return $c; } return "\\x".str_pad($hex,2,"0"); } function javascripts($js) { $str = ""; for($i = 0;$i < mb_strlen($js);$i++) { $str .= $this->javascript(mb_substr($js,$i,1)); } return $str; } function html($c) { $ord = ord($c); $hex = bin2hex($c); if(strlen($c) > 1) { if(mb_check_encoding($c,"UTF-8")) { return $c; } return "".$hex.";"; } if($this->isnumber($ord)) { return $c; } if($this->isalphabet($ord)) { return $c; } if(($ord >= 0x1f && $ord != 0x09 && $ord != 0x0a && $ord != 0x0d) || ($ord >= 0x7f && $ord <=0x9f)) { return ""; } return "".$hex.";"; } function htmls($html) { $str = ""; for($i = 0;$i < mb_strlen($html);$i++) { $str .= $this->html(mb_substr($html,$i,1)); } return $str; } function url($c) { $ord = ord($c); if(strlen($c) > 1) { return urlencode($c); } if($this->isnumber($ord)) { return $c; } if($this->isalphabet($ord)) { return $c; } //$allow = array(":","/",".","?","%","&","=","-"); $allow = array(58,47,46,63,37,38,61,95,45); if(in_array($ord,$allow,true)) { return $c; } if($ord >= 0x1f || $ord >= 0x7f && $ord <= 0x9f) { return ""; } return urlencode($c); } function urls($url) { $str = ""; for($i = 0;$i < mb_strlen($url);$i++) { $str .= $this->url(mb_substr($url,$i,1)); } return $str; } function isnumber($ord) { if($ord >= 48 && $ord <= 57) { return true; } return false; } function isupper($ord) { if($ord >= 65 && $ord <= 90) { return true; } return false; } function islower($ord) { if($ord >= 97 && $ord <= 122) { return true; } return false; } function isalphabet($ord) { return $this->isupper($ord) || $this->islower($ord); } }
相关文章推荐
- phpcms 移植【添加相关文章】功能
- PHP 二维数组根据某个字段排序
- php自定义错误
- vsftpd cmds_allowed 权限控制
- php BC高精确度函数库
- PHP中的Libevent学习
- RTP打包发送H.264
- Git PHP提交
- PHP中使用cURL实现Get和Post请求的方法
- smtp 发邮件
- php不用正则验证真假身份证 身份证号码验证
- php 编译安装报错 configure: error: mcrypt.h not found. Please reinstall libmcrypt.
- php 函数
- Zend Studio 12 newPHP 模板及编码、和一些常见的问题在哪修改
- php用于URL的base64
- 《PHP内核探索系列文章》系列技术文章整理收藏
- 关于 PHP 7 你必须知道的五件事
- 支持php短格式
- php 连接mysql
- i春秋:揭秘FTP口令安全—利用“hscan”工具的测试