Community程式設計與開發github.com

mianhtsys/landingMod3

SQL Server 安全性與驗證全方位指南:涵蓋驗證模式、授權、加密、資料列層級安全性、動態資料遮罩、稽核與強化。

landingMod3 是什麼?

此技能為 Microsoft SQL Server (2016-2025) 提供全方位的安全性和驗證專業知識,涵蓋內部部署 (Windows/Linux/容器) 與雲端 (Azure SQL Database、Azure SQL 受控執行個體、Azure VM 上的 SQL、AWS RDS)。它系統性地處理五個互補層:驗證 (Windows、SQL、Microsoft Entra ID、憑證、自主資料庫)、授權 (主體、安全性實體、權限層級、固定及自訂角色、結構描述、最低權限)、加密 (TDE、Always Encrypted、TLS、備份與資料格層級)、資料保護 (資料列層級安全性、動態資料遮罩、分類帳) 以及強化與稽核 (SQL Server Audit、CIS 基準、攻擊面縮減)。此技能會優先根據引擎版本 (`SELECT @@VERSION`) 和平台來確認功能可用性,因為在內部部署產品與 PaaS 產品之間存在顯著差異 (例如 Azure SQL Database 上不提供 Windows 驗證或 `xp_cmdshell`)。它提供可採取行動的 T-SQL 範例 (唯讀稽核指令碼) 和參考架構,強調深度防禦和最低權限原則,同時提醒使用者任何啟用功能、授與權限或輪替金鑰的變更都需要進行審查。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/mianhtsys/landingMod3/tree/HEAD/MODULO_3_DESARROLLO_AL_LADO_DEL_CLIENTE_Y_SERVIDOR_WEB/architecture-skills-framework/.agents/skills/sqlserver-security

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

SQL Server 安全性與驗證

您是 Microsoft SQL Server (2016-2025 版) 在 Windows、Linux、容器以及雲端 (Azure SQL Database、Azure SQL 受控執行個體、Azure VM 上的 SQL、AWS RDS) 的安全性與驗證專家。您負責完整的鏈結:誰可以連線 (驗證)、他們可以做什麼 (授權)、資料如何被保護 (靜態與傳輸中的加密)、什麼被隱藏或過濾 (RLS / DDM)、什麼被記錄且具備防竄改能力 (稽核/分類帳),以及如何鎖定攻擊面 (強化)。

採用 SQL Server 特定的推理,而非通用的資料庫建議。務必確認引擎版本 (SELECT @@VERSION) 和平台,因為功能可用性以及某些功能的定義 (例如 Windows 驗證、TDE 憑證管理、xp_cmdshell) 在內部部署產品和 PaaS 產品之間有極大差異。

如何處理安全性要求

  1. 首先確認版本和平台。 TDE 在歷史上僅限 Enterprise/Developer 版本,從 2019 起適用 Standard/Web/Express;Always Encrypted 從 2016 起支援,安全記憶體保護區從 2019 起支援 (內部部署使用具 HGS 證明功能的 VBS 記憶體保護區);分類帳與嚴格的 TLS / TDS 8.0 從 2022 起支援;RLS 和 DDM 從 2016 起支援。在 Azure SQL Database 上,沒有 Windows 驗證、沒有執行個體層級登入、沒有 xp_cmdshell,金鑰管理是透過 Azure Key Vault / Entra ID。在 Azure SQL 受控執行個體 上,表面範圍更接近內部部署,但依然沒有 Windows 驗證 (由 Entra ID 取代)。在 AWS RDS 上,沒有 sa、沒有真正的 sysadmin,且作業系統層級功能被封鎖。
  2. 分類要求觸及的層 並載入對應的參考資料:
    • 驗證/登入/Kerberos/Entra ID/孤立使用者 -> references/authentication.md
    • 權限/角色/結構描述/最低權限/模擬 -> references/authorization-rbac.md
    • TDE / Always Encrypted / TLS / 備份或資料格加密/金鑰階層 -> references/encryption.md
    • 資料列層級安全性/動態資料遮罩/分類/分類帳 -> references/data-protection.md
    • 攻擊面強化/SQL Server Audit/sa 帳戶/CIS -> references/hardening-and-auditing.md
  3. 預設採用最低權限和深度防禦。 沒有任何單一控制措施是足夠的。缺少授權的驗證、缺少稽核的加密,或將遮罩誤認為安全性界限在實務上都會失敗。
  4. 提供可操作、可驗證的 T-SQL 並指向 scripts/ 中的唯讀稽核指令碼。將任何授與權限、啟用功能或輪替金鑰的做法視為需要審查的變更 — 此技能中的指令碼嚴格來說是唯讀的,並僅以註解範本形式提供補救措施。
  5. 當要求跨領域時,交叉參考同級項目 (請參閱此檔案結尾)。

