Exemplo n.º 1
0
        public TokenProperties(IWithToken obj)
        {
            InitializeComponent();

            listPrivileges.SetDoubleBuffered(true);
            listPrivileges.ListViewItemSorter = new SortedListViewComparer(listPrivileges);
            GenericViewMenu.AddMenuItems(copyMenuItem.MenuItems, listPrivileges, null);
            listPrivileges.ContextMenu = menuPrivileges;

            if (obj == null)
            {
                return;
            }

            _object = obj;

            try
            {
                using (TokenHandle thandle = _object.GetToken(TokenAccess.Query))
                {
                    // "General"
                    try
                    {
                        textUser.Text         = thandle.GetUser().GetFullName(true);
                        textUserSID.Text      = thandle.GetUser().StringSid;
                        textOwner.Text        = thandle.GetOwner().GetFullName(true);
                        textPrimaryGroup.Text = thandle.GetPrimaryGroup().GetFullName(true);
                    }
                    catch (Exception ex)
                    {
                        textUser.Text = "(" + ex.Message + ")";
                    }

                    try
                    {
                        textSessionID.Text = thandle.GetSessionId().ToString();
                    }
                    catch (Exception ex)
                    {
                        textSessionID.Text = "(" + ex.Message + ")";
                    }

                    try
                    {
                        var type = thandle.GetElevationType();

                        if (type == TokenElevationType.Default)
                        {
                            textElevated.Text = "N/A";
                        }
                        else if (type == TokenElevationType.Full)
                        {
                            textElevated.Text = "True";
                        }
                        else if (type == TokenElevationType.Limited)
                        {
                            textElevated.Text = "False";
                        }
                    }
                    catch (Exception ex)
                    {
                        textElevated.Text = "(" + ex.Message + ")";
                    }

                    // Determine if the token has a linked token.
                    if (OSVersion.HasUac)
                    {
                        try
                        {
                            TokenHandle linkedToken = thandle.GetLinkedToken();

                            if (linkedToken != null)
                            {
                                linkedToken.Dispose();
                            }
                            else
                            {
                                buttonLinkedToken.Visible = false;
                            }
                        }
                        catch
                        {
                            buttonLinkedToken.Visible = false;
                        }
                    }
                    else
                    {
                        buttonLinkedToken.Visible = false;
                    }

                    try
                    {
                        bool virtAllowed = thandle.IsVirtualizationAllowed();
                        bool virtEnabled = thandle.IsVirtualizationEnabled();

                        if (virtEnabled)
                        {
                            textVirtualized.Text = "Enabled";
                        }
                        else if (virtAllowed)
                        {
                            textVirtualized.Text = "Disabled";
                        }
                        else
                        {
                            textVirtualized.Text = "Not Allowed";
                        }
                    }
                    catch (Exception ex)
                    {
                        textVirtualized.Text = "(" + ex.Message + ")";
                    }

                    try
                    {
                        using (TokenHandle tokenSource = _object.GetToken(TokenAccess.QuerySource))
                        {
                            var source = tokenSource.GetSource();

                            textSourceName.Text = source.SourceName.TrimEnd('\0', '\r', '\n', ' ');

                            long luid = source.SourceIdentifier.QuadPart;

                            textSourceLUID.Text = "0x" + luid.ToString("x");
                        }
                    }
                    catch (Exception ex)
                    {
                        textSourceName.Text = "(" + ex.Message + ")";
                    }

                    // "Advanced"
                    try
                    {
                        var statistics = thandle.GetStatistics();

                        textTokenType.Text          = statistics.TokenType.ToString();
                        textImpersonationLevel.Text = statistics.ImpersonationLevel.ToString();
                        textTokenId.Text            = "0x" + statistics.TokenId.ToString();
                        textAuthenticationId.Text   = "0x" + statistics.AuthenticationId.ToString();
                        textMemoryUsed.Text         = Utils.FormatSize(statistics.DynamicCharged);
                        textMemoryAvailable.Text    = Utils.FormatSize(statistics.DynamicAvailable);
                    }
                    catch (Exception ex)
                    {
                        textTokenType.Text = "(" + ex.Message + ")";
                    }

                    try
                    {
                        var groups = thandle.GetGroups();

                        _groups = new TokenGroupsList(groups);

                        foreach (var group in groups)
                        {
                            group.Dispose();
                        }

                        _groups.Dock = DockStyle.Fill;
                        tabGroups.Controls.Add(_groups);
                    }
                    catch (Exception ex)
                    {
                        tabGroups.Text = "(" + ex.Message + ")";
                    }

                    try
                    {
                        var privileges = thandle.GetPrivileges();

                        for (int i = 0; i < privileges.Length; i++)
                        {
                            this.AddPrivilege(privileges[i]);
                        }
                    }
                    catch (Exception ex)
                    {
                        tabPrivileges.Text = "(" + ex.Message + ")";
                    }
                }
            }
            catch (Exception ex)
            {
                tabControl.Visible = false;

                Label errorMessage = new Label();

                errorMessage.Text = ex.Message;

                this.Padding = new Padding(15, 10, 0, 0);
                this.Controls.Add(errorMessage);
            }

            if (!OSVersion.HasUac)
            {
                labelElevated.Enabled       = false;
                textElevated.Enabled        = false;
                textElevated.Text           = "";
                labelVirtualization.Enabled = false;
                textVirtualized.Enabled     = false;
                textVirtualized.Text        = "";
            }

            if (tabControl.TabPages[Settings.Instance.TokenWindowTab] != null)
            {
                tabControl.SelectedTab = tabControl.TabPages[Settings.Instance.TokenWindowTab];
            }

            ColumnSettings.LoadSettings(Settings.Instance.PrivilegeListColumns, listPrivileges);
            listPrivileges.AddShortcuts();
        }
Exemplo n.º 2
0
        public static void Main(Dictionary <string, string> pArgs)
        {
            args = pArgs;

            EnablePrivilege("SeAssignPrimaryTokenPrivilege");
            EnablePrivilege("SeBackupPrivilege");
            EnablePrivilege("SeRestorePrivilege");

            try
            {
                bool bad = false;

                if (!args.ContainsKey("-w"))
                {
                    if (!args.ContainsKey("-c") && !args.ContainsKey("-f"))
                    {
                        bad = true;
                    }

                    if (args.ContainsKey("-c") && args.ContainsKey("-f"))
                    {
                        bad = true;
                    }

                    if (!args.ContainsKey("-u") && !args.ContainsKey("-P"))
                    {
                        bad = true;
                    }

                    if (args.ContainsKey("-u") && args.ContainsKey("-P"))
                    {
                        bad = true;
                    }
                }

                if (args.ContainsKey("-v") || args.ContainsKey("-h"))
                {
                    bad = true;
                }

                if (bad)
                {
                    PrintUsage();
                    Exit();
                }
            }
            catch
            {
                PrintUsage();
                Exit();
            }

            if (args.ContainsKey("-w"))
            {
                try
                {
                    SetDesktopWinStaAccess();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Warning: Could not set desktop and window station access: " + ex.Message);
                }
            }

            TokenHandle token    = null;
            string      domain   = null;
            string      username = "";

            if (args.ContainsKey("-u"))
            {
                string user = args["-u"];

                if (user.Contains("\\"))
                {
                    domain   = user.Split('\\')[0];
                    username = user.Split('\\')[1];
                }
                else if (user.Contains("@"))
                {
                    username = user.Split('@')[0];
                    domain   = user.Split('@')[1];
                }
                else
                {
                    username = user;
                }

                LogonType type = LogonType.Interactive;

                if (args.ContainsKey("-t"))
                {
                    try
                    {
                        type = (LogonType)Enum.Parse(typeof(LogonType), args["-t"], true);
                    }
                    catch
                    {
                        Console.WriteLine("Error: Invalid logon type.");
                        Exit(-1);
                    }
                }

                try
                {
                    token = TokenHandle.Logon(
                        username,
                        domain,
                        args.ContainsKey("-p") ? args["-p"] : "",
                        type,
                        LogonProvider.Default
                        );
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: Could not logon as user: "******"-P"))
                    {
                        pid = int.Parse(args["-P"]);
                    }
                }
                catch
                {
                    Console.WriteLine("Error: Invalid PID.");
                }

                try
                {
                    using (var phandle = new ProcessHandle(pid, OSVersion.MinProcessQueryInfoAccess))
                    {
                        try
                        {
                            token = phandle.GetToken(TokenAccess.All);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error: Could not open process token: " + ex.Message);
                            Exit(Marshal.GetLastWin32Error());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: Could not open process: " + ex.Message);
                    Exit(Marshal.GetLastWin32Error());
                }

                // Need to duplicate the token if we're going to set the session ID.
                if (args.ContainsKey("-s"))
                {
                    try
                    {
                        TokenHandle dupToken;

                        dupToken = token.Duplicate(
                            TokenAccess.All,
                            SecurityImpersonationLevel.SecurityImpersonation,
                            TokenType.Primary
                            );
                        token.Dispose();
                        token = dupToken;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: Could not duplicate own token: " + ex.Message);
                        Exit(Marshal.GetLastWin32Error());
                    }
                }
            }

            if (args.ContainsKey("-s"))
            {
                int sessionId = int.Parse(args["-s"]);

                try
                {
                    token.SetSessionId(sessionId);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: Could not set token session ID: " + ex.Message);
                }
            }

            if (args.ContainsKey("-c") || args.ContainsKey("-f"))
            {
                if (!args.ContainsKey("-e"))
                {
                    EnvironmentBlock environment;
                    StartupInfo      startupInfo = new StartupInfo();
                    ProcessHandle    processHandle;
                    ThreadHandle     threadHandle;
                    ClientId         clientId;

                    environment         = new EnvironmentBlock(token);
                    startupInfo.Desktop = "WinSta0\\Default";

                    try
                    {
                        processHandle = ProcessHandle.CreateWin32(
                            token,
                            args.ContainsKey("-f") ? args["-f"] : null,
                            args.ContainsKey("-c") ? args["-c"] : null,
                            false,
                            ProcessCreationFlags.CreateUnicodeEnvironment,
                            environment,
                            args.ContainsKey("-d") ? args["-d"] : null,
                            startupInfo,
                            out clientId,
                            out threadHandle
                            );
                        processHandle.Dispose();
                        threadHandle.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: Could not create process: " + ex.Message);
                        Exit(Marshal.GetLastWin32Error());
                    }
                    finally
                    {
                        environment.Destroy();
                    }
                }
            }

            token.Dispose();

            Exit();
        }