asp网站空间如何过滤xss攻击

softyun3年前云服务器35

asp网站空间过滤xss攻击的方法:1、在web.config增加httpModules节点;2、编写一个过滤器,过滤危险关键词,并增加安全的header。

具体内容如下:

1、在web.config增加httpModules节点

<httpModules>

<addname="HttpAccessInterceptModule"type="Org.Core.Commons.HttpAccessInterceptModule,Org.Core.Commons"/>

</httpModules>

2、再编写一个过滤器

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Configuration;

usingSystem.Linq;

usingSystem.Text.RegularExpressions;

usingSystem.Web;namespaceOrg.Core.Commons

{

///<summary>

///http访问拦截器模块

///1.过滤危险关键词

///2.增加安全Header

///</summary>

publicclassHttpAccessInterceptModule:IHttpModule

{

privatestaticList<string>_RegexWords;

staticHttpAccessInterceptModule()

{

_RegexWords=newList<string>()

{

@"<[^>]+>'",

@"</[^>]+>'",

@"<[^>]+?style=[\w]+?:expression\(|\b(alert|confirm|prompt|window|location|eval|console|debugger|new|Function|var|let)\b|^\+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|\b(and|or)\b.{1,6}?(=|>|<|\bin\b|\blike\b)|/\*.+?\*/|<\s*script\b|\bEXEC\b|UNION.+?SELECT|UPDATE.+?SET|INSERT\s+INTO.+?VALUES|(SELECT|DELETE).+?FROM|(CREATE|ALTER|DROP|TRUNCATE)\s+(TABLE|DATABASE)"

};

string[]keyWords={};

//{"'","alert","script","case","catch","const","continue","debugge","delete","export*","final","finally","for","function","goto","if","implements","import*","return","switch","synchronized","throw","throws","transient","try","break"}

//newstring[]{"select","insert","update","delete","drop","truncate"};_RegexWords.AddRange(keyWords.Select(o=>@"(^|(\W+))"+o+@"((\W+)|$)"));

}publicvoidDispose()

{

}publicvoidInit(HttpApplicationcontext)

{

context.BeginRequest+=newEventHandler(Context_BeginRequest);

context.EndRequest+=newEventHandler(Context_EndRequest);

}privatevoidContext_BeginRequest(objectsender,EventArgse)

{

HttpApplicationapp=(HttpApplication)sender;

try

{

if(IgnoreRequest(app.Request.CurrentExecutionFilePath))

return;RequestFiller(app.Request);

AddHeader(app.Response);

}

catch(Exceptionex)

{

if(!(exisPSBaseException))

PSLog4net.Error(this,ex);

app.Response.Write(ex.Message);

app.Response.Flush();

app.Response.End();

}

}privatevoidContext_EndRequest(objectsender,EventArgse)

{

HttpApplicationapp=(HttpApplication)sender;SetContentType(app);

}privatevoidRequestFiller(HttpRequestrequest)

{

stringerror="";if(request.Path.IndexOf("/log/",StringComparison.CurrentCultureIgnoreCase)>=0)

error="不允许访问/log/目录";

if(string.IsNullOrEmpty(error)&&

request.Path.IndexOf("/bak/",StringComparison.CurrentCultureIgnoreCase)>=0)

error="不允许访问/bak/目录";

if(string.IsNullOrEmpty(error))

{

foreach(stringkeyinrequest.Params.AllKeys)

{

if(key=="aspxerrorpath")

continue;

stringvalue=request.Params[key];

if(!string.IsNullOrEmpty(value)&&(value.Contains("jquery.alert")||value.Contains("image")))

continue;

if(!string.IsNullOrEmpty(key))

{

//if(Regex.IsMatch(key,@"\W+"))

//{

//error=string.Format("存在访问风险,参数[{0}={1}]无法通过“{2}”校验.",key,value,@"\W+");

//break;

//}

foreach(stringregexin_RegexWords)

{

if(Regex.IsMatch(key,regex,RegexOptions.IgnoreCase))

{

error=$"存在访问风险,参数[{key}={value}]无法通过“{regex}”校验.";

break;

}

}

}if(!string.IsNullOrEmpty(error))

break;

if(!string.IsNullOrEmpty(value))

{

foreach(stringregexin_RegexWords)

{

if(Regex.IsMatch(value,regex,RegexOptions.IgnoreCase))

{

error=$"存在访问风险,参数[{key}={value}]无法通过“{regex}”校验.";

break;

}

}

}if(!string.IsNullOrEmpty(error))

break;

}

}if(!string.IsNullOrEmpty(error))

{

Log4net.Error(this,error);

thrownewPSBaseException("存在访问风险,请求无法通过系统校验规则.");

}

}privatevoidAddHeader(HttpResponseresponse)

{}privatevoidSetContentType(HttpApplicationapp)

{

if(app.Request.Url.AbsolutePath.EndsWith(".png",StringComparison.CurrentCultureIgnoreCase))

app.Response.ContentType="image/png";

if(string.IsNullOrEmpty(app.Response.ContentType))

app.Response.ContentType="text/plain;charset=utf-8";

}privateboolIgnoreRequest(stringrequestPath)

{

if(requestPath.EndsWith(".assx",StringComparison.CurrentCultureIgnoreCase)||

requestPath.EndsWith(".sjs",StringComparison.CurrentCultureIgnoreCase)||

requestPath.EndsWith(".asmx",StringComparison.CurrentCultureIgnoreCase))

returntrue;

else

returnfalse;

}

}

}

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

使用网站服务器要注意哪些问题

使用网站服务器要注意的问题有:1、定期修改网站服务器的密码,防止被黑客轻易破解利用;2、不要运行未知来源或带有安全问题的应用程序,降低网站服务器中毒的情况发生;3、在设置网站服务器防火墙过程中,不熟悉...

如何租用海外服务器

租用海外服务器的技巧:1、租用海外服务器时,需要对服务器的性能进行测试,保障服务器稳定性;2、选择海外服务器配件时,需要根据自身网站业务需求进行选择;3、租用海外服务器要考虑扩展性,避免因业务不断发展...

香港服务器多人选择的原因有哪些

香港服务器多人选择的原因有:1、将网站部署在香港服务器内能够提高响应速度;2、香港服务器的频宽越高,速度越快;3、香港服务器限制内容少,能够自由搭建网站;4、服务器出故障时需要进行沟通,选择香港服务器...

为什么要用云存储

要用云存储的原因:1、云存储属于分布式的,因此可用性比较好;2、云存储一般都是三副本模式,当数据丢失后,可通过副本恢复,可靠性高;3、云存储能够提高系统的安全性;4、云存储网络资源丰富,能够提供BGP...

香港服务器托管有哪些优势

香港服务器托管的优势有:1、香港服务器托管不需要进行繁琐的备案流程;2、能够解决国内电信、网通互联不互通的问题;3、香港服务器的国际带宽充足,访问速度快;4、香港服务器稳定性好,能够保障业务正常运作;...

香港cn2高防服务器适合哪些行业租用

适合租用香港cn2高防服务器的行业有:1、网站用户位于大陆,香港,台湾,东南亚等接近香港的地方,适合使用香港cn2高防服务器;2、香港cn2高防服务器适合于流量更小,攻击更少,访问速度更快的网站;3、...