简体   繁体   中英

Access Bitlocker recovery info from Active Directory

I am finding links to recover bitlocker passwords from active directory using vbscript, but I would like to do this using my c# program.

I have found some information on this, but nothing has been successful so far. Can anyone help out and correct the code, or point me in the direction of solving this?

here is the latest code that I have found and tested, but it doesn't work

class BitlockerModel
{
    public string RecoveryGuid { get; set; }
    public string RecoveryPassword { get; set; }
    public string Date { get; set; }
    public string Time { get; set; }

    public BitlockerModel()
    {
        RecoveryGuid = string.Empty;
        RecoveryPassword = string.Empty;
        Date = string.Empty;
        Time = string.Empty;
    }
}

private void btnBitlockerSearch_Click(object sender, EventArgs e)
    {
        try
        {
            var computerName = txtBitlockerSearch.Text;
            if ((string.IsNullOrEmpty(computerName)))
            {
                MessageBox.Show(@"Computername can't be empty");
                return;
            }
            var bitlockerObject = new BitlockerLookup();
            var result = bitlockerObject.GetBitlockerInfo(computerName).ToList();

            foreach (var items in result)
            {
                lsBitlockerInformation.Items.Add(string.Format("Date: {0}", items.Date));
                lsBitlockerInformation.Items.Add(string.Format("Time: {0}", items.Time));
                lsBitlockerInformation.Items.Add(string.Format("RecoveryGUID: {0}", items.RecoveryGuid));
                lsBitlockerInformation.Items.Add(string.Format("Recovery Password: {0}", items.RecoveryPassword));
                lsBitlockerInformation.Items.Add(Environment.NewLine);
            }
        }
        catch
        {
            MessageBox.Show(@"Enter a Valid ComputerName");
        }
    }

 internal class BitlockerLookup
{
    public List<BitlockerModel> GetBitlockerInfo(string computerName)
    {
        var returnRecoveryInfo = LookupBitlockerRecoveryInfor(computerName);
        return returnRecoveryInfo;
    }
    private static List<BitlockerModel> LookupBitlockerRecoveryInfor(string computerName)
    {    
        var bitlockerModelList = new List<BitlockerModel>();

        var dsSearcher = new DirectorySearcher
        {
            Filter = String.Format("(&(objectCategory=Computer)(cn={0}))", computerName)
        };

        try
        {
            var srResult = dsSearcher.FindOne();
            if (srResult == null)
            {
                MessageBox.Show(@"Failed to find the computer object.", @"Error", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
            else
            {
                var objValue = Marshal.BindToMoniker(srResult.GetDirectoryEntry().Path.Replace("GC://", "LDAP://"));
                var tType = objValue.GetType();
                tType.InvokeMember("Filter",
                    System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.Public, null,
                    objValue, new Object[] {"msFVE-RecoveryInformation"});

                foreach (var obj in (IEnumerable) objValue)
                {
                    var tempList = new BitlockerModel();
                    var gRecoveryGuid =
                        new Guid(
                            (Byte[])
                                obj.GetType()
                                    .InvokeMember("msFVE-RecoveryGuid",
                                        System.Reflection.BindingFlags.GetProperty |
                                        System.Reflection.BindingFlags.Public |
                                        System.Reflection.BindingFlags.Instance, null, obj, null, null, null, null));
                    var name =
                        obj.GetType()
                            .InvokeMember("name",
                                System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public |
                                System.Reflection.BindingFlags.Instance, null, obj, null, null, null, null)
                            .ToString();
                    var date = name.Substring(3, name.IndexOf("T", System.StringComparison.Ordinal) - 3);
                    var objDate = Convert.ToDateTime(date);
                    date = string.Format("{0:dddd, MMMM d, yyyy}", objDate);

                    var time = name.Substring(name.IndexOf("T", System.StringComparison.Ordinal) + 1,
                        name.IndexOf("{", System.StringComparison.Ordinal) - 20);
                    var objTime = DateTime.Parse(time).ToString("h:mm:ss tt");
                    time = objTime;

                    tempList.Time = time;
                    tempList.Date = date;
                    tempList.RecoveryGuid = "{" + gRecoveryGuid.ToString().ToUpper() + "}";
                    tempList.RecoveryPassword =
                        obj.GetType()
                            .InvokeMember("msFVE-RecoveryPassword",
                                System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Public |
                                System.Reflection.BindingFlags.Instance, null, obj, null, null, null, null)
                            .ToString();

                    bitlockerModelList.Add(tempList);
                }
                return bitlockerModelList;
            }
        }
        catch
        {
            MessageBox.Show(@"Error, you must enter a computer name");
            return null;
        }

        return new List<BitlockerModel>();
    }
}

弄清楚了,尽管我必须建立一个模型来保存响应并使用管理员帐户运行应用程序,但是开始的代码运行良好。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM