Exemple #1
0
        [Fact, PlatformSpecific(PlatformID.Windows), OuterLoop] // Requires admin privileges
        public void TestUserCredentialsPropertiesOnWindows()
        {
            string username = "******", password = "******";

            try
            {
                Interop.NetUserAdd(username, password);
            }
            catch (Exception exc)
            {
                Console.Error.WriteLine("TestUserCredentialsPropertiesOnWindows: NetUserAdd failed: {0}", exc.Message);
                return; // test is irrelevant if we can't add a user
            }

            Process p = CreateProcessLong();

            p.StartInfo.LoadUserProfile     = true;
            p.StartInfo.UserName            = username;
            p.StartInfo.PasswordInClearText = password;

            SafeProcessHandle handle = null;

            try
            {
                p.Start();
                if (Interop.OpenProcessToken(p.SafeHandle, 0x8u, out handle))
                {
                    SecurityIdentifier sid;
                    if (Interop.ProcessTokenToSid(handle, out sid))
                    {
                        string actualUserName = sid.Translate(typeof(NTAccount)).ToString();
                        int    indexOfDomain  = actualUserName.IndexOf('\\');
                        if (indexOfDomain != -1)
                        {
                            actualUserName = actualUserName.Substring(indexOfDomain + 1);
                        }

                        bool isProfileLoaded = GetNamesOfUserProfiles().Any(profile => profile.Equals(username));

                        Assert.Equal(username, actualUserName);
                        Assert.True(isProfileLoaded);
                    }
                }
            }
            finally
            {
                if (handle != null)
                {
                    handle.Dispose();
                }

                if (!p.HasExited)
                {
                    p.Kill();
                }

                Interop.NetUserDel(null, username);
                Assert.True(p.WaitForExit(WaitInMS));
            }
        }
Exemple #2
0
        public static string?GetProcessUserName(Process p)
        {
            try
            {
                if (Interop.OpenProcessToken(p.SafeHandle, 0x8u, out var handle))
                {
                    if (Interop.ProcessTokenToSid(handle, out var sid))
                    {
                        string userName      = sid.Translate(typeof(NTAccount)).ToString();
                        int    indexOfDomain = userName.IndexOf('\\');
                        if (indexOfDomain != -1)
                        {
                            userName = userName.Substring(indexOfDomain + 1);
                        }

                        return(userName);
                    }
                }
            }
            catch (Win32Exception) { } // Process.SafeHandle can throw unauthorized since it uses OpenProcess with PROCESS_ALL_ACCESS.

            return(null);
        }
        [ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported)), PlatformSpecific(TestPlatforms.Windows), OuterLoop]         // Uses P/Invokes, Requires admin privileges
        public void TestUserCredentialsPropertiesOnWindows()
        {
            // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Unit test dummy credentials.")]
            string username = "******", password = "******";

            try
            {
                Interop.NetUserAdd(username, password);
            }
            catch (Exception exc)
            {
                Console.Error.WriteLine("TestUserCredentialsPropertiesOnWindows: NetUserAdd failed: {0}", exc.Message);
                return; // test is irrelevant if we can't add a user
            }

            bool hasStarted          = false;
            SafeProcessHandle handle = null;
            Process           p      = null;

            try
            {
                p = CreateProcessLong();

                p.StartInfo.LoadUserProfile     = true;
                p.StartInfo.UserName            = username;
                p.StartInfo.PasswordInClearText = password;

                hasStarted = p.Start();

                if (Interop.OpenProcessToken(p.SafeHandle, 0x8u, out handle))
                {
                    SecurityIdentifier sid;
                    if (Interop.ProcessTokenToSid(handle, out sid))
                    {
                        string actualUserName = sid.Translate(typeof(NTAccount)).ToString();
                        int    indexOfDomain  = actualUserName.IndexOf('\\');
                        if (indexOfDomain != -1)
                        {
                            actualUserName = actualUserName.Substring(indexOfDomain + 1);
                        }

                        bool isProfileLoaded = GetNamesOfUserProfiles().Any(profile => profile.Equals(username));

                        Assert.Equal(username, actualUserName);
                        Assert.True(isProfileLoaded);
                    }
                }
            }
            finally
            {
                IEnumerable <uint> collection = new uint[] { 0 /* NERR_Success */, 2221 /* NERR_UserNotFound */ };
                Assert.Contains <uint>(Interop.NetUserDel(null, username), collection);

                if (handle != null)
                {
                    handle.Dispose();
                }

                if (hasStarted)
                {
                    p.Kill();

                    Assert.True(p.WaitForExit(WaitInMS));
                }
            }
        }