当前位置: 首页 > news >正文

ASP.NET常见安全漏洞及修复方式

Microsoft IIS 版本信息泄露

查看网页返回的 Header 信息,默认会包含 IIS,ASP.NET 版本信息:

隐藏 Server 标头

编辑 web.config 文件,在 system.webServer 节点中配置 requestFiltering 来移除Server标头:

<security>
+  <requestFiltering removeServerHeader ="true" />
</security>

隐藏 X-ASPNET-Version 标头

编辑 web.config 文件,在 system.web 节点, 添加以下配置代码:

<system.web>
+  <httpRuntime enableVersionHeader="false" />
</system.web>

隐藏 X-Powered-By 标头

编辑 web.config 文件,在 system.webServer.customeHeaders 节点, 添加以下代码:

<system.webServer><httpProtocol><customHeaders>
+	    <remove name="X-Powered-By" /></customHeaders></httpProtocol></system.webServer>

修改完响应 Header 如下所示:

未加密的__VIEWSTATE参数

编辑 web.config 文件,在 system.web 节点, 添加以下配置代码:

	<system.web>
+		<pages enableEventValidation="true" validateRequest="false" viewStateEncryptionMode="Always" enableViewStateMac="true"></system.web>

以上配置加密__VIEWSTATE 参数,并启用__EVENTVALIDATION;

HTML FORM 表单没有CSRF防护

通过全局向 注入隐藏域 CSRF 标签,即 , 服务端收到请求时,会验证 CSRFToken 是否正确,不正确则拒绝请求。

添加 App_code/CSRFInjectingFilter.cs

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;public class CSRFInjectingFilter : Stream
{private Stream _responseStream;private StringBuilder _buffer = new StringBuilder();private string _csrfToken;public CSRFInjectingFilter(Stream responseStream, string csrfToken) {_responseStream = responseStream;_csrfToken = csrfToken;}public override void Write(byte[] buffer, int offset, int count) {string content = Encoding.UTF8.GetString(buffer, offset, count);content = InjectToken(content);byte[] outData = Encoding.UTF8.GetBytes(content);_responseStream.Write(outData, 0, outData.Length);}private string InjectToken(string html) {string hiddenInput = string.Format("<input type='hidden' name='CSRFToken' value='{0}' />", _csrfToken);return Regex.Replace(html, @"<form[^>]*>", match => match.Value + hiddenInput, RegexOptions.IgnoreCase);}// 其他 Stream 抽象成员实现public override bool CanRead { get { return false; } }public override bool CanSeek { get { return false; } }public override bool CanWrite { get { return true; } }public override void Flush() { _responseStream.Flush(); }public override long Length {get { throw new NotSupportedException(); }}public override long Position {get { throw new NotSupportedException(); }set { throw new NotSupportedException(); }}public override int Read(byte[] buffer, int offset, int count) { throw new NotSupportedException();}public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }public override void SetLength(long value) { throw new NotSupportedException();}}

添加 App_code/CSRFModule.cs

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;public class CSRFModule : IHttpModule
{public void Init(HttpApplication context) {context.AcquireRequestState += OnAcquireRequestState;context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;}private void OnAcquireRequestState(object sender, EventArgs e) {var app = (HttpApplication)sender;var context = app.Context;// 对 POST 请求做校验if (context.Request.HttpMethod == "POST" && context.CurrentHandler is System.Web.UI.Page) {string tokenFromForm = context.Request.Form["CSRFToken"];string tokenFromSession = null;if (context.Session != null) {tokenFromSession = context.Session["CSRFToken"] as string;}if (string.IsNullOrEmpty(tokenFromForm) || tokenFromForm != tokenFromSession) {context.Response.StatusCode = 403;context.Response.Write("CSRF 验证失败");context.Response.End();}}}private void OnPreRequestHandlerExecute(object sender, EventArgs e){var app = (HttpApplication)sender;var context = app.Context;// 只处理 text/html 类型响应if (context.CurrentHandler is System.Web.UI.Page && context.Response.ContentType == "text/html") {if (context.Session["CSRFToken"] == null) {context.Session["CSRFToken"] = Guid.NewGuid().ToString();}context.Response.Filter = new CSRFInjectingFilter(context.Response.Filter, context.Session["CSRFToken"].ToString());}}public void Dispose() { }
}

配置 web.config , 添加以下配置

在 system.web/httpModules 节点中添加 CSRFModule

<system.web><httpModules>
+			<add name="CSRFModule" type="CSRFModule" />

在 system.webServer/modules 节点中添加 CSRFModule

<system.webServer><modules>
+			<add name="CSRFModule" type="CSRFModule" />

其他

增加 IP 地址黑名单

首先打开“服务器管理器”中,进入“管理”,点击“添加角色和功能”,在“服务器角色”的 Web服务器/Web服务器/安全性 中找到 “IP和域限制”,勾选并安装。

安装成功后,在 IIS 中点击对应的网站,右侧面板中找到 “IP和域限制”,

双击进入,右键添加拒绝条目即可。

相关文章:

  • 【Drools+springboot3规则匹配】
  • 利用 DeepSeek-R1 模型和计算机视觉微调模型实现马铃薯叶片疾病识别和分类
  • STM32使用rand()生成随机数并显示波形
  • 如何在PDF.js中改造viewer.html以实现PDF的动态加载
  • 优化WAV音频文件
  • hooker frida版just_trust_me.js 2025升级 支持boringssl unpinning
  • Skipped breakpoint at ... because of stepping in another thread问题分析
  • AI Agents系列之构建多智能体系统
  • linux驱动之poll
  • Kaggle-Bag of Words Meets Bags of Popcorn-(二分类+NLP+Bert模型)
  • Redis ③-Linux下载Redis
  • 【医学影像 AI】早产儿视网膜病变国际分类(第三版)
  • python中,处理多分类时,模型之间的参数设置
  • 《计算机视觉度量:从特征描述到深度学习》—工业检测大模型RAG白皮书
  • 星露谷物语 7000+ 大型MOD整合包
  • 18-算法打卡-哈希表-两数之和-leetcode(1)-第十八天
  • 从零开始学A2A一:A2A 协议的高级应用与优化
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(7):(1)ながら 一边。。一边 (2)。。。し。。。し。。 又……又……
  • SuperMap iClient3D for WebGL 如何加载WMTS服务
  • 天梯赛DFS合集
  • 特朗普:乌克兰问题谈判短期内若无进展美将不再斡旋
  • 国家主席习近平出席柬埔寨国王西哈莫尼举行的欢迎仪式
  • 上海加大对中小微企业信贷支持力度,奖补资金规模提升到5亿元
  • 世界银行行长:不确定性将导致全球经济增长低于预期
  • 鸿蒙智行第五界“尚界”来了:首期投入60亿元,首款车秋季上市
  • 大卫·第艾维瑞谈历史学与社会理论①丨过去一百年的史学潮流