分層安全性模型

將 SQL Server 安全性視為五個同心圓層。要求幾乎總是落在其中一層,但一個好的答案會檢查其周圍的層。

                 +-----------------------------------------+
                 |          5. 強化/攻擊面                 |   xp_cmdshell, CLR, sa,
                 |   +---------------------------------+   |   連接埠, 修補程式, CIS
                 |   |        4. 稽核/分類帳            |   |   SQL Audit, 登入稽核,
                 |   |  +---------------------------+   |   |   防竄改證明
                 |   |  |     3. 加密                |   |   |   TDE, Always Encrypted,
                 |   |  |  +-------------------+    |   |   |   TLS, 備份, 資料格層級
                 |   |  |  | 2. 授權            |    |   |   |   主體 -> 安全性實體,
                 |   |  |  | +-------------+   |    |   |   |   角色, 最低權限
                 |   |  |  | |1. 驗證      |   |    |   |   |   Windows/AD, SQL, Entra ID,
                 |   |  |  | +-------------+   |    |   |   |   憑證, 自主資料庫
                 |   |  |  +-------------------+    |   |   |
                 |   |  +---------------------------+   |   |
                 |   +---------------------------------+   |
                 +-----------------------------------------+
  1. 驗證 — 證明身分以取得連線 (在伺服器層級的登入,或限定於資料庫的自主/Entra 主體)。
  2. 授權 — 將身分對應到使用者,並透過安全性實體權限和角色決定其可以執行的操作。
  3. 加密 — 保護靜態 (TDE、資料格層級)、資料行中 (Always Encrypted) 和傳輸中 (TLS) 的位元組。
  4. 稽核/分類帳 — 記錄誰做了什麼;分類帳使記錄具有加密方式防竄改的能力。
  5. 強化 — 縮小攻擊面並移除危險的預設值,使上層不會被繞過。

第 1 層 — 驗證模式

在內部部署環境中,執行個體會以下列兩種驗證模式之一執行:僅限 Windows 驗證 或 混合模式 (Windows + SQL)。使用 SERVERPROPERTY('IsIntegratedSecurityOnly') 檢查 (1 = 僅限 Windows,0 = 混合)。在 Azure SQL DB/MI 上,模型以 Entra ID 為中心,且不存在 Windows 驗證。

模式身分證明方式存放位置最佳用途附註 / 版本與平台
Windows / Active DirectoryOS Token;Kerberos (建議) 或 NTLM 後援sys.server_principals 類型 U (使用者) / G (Windows 群組)已加入網域的內部部署產品最強大的內部部署選項;遵循上游的 AD 密碼原則、帳戶鎖定、MFA。需要 SPN 才能使用 Kerberos;否則會靜默回退到 NTLM (無法委派,「雙躍點」會失敗)。Azure SQL DB/MI 上不提供。
SQL 驗證登入名稱 + 密碼 (在 master 中雜湊處理)類型 S混合環境、無法使用 AD 的應用程式、跨平台啟用 CHECK_POLICY 以及 (適當時) CHECK_EXPIRATION。密碼透過網路傳輸 — 加密連線 (登入交握始終已加密,但請強制執行工作階段的 TLS)。除非使用 AD 驗證,否則 Linux 上需要此模式。
Microsoft Entra ID (Azure AD)Entra Token (整合式/密碼/互動式-MFA/受控識別/服務主體)類型 E (Entra 使用者/SP) / X (Entra 群組)Azure SQL DB、MI 以及已啟用 Arc 的內部部署雲端環境中 Windows 驗證的替代方案。支援 MFA、條件式存取、受控識別 (無密碼)。可以強制執行 僅限 Entra 驗證 以完全停用 SQL 登入。每個伺服器/執行個體有一個 Entra 管理員。
憑證/非對稱金鑰登入擁有憑證的私密金鑰類型 C (憑證) / K (非對稱金鑰)端點驗證 (鏡像/AG、Service Broker)、模組簽署不適用於互動式使用者。用於授與已簽署模組較高的權限,而不將這些權限賦予呼叫者。
自主資料庫使用者密碼 (SQL 樣式) 或 Windows/Entra 身分,儲存在使用者資料庫中DB 層級主體,無伺服器登入可攜式資料庫、無需登入同步的 AG 容錯移轉需要 CONTAINMENT = PARTIAL。連線字串必須設定 Initial Catalog 指向自主資料庫 (驗證在該資料庫中進行,而非 master)。以更大的攻擊面換取可攜性 — 請仔細審查。

深入探討驗證 — Kerberos vs NTLM 選擇、使用 setspn 註冊 SPN、MSA/gMSA 服務帳戶、雙躍點問題與限制委派、完整的 Entra ID 變體清單、憑證/模組簽署登入、自主資料庫安全性考量、18456 登入失敗狀態碼表格,以及 sys.server_principals 類型圖例 — 請參閱 references/authentication.md。

-- 執行個體驗證模式:1 = 僅限 Windows,0 = 混合模式 (唯讀)
SELECT SERVERPROPERTY('IsIntegratedSecurityOnly') AS is_windows_auth_only;
-- [安全性變更] 僅供說明;請使用預留位置名稱,確認目標執行個體。
-- (a) 人類 / 互動式 SQL 登入:強制執行原則和到期日。
CREATE LOGIN [a_person] WITH PASSWORD = N'<generate-32+char-random-secret>',
    CHECK_POLICY = ON, CHECK_EXPIRATION = ON;

-- (b) 服務 / 應用程式 SQL 登入:強制執行原則,但關閉到期日
--     (應用程式無法以互動方式輪替) -> 依排程**頻外**輪替密碼。
--     在平台允許的情況下,建議服務使用 Windows/Entra/受控識別。
CREATE LOGIN [app_svc] WITH PASSWORD = N'<generate-32+char-random-secret>',
    CHECK_POLICY = ON, CHECK_EXPIRATION = OFF;
-- 從秘密管理員取得秘密;切勿提交它們;避免暴露於 T-SQL/殼層歷程記錄中;
-- 輪替任何曾從文件貼上的值。請參閱 references/authentication.md §2。

-- 從 Windows AD 群組建立登入 (成員資格繼承自 AD)
CREATE LOGIN [CONTOSO\SQL-App-Readers] FROM WINDOWS;

-- Azure SQL DB/MI:從 Entra 群組建立使用者 (不儲存密碼)
-- CREATE USER [SQL-App-Readers] FROM EXTERNAL PROVIDER;

第 2 層 — 授權基本概念

鏈結是 登入 (伺服器) -> 使用者 (資料庫) -> 結構描述 -> 權限,應用於安全性實體階層 伺服器 -> 資料庫 -> 結構描述 -> 物件。權限包括 GRANT、REVOKE 和 DENY — 而 DENY 永遠勝過任何 GRANT,無論角色成員資格為何。

固定伺服器角色 (叢集範圍權力):sysadmin (繞過所有權限檢查 — 嚴加防範)、securityadmin (可以授與任何權限,且實際上等同於 sysadmin — 視為同等)、serveradmin、dbcreator、processadmin、setupadmin、bulkadmin、diskadmin,再加上精細的 2022+ 角色 (##MS_DatabaseConnector##、##MS_LoginManager## 等)。

固定資料庫角色:db_owner (資料庫的完整控制權)、db_securityadmin、db_accessadmin、db_ddladmin、db_datareader / db_datawriter,以及其拒絕對應角色 db_denydatareader / db_denydatawriter。

最低權限配方 (完整版本請參閱 references/authorization-rbac.md):

-- [安全性變更] 僅供說明;透過 DB_NAME() 確認 DB,使用預留位置名稱,先在測試用 DB 中執行。
-- 唯讀應用程式使用者 (如果需要範圍界定,建議使用結構描述/資料庫角色而非 db_datareader)
CREATE USER [app_ro] FOR LOGIN [app_ro];
ALTER ROLE [db_datareader] ADD MEMBER [app_ro];

-- 在結構描述範圍授與的自訂角色 (最低權限,即使結構描述中新增物件仍可維持)
CREATE ROLE [sales_rw];
GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA::[Sales] TO [sales_rw];
DENY ALTER ON SCHEMA::[Sales] TO [sales_rw];  -- 即使其他地方有授與 ALTER,在此封鎖 DDL
-- 檢查目前主體的 EFFECTIVE 權限 (唯讀)
SELECT * FROM sys.fn_my_permissions(NULL, 'SERVER');
SELECT * FROM sys.fn_my_permissions('Sales.Orders', 'OBJECT');

切勿將 sysadmin、securityadmin 或 db_owner 授予應用程式帳戶。建議使用在結構描述層級授與的自訂資料庫角色,透過模組簽署或 EXECUTE AS 進行受控的權限提升,並留意擁有權鏈結的意外情況 (以及高風險的跨資料庫擁有權鏈結選項)。關於主體、安全性實體、WITH GRANT OPTION、CONTROL vs ALTER vs 精細權限、guest/public、應用程式角色和有效權限稽核 (sys.fn_my_permissions、HAS_PERMS_BY_NAME) 的完整說明,請參閱 references/authorization-rbac.md。

第 3 層 — 加密概覽

所有金鑰都源自一個階層:服務主要金鑰 (SMK) -> 資料庫主要金鑰 (DMK) -> 憑證/非對稱金鑰 -> 對稱金鑰。遺失保護加密資料的憑證意味著遺失資料 — 在建立金鑰和憑證的當下立即備份它們。

功能防護對象加密點可查詢?效能版本/平台
TDE被盜的 .mdf/.ldf/備份檔案 (靜態資料)引擎,頁面 I/O完全 (透明)~3-5% CPU傳統上僅限 Enterprise/Developer;Standard/Web/Express 從 2019 起 (Express 有其自身的備份限制)。也會加密 tempdb。Azure SQL:預設開啟,由服務管理。
Always EncryptedDBA / 雲端營運商 (資料絕不以純文字形式存在於伺服器端)用戶端驅動程式有限 — 決定性允許僅限相等;隨機則無用戶端 CPU + 大小增長2016+;安全記憶體保護區 2019+ (內部部署為 VBS 記憶體保護區,HGS 證明) 啟用範圍/LIKE、就地加密。CMK 位於 Windows 憑證存放區或 AKV。
TLS (傳輸中)網路竊聽 / MITM網路通道N/A極小所有版本。嚴格加密 / TDS 8.0 2022+ (Encrypt=Strict)。
備份加密被盜的備份檔案BACKUP 作業N/A極小2014+。Enterprise/Standard 可以建立加密備份;Express/Web 無法建立 (任何版本都可以還原)。獨立於 TDE;使用憑證或非對稱金鑰。
資料格層級/資料行無需重寫應用程式的目標資料行機密性 (伺服器端金鑰)ENCRYPTBYKEY / DECRYPTBYKEY手動,破壞 SARGability每次呼叫 CPU所有版本。對於真正的用戶端機密性,建議使用 Always Encrypted。
-- TDE 憑證狀態和 DEK 進度 (唯讀)
SELECT DB_NAME(database_id) AS db, encryption_state_desc,
       key_algorithm, key_length, percent_complete
FROM sys.dm_database_encryption_keys;

-- 憑證到期監控 (忘記到期可能會封鎖復原)
SELECT name, subject, expiry_date, pvt_key_encryption_type_desc
FROM sys.certificates ORDER BY expiry_date;

關於金鑰階層、TDE 設定/輪替/EKM-AKV 保護器、Always Encrypted (決定性 vs 隨機、哪些查詢會中斷、參數化、安全記憶體保護區和證明)、TLS / Force Encryption / Encrypt+TrustServerCertificate / 嚴格的 TDS 8.0、備份加密、資料格層級加密以及憑證生命週期管理的深入探討,請參閱 references/encryption.md。

第 3.5 層 — 資料列層級安全性與動態資料遮罩

兩者均為 2016+,且兩者都容易被誤用。

  • 資料列層級安全性 (RLS) 限制哪些資料列

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

mianhtsys/landingMod3

Implement WCAG 2.2 compliant interfaces with mobile accessibility, inclusive design patterns, and assistive technology support. Use when auditing accessibility, implementing ARIA patterns, building for screen readers, or ensuring inclusive user experiences.

mianhtsys/landingMod3

Master REST and GraphQL API design principles to build intuitive, scalable, and maintainable APIs that delight developers. Use when designing new APIs, reviewing API specifications, or establishing API design standards.

mianhtsys/landingMod3

Diseña y compara alternativas arquitectónicas trazables a RF/RNF sin implementar código.

mianhtsys/landingMod3

Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.

mianhtsys/landingMod3

Revisión adversarial de la arquitectura contra RF/RNF, detectando contradicciones, cobertura faltante y complejidad innecesaria.

mianhtsys/landingMod3

Master authentication and authorization patterns including JWT, OAuth2, session management, and RBAC to build secure, scalable access control systems. Use when implementing auth systems, securing APIs, or debugging security issues.

mianhtsys/landingMod3

>- Generate grounded-and-verified, engine-agnostic database documentation that reaches 100% parity with the real schema. Introspects the LIVE database as ground truth and cross-validates it against ORM models, migrations, generated types, seeds, and application queries, then proves completeness by diffing the docs back against the database. Produces ER diagrams (mermaid), per-table data dictionaries, and a machine-readable schema.json. Works with PostgreSQL, MySQL, SQL Server, and SQLite across any ORM (Prisma, TypeORM, Drizzle, Sequelize, Knex, Django, Rails) or raw SQL. Use when asked to document a database, produce an ERD or data dictionary, write db/schema docs, audit schema drift, or refresh existing DB docs.

mianhtsys/landingMod3

Compara estrategias de persistencia y motores de datos según consistencia, concurrencia, volumen, recuperación y operación.

mianhtsys/landingMod3

Design robust, scalable database schemas for SQL and NoSQL databases. Provides normalization guidelines, indexing strategies, migration patterns, constraint design, and performance optimization. Ensures data integrity, query performance, and maintainable data models.

mianhtsys/landingMod3

Consolida análisis especializados en una recomendación arquitectónica, matriz de decisión, ADR y plan de validación.

mianhtsys/landingMod3

Master error handling patterns across languages including exceptions, Result types, error propagation, and graceful degradation to build resilient applications. Use when implementing error handling, designing APIs, or improving application reliability.

mianhtsys/landingMod3

Build and maintain web frontends — component architecture, state management, API integration, responsive layout, client-side performance, and frontend testing patterns. Framework agnostic, focused on web frontend implementation. Do not use for backend service implementation, data engineering, or platform infrastructure work.

mianhtsys/landingMod3

Evalúa despliegue, contenedores, escalado, alta disponibilidad, observabilidad, backup, recuperación y CI/CD de forma proporcional.

mianhtsys/landingMod3

Execute read-only SQL queries against multiple Microsoft SQL Server databases. Use when: (1) querying MSSQL/SQL Server databases, (2) exploring database schemas/tables, (3) running SELECT queries for data analysis, (4) checking database contents. Supports multiple database connections with descriptions for intelligent auto-selection. Blocks all write operations (INSERT, UPDATE, DELETE, DROP, etc.) for safety.

mianhtsys/landingMod3

Generate and maintain OpenAPI 3.1 specifications from code, design-first specs, and validation patterns. Use when creating API documentation, generating SDKs, or ensuring API contract compliance.

mianhtsys/landingMod3

PHP 8.0+ development — XAMPP, RESTful APIs, PDO/MySQL/MariaDB, and authentication. Use when building PHP backends, creating API endpoints, configuring XAMPP, or integrating PHP with databases.

mianhtsys/landingMod3

Use this skill when designing or reviewing a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features

mianhtsys/landingMod3

Analiza requisitos funcionales y no funcionales, reglas, restricciones, capacidad y ambigüedades antes de seleccionar tecnologías.

mianhtsys/landingMod3

Implement modern responsive layouts using container queries, fluid typography, CSS Grid, and mobile-first breakpoint strategies. Use when building adaptive interfaces, implementing fluid layouts, or creating component-level responsive behavior.

mianhtsys/landingMod3

>- Use when designing or implementing software securely: define security requirements, threat-model a feature, choose secure defaults, design authentication and authorization, handle untrusted data and secrets, evaluate dependencies, design multi-tenant trust boundaries, or review security-sensitive changes. Use for prevention during requirements, design, implementation, and review; not for post-build security assessments or scanning an existing codebase.

相關技能