posts - 930,  comments - 588,  views - 402万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

        我们在开发Asp.net中,最后部署在IIS上. 然后发送HTTP请求,返回的HTTP头中包含Server, X-Powered-By, 和 X-AspNet-Version信息. 这些信息有时给攻击者找寻你的站点漏洞提供的依据. 如下图我们通过FireBug查看到:

2011-08-07_header

        移除X-AspNet-Version很简单,只需要在Web.config中增加这个配置节:

 <httpRuntime enableVersionHeader="false" />

         移除Server呢, 我们可以写一个自定义HttpModule,看下来代码:

   1:  namespace MyWeb
   2:  {
   3:      public class RemoveServerInfoModule: IHttpModule
   4:      {
   5:          #region IHttpModule Members
   6:   
   7:          public void Dispose(){
   8:              //no code nescessary
   9:          }
  10:          
  11:          public void Init(HttpApplication context)
  12:          {
  13:              context.PreSendRequestHeaders += new EventHandler(context_PreSendRequestHeaders);
  14:          }
  15:   
  16:          void context_PreSendRequestHeaders(object sender, EventArgs e)
  17:          {
  18:              // strip the "Server" header from the current Response 
  19:              HttpContext.Current.Response.Headers.Remove("Server");
  20:          }
  21:   
  22:          #endregion
  23:      }
  24:  }

         上面这段代码会arise exceptioin,我们最好这样实现PreSendRequestHeaders方法:

   1:          void context_PreSendRequestHeaders(object sender, EventArgs e)
   2:          {
   3:              try
   4:              {
   5:                  HttpApplication app = sender as HttpApplication;
   6:                  if (null != app && null != app.Request && !app.Request.IsLocal && null != app.Context && null != app.Context.Response)
   7:                  {
   8:                      var headers = app.Context.Response.Headers;
   9:                      if (null != headers)
  10:                      {
  11:                          headers.Remove("Server");
  12:                      }
  13:                  }
  14:              }
  15:              catch (Exception ex)
  16:              {
  17:                  Log.HandleException(ex);
  18:              }
  19:          }

         最后在Web.config中配置这个HttpModule:

    <httpModules>
      <add name="RemoveServerInfoModule" type="MyWeb.RemoveServerInfoModule"/>
    </httpModules>

  For IIS 7:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" >
      <add name="RemoveServerInfoModule" type="MyWeb.RemoveServerInfoModule"/>
    </modules>
  </system.webServer

       这样就OK了, 你再运行Asp.net web application时, Server,X-AspNet-Version等信息已经不显示了.

       希望对您开发,有帮助.


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on   PetterLiu  阅读(14551)  评论(4编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示