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

softyun3年前云服务器18

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、看服务水平协议及合同,包括定价政策、付款条件、合同期限和终止条件、状况处理预...

免实名香港服务器适合搭建游戏网站吗

免实名香港服务器可以搭建游戏网站,但需要注意以下几点:1、服务器性能游戏网站需要较高的服务器性能,以确保游戏的流畅运行。因此,在选择免实名香港服务器时,需要选择具有足够性能的服务器。2、网络带宽游戏网...

日本云主机的优势有哪些

日本云主机的优势有:1. 日本云主机机房的线路类型多样,满足业务的线路需求。2. 日本机房的带宽充足,保证云主机的抗攻击能力和稳定性。3. 日本云主机采用SDN网络体系结构,根据网站访问者的数量自动调...

高防服务器租用需要注意哪些事项

高防服务器租用需要注意的事项有:1、注意高防服务器的防御范围,高防服务器的租用较贵,所以租用时要知道高仿服务器的的防御范围是否满足自身的需求;2、注意高防服务器的防御类型,高防服务器的自动牵引功能主要...

选择云服务器搭载电商业务的标准有哪些

选择云服务器搭载电商业务的标准:1. 选择云服务器搭载电商业务要有效防止宕机的高可用性,选择那些提供完善的服务水平保障协议的云服务器供应商,并且其云服务器在可用性服务方面拥有良好记录,可以放心采取措施...

美国独享主机租用有哪些优势

美国独享主机租用的优势有:1、稳定性强,用户独享主机上的所有资源以及安装独立操作系统;2、安全性更高,用户可以自行配置主机权限和安全防御策略;3、灵活性好,用户能够完全控制其网站,并且可自由选择网络带...