103 security iis-7 header response
有没有办法从IIS7中删除"服务器"响应头?有一些文章显示使用HttpModules我们可以实现同样的目的.如果我们没有服务器的管理权限,这将非常有用.另外我不想写ISAPI过滤器.
我有我的服务器的管理员权限.所以我不想做上面的事情.所以,请帮我做同样的事.
bka*_*aid 105
将其添加到您的global.asax.cs:
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
Response.Headers.Remove("X-AspNet-Version");
Response.Headers.Remove("X-AspNetMvc-Version");
}
Run Code Online (Sandbox Code Playgroud)
luk*_*fer 77
在IIS7中,您必须使用HTTP模块.在VS中将以下内容构建为类库:
namespace StrongNamespace.HttpModules
{
public class CustomHeaderModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += OnPreSendRequestHeaders;
}
public void Dispose() { }
void OnPreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Set("Server", "Box of Bolts");
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后将以下内容添加到web.config中,或者在IIS中进行配置(如果在IIS中进行配置,则程序集必须位于GAC中).
<configuration>
<system.webServer>
<modules>
<add name="CustomHeaderModule"
type="StrongNamespace.HttpModules.CustomHeaderModule" />
</modules>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
Jef*_*hnn 39
使用IIS UrlRewrite 2.0
<outboundRules>
<rule name="Remove RESPONSE_Server" >
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="" />
</rule>
</outboundRules>
Run Code Online (Sandbox Code Playgroud)
Fré*_*ric 28
Scott Mitchell在博客文章中提供了删除不必要标题的解决方案.
正如在其他答案中已经说过的那样,对于Server
标题,有http模块解决方案或UrlScan模块.(IIS7.5 +中不再提供URLScan模块.请使用URLRewrite来消隐它.)
对于X-AspNet-Version
和X-AspNetMvc-Version
,他提供了一种比在每个响应中删除它们更好的方法:根本就不生成它们.
使用enableVersionHeader
禁用X-AspNet-Version
,在web.config中
<httpRuntime enableVersionHeader="false" />
Run Code Online (Sandbox Code Playgroud)
使用MvcHandler.DisableMvcResponseHeader
在.net Application_Start事件禁用X-AspNetMvc-Version
MvcHandler.DisableMvcResponseHeader = true;
Run Code Online (Sandbox Code Playgroud)
最后,在IIS配置中删除X-Powered-By
自定义标头.(这可以在web.config中完成configuration/system.webServer/httpProtocol/customHeaders/remove[name=X-Powered-By]
)
请注意,如果您有ARR(应用程序请求路由),它也会添加自己的ARR X-Powered-By
,自定义标头设置不会删除它.必须通过IIS管理器上的IIS管理器(不在站点上)上进行编辑器配置来删除这个:转到system.webServer/proxy
节点并设置arrResponseHeader
为false
.之后IISReset
,它被考虑在内.
(我在这里找到了这个,除了这篇文章是关于旧IIS 6.0配置事物的方式.)
不要忘记应用程序代码的解决方案默认情况下不适用于在静态内容上生成的标头(您可以激活runAllManagedModulesForAllRequests
用于更改它的标题,但它会导致所有请求运行.Net管道).这不是问题,X-AspNetMvc-Version
因为它没有添加到静态内容上(至少如果静态请求不在.Net管道中运行).
附注:当目的是为了掩盖使用的技术,你也应该改变标准.NET cookie名称(.ASPXAUTH
如果窗体身份验证激活(使用name
的属性forms
在web.config中标记), ASP.NET_SessionId
(使用<sessionState cookieName="yourName" />
在web.config文件下的system.web
标签), __RequestVerificationToken
(改通过代码AntiForgeryConfig.CookieName
,但遗憾的是不适用于此系统在html中生成的隐藏输入)).
Dan*_*are 18
实际上,上面显示的编码模块和Global.asax示例仅适用于有效请求.
例如,在URL的末尾添加<,您将收到一个"错误请求"页面,该页面仍然显示服务器标头.许多开发人员忽视了这一点.
显示的注册表设置也不起作用.URLScan是删除"服务器"标头的唯一方法(至少在IIS 7.5中).
小智 13
或者在web.config中添加:
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-AspNet-Version" />
<remove name="X-AspNetMvc-Version" />
<remove name="X-Powered-By" />
<!-- <remove name="Server" /> this one doesn't work -->
</customHeaders>
</httpProtocol>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
Vai*_*arg 11
除了URL Rewrite答案之外,这里还有完整的XMLweb.config
<system.webServer>
<rewrite>
<outboundRules>
<rule name="Remove RESPONSE_Server" >
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="Company name" />
</rule>
</outboundRules>
</rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
Nic*_*ans 10
要删除Server:
标题,请转到Global.asax
,查找/创建Application_PreSendRequestHeaders
事件并添加如下行(感谢BK和此博客,这也不会在Cassini/local dev上失败):
protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
// Remove the "Server" HTTP Header from response
HttpApplication app = sender as HttpApplication;
if (null != app && null != app.Request && !app.Request.IsLocal &&
null != app.Context && null != app.Context.Response)
{
NameValueCollection headers = app.Context.Response.Headers;
if (null != headers)
{
headers.Remove("Server");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想要一个完整的解决方案来删除Azure/IIS7上的所有相关标头并且也可以使用Cassini,请参阅此链接,该链接显示了在不使用HttpModules或URLScan的情况下禁用这些标头的最佳方法.
Ily*_*dik 10
此web.config
安装程序可以从ASP.NET响应中删除所有不必要的标头(至少从IIS 10开始):
<!--Removes version headers from response -->
<httpRuntime enableVersionHeader="false" />
<httpProtocol>
<customHeaders>
<!--Removes X-Powered-By header from response -->
<clear />
</customHeaders>
</httpProtocol>
<security>
<!--Removes Server header from response-->
<requestFiltering removeServerHeader ="true" />
</security>
Run Code Online (Sandbox Code Playgroud)
请注意,这将隐藏“应用程序”的所有标头,其他所有方法也是如此。如果您到达应用程序外部的IIS本身或ASP.NET生成的某些默认页面或错误页面,则这些规则将不适用。因此,理想情况下,它们应该位于IIS的根级别,并且该底线可能会对IIS本身留下一些错误响应。
PS:IIS 10中存在一个错误,即使使用正确的配置,它有时也会显示服务器标头。现在应该修复它,但是必须更新IIS / Windows。
如果您只想删除标题,可以使用缩短版本的lukiffer的答案:
using System.Web;
namespace Site
{
public sealed class HideServerHeaderModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders +=
(sender, e) => HttpContext.Current.Response.Headers.Remove("Server");
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Web.config
:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="CustomHeaderModule" type="Site.HideServerHeaderModule" />
</modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
尝试将HKLM\SYSTEM\CurrentControlSet\Services\HTTP\Parameters\DisableServerHeader
注册表项设置REG_DWORD
为1
.
归档时间: |
|
查看次数: |
115157 次 |
最近记录: |