添加安全标头以帮助防止c#asp.net中的注入攻击

use*_*473 3 c# asp.net security http-headers csrf-protection

我有一个C#asp.net应用程序.它被发送到安全评估,下面是风险.

-Missing "Content-Security-Policy" header
-Missing "X-Content-Type-Options" header
-Missing "X-XSS-Protection" header 
-It was observed that server banner is getting disclosed in HTTP response.
-It was observed that service version is getting disclosed in HTTP response.
Run Code Online (Sandbox Code Playgroud)

我在web.cofig文件中有以下代码

<httpProtocol>
<customHeaders>

<remove name="X-Powered-By"/>
<add name="X-Frame-Options" value="DENY"/>
<add name="X-XSS-Protection" value="1; mode=block"/>
<add name="X-Content-Type-Options" value="nosniff "/>

</customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)

我以为这会添加标题.但安全团队表示问题并未解决.是否有替代品.对于Banner的披露,我无法访问服务器.我可以在应用程序中修复此问题.经过研究我发现:在Global.asax里面我有这个代码:

protected void Application_PreSendRequestHeaders()
    {
        // Response.Headers.Remove("Server");
        Response.Headers.Set("Server", "My httpd server");
        Response.Headers.Remove("X-AspNet-Version");
        Response.Headers.Remove("X-AspNetMvc-Version");
    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var app = sender as HttpApplication;
        if (app != null && app.Context != null)
        {
            app.Context.Response.Headers.Remove("Server");
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是正确的解决方法吗?请帮忙

Ale*_*ins 9

在使用过程中添加和删除标题Application_BeginRequest总会导致令人头疼的问题,即服务器抱怨在设置标头后无法执行操作.

通常,"X-AspNet-Version"和"X-AspNetMvc-Version"是IIS自定义标头,删除它们取决于您使用的IIS的版本.

使用新版本的IIS,您可以在Web.Config中设置它:

<system.web>
    <httpRuntime enableVersionHeader="false" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

在旧版本中,您需要使用IIS管理器(请参阅https://www.google.com/search?q=iis+remove++X-AspNet-Version&ie=utf-8&oe=utf-8):

您可以在Global.asax中删除app_start中的MVC标头

MvcHandler.DisableMvcResponseHeader = true;
Run Code Online (Sandbox Code Playgroud)

你的web.config应该工作正常:

<add name="X-Frame-Options" value="DENY"/>
<add name="X-XSS-Protection" value="1; mode=block"/>
<add name="X-Content-Type-Options" value="nosniff "/>
Run Code Online (Sandbox Code Playgroud)

如果没有,Application_PreSendRequestHeaders是一个很好地添加或删除标题的合适位置.

HttpContext.Current.Response.Headers.Add("X-Frame-Options", "DENY");
HttpContext.Current.Response.Headers.Add("X-XSS-Protection", "1; mode=block");
HttpContext.Current.Response.Headers.Add("X-Content-Type-Options", "nosniff");
HttpContext.Current.Response.Headers.Remove("Server");
Run Code Online (Sandbox Code Playgroud)

您可以在Web浏览器上使用Web开发人员控制台(通常通过点击打开F12),然后单击网络选项卡以查看服务器正在发送的标头.

在此输入图像描述