国内服务器
亚洲服务器
欧洲服务器
北美洲服务器
南美洲服务器
大洋洲服务器
非洲服务器

首页>>服务器

asp网站服务器如何过滤xss攻击

发表时间:2023-02-14 07:54:45

asp网站服务器如何过滤xss攻击

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

下面是详细介绍:

1、在web.config增加httpModules节点

<httpModules>

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

</httpModules>

2、再编写一个过滤器

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Linq;

using System.Text.RegularExpressions;

using System.Web;namespace Org.Core.Commons

{

/// <summary>

/// http访问拦截器模块

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

/// 2.增加安全Header

/// </summary>

public class HttpAccessInterceptModule : IHttpModule

{

private static List<string> _RegexWords;

static HttpAccessInterceptModule()

{

_RegexWords = new List<string>()

{

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

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

@"<[^>]+?style=[w]+?:expression(|(alert|confirm|prompt|window|location|eval|console|debugger|new|Function|var|let)|^+/v(8|9)|<[^>]*?=[^>]*?&#[^>]*?>|(and|or).{1,6}?(=|>|<|in|like)|/*.+?*/|<s*script|EXEC|UNION.+?SELECT|UPDATE.+?SET|INSERTs+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"}

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

}public void Dispose()

{

}public void Init(HttpApplication context)

{

context.BeginRequest += new EventHandler(Context_BeginRequest);

context.EndRequest += new EventHandler(Context_EndRequest);

}private void Context_BeginRequest(object sender, EventArgs e)

{

HttpApplication app = (HttpApplication) sender;

try

{

if (IgnoreRequest(app.Request.CurrentExecutionFilePath))

return;RequestFiller(app.Request);

AddHeader(app.Response);

}

catch (Exception ex)

{

if (!(ex is PSBaseException))

PSLog4net.Error(this, ex);

app.Response.Write(ex.Message);

app.Response.Flush();

app.Response.End();

}

}private void Context_EndRequest(object sender, EventArgs e)

{

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

}private void RequestFiller(HttpRequest request)

{

string error = "";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 (string key in request.Params.AllKeys)

{

if (key == "aspxerrorpath")

continue;

string value = 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 (string regex in _RegexWords)

{

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

{

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

break;

}

}

}if (!string.IsNullOrEmpty(error))

break;

if (!string.IsNullOrEmpty(value))

{

foreach (string regex in _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);

throw new PSBaseException("存在访问风险,要求没法通过系统校验规则.");

}

}private void AddHeader(HttpResponse response)

{}private void SetContentType(HttpApplication app)

{

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⑻";

}private bool IgnoreRequest(string requestPath)

{

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

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

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

return true;

else

return false;

}

}

}

上一篇 下一篇
最新文章

如何提高云服务器的安全系数

香港独立IP空间有什么优势

香港空间影响百度收录吗

为啥要租用美国VPS

VPS的缺点有哪些

香港vps作用在哪里

VPS的优点有哪些

外贸网站为啥选择美国vps

VPS能建多少个网站

VPS要如何选择位置

相关文章

建立电商网站的神器:WordPress和WooCommerce

怎么解决DNS服务器未响应的问题

CDN服务器的概念是什么

使用国外vps云服务器要有哪些需要注意的

快速搭建GitLab服务宝塔面板指南带你飞

游戏服务器为啥被攻击,游戏服务器防御方法

对初创企业,如何公道选择香港服务器?

BinaryRacks-$2.5每个月/1核1G内存20G 空间/1T流量@1Gbps端口/埃及/英国/土耳其

SDWAN与传统网络对比

欧洲德国服务器好处有哪些

X

截屏,微信识别二维码

微信号:muhuanidc

(点击微信号复制,添加好友)

打开微信

微信号已复制,请打开微信添加咨询详情!