/// <summary>
        /// Overridden ProcessRecord method.
        /// </summary>
        protected override void ProcessRecord()
        {
            NtToken token = null;

            if (Duplicate)
            {
                using (NtToken base_token = GetToken(TokenAccessRights.Duplicate))
                {
                    if (base_token != null)
                    {
                        token = base_token.DuplicateToken(TokenType, ImpersonationLevel, Access);
                        if (IntegrityLevel.HasValue)
                        {
                            using (NtToken set_token = token.Duplicate(TokenAccessRights.AdjustDefault))
                            {
                                set_token.SetIntegrityLevel(IntegrityLevel.Value);
                            }
                        }
                    }
                }
            }
            else
            {
                token = GetToken(Access);
            }
            WriteObject(token);
        }
        private void btnSetIL_Click(object sender, EventArgs e)
        {
            TokenIntegrityLevel il = GetILFromComboBox(comboBoxIL);

            if (_token.IntegrityLevel != il)
            {
                try
                {
                    _token.SetIntegrityLevel(il);
                    btnSetIL.Enabled = false;
                    UpdatePrivileges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
 private void btnDuplicate_Click(object sender, EventArgs e)
 {
     try
     {
         using (NtToken token = _token.DuplicateToken((TokenType)comboBoxTokenType.SelectedItem,
                                                      (SecurityImpersonationLevel)comboBoxImpLevel.SelectedItem, TokenAccessRights.MaximumAllowed))
         {
             TokenIntegrityLevel il = GetILFromComboBox(comboBoxILForDup);
             if (il != token.IntegrityLevel)
             {
                 token.SetIntegrityLevel(il);
             }
             OpenForm(token, "Duplicate", true);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        private void btnImpersonate_Click(object sender, EventArgs e)
        {
            SecurityImpersonationLevel implevel = SecurityImpersonationLevel.Impersonation;

            try
            {
                if (_token.TokenType == TokenType.Impersonation)
                {
                    implevel = _token.ImpersonationLevel;
                }

                using (NtToken token = _token.DuplicateToken(TokenType.Impersonation, implevel, TokenAccessRights.MaximumAllowed))
                {
                    TokenIntegrityLevel il = GetILFromComboBox(comboBoxILForDup);
                    if (il != token.IntegrityLevel)
                    {
                        token.SetIntegrityLevel(il);
                    }

                    NtToken imptoken = null;
                    using (var imp = token.Impersonate())
                    {
                        imptoken = NtThread.Current.OpenToken();
                    }
                    if (imptoken != null)
                    {
                        OpenForm(imptoken, "Impersonation", false);
                    }
                    else
                    {
                        MessageBox.Show(this, "Couldn't open thread token", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            Win32Process new_process = null;

            try
            {
                CreateProcessFlags flags = CreateProcessFlags.None;
                bool parent_process      = false;
                bool set_il            = false;
                TokenIntegrityLevel il = 0;
                bool show_help         = false;

                OptionSet opts = new OptionSet()
                {
                    { "p", "Use parent technique to create the new process", v => parent_process = v != null },
                    { "j", "Try and break away from the current process job", v => flags |= v != null ? CreateProcessFlags.BreakawayFromJob : 0 },
                    { "c", "Create a new console for the process", v => flags |= v != null ? CreateProcessFlags.NewConsole : 0 },
                    { "s", "Create the process suspended", v => flags |= v != null ? CreateProcessFlags.Suspended : 0 },
                    { "i|il=", "Set the process IL level", v => {
                          il = ParseIL(v); set_il = true;
                      } },
                    { "h|help", "show this message and exit", v => show_help = v != null },
                };

                int pid;

                List <string> commands = opts.Parse(args);
                if (show_help || commands.Count < 2)
                {
                    ShowHelp(opts);
                }

                if (!int.TryParse(commands[0], out pid))
                {
                    throw new ArgumentException("Couldn't parse PID value");
                }

                if (!NtToken.EnableDebugPrivilege())
                {
                    Console.WriteLine("WARNING: Couldn't enable Debug privilege");
                }

                using (NtProcess process = NtProcess.Open(pid, ProcessAccessRights.MaximumAllowed))
                {
                    if (parent_process)
                    {
                        new_process = Win32Process.CreateProcess(process, null, commands[1], set_il ? flags | CreateProcessFlags.Suspended : flags, null);
                        if (set_il)
                        {
                            using (NtToken token = new_process.Process.OpenToken())
                            {
                                token.SetIntegrityLevel(il);
                            }
                            if ((flags & CreateProcessFlags.Suspended) == 0)
                            {
                                new_process.Thread.Resume();
                            }
                        }
                    }
                    else
                    {
                        using (NtToken token = process.OpenToken())
                        {
                            using (NtToken target_token = token.DuplicateToken(TokenType.Primary, SecurityImpersonationLevel.Anonymous, TokenAccessRights.MaximumAllowed))
                            {
                                if (set_il)
                                {
                                    target_token.SetIntegrityLevel(il);
                                }

                                new_process = Win32Process.CreateProcessAsUser(target_token, null, commands[1], flags, null);
                            }
                        }
                    }

                    using (new_process)
                    {
                        Console.WriteLine("Created Process: PID: {0}, SID {1}", new_process.Pid, new_process.Process.SessionId);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
                if (new_process != null && new_process.Process != null)
                {
                    try
                    {
                        new_process.Process.Terminate(NtStatus.STATUS_WAIT_1);
                    }
                    catch
                    {
                    }
                }
            }
        }