广告
返回顶部
首页 > 资讯 > 精选 >asp如何过滤xss攻击
  • 950
分享到

asp如何过滤xss攻击

2024-04-02 19:04:59 950人浏览 安东尼
摘要

asp过滤xss攻击的方法:在WEB.config增加HttpModules节点,例如:<httpModules><add name="HttpAccessInterceptModule"&n

asp如何过滤xss攻击

asp过滤xss攻击的方法:

WEB.config增加HttpModules节点,例如:

<httpModules>

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

</httpModules>

再编写一个过滤器:

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\(|\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"}

//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-8";

}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;

}

}

}

--结束END--

本文标题: asp如何过滤xss攻击

本文链接: https://www.lsjlt.com/news/115364.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • asp如何过滤xss攻击
    asp过滤xss攻击的方法:在web.config增加httpModules节点,例如:<httpModules><add name="HttpAccessInterceptModule"&n...
    99+
    2022-10-12
  • 如何过滤xss攻击
    过滤xss攻击的方法:XSS过滤器示例代码:package com.devframe.filter;import javax.servlet.*;import javax.servlet.http.HttpSe...
    99+
    2022-10-09
  • php如何过滤xss攻击
    php过滤xss攻击的示例:在对应的php文件中添加以下代码:<php  function RemoveXSS($val) {     //&n...
    99+
    2022-10-16
  • asp网站空间如何过滤xss攻击
    asp网站空间过滤xss攻击的方法:1、在web.config增加httpModules节点;2、编写一个过滤器,过滤危险关键词,并...
    99+
    2023-02-13
    asp网站空间 空间 xss攻击
  • JSP如何写过滤器防止xss攻击
    JSP写过滤器防止xss攻击的方法:利用Servlet的过滤器机制,编写定制的XssFilter,将request请求代理,覆盖getParameter和getHeader方法将参数名和参数值里的指定半角字符,强制替换成全角字符,代码如下:...
    99+
    2022-10-24
  • java过滤特殊字符操作(xss攻击解决方案)
    XSS ,全名:cross-site scripting(跨站点脚本),是当前 web 应用中最危险和最普遍的漏洞之一。攻击者尝试注入恶意脚本代码(常js脚本)到受信任的网站上执行恶...
    99+
    2022-11-12
  • xss攻击如何修复
    xss攻击的修复方案:对输入的数据进行HTML转义,使其不会识别为可执行脚本,例如:String result = HtmlUtils.htmlEscape(source);根据白名单的标签和属性对数据进行过滤...
    99+
    2022-10-09
  • tp5如何防xss攻击
    tp5防xss攻击的方法:在公共函数文件common.php中加入以下方法:function filter_default($value){return htmlspecialchars($value, ENT_NOQUOTES);}在配置...
    99+
    2022-10-10
  • vue如何防止xss攻击
    这篇“vue如何防止xss攻击”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue如何防止xss攻击”文章吧。vue防止xs...
    99+
    2023-07-04
  • yii2如何防止xss攻击
    yii2防止xss攻击的示例代码:在对应文件中添加以下代码进行调用:function actionClean($str){$str=trim($str);$str=strip_tags($str);$str=stripslashe...
    99+
    2022-10-19
  • django如何防止xss攻击
    django防止xss攻击的方法:使用escape过滤器,无需转义时使用safe过滤器,对单一变量进行转义过滤,例如:Hello {{ a|escape }} # 转义{{a}} # 转义{{a|safe}} # 认为a安全,不进行转义利用...
    99+
    2022-10-09
  • react如何防止xss攻击
    react防止xss攻击的方法:react在渲染html内容和渲染dom属性时都会将 "'&<>这几个字符进行转义,转义部分源码如下:for (index = match....
    99+
    2022-10-13
  • jsp如何解决XSS攻击
    jsp解决XSS攻击的方案:采用struts2的拦截器过滤,将提交上来的参数转码来解决,例如配置struts.xml,代码如下:<package name="default" namespace...
    99+
    2022-10-21
  • angular如何防止xss攻击
    angular防止xss攻击的示例:angular提供了一个DomSanitizer服务,提供的方法如下:export enum SecurityContext { NONE, HTML, STYLE, SCRIPT, URL, RESOU...
    99+
    2022-10-17
  • java如何防止xss攻击
    java防止xss攻击的方案:配置过滤器,代码如下:public class XSSFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig)...
    99+
    2022-10-21
  • emlog如何防止xss攻击
    emlog防止xss攻击的方法:给cookie设置上httponly检查,操作步骤:打开“include\lib\loginauth.php”文件,找到第134行的setAuthCookie函数,改成以下代码:public static f...
    99+
    2022-10-15
  • js如何防止xss攻击
    js防止xss攻击的方法:对用户的输入及请求都进行过滤检查,如对特殊字符进行过滤,设置输入域的匹配规则等,例如:function removeXss(val){val = val.replace(/([\x00-\x08][\x0b-\x0...
    99+
    2022-10-21
  • jwt如何防范xss攻击
    jwt防范xss攻击的方法:使用jwt验证,由于服务端不保存用户信息,因此不用做sessonid复制,同时用户发请求给服务端时,前端使用JS将jwt放在header中手动发送给服务端,服务端验证header中的JWT字段,而非cookie信...
    99+
    2022-10-08
  • jsp如何修复xss攻击
    jsp修复xss攻击的方法:jsp页面接收参数时,对参数进行过滤处理,例如:请求链接:http://a.b.com/login.jspsuccessUrl=<script>alert(111)</script>参数过...
    99+
    2022-10-23
  • 前端如何防止xss攻击
    前端防止xss攻击的方法:过滤非法字符,例如:// 过滤XSS反射型漏洞filterInputTxt: function (html) {html = html.replace(/(.*]+>.*)/g,""); // HTML标记html...
    99+
    2022-10-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作