`
caozuiba
  • 浏览: 903803 次
文章分类
社区版块
存档分类
最新评论

危险字符过滤的类

 
阅读更多
usingSystem;
usingSystem.IO;
usingSystem.Text;
usingSystem.Text.RegularExpressions;
usingSystem.Runtime.Remoting;
usingSystem.Runtime.Remoting.Proxies;
usingSystem.Runtime.Remoting.Messaging;
usingSystem.Reflection;

namespaceFilterRealProxy
{
/**////<summary>
///FilterRealProxy类:一个真实代理,拦截它所代理对象中方法的返回值,并对需要过滤的返回值进行过滤。
///</summary>

publicclassFilterRealProxy:RealProxy
{
privateMarshalByRefObjecttarget;
publicFilterRealProxy(MarshalByRefObjecttarget):base(target.GetType())
{
this.target=target;
}

publicoverrideIMessageInvoke(IMessagemsg)
{
IMethodCallMessagecallMsg
=msgasIMethodCallMessage;
IMethodReturnMessagereturnMsg
=RemotingServices.ExecuteMessage(target,callMsg);
//检查返回值是否为String,如果不是String,就没必要进行过滤
if(this.IsMatchType(returnMsg.ReturnValue))
{
stringreturnValue=this.Filter(returnMsg.ReturnValue.ToString(),returnMsg.MethodName);
returnnewReturnMessage(returnValue,null,0,null,callMsg);
}

returnreturnMsg;
    }

protectedstringFilter(stringReturnValue,stringMethodName)
{
MethodInfomethodInfo
=target.GetType().GetMethod(MethodName);
object[]attributes=methodInfo.GetCustomAttributes(typeof(StringFilter),true);
foreach(objectattribinattributes)
{
returnFilterHandler.Process(((StringFilter)attrib).FilterType,ReturnValue);
}

returnReturnValue;
}

protectedboolIsMatchType(objectobj)
{
returnobjisSystem.String;
}

}


/**////<summary>
///StringFilter类:自定义属性类,定义目标元素的过滤类型
///</summary>

publicclassStringFilter:Attribute
{
protectedFilterType_filterType;

publicStringFilter(FilterTypefilterType)
{
this._filterType=filterType;
}

publicFilterTypeFilterType
{
get
{
return_filterType;
}

}

}


/**////<summary>
///枚举类:用于指定过滤类型,例如:对script过滤还是对html进行过滤?
///</summary>

[Flags()]
publicenumFilterType
{
Script
=1,
Html
=2,
Object
=3,
AHrefScript
=4,
Iframe
=5,
Frameset
=6,
Src
=7,
BadWords
=8,
//Include=9,
All=16
}


/**////<summary>
///过滤处理类:根据过滤类型,调用相应的过滤处理方法。
///</summary>


publicclassFilterHandler
{
privateFilterHandler()
{
}

publicstaticstringProcess(FilterTypefilterType,stringfilterContent)
{
switch(filterType)
{
caseFilterType.Script:
filterContent
=FilterScript(filterContent);
break;
caseFilterType.Html:
filterContent
=FilterHtml(filterContent);
break;
caseFilterType.Object:
filterContent
=FilterObject(filterContent);
break;
caseFilterType.AHrefScript:
filterContent
=FilterAHrefScript(filterContent);
break;
caseFilterType.Iframe:
filterContent
=FilterIframe(filterContent);
break;
caseFilterType.Frameset:
filterContent
=FilterFrameset(filterContent);
break;
caseFilterType.Src:
filterContent
=FilterSrc(filterContent);
break;
//caseFilterType.Include:
//filterContent=FilterInclude(filterContent);
//break;
caseFilterType.BadWords:
filterContent
=FilterBadWords(filterContent);
break;
caseFilterType.All:
filterContent
=FilterAll(filterContent);
break;
default:
//donothing
break;
}

returnfilterContent;
}


publicstaticstringFilterScript(stringcontent)
{
stringcommentPattern=@"(?'comment'<!--.*?--[/n/r]*>)";
stringembeddedScriptComments=@"(///*.*?/*//|////.*?[/n/r])";
stringscriptPattern=String.Format(@"(?'script'<[/n/r]*script[^>]*>(.*?{0}?)*<[/n/r]*/script[^>]*>)",embeddedScriptComments);
//包含注释和Script语句
stringpattern=String.Format(@"(?s)({0}|{1})",commentPattern,scriptPattern);

returnStripScriptAttributesFromTags(Regex.Replace(content,pattern,string.Empty,RegexOptions.IgnoreCase));
}


privatestaticstringStripScriptAttributesFromTags(stringcontent)
{
stringeventAttribs=@"on(blur|c(hange|lick)|dblclick|focus|keypress|(key|mouse)(down|up)|(un)?load
|mouse(move|o(ut|ver))|reset|s(elect|ubmit))
";

stringpattern=String.Format(@"(?inx)
/<(/w+)/s+
(
(?'attribute'
(?'attributeName'{0})/s*=/s*
(?'delim'['""]?)
(?'attributeValue'[^'"">]+)
(/3)
)
|
(?'attribute'
(?'attributeName'href)/s*=/s*
(?'delim'['""]?)
(?'attributeValue'javascript[^'"">]+)
(/3)
)
|
[^>]
)*
/>
",eventAttribs);
Regexre
=newRegex(pattern);
//使用MatchEvaluator的委托
returnre.Replace(content,newMatchEvaluator(StripAttributesHandler));
}


privatestaticstringStripAttributesHandler(Matchm)
{
if(m.Groups["attribute"].Success)
{
returnm.Value.Replace(m.Groups["attribute"].Value,"");
}

else
{
returnm.Value;
}

}


publicstaticstringFilterAHrefScript(stringcontent)
{
stringnewstr=FilterScript(content);
stringregexstr=@"href[^=]*=*[/s/S]*script*:";
returnRegex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}


publicstaticstringFilterSrc(stringcontent)
{
stringnewstr=FilterScript(content);
stringregexstr=@"src*=*['""]?[^/.]+/.(js|vbs|asp|aspx|php|jsp)['""]";
returnRegex.Replace(newstr,regexstr,@"",RegexOptions.IgnoreCase);
}

/**//*
publicstaticstringFilterInclude(stringcontent)
{
stringnewstr=FilterScript(content);
stringregexstr=@"<[/s/S]*include*(file|virtual)*=*[/s/S]*/.(js|vbs|asp|aspx|php|jsp)[^>]*>";
returnRegex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}
*/

publicstaticstringFilterHtml(stringcontent)
{
stringnewstr=FilterScript(content);
stringregexstr=@"<[^>]*>";
returnRegex.Replace(newstr,regexstr,string.Empty,RegexOptions.IgnoreCase);
}


publicstaticstringFilterObject(stringcontent)
{
stringregexstr=@"(?i)<Object([^>])*>(/w|/W)*</Object([^>])*>";
returnRegex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
}


publicstaticstringFilterIframe(stringcontent)
{
stringregexstr=@"(?i)<Iframe([^>])*>(/w|/W)*</Iframe([^>])*>";
returnRegex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
}


publicstaticstringFilterFrameset(stringcontent)
{
stringregexstr=@"(?i)<Frameset([^>])*>(/w|/W)*</Frameset([^>])*>";
returnRegex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);
}


//移除非法或不友好字符
privatestaticstringFilterBadWords(stringchkStr)
{
//这里的非法和不友好字符由你任意加,用“|”分隔,支持正则表达式,由于本Blog禁止贴非法和不友好字符,所以这里无法加上。
stringBadWords=@"";
if(chkStr=="")
{
return"";
}


string[]bwords=BadWords.Split('#');
inti,j;
stringstr;
StringBuildersb
=newStringBuilder();
for(i=0;i<bwords.Length;i++)
{
str
=bwords[i].ToString().Trim();
stringregStr,toStr;
regStr
=str;
Regexr
=newRegex(regStr,RegexOptions.IgnoreCase|RegexOptions.Singleline|RegexOptions.Multiline);
Matchm
=r.Match(chkStr);
if(m.Success)
{
j
=m.Value.Length;
sb.Insert(
0,"*",j);
toStr
=sb.ToString();
chkStr
=Regex.Replace(chkStr,regStr,toStr,RegexOptions.IgnoreCase|RegexOptions.Singleline|RegexOptions.Multiline);
}

sb.Remove(
0,sb.Length);
}

returnchkStr;
}


publicstaticstringFilterAll(stringcontent)
{
content
=FilterHtml(content);
content
=FilterScript(content);
content
=FilterAHrefScript(content);
content
=FilterObject(content);
content
=FilterIframe(content);
content
=FilterFrameset(content);
content
=FilterSrc(content);
content
=FilterBadWords(content);
//content=FilterInclude(content);
returncontent;
}

}

}

分享到:
评论

相关推荐

    链接sql 数据库 过滤危险字符串,,mssql ,注入防御

    链接sql 数据库 过滤危险字符串,,mssql ,注入防御

    IIS 过滤字符 防火墙

    IIS 过滤字符 防火墙IIS 过滤字符 防火墙IIS 过滤字符 防火墙 基于IIS 可以自定义 是一个dll ,调用进去就可以了

    过滤器过滤用户输入的非法字符

    过滤器过滤用户输入的非法字符,如“” “%” “+”等需要的两个类XssFilter.java和XssHttpServletRequestWrapper.java

    C#检测是否有危险字符的SQL字符串过滤方法

    本文以一个C#的SQL数据库字串操作函数为例,说明如何实现对SQL字符串过滤、检测SQL是否有危险字符、修正sql语句中的转义字符,确保SQL不被注入等功能。具体实现代码如下: SQL字符串过滤函数: public static bool ...

    C#-字符串操作类

    C#-字符串操作类(替换字符串中危险字符、指定位置替换字符串、指定长度缩减字段并加...、指定字符串分割字符串、指定字符串位置获取字符串、过滤SQL中非法字符、检查SQL语句中是否有非法关键字、随机字符串生成、...

    C#实现过滤sql特殊字符的方法集合

    主要介绍了C#实现过滤sql特殊字符的方法,以实例形式分析总结了C#针对SQL危险字符的几种常用的过滤技巧,非常具有实用价值,需要的朋友可以参考下

    php中url传递中文字符,特殊危险字符的解决方法

    现在,我们需要这些危险字符,该这么办? 我想到的办法是 先给它们 base64_encode($text) 编码,到服务端时,又给它们 base64_decode($text) 解码, 貌似很完美,但是在使用的过程中又遇到一个问题,bas

    防止xss和sql注入:JS特殊字符过滤正则

    代码如下:function stripscript(s) { var ...]”) //格式 RegExp(“[在中间定义特殊过滤字符]”)var rs = “”; for (var i = 0; i &lt; s.length; i++) { rs = rs+s.substr(i, 1).replace(pattern, ”); }return rs;}

    C#验证用户输入信息是否包含危险字符串的方法

    主要介绍了C#验证用户输入信息是否包含危险字符串的方法,可针对and、or、exec、insert、select等SQL操作技巧进行过滤操作,非常具有一定参考借鉴价值,需要的朋友可以参考下

    angular-cleanurl:Angular Web 安全 url 字符串过滤器

    angular-cleanurl 简单的 Angular 过滤器将字符串转换为 Web 安全的 url演示如何使用 {{name | cleanurl}}“示例文本”到“示例文本”

    javascript过滤危险脚本方法

    下面是他们的字符串规则: 1、&lt;(script|link|style|iframe)(.|\n)*&lt;\/\1&gt;\s* 2、\s*on[a-z]+\s*=\s*(“[^”]+”|'[^’]+’|[^\s]+)\s*(?=&gt;) 3、\s*(href|src)\s*=\s*(“\s*(javascript|vbscript):[^”]+”|’\...

    xss特殊字符拦截与过滤

    滤除content中的危险 HTML 代码, 主要是脚本代码, 滚动字幕代码以及脚本事件处理代码

    详解WordPress中过滤链接与过滤SQL语句的方法

    esc_url()(过滤链接) ...删除无效字符和危险的字符 将字符转换成 HTML 实体字符 使用方法 esc_url( $url, $protocols, $_context ); 参数 $url (字符串)(必须)要被过滤的 URL. 默认值:None $pr

    PHP软件安全编码规范V2.4.docx

    2.1 POST/GET参数传值/字符串输入/数据入库等严格的危险字符过滤处理 6 2.1.1 说明 6 2.2.2 应对 6 2.2.3 举例 6 2.2 相关PHP的I/O操作,需要注意限制权限,文件名或目录名或内容都要做好过滤处理 8 2.2.1 说明 8 ...

    PHP中将字符串转化为整数(int) intval() printf() 性能测试

    早在Sql注入横行的前几年,字符串转化为整数就已经被列为每个web程序必备的操作了。web程序将get或post来的id、整数等值强制经过转化函数转化为整数,过滤掉危险字符,尽可能降低系统本身被Sql注入的可能性

    AntiXss 4.2.1.msi

    使用这些Html编辑器控件的潜在危险,是用户可能会输入一些危险字符,注入到网站中,形成XSS攻击。一个最简单的输入就是: &lt;javascript&gt;alert&#40;'xss'&#41; 如何防止呢?大致思路有三种: 1. 正则表达式的白名单...

    PHP中的代码安全和SQL Injection防范3

    函数作用:对提交的字符串进行过滤 参 数:$var: 要处理的字符串 返 回 值:返回过滤后的字符串 函数作者:heiyeluren */ function str_check( $str ) { if (!get_magic_quotes_gpc()) // 判断magic_quotes_gpc是否...

    flamelu#Vulnerability-1#金山 V8 终端安全系统 pdf_maker.php 未授权 RCE1

    金山 V8 终端安全系统 pdf_maker.php 存在命令执行漏洞,由于没有过滤危险字符,导致构造特殊字符即可进行命令拼接执行任意命令.漏洞文件:Kings

Global site tag (gtag.js) - Google Analytics