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)]