재우니의 블로그

 

https://blog.elmah.io/improving-security-in-asp-net-mvc-using-custom-headers/

 

Improving security in ASP.NET MVC using custom headers

Improving the security in your ASP.NET MVC and Web API app is easy using custom headers and a bit of C#. Using this guide, you will learn about the entire list of headers needed to make it hard for hackers to exploit your website. No need to browse through

blog.elmah.io

이것은 ASP.NET 보안에 대한 시리즈의 첫 번째 게시물입니다.

 

최근에 생산적인 Scott Helme가 제작 한 securityheaders.io를 발견 했습니다. securityheaders.io는 웹 사이트를 검색하고 보안을 향상시키기 위해 추가 할 HTTP 응답 헤더를 제안합니다. 나는 이미 Troy Hunt의 ASafaWeb을 사용 하고 있지만, 약간 다른 초점을두고 있으며 일부 헤더가 부족합니다.

 

 

 

이 게시물은 최근 추가 된 elmah.io 헤더뿐만 아니라 해커가 일반적으로 ASP.NET (MVC, Web API, Core) 웹 사이트를 손상시키는 것을 어렵게 만드는 방법에 대한 요약입니다. 다음 단락에서는 쉽게 구성되고 모든 사람들이 구현해야하는 많은 HTTP 헤더를 나열했습니다.

 

X-Frame-Options

X-Frame-Options헤더는 해커가 당신이 결코 의도하지 링크를 클릭하도록 유도하기 위해 사이트를하면 iframe하지 않는 것을 확인합니다. ASP.NET MVC 5 이상을 사용하는 경우이 헤더가 자동으로 추가됩니다. 이전 버전 또는 다른 웹 프레임 워크에 헤더를 추가하는 것은 다음을 사용하면 쉽습니다 web.config.

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="DENY" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

이 예에서는 웹 사이트의 iframe 가능성을 부인합니다. 동일한 도메인에서 iframe을 사용하는 경우 값을로 변경할 수 있습니다 SAMEORIGIN.

 

X-Xss-Protection

The X-Xss-Protection는 대부분의 최신 브라우저에 구현 된 기능으로, 사이트 간 스크립팅 공격이 감지 될 때 페이지 로딩을 중지합니다. 헤더 추가 web.config도 다음과 같이 진행됩니다.

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="X-Xss-Protection" value="1; mode=block" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

이 값은 1단순히 보호를 사용 가능으로 표시합니다.

 

X-Content-Type-Options

MIME 유형 스니핑을 방지하기 위해 X-Content-Type-Options헤더를 추가 할 수 있습니다 따라서 해커가 콘텐츠를 검사하여 올바른 MIME 형식을 추측하기가 더 어려워집니다. 다음을 통해 헤더를 쉽게 추가 할 수 있습니다 web.config.

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="X-Content-Type-Options" value="nosniff" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

 

Referrer-Policy

Referer사용자가 사이트의 링크를 클릭하면 브라우저가 자동으로 헤더를 추가합니다 즉, 링크 된 웹 사이트는 사용자의 출처를 볼 수 있습니다. 웹 로그 분석에서는 이러한 기능이 유용하지만 다른 URL로 전달하지 않으려는 URL에 민감한 정보가있을 수 있습니다. 리퍼러를 완전히 제거하려면 다음 헤더를 다음에 추가하십시오 web.config.

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Referrer-Policy" value="no-referrer" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

실생활에서는 다른 가치를 원할 수 있습니다 Referrer-Policy리퍼러를 완전히 제거하면 웹 사이트에서 내부 트래픽 흐름을 볼 수 없습니다. 가능한 값 목록을 보려면 mozilla.org의 Referrer-Policy를 확인하십시오 .

 

X-Permitted-Cross-Domain-Policies

cross-origin 요청을하기 위해 Flash 구성 요소를 제한하려면 Flash  를 사용하지 않는 한 Flash  구성 요소를 완전히 사용 중지해야합니다. 이렇게하려면를 추가 X-Permitted-Cross-Domain-Policies합니다 web.config:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="X-Permitted-Cross-Domain-Policies" value="none" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

 

Strict-Transport-Security

웹 사이트에 HTTPS를 구현하지 않았다면, 그렇게해야합니다. 이렇게하면 Strict-Transport-Security헤더를 사용하여 HTTP를 통해 발생하는 모든 통신을 차단할 수 있습니다 .

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

이 max-age값은 브라우저에 지정된 초 동안이 설정을 사용하도록 지시합니다. 이 경우 1 년. includeSubDomains당신이 (블로그 등) 하위 도메인에 비 HTTPS 웹 사이트를 호스팅하는 경우 부분은 제외 할 수 있습니다.

 

X-Powered-By

X-Powered-By헤더가 자동으로 ASP.NET에 의해 추가됩니다. 웹 사이트를 호스팅하기 위해 어떤 기술을 사용하는지 명확하지 않게하려면 다음을 통해이 헤더를 제거해야합니다 web.config.

<system.webServer>
  <httpProtocol>
    <customHeaders>
        <remove name="X-Powered-By" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

 

X-AspNetMvc- Version

흡사 X-Powered-ByX-AspNetMvc-Version자동 프레임 워크에 의해 추가 된 헤더이다. MVC와 어떤 버전을 사용하는지 해커에게 알리지 않으려면 제거해야합니다. X-AspNetMvc-Version제거 web.config할 수는 없지만 코드에서 사용 중지 할 수는 있습니다. 귀하의 Startup.cs또는에 다음을 추가하십시오 Global.asax.cs:

MvcHandler.DisableMvcResponseHeader = true;

 

Server

ASP.NET은 또한 응용 프로그램을 호스팅하는 서버를 나타냅니다. 해커가 IIS를 사용하고 있음을 알게되면해볼 필요가있는 취약점의 수를 줄입니다. Server머리글 을 제거하려면 필터 또는 다음 코드를 사용 하여 머리글을 제거하십시오 Global.asax.cs.

protected void Application_PreSendRequestHeaders()
{
    if (HttpContext.Current != null)
    {
        HttpContext.Current.Response.Headers.Remove("Server");
    }
}

 

콘텐츠 보안 정책

Content-Security-Policy헤더를 사용 하려면 좀 더 설명이 필요합니다. 자세한 내용은이 시리즈의 다음 게시물을 확인하십시오. ASP.NET MVC의 Content-Security-Policy


 

web.config 가 아닌 간단하게 global.asax.cs 에서 모든걸 소스로 처리하는 방법은 아래 코드와 같습니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace SKTHappyInsight_2020
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {

            MvcHandler.DisableMvcResponseHeader = true;

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Headers.Remove("X-Powered-By");
            HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
            HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version");
            HttpContext.Current.Response.Headers.Remove("Server");
        }
    }
}