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

softyun3年前云服务器17

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、带宽资源充足,高达40G,且拥有USP双路不间断供电,能保证完整的数据传输通道缓存;3、安全性与稳定性好,能保证...

ssl加速技术有哪些优势

ssl加速技术的优势有:1、安全性高,能保证网络上数据传输的安全性;2、支持各种应用层协议,能为任何基于TCP等可靠连接的应用层协议提供安全性保证;3、部署简单,不需要安装额外的客户端软件,能鉴别网站...

台湾服务器租用价格哪家的便宜

如今IDC行业发展非常迅速,提供的服务器种类也多种多样,国内许多企业由于业务需要租用台湾服务器。面对一系列的价格和配置,怎么租用到性价比高的台湾服务器,台湾服务器租用价格哪家的便宜?质优价廉的台湾服务...

在找到适合你的海外服务器租用

想从事互联网创业的人,甚至部分企业官网的管理人,都会首选将网站部署在海外。这里我们先不谈监管的问题。单单是价格和审核时长,就让很多人选择海外服务器了。任何公司都是有可能用到海外服务器的,根据自己公司业...

新加坡云服务器适用哪些行业

新加坡云服务器适用的行业有:1、新加坡云服务器的带宽和独立IP资源充足,性价比高,可以当虚拟主机使用,很适合中小企业,小型门户网站租用;2、新加坡云服务器能满足电子商务平台的需求;3、新加坡云服务器的...

美国服务器如何防止木马病毒入侵

美国服务器防止木马病毒入侵的方法有:1、不执行来历不明的软件;2、注意邮箱中带毒的垃圾邮件;3、不要随便留下重要信息在主机中;4、不要随便下载软件;5、不要轻易打开广告邮件的附件和链接;6、显示文件扩...