locked
Running Notepad from asp.net using PsExec or WMI RRS feed

  • Question

  • User405296658 posted

    Hello there.

    I develpoed an ASP.NET webpage that suppose to open a Notepad program on server pc.

    i tried two options but both failed:

    First - Using PsExec.exe tool to execute the proccess as myUser (Current logged - Administrator).

                ProcessStartInfo start = new ProcessStartInfo();
               
                start.WorkingDirectory =@"C:\Windows\System32\";
    
                start.Arguments = "/k D:\\Downloads\\PSTools\\PsExec.exe \\\\Domain -u Username -p password \"Notepad.exe\"";
                
                start.FileName = @"cmd.exe";
                
                start.CreateNoWindow = false;
    
                int exitCode;
    
    
                using (Process proc = Process.Start(start))
                {
                    proc.WaitForExit();
    
                    
                    // Retrieve the app's exit code
                    exitCode = proc.ExitCode;
                }
    
                

    This code doesn't run the process at all and stuck when waiting for the process to exit.

    Second - Using WMI :

                string strDomain = "Domain";
    
                var connOpts = new ConnectionOptions()
                {
                    // if i use this i get User credentials cannot be used for local connections‬
                    Username = "Username",
                    Password = "Password"
                }; 
    
                ConnectionOptions remoteConnectionOptions = new ConnectionOptions();
                remoteConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;
                remoteConnectionOptions.EnablePrivileges = true;
                remoteConnectionOptions.Authentication = AuthenticationLevel.Packet;
                
                ManagementScope scope = new ManagementScope(@"\\" + Domain + @"\root\CIMV2", remoteConnectionOptions);
                ManagementPath p = new ManagementPath("Win32_Process");
    
                
    
                ManagementClass classInstance = new ManagementClass(scope, p, null);
    
    
                
    
                object[] theProcessToRun = { @"Notepad.exe" };
    
                classInstance.InvokeMethod("Create", theProcessToRun);
    

    This code runs Notepad but as Application pool user.

    It is important to say that im using IIS Authentication :( Windows, & Impersonated).

    If someone has any idea why i notepad doesn't run.

    Thank you for help.

    Wednesday, March 30, 2016 2:35 PM

Answers

  • User-2057865890 posted

    Hi Guy Ben Hemo,

    1. Make sure impersonation is enabled.
    2. The service account in which the ASP.Net is running should have proper access to exe location and should have all the access to the resources the exe is trying to access.
    3. Set the app pool to run under LocalSystem.

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 31, 2016 7:52 AM