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和域限制”,
雙擊進入,右鍵添加拒絕條目即可。