[研究]ASP.NET WebForm 網站【具有不安全、不正確或缺少 SameSite 屬性的 Cookie】 弱點處理
2025-07-04
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
********************************************************************************
先做個具有 SameSite 弱點的簡易網站
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<h2>SameSite 弱點示範</h2>
<asp:Label ID="Label1" runat="server" Text="尚未設定 Cookie"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text="尚未設定 Cookie"></asp:Label><br />
<asp:Label ID="Label3" runat="server" Text="尚未設定 Cookie"></asp:Label><br />
</form>
</body>
</html>
|
Default.aspx.cs
using System;
using System.Web;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["TestCookie"] != null)
{
Label1.Text = "Cookie 值為: " + Request.Cookies["TestCookie"].Value;
}
HttpCookie cookie = new HttpCookie("TestCookie", "SameSiteVulnerableValue");
// 故意不設定 SameSite 屬性,導致 SameSite 弱點
// cookie.SameSite = SameSiteMode.None; // ← 未設定,形成弱點
cookie.HttpOnly = true;
cookie.Secure = false; // 測試環境不強制 HTTPS
Response.Cookies.Add(cookie);
Label2.Text = "已設定 Cookie,請重新整理頁面觀察";
if (Request.Cookies["TestCookie"] != null)
{
Label3.Text = "Cookie 值為: " + Request.Cookies["TestCookie"].Value;
}
}
}
}
|
OWASP ZAP 2.16.1 掃描結果 Cookie without SameSite Attribute
HCL AppScan 10.8.0 掃描結果
具有不安全、不正確或缺少 SameSite 屬性的 Cookie
嚴重性: 中
CVSS 分數: 4.7
CVSS 向量: AV:N/AC:L/PR:L/UI:N/S:U/C:L/I:L/A:N/E:U/RL:O/RC:C/CR:X/IR:X/AR:X/MAV:X/MAC:X/MPR:X/MUI:X/MS:X/MCMAX/MS:X/MAC:X/
URL:(略)
實體: TestCookie (Cookie)
風險: 透過將 Cookie 限制為第一方或相同網站上下文來防止 Cookie 資訊洩漏,如果沒有額外的保護措施(如反 CSRF 令牌),攻擊可能會擴展為跨站點請求偽造 (CSRF)
攻擊。
原因: 具有不正確、不安全或缺少 SameSite 屬性的敏感 Cookie
固定值: 查看將 SameSite Cookie 屬性配置為建議值的可能解決方案
----------
推理: 回應包含不安全、不正確或缺少 SameSite 屬性的敏感 Cookie,這可能會導致 Cookie 資訊洩露,如果沒有額外的保護措施,可能會擴展為跨站點請求偽造
(CSRF) 攻擊。
測試請求和回應:Set-Cookie: TestCookie=SameSiteVulnerableValue; path=/; HttpOnly
********************************************************************************
若使用 .NET Framework 4.7.2 以上版本,微軟已內建對 SameSite 的改善,部分情況下可透過配置控制:
Web.Confiig 增加設定
<system.web>
<httpCookies sameSite="Strict" requireSSL="true" />
</system.web>
|
注意,Visual Studio 2019 Enterprise 會有警告,因為不認得這個屬性,不理警告
OWASP ZAP 2.16.1 掃描結果,沒有 SameSite 問題了 (下圖)




沒有留言:
張貼留言