当前位置: 首页 > 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和域限制”,

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

http://www.xdnf.cn/news/14707.html

相关文章:

  • ARINC818协议(五)
  • xxljob 执行器流程-笔记
  • PHP腾讯云人脸核身生成 SDK 接口调用步骤使用签名
  • Vue3中provide和inject的用法示例
  • opencv函数展示3
  • Git LFS 学习笔记:原理、配置、实践与心路历程
  • 直播人脸美型核心技术详解:卷积神经网络与图像增强在美颜SDK中的应用
  • pdfjs库使用记录1
  • Web3区块链网络中数据隐私安全性探讨
  • 深度解析生成对抗网络:原理、应用与未来趋势
  • #systemverilog# 进程控制问题#(八)关于#0 问题的使用(三)
  • 全志H5,NanopiKP1lus移植QT5.12记录
  • 如何在 Electron 应用中安全地进行主进程与渲染器进程通信
  • 通过特定协议拉起 electron 应用
  • electron 渲染进程按钮创建新window,报BrowserWindow is not a constructor错误;
  • 嵌入式设备网络的动态ID分配机制实现
  • 极狐GitLab 用户 API 速率限制如何设置?
  • CenterTrack
  • DNS解析失败怎么解决?
  • 【Spring Boot 源码学习】深入 ConfigurableEnvironment 的初始化过程
  • 论文阅读笔记——Mixtral of Experts
  • 中级社会工作者考试精选练习题
  • 深度学习-全连接神经网络-1
  • C++代码优化
  • 梯度下降代码
  • fatdds:传输层SHM和DATA-SHARING的区别
  • 数据结构|基数排序及八个排序总结
  • Python爬虫入门
  • 使用veaury,在vue项目中运行react组件
  • 汉诺塔专题:P1760 通天之汉诺塔 题解 + Problem D: 汉诺塔 题解