如何在没有管理员权限的情况下判断驱动器是否已经启用了 BitLocker 加密?

5

对于我的目的,我只需要通过其DOS路径了解驱动器的BitLocker加密状态。就像这样:

enum DriveEncryptionStatus{
    Unprotected,
    Protected,
    Unknown
};

DriveEncryptionStatus = GetDriveBitlockerEncryptionStatus(L"C:\\");

我能找到Win32_EncryptableVolume类,但不幸的是,它带有以下警告:

要使用Win32_EncryptableVolume方法,必须满足以下条件:您必须拥有管理员特权。

有没有办法在不以管理员身份运行时完成此操作?

4个回答

7

BitLocker状态可在shell中由任何普通用户获取。Windows使用Win32 API中的Windows Property System检查未记录的shell属性System.Volume.BitLockerProtection来获取状态。您的程序也可以在不需要提升权限的情况下检查此属性。

如果此属性的值为1、3或5,则表示驱动器上启用了BitLocker。任何其他值都被视为关闭。

您可以使用Win32 API来检查此shell属性。作为一种礼貌,我已经将我的托管实现从我对类似问题的另一个回答进行了移植。

#include <shlobj.h>
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "propsys.lib")

DriveEncryptionStatus getDriveEncryptionStatus(LPCWSTR parsingName)
{
    IShellItem2 *drive = NULL;
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    hr = SHCreateItemFromParsingName(parsingName, NULL, IID_PPV_ARGS(&drive));
    if (SUCCEEDED(hr)) {
        PROPERTYKEY pKey;
        hr = PSGetPropertyKeyFromName(L"System.Volume.BitLockerProtection", &pKey);
        if (SUCCEEDED(hr)) {
            PROPVARIANT prop;
            PropVariantInit(&prop);
            hr = drive->GetProperty(pKey, &prop);
            if (SUCCEEDED(hr)) {
                int status = prop.intVal;

                drive->Release();

                if (status == 1 || status == 3 || status == 5)
                    return DriveEncryptionStatus::Protected;
                else
                    return DriveEncryptionStatus::Unprotected;
            }
        }
    }

    if (drive)
        drive->Release();

    return DriveEncryptionStatus::Unknown;
}

int main()
{
    DriveEncryptionStatus status = getDriveEncryptionStatus(L"C:");
    return 0;
}

1
谢谢。我会尝试一下。在此之前,你知道这些值:1到5代表什么吗? - c00000fd
2
我在Bitlocker Powershell模块的Microsoft.BitLocker.Structures.BitLockervolumeStatus中找到了它们:0-完全解密,1-完全加密,2-加密进行中,3-解密进行中,4-加密暂停,5-解密暂停,6-完全加密擦除进行中,7-完全加密擦除暂停。 - Zerqent
由于此处未记录,有人知道它是否仍然适用于 Windows 10 [版本 10.0.17763.973] 及更高版本吗? - Weej Jamal
为了让代码正常运行,您需要在函数上方添加以下行: enum class DriveEncryptionStatus { Protected, Unprotected, Unknown }; @Weej Jamal:该代码可在Windows 10(19041)上运行。 - Holger Schmeken
一个Python实现: from win32com.propsys import propsys; bitlocker_status = propsys.SHGetPropertyStoreFromParsingName("C:").GetValue(propsys.PSGetPropertyKeyFromName("System.Volume.BitLockerProtection")).GetValue() - Vynce
显示剩余2条评论

7

此答案的基础上进行扩展...

通过实证确定了 Windows 10 1909 (10.0.18363.1082) 上 System.Volume.BitLockerProtection 的值:

| System.Volume.      | Control Panel                    | manage-bde conversion     | manage-bde     | Get-BitlockerVolume          | Get-BitlockerVolume |
| BitLockerProtection |                                  |                           | protection     | VolumeStatus                 | ProtectionStatus    |
| ------------------- | -------------------------------- | ------------------------- | -------------- | ---------------------------- | ------------------- |
|                   1 | BitLocker on                     | Used Space Only Encrypted | Protection On  | FullyEncrypted               | On                  |
|                   1 | BitLocker on                     | Fully Encrypted           | Protection On  | FullyEncrypted               | On                  |
|                   1 | BitLocker on                     | Fully Encrypted           | Protection On  | FullyEncryptedWipeInProgress | On                  |
|                   2 | BitLocker off                    | Fully Decrypted           | Protection Off | FullyDecrypted               | Off                 |
|                   3 | BitLocker Encrypting             | Encryption In Progress    | Protection Off | EncryptionInProgress         | Off                 |
|                   3 | BitLocker Encryption Paused      | Encryption Paused         | Protection Off | EncryptionSuspended          | Off                 |
|                   4 | BitLocker Decrypting             | Decryption in progress    | Protection Off | DecyptionInProgress          | Off                 |
|                   4 | BitLocker Decryption Paused      | Decryption Paused         | Protection Off | DecryptionSuspended          | Off                 |
|                   5 | BitLocker suspended              | Used Space Only Encrypted | Protection Off | FullyEncrypted               | Off                 |
|                   5 | BitLocker suspended              | Fully Encrypted           | Protection Off | FullyEncrypted               | Off                 |
|                   6 | BitLocker on (Locked)            | Unknown                   | Unknown        | $null                        | Unknown             |
|                   7 |                                  |                           |                |                              |                     |
|                   8 | BitLocker waiting for activation | Used Space Only Encrypted | Protection Off | FullyEncrypted               | Off                 |

你能说明一下你是怎么建立这张表的吗? - drewish

2

在多次尝试使用C#实现这个功能失败后,我终于做到了。我对C++/C#开发还很新,如果我的答案完全无关,请告诉我,我会撤回。

    public static string GetBitLockerStatus()           
    {
        Process process = new Process();
        process.StartInfo.FileName = "powershell.exe";
        process.StartInfo.Arguments = "-command (New-Object -ComObject Shell.Application).NameSpace('C:').Self.ExtendedProperty('System.Volume.BitLockerProtection')"; 
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.Start();
        StreamReader reader = process.StandardOutput;
        string output = reader.ReadToEnd().Substring(0,1); //needed as output would otherwise be 1\r\n (if encrypted)
        Console.WriteLine(output);
        process.WaitForExit();
        return output;
    }

是的,这是一种高级的方法。由于这个问题被标记为C++和WinAPI,我想知道.NET(或代理的PowerShell)内部使用什么来确定? - c00000fd

0

在CMD和Powershell中也很容易实现。在CMD shell中,您可以使用以下一行命令要求Powershell将值作为退出代码返回:

powershell -command exit 1000 + (New-Object -ComObject Shell.Application).NameSpace('C:').Self.ExtendedProperty('System.Volume.BitLockerProtection')

并检查在CMD shell中返回的%ERRORLEVEL%


1
你看到这个问题被标记为WinAPI了吗? - c00000fd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接