0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ASP.NETCoreで作成したAPIの脆弱性を改善する

Posted at

ASP.NETCore でAPIを作成したので
はじめて自分で脆弱性診断を行ってみた。診断結果をもとに改善してみる。
そのときの備忘録としてここに記す。

参考サイト

脆弱性の解決に導いてくれたサイト達

環境

  • .NETCore3.1
  • OS: Windows
  • OWASP ZAP 2.9.0

改善作業

Server Leaks Information via "X-Powered-By" HTTP Response Header Field(s)

  • 原因:Web /アプリケーションサーバーは、1つ以上の「X-Powered-By」HTTP応答ヘッダーを介して情報を漏えいしている。
    このような情報にアクセスすると、攻撃者はWebアプリケーションが依存している他のフレームワーク/コンポーネントや
    そのようなコンポーネントがさらされる可能性のある脆弱性を特定しやすくなる可能性がある。
  • 解決:不要なレスポンスヘッダを削除する。

ASP.NETWebApplicationプロジェクトにweb.configファイルを追加。

web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering removeServerHeader="true" />
    </security>
    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

web.configはIIS環境での設定ファイルなので、Linux環境用にコードでも制御する。

Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureKestrel(serverOptions => serverOptions.AddServerHeader = false)
                    .UseStartup<Startup>();
            });
}

※web.configを追加せず、Program.csだけの修正では動かないようです。

X-Content-Type-Optionsヘッダの設定ミス

原因:Anti-MIME-SniffingヘッダーX-Content-Type-Optionsに nosniff が設定されていない。

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

不完全なキャッシュ制御やPragma HTTP ヘッダー設定

  • 原因:Cache-Controlにpragma HTTP Headerを適切に設定していない

  • 解決

web.config
<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Cache-Control" value="private, no-store, no-cache, must-revalidate"/>
    </customHeaders>
  </httpProtocol>
</system.webServer>

おまけ:👇のようにControllerの属性に付与しても対応できるが、各Controllerに追加しないとなのでめんどくさい。

HogeController.cs
// no-store, no-cache
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?