본문 바로가기
개발/ASP.NET

ASP.NET MVC : 어플 단위에서 설정 가능한 security 대책 방법

by ispie 2019. 1. 28.

ASP.NET MVC의 웹 어플리케이션의 보안과 관련해서 업무중에 실시한 보안 대책의 일부를 정리해 보았습니다.

 

사내 기준으로 웹 어플과 인프라 쪽의 보안 대책을 동시에 진행했었습니다만 저는 웹 어플만을 다뤘기 때문에 인프라쪽의 대책에 대해서는 별도로 언급하지 않겠습니다.

 

1. Cookie 설정

 

Web.config

<system.web>
    <!-- 기존 설정 -->
    <httpCookies httpOnlyCookies="true" requireSSL="true" />
</system.web>

 

SSL의 경우에만 Cookie를 유효화 시킵니다.

(예) http로 접속할 경우엔 Cookie가 무효이므로 유저 로긴 불가)

 

 

2. Response Header 설정

 

Web.config

<system.webServer>
    <!-- 기존 설정 -->
    <httpProtocol>
      <customHeaders>
        <remove name="Cache-Control" />
        <remove name="X-Powered-By" />
        <add name="X-Frame-Options" value="SAMEORIGIN" />
        <add name="X-XSS-Protection" value="1; mode=block" />
        <add name="X-Content-Type-Options" value="nosniff" />
        <add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
        <add name="Pragma" value="no-cache" />
      </customHeaders>
    </httpProtocol>
</system.webServer>

 

[X-Frame-Options:SAMEORIGIN]

 

iframe의 제한을 자기자신과 생산처가 같은 프레임 안이었을 경우에만 가능하도록 제한 합니다.

 

[X-XSS-Protection:1; mode=block]

 

XSS필터를 유효화 합니다.

 

[X-Content-Type-Options:nosniff]

 

Web브라우저에 의한 「Content-Type」의 예측을 회피하여 브라우저에 의한 의도하지 않은 동작을 회피 합니다.

 

 

3. Response Header의 X-Frame-Options의 자동 출력의 회피

 

ASP.NET MVC 5 では X-Frame-Options が自動で出力される時がある

 

[X-Frame-Options:SAMEORIGIN]을 적용하고 싶지 않을 경우의 설정입니다.

(물론 2의 [X-Frame-Options:SAMEORIGIN] 설정이 없는것이 전제조건으로, 2를 설정했을 경우엔 2의 설정이 우선시 됩니다.)

 

Global.asax.cs

using System.Web.Helpers;
using System.Security.Claims;

protected void Application_Start()
{
    // 기존 코드
    // .....

    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
    AntiForgeryConfig.SuppressXFrameOptionsHeader = true;
}

 

4. 브라우저 캐시의 무효화

 

ActionFilterAttribute를 계승하는 클래스인 OnActionExecuted에 캐시 무효화를 위한 코드를 추가

 

FilterConfig.cs

public class FilterConfig
{
	public static void RegisterGlobalFilters(GlobalFilterCollection filters)
	{
		filters.Add(new CacheAttribute());
	}
}

 

CacheAttribute.cs

public class CacheAttribute: ActionFilterAttribute
{
	public override void OnActionExecuted(ActionExecutedContext filterContext)
	{
		var response = filterContext.HttpContext.Response;
		response.Cache.SetCacheability(HttpCacheability.NoCache);

		base.OnActionExecuted(filterContext);
	}
}

 

Web.config에서 대응할 경우(2와 3을 양쪽 다 대응할 경우의 예)

<httpProtocol>
  <customHeaders>
	<remove name="Cache-Control" />
	<remove name="X-Powered-By" />
	<add name="X-Frame-Options" value="SAMEORIGIN" />
	<add name="X-XSS-Protection" value="1; mode=block" />
	<add name="X-Content-Type-Options" value="nosniff" />
	<add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
	<add name="Pragma" value="no-cache" />
  </customHeaders>
</httpProtocol>

 

5. 설정이 정상적으로 동장하는지 확인

 

 

 

참고 사이트


http://www.yunabe.jp/docs/cookie_and_security.html#secure
https://devcentral.f5.com/articles/7http
http://www.atmarkit.co.jp/fdotnet/bookpreview/learnaspnet_0701/learnaspnet_0701_02.html
https://developer.mozilla.org/docs/Web/HTTP/X-Frame-Options
https://developer.mozilla.org/docs/Web/HTTP/Headers/X-XSS-Protection
https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Content-Type-Options

https://developer.mozilla.org/docs/Web/HTTP/Caching
http://blog.shibayan.jp/entry/20131019/1382147363
https://qiita.com/tamura__246/items/6307889936d6e7c98403
http://senmon.cfc.ac.jp/studentreport/report2/OS.html
https://technet.microsoft.com/ja-jp/library/dd362952.aspx
https://www.slideshare.net/zaki4649/free-securitycheck
https://www.websec-room.com/2013/04/06/835

 

댓글