<php
/**
* 安全过滤函数
*
* @param $string
* @return string
*/
function safe_replace($string) {
$string = str_replace(‘%20‘,‘‘,$string);
$string = str_replace(‘%27‘,‘‘,$string);
$string = str_replace(‘%2527‘,‘‘,$string);
$string = str_replace(‘*‘,‘‘,$string);
$string = str_replace(‘"‘,‘"‘,$string);
$string = str_replace("‘",‘‘,$string);
$string = str_replace(‘"‘,‘‘,$string);
$string = str_replace(‘;‘,‘‘,$string);
$string = str_replace(‘<‘,‘<‘,$string);
$string = str_replace(‘>‘,‘>‘,$string);
$string = str_replace("{",‘‘,$string);
$string = str_replace(‘}‘,‘‘,$string);
$string = str_replace(‘‘,‘‘,$string);
return $string;
}
?>
<?php
/**
* 返回经addslashes处理过的字符串或数组
* @param $string 需要处理的字符串或数组
* @return mixed
*/
function new_addslashes($string) {
if(!is_array($string)) return addslashes($string);
foreach($string as $key => $val) $string[$key] = new_addslashes($val);
return $string;
}
?>
<?php
//对请求的字符串进行安全处理
/*
$safestep
0 为不处理,
1 为禁止不安全HTML内容(javascript等),
2 完全禁止HTML内容,并替换部份不安全字符串(如:eval(、union、CONCAT(、--、等)
*/
function StringSafe($str, $safestep=-1){
$safestep = ($safestep > -1) ? $safestep : 1;
if($safestep == 1){
$str = preg_replace("#script:#i", "script:", $str);
$str = preg_replace("#<[/]{0,1}(link|meta|ifr|fra|scr)[^>]*>#isU", ‘‘, $str);
$str = preg_replace("#[ ]{1,}#", ‘ ‘, $str);
return $str;
}else if($safestep == 2){
$str = addslashes(htmlspecialchars(stripslashes($str)));
$str = preg_replace("#eval#i", ‘eval‘, $str);
$str = preg_replace("#union#i", ‘union‘, $str);
$str = preg_replace("#concat#i", ‘concat‘, $str);
$str = preg_replace("#--#", ‘--‘, $str);
$str = preg_replace("#[ ]{1,}#", ‘ ‘, $str);
return $str;
}else{
return $str;
}
}
?>
<?php
/**
+----------------------------------------------------------
* 输出安全的html,用于过滤危险代码
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $text 要处理的字符串
* @param mixed $tags 允许的标签列表,如 table|td|th|td
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
static public function safeHtml($text, $tags = null)
{
$text = trim($text);
//完全过滤注释
$text = preg_replace(‘/<!--?.*-->/‘,‘‘,$text);
//完全过滤动态代码
$text = preg_replace(‘/<?|?‘.‘>/‘,‘‘,$text);
//完全过滤js
$text = preg_replace(‘/<script?.*/script>/‘,‘‘,$text);
$text = str_replace(‘[‘,‘[‘,$text);
$text = str_replace(‘]‘,‘]‘,$text);
$text = str_replace(‘|‘,‘|‘,$text);
//过滤换行符
$text = preg_replace(‘/ ? /‘,‘‘,$text);
//br
$text = preg_replace(‘/<br(s/)?‘.‘>/i‘,‘
‘,$text);
$text = preg_replace(‘/(
s*){10,}/i‘,‘
‘,$text);
//过滤危险的属性,如:过滤on事件lang js
while(preg_match(‘/(<[^><]+)(lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i‘,$text,$mat)){
$text=str_replace($mat[0],$mat[1],$text);
}
while(preg_match(‘/(<[^><]+)(window.|javascript:|js:|about:|file:|document.|vbs:|cookie)([^><]*)/i‘,$text,$mat)){
$text=str_replace($mat[0],$mat[1].$mat[3],$text);
}
if( empty($allowTags) ) { $allowTags = self::$htmlTags[‘allow‘]; }
//允许的HTML标签
$text = preg_replace(‘/<(‘.$allowTags.‘)( [^><[]]*)>/i‘,‘[12]‘,$text);
//过滤多余html
if ( empty($banTag) ) { $banTag = self::$htmlTags[‘ban‘]; }
$text = preg_replace(‘/</?(‘.$banTag.‘)[^><]*>/i‘,‘‘,$text);
//过滤合法的html标签
while(preg_match(‘/<([a-z]+)[^><[]]*>[^><]*</1>/i‘,$text,$mat)){
$text=str_replace($mat[0],str_replace(‘>‘,‘]‘,str_replace(‘<‘,‘[‘,$mat[0])),$text);
}
//转换引号
while(preg_match(‘/([[^[]]*=s*)("|‘)([^2=[]]+)2([^[]]*])/i‘,$text,$mat)){
$text=str_replace($mat[0],$mat[1].‘|‘.$mat[3].‘|‘.$mat[4],$text);
}
//空属性转换
$text = str_replace(‘‘‘‘,‘||‘,$text);
$text = str_replace(‘""‘,‘||‘,$text);
//过滤错误的单个引号
while(preg_match(‘/[[^[]]*("|‘)[^[]]*]/i‘,$text,$mat)){
$text=str_replace($mat[0],str_replace($mat[1],‘‘,$mat[0]),$text);
}
//转换其它所有不合法的 < >
$text = str_replace(‘<‘,‘<‘,$text);
$text = str_replace(‘>‘,‘>‘,$text);
$text = str_replace(‘"‘,‘"‘,$text);
//反转换
$text = str_replace(‘[‘,‘<‘,$text);
$text = str_replace(‘]‘,‘>‘,$text);
$text = str_replace(‘|‘,‘"‘,$text);
//过滤多余空格
$text = str_replace(‘ ‘,‘ ‘,$text);
return $text;
}
?>
<?php
function RemoveXSS($val) {
// remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
// this prevents some character re-spacing such as <javascript>
// note that you have to handle splits with , , and later since they *are* allowed in some // inputs
$val = preg_replace(‘/([x00-x08,x0b-x0c,x0e-x19])/‘, ‘‘, $val);
// straight replacements, the user should never need these since they‘re normal characters
// this prevents like <IMG [email protected]:alert(‘XSS‘)>
$search = ‘abcdefghijklmnopqrstuvwxyz‘;
$search .= ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘;
$search .= ‘[email protected]#$%^&*()‘;
$search .= ‘~`";:?+/={}[]-_|‘‘;
for ($i = 0; $i < strlen($search); $i++) {
// ;? matches the ;, which is optional
// 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
// @ @ search for the hex values
$val = preg_replace(‘/(&#[xX]0{0,8}‘.dechex(ord($search[$i])).‘;?)/i‘, $search[$i], $val);//with a ;
// @ @ 0{0,7} matches ‘0‘ zero to seven times
$val = preg_replace(‘/(?{0,8}‘.ord($search[$i]).‘;?)/‘, $search[$i], $val); // with a ;
}
// now the only remaining whitespace attacks are , , and
$ra1 = Array(‘javascript‘, ‘vbscript‘, ‘expression‘, ‘applet‘, ‘meta‘, ‘xml‘, ‘blink‘, ‘link‘, ‘style‘, ‘script‘, ‘embed‘, ‘object‘, ‘iframe‘, ‘frame‘, ‘frameset‘, ‘ilayer‘, ‘layer‘, ‘bgsound‘, ‘title‘, ‘base‘);
$ra2 = Array(‘onabort‘, ‘onactivate‘, ‘onafterprint‘, ‘onafterupdate‘, ‘onbeforeactivate‘, ‘onbeforecopy‘, ‘onbeforecut‘, ‘onbeforedeactivate‘, ‘onbeforeeditfocus‘, ‘onbeforepaste‘, ‘onbeforeprint‘, ‘onbeforeunload‘, ‘onbeforeupdate‘, ‘onblur‘, ‘onbounce‘, ‘oncellchange‘, ‘onchange‘, ‘onclick‘, ‘oncontextmenu‘, ‘oncontrolselect‘, ‘oncopy‘, ‘oncut‘, ‘ondataavailable‘, ‘ondatasetchanged‘, ‘ondatasetcomplete‘, ‘ondblclick‘, ‘ondeactivate‘, ‘ondrag‘, ‘ondragend‘, ‘ondragenter‘, ‘ondragleave‘, ‘ondragover‘, ‘ondragstart‘, ‘ondrop‘, ‘onerror‘, ‘onerrorupdate‘, ‘onfilterchange‘, ‘onfinish‘, ‘onfocus‘, ‘onfocusin‘, ‘onfocusout‘, ‘onhelp‘, ‘onkeydown‘, ‘onkeypress‘, ‘onkeyup‘, ‘onlayoutcomplete‘, ‘onload‘, ‘onlosecapture‘, ‘onmousedown‘, ‘onmouseenter‘, ‘onmouseleave‘, ‘onmousemove‘, ‘onmouseout‘, ‘onmouseover‘, ‘onmouseup‘, ‘onmousewheel‘, ‘onmove‘, ‘onmoveend‘, ‘onmovestart‘, ‘onpaste‘, ‘onpropertychange‘, ‘onreadystatechange‘, ‘onreset‘, ‘onresize‘, ‘onresizeend‘, ‘onresizestart‘, ‘onrowenter‘, ‘onrowexit‘, ‘onrowsdelete‘, ‘onrowsinserted‘, ‘onscroll‘, ‘onselect‘, ‘onselectionchange‘, ‘onselectstart‘, ‘onstart‘, ‘onstop‘, ‘onsubmit‘, ‘onunload‘);
$ra = array_merge($ra1, $ra2);
$found = true; // keep replacing as long as the previous round replaced something
while ($found == true) {
$val_before = $val;
for ($i = 0; $i < sizeof($ra); $i++) {
$pattern = ‘/‘;
for ($j = 0; $j < strlen($ra[$i]); $j++) {
if ($j > 0) {
$pattern .= ‘(‘;
$pattern .= ‘(&#[xX]0{0,8}([9ab]);)‘;
$pattern .= ‘|‘;
$pattern .= ‘|(?{0,8}([9|10|13]);)‘;
$pattern .= ‘)*‘;
}
$pattern .= $ra[$i][$j];
}
$pattern .= ‘/i‘;
$replacement = substr($ra[$i], 0, 2).‘<x>‘.substr($ra[$i], 2); // add in <> to nerf the tag
$val = preg_replace($pattern, $replacement, $val); // filter out the hex tags
if ($val_before == $val) {
// no replacements were made, so exit the loop
$found = false;
}
}
}
return $val;
}
?>
怎么在php里面利用str_replace防注入
时间: 2024-08-24 04:54:30
怎么在php里面利用str_replace防注入的相关文章
PHP防注入
php网站如何防止sql注入? 网站的运行安全肯定是每个站长必须考虑的问题,大家知道,大多数黑客攻击网站都是采用sql注入,这就是我们常说的为什么最原始的静态的网站反而是最安全的. 今天我们讲讲PHP注入的安全规范,防止自己的网站被sql注入. 如今主流的网站开发语言还是php,那我们就从php网站如何防止sql注入开始说起: Php注入的安全防范通过上面的过程,我们可以了解到php注入的原理和手法,当然我们也同样可以制定出相应该的防范方法:首先是对服务器的安全设置,这里主要是php+mysql
绕过防注入系统的方法
路过这个网站,检测了一下.http://www.xxx.cn/Article.asp?ID=117 and 1=1直接返回主页http://www.xxx.cn/Article.asp?ID=117 or 1=1直接返回主页http://www.xxx.cn/Article.asp?ID=117 or没有返回主页 没有过滤orhttp://www.xxx.cn/Article.asp?ID=117 and直接返回主页 看来过滤了andhttp://www.xxx.cn/Article.asp?I
php sql防注入
没有太多的过滤,主要是针对php和mysql的组合.一般性的防注入,只要使用php的 addslashes 函数就可以了. 以下是一段copy来的代码: PHP代码 $_POST = sql_injection($_POST); $_GET = sql_injection($_GET); function sql_injection($content) { if (!get_magic_quotes_gpc()) { if (is_array($content)) { foreach ($con
绕过防注入的几种方法
1.运用编码技术绕过 如URLEncode编码,ASCII编码绕过.例如or 1=1即%6f%72%20%31%3d%31,而Test也可以为CHAR(101)+CHAR(97)+CHAR(115)+CHAR(116). 2.通过空格绕过 如两个空格代替一个空格,用Tab代替空格等,或者删除所有空格,如 or swords =‘swords,由于mssql的松散性,我们可以把or swords 之间的空格去掉,并不影响运行. 3.运用字符串判断代替 用经典的or 1=1判断绕过,如or swor
简单实用的PHP防注入类实例
这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下 本文实例讲述了简单实用的PHP防注入类.分享给大家供大家参考.具体如下: PHP防注入注意要过滤的信息基本是get,post,然后对于sql就是我们常用的查询,插入等等sql命令了,下面我给各位整理两个简单的例子,希望这些例子能给你网站带来安全. PHP防注入类代码如下: 复制代码 代码如下: <?php /** * 参数处理类 *
php之防注入程序绕过浅谈
<?php/*判断传递的变量是否含有非法字符如:$_POST/$_GET功能:SQL防注入系统*/ //屏蔽错误提示error_reporting(7); //需要过滤的字符 $ArrFiltrate = array("'", "or", "and", "union", "where"); //1.如果规则不完全,也会引起安全问题 //出错跳转的URL$StrGoUrl = "";
PDO防注入原理分析以及使用PDO的注意事项
我们都知道,只要合理正确使用PDO,可以基本上防止SQL注入的产生,本文主要回答以下两个问题: 为什么要使用PDO而不是mysql_connect? 为何PDO能防注入? 使用PDO防注入的时候应该特别注意什么? 一.为何要优先使用PDO? PHP手册上说得很清楚: Prepared statements and stored proceduresMany of the more mature databases support the concept of prepared statement
asp.net使用ODP的防注入登录验证程序
网上有很多SQL连接方式的登录验证,但没有oracle连接方式的,我摸索了一上午写了这个可执行的函数,分享给大家 // 用户登录检查 public bool LoginCheck(string f_LoginName, string f_LoginPass) { bool result = false; // 正则表达式检查 if (Regex.IsMatch(f_LoginName,@"^[a-zA-Z0-9]{1,15}$") && Regex.IsMatch(f_
asp防注入安全问题
一.古老的绕验证漏洞虽然古老,依然存在于很多小程序之中,比如一些企业网站的后台,简单谈谈.这个漏洞出现在没有对接受的变量进行过滤,带入数据库判断查询时,造成SQL语句的逻辑问题.例如以下代码存在问题:username=request("username")password=request("password")sql = "select * from user where username='" & username & &quo