Example #1
0
        static void Main(string[] args)
        {
            //Example:
            // "C:\Windows\System32\dsa.msc /domain=something.local"
            if (args.Length >= 4)
            {
                _settings.UserName = args[0];
                _settings.Domain   = args[1];
                _settings.Password = args[2];
                _settings.Command  = args[3];

                if (args.Length == 7)
                {
                    _settings.UseCustomCommandExecutor = bool.Parse(args[4]);
                    _settings.CustomCommandExecutor    = args[5];
                    _settings.CommandArgument          = args[6];
                }
            }

            StartupInfo startupInfo = new StartupInfo
            {
                cb    = Marshal.SizeOf(typeof(StartupInfo)),
                title = $"Impersonated command prompt - [{_settings.Domain}\\{_settings.UserName}]"
            };

            RunAsContext runAsContext = SetupRunAsContext(_settings);

            ProcessInfo processInfo = new ProcessInfo();

            if (Win32Wrapper.CreateProcessWithLogonW(_settings.UserName, _settings.Domain, _settings.Password, LogonFlags.LOGON_NETCREDENTIALS_ONLY, runAsContext.Executor, runAsContext.Command, 0, IntPtr.Zero, null, ref startupInfo, out processInfo))
            {
                Win32Wrapper.CloseHandle(processInfo.hProcess);
                Win32Wrapper.CloseHandle(processInfo.hThread);
            }
            else
            {
                string errorString = Marshal.GetLastWin32Error().ToString();
                Console.WriteLine(errorString);

                Console.WriteLine("\n\nPress any key to continue...");
                Console.ReadKey();
            }
        }