IT業界のすみっこ暮らし

ふと気がついたときの記録



ASP.NET MVC:セキュリティ対応関連メモ

対応

1、クッキーの設定

Web.config

<system.web>
    <!-- 既存のコード -->
    <httpCookies httpOnlyCookies="true" requireSSL="true" />
</system.web>

SSL通信のときのみ、クッキーを有効にする

2、レスポンスヘッダーの設定

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、レスポンスヘッドのX-Frame-Optionsの自動出力の回避

ASP.NET MVC 5 では X-Frame-Options が自動で出力される時がある - しばやん雑記

[X-Frame-Options:SAMEORIGIN]を適用させたくない場合の設定(もちろん、2の[X-Frame-Options:SAMEORIGIN]設定がないことが前提。あると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>

確認

f:id:papamau:20171026153614p:plain

参考サイト

Cookie の仕様とセキュリティ

devcentral.f5.com

ASP.NETの状態管理:ビューステート/クッキー/セッション情報(2/3) - @IT

developer.mozilla.org

developer.mozilla.org

developer.mozilla.org

blog.shibayan.jp

developer.mozilla.org

qiita.com

OSコマンドインジェクション

SQL インジェクション攻撃とその対策 | Microsoft Docs

www.slideshare.net

www.websec-room.com





プライバシーポリシー