验证账本表以检测篡改

适用于: SQL Server 2022 (16.x) Azure SQL 数据库Azure SQL 托管实例

本文将验证账本表中的数据的完整性。 如果已在数据库上配置了“自动摘要存储”设置,请按照使用自动摘要存储的 T-SQL 部分进行操作。 否则,请按照使用手动生成的摘要的 T-SQL 部分进行操作

先决条件

运行数据库的账本验证

  1. 使用 SQL Server Management StudioAzure Data Studio 连接到数据库。

  2. 使用以下 T-SQL 语句创建新查询:

    DECLARE @digest_locations NVARCHAR(MAX) = (SELECT * FROM sys.database_ledger_digest_locations FOR JSON AUTO, INCLUDE_NULL_VALUES);SELECT @digest_locations as digest_locations;
    BEGIN TRY
        EXEC sys.sp_verify_database_ledger_from_digest_storage @digest_locations;
        SELECT 'Ledger verification succeeded.' AS Result;
    END TRY
    BEGIN CATCH
        THROW;
    END CATCH
    

    注意

    验证脚本还可以在 Azure 门户中找到。 打开 Azure 门户并找到要验证的数据库。 在“安全性”中,选择“账本”选项。 在“账本”窗格中,选择“</> 验证数据库”

  3. 执行查询。 你将看到 digest_locations 返回存储存储数据库摘要的当前位置和任何以前的位置。 “结果”返回账本验证成功或失败消息。

    Screenshot of running ledger verification by using Azure Data Studio.

  4. 打开 digest_locations 结果集以查看摘要的位置。 以下示例显示了此数据库的两个摘要存储位置:

    • path 指示摘要的位置。

    • last_digest_block_id 指示 path 位置中存储的最后一个摘要的块 ID。

    • is_current 指示 path 中的位置是当前位置 (true) 还是之前的位置 (false)。

      [
       {
           "path": "https:\/\/digest1.blob.core.windows.net\/sqldbledgerdigests\/janderstestportal2server\/jandersnewdb\/2021-05-20T04:39:47.6570000",
           "last_digest_block_id": 10016,
           "is_current": true
       },
       {
           "path": "https:\/\/jandersneweracl.confidential-ledger.azure.com\/sqldbledgerdigests\/janderstestportal2server\/jandersnewdb\/2021-05-20T04:39:47.6570000",
           "last_digest_block_id": 1704,
           "is_current": false
       }
      ]
      

    重要

    运行账本验证时,请检查 digest_locations 的位置,以确保从预期位置检索验证中使用的摘要。 你希望确保特权用户在没有配置和锁定的不可变策略的情况下未将摘要存储的位置更改为不受保护的存储位置,例如 Azure 存储。

  5. 验证在“结果”窗口中返回以下消息。

    • 如果没有对数据库进行篡改,消息将如下所示:

      Ledger verification successful
      
    • 如果对数据库进行了篡改,“消息”窗口将显示以下错误:

      Failed to execute query. Error: The hash of block xxxx in the database ledger doesn't match the hash provided in the digest for this block.