0

So I've created a GUI which activates and deactives network adapters using impersonation and powershell commands.

Here is a snippet from the code:

Get_adapters gets all adapters and bind them to the listview:

    public MainWindow()
    {
        InitializeComponent();
        Get_adapters();
    }

These are my two button commands:

    private void Activate_interface(object sender, RoutedEventArgs e)
    {
        string interface_name = ((Netadapter)ListView.SelectedItem).Name;
        Change_adapter_status(true, interface_name);
    }

    private void Deactivate_interface(object sender, RoutedEventArgs e)
    {
        string interface_name = ((Netadapter)ListView.SelectedItem).Name;
        Change_adapter_status(false, interface_name);
    }

This is the powershell method: (Account credentials hidden; but I've cheked them multiple times and they're correct).

    public void Change_adapter_status(bool _option, string _interface)
    {
        using (new Impersonator("Administrator", "DOMAIN", "pw"))
        {
            var process_info = new ProcessStartInfo();
            process_info.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
            process_info.Verb = "runas";

            if (_option == true)
            {
                process_info.Arguments = String.Format("netsh interface set interface „{0}“ enable", _interface);
            }
            else if (_option == false)
            {
                process_info.Arguments = String.Format("netsh interface set interface „{0}“ disable", _interface);
            }
            Process.Start(process_info);
        }
    }

As you can see, I use impersonation to start a powershell window as an admin user.

On my machine (Admin acc) everything works as excepted. Button is clicked, powershell window opens and does its thing.

On other computers (non-admins BUT are in the same domain, and have access to the admin account with the credentials from code above) the exe won't even start.

The computers have the same images (Windows 10) and the targeted .net framework is also correct.

On other admin accounts the exe is executable. So what could it be that the exe won't start for non-admin users? Thanks is advance.

Event output: - System

  • Provider

    [ Name] .NET Runtime

  • EventID 1026

    [ Qualifiers] 0

    Level 2

    Task 0

    Keywords 0x80000000000000

  • TimeCreated

    [ SystemTime] 2019-05-23T10:01:14.748998500Z

    EventRecordID 6679

    Channel Application

    Computer PLMC12906.greencorps.intra

    Security

    • EventData

    Application: Adapter.exe Frameworkversion: v4.0.30319 Description: The process was terminated due to an unhandled exception . Ausnahmeinformationen: System.Runtime.InteropServices.COMException bei System.Management.MTAHelper.CreateInMTA(System.Type) bei System.Management.ManagementPath.CreateWbemPath(System.String) bei System.Management.ManagementPath..cctor() Ausnahmeinformationen: System.TypeInitializationException bei System.Management.ManagementScope._Clone(System.Management.ManagementScope, System.Management.IdentifierChangedEventHandler) bei System.Management.ManagementObjectSearcher..ctor(System.Management.ManagementScope, System.Management.ObjectQuery, System.Management.EnumerationOptions) bei System.Management.ManagementObjectSearcher..ctor(System.Management.ObjectQuery) bei Adapter.MainWindow.Get_adapters() bei Adapter.MainWindow..ctor() Ausnahmeinformationen: System.Windows.Markup.XamlParseException bei System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri) bei System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri) bei System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean) bei System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext) bei System.Windows.Application.LoadComponent(System.Uri, Boolean) bei System.Windows.Application.DoStartup() bei System.Windows.Application.<.ctor>b__1_0(System.Object) bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) bei System.Windows.Threading.DispatcherOperation.InvokeImpl() bei System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object) bei MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object) bei System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) bei System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) bei MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) bei System.Windows.Threading.DispatcherOperation.Invoke() bei System.Windows.Threading.Dispatcher.ProcessQueue() bei System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) bei MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr) bei MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef) bei System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame) bei System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame) bei System.Windows.Application.RunDispatcher(System.Object) bei System.Windows.Application.RunInternal(System.Windows.Window) bei System.Windows.Application.Run(System.Windows.Window) bei System.Windows.Application.Run() bei Adapter.App.Main()

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Cross
  • 75
  • 8

3 Answers3

1

Try to add app.manifest file to project with your executable and set <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> on it

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
  • Added app.manifest and replaced asInvoker with requireAdministrator. Unfortunately I'm getting the same output. System tried to launch (cursor loading) but nothing happens. – Cross May 23 '19 at 09:23
  • Have you checked event log? Does it have something helpful? Try to check this [thread](https://stackoverflow.com/questions/9909784/impersonating-a-windows-user) and modify impersonation – Pavel Anikhouski May 23 '19 at 09:29
  • I've edited my question and eidtet the Event log. It's a runtime error. I'm trying the solution from the thread you sent right now and will update if it worked or not. Thanks for the link! – Cross May 23 '19 at 10:15
  • @Cross There is a link with the same exception [here](https://blogs.msdn.microsoft.com/alejacma/2010/10/20/get-wmiobject-the-type-initializer-for-system-management-mtahelper-threw-an-exception/) The problem can be in _C:\Windows\SysWOW64_ path hardcoded – Pavel Anikhouski May 23 '19 at 10:38
0

You have to use WindowsIdentity.Impersonate Method to impersonate.

user11909
  • 1,235
  • 11
  • 27
0

So this is how I ended up doing it: I removed the impersonation part of the code and replaced it with the inbuild methods from Process.StartInfo(); This is part of a gui to configure network adapters for non-admin users. Feel free to use.

public void Change_adapter_status(bool _option, string _interface)
        {
            SecureString password = new SecureString();
            password.AppendChar('p');
            password.AppendChar('a');
            password.AppendChar('s');
            password.AppendChar('s');
            password.AppendChar('w');
            password.AppendChar('o');
            password.AppendChar('r');
            password.AppendChar('d');

            Process p = new Process();
            p.StartInfo.UserName = "Administrator";
            p.StartInfo.Domain = "domain";
            p.StartInfo.Password = password;
            p.StartInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
            p.StartInfo.Verb = "runas";
            p.StartInfo.UseShellExecute = false;

            if (_option == true)
            {
                p.StartInfo.Arguments = String.Format("netsh interface set interface „{0}“ enable", _interface);
            }
            else if (_option == false)
            {
                p.StartInfo.Arguments = String.Format("netsh interface set interface „{0}“ disable", _interface);
            }
            p.Start();
            p.WaitForExit();
            Get_adapters(true);
        }
Cross
  • 75
  • 8