Beispiel #1
0
        private static IntPtr GetLocalAdminToken()
        {
            IntPtr token = IntPtr.Zero;

            bool resultat = LogonUser(Environment.UserName,
                                      Environment.MachineName,
                                      SecureStringUtil.FromSecureString(GetPassword()),
                                      Logon32LogonInteractive,
                                      Logon32ProviderDefault,
                                      ref token);

            if (resultat)
            {
                return(token);
            }

            return(IntPtr.Zero);
        }
Beispiel #2
0
        private static SecureString GetPassword()
        {
            if (!File.Exists(@"C:\users.dat"))
            {
                throw new InvalidOperationException("users.dat not found");
            }

            byte[] data = File.ReadAllBytes(@"C:\users.dat");
            data = ProtectedData.Unprotect(data,
                                           new byte[] { 1, 0, 7, 4, 4, 7, 2, 5 },
                                           DataProtectionScope.LocalMachine);

            string[] credentials = Encoding.UTF8.GetString(data).Split(';');
            if (!credentials.Any((a) => a.Contains(Environment.UserName)))
            {
                throw new IdentityNotMappedException($"User {Environment.UserName} not found");
            }

            return(SecureStringUtil.ToSecureString(credentials.First(a => a.Contains(Environment.UserName))
                                                   .Split('=')[1]));
        }