понедельник, 6 февраля 2012 г.

Get local password policy (PasswordComplexity, ReversibleEncryption) C#


private void GetPasswordInfo()
        {
   
            var tempFile = Path.GetTempFileName();

            using (Process p = new Process
                                        {
                                            StartInfo =
                                                {
                                                    FileName = Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) + Path.DirectorySeparatorChar + "secedit.exe",
                                                    Arguments = String.Format(@"/export /areas SECURITYPOLICY /cfg ""{0}"" /quiet", tempFile),
                                                    CreateNoWindow = true,
                                                    UseShellExecute = false
                                                }
                                        })
            {
                p.Start();
                p.WaitForExit();
            }

            var file = IniFile.Load(tempFile);

            //TODO Use regex to find nessesary string values
            IniSection systemAccess;
            string passwordComplexityString;
            int passwordComplexityInt;

            var passwordComplexity = file.Sections.TryGetValue("System Access", out systemAccess)
                && systemAccess.TryGetValue("PasswordComplexity", out passwordComplexityString)
                && Int32.TryParse(passwordComplexityString, out passwordComplexityInt)
                && passwordComplexityInt == 1;

            string reversibleEncryptionString;
            int reversibleEncryptionInt;

            var reversibleEncryption = file.Sections.TryGetValue("System Access", out systemAccess)
                && systemAccess.TryGetValue("ClearTextPassword", out reversibleEncryptionString)
                && Int32.TryParse(reversibleEncryptionString, out reversibleEncryptionInt)
                && reversibleEncryptionInt == 1;
        }


 private class IniFile
        {
            public static IniFile Load(string filename)
            {
                var result = new IniFile { Sections = new Dictionary<string, IniSection>() };
                var section = new IniSection(String.Empty);
                result.Sections.Add(section.Name, section);

                foreach (var line in File.ReadAllLines(filename))
                {
                    var trimedLine = line.Trim();
                    switch (line[0])
                    {
                        case ';':
                            continue;
                        case '[':
                            section = new IniSection(trimedLine.Substring(1, trimedLine.Length - 2));
                            result.Sections.Add(section.Name, section);
                            break;
                        default:
                            var parts = trimedLine.Split('=');
                            if (parts.Length > 1)
                            {
                                section.Add(parts[0].Trim(), parts[1].Trim());
                            }
                            break;
                    }
                }

                return result;
            }

            public IDictionary<string, IniSection> Sections { get; private set; }
        }

        private class IniSection : Dictionary<string, string>
        {
            public IniSection(string name)
                : base(StringComparer.OrdinalIgnoreCase)
            {
                Name = name;
            }

            public string Name { get; private set; }
        }


Комментариев нет:

Отправить комментарий