2016-06-20 3 views
0

Derzeit haben wir eine C# MVC-Webanwendung mit einfacher Windows-Authentifizierung auf IIS 7.5 unter Win Server 2012 R2 bereitgestellt.PowerShell-Skript, das auf IIS fehlschlägt

Controller:

public ActionResult Index([Bind(Include = "ID,cmd,arg1,arg2")] PowerShellCMD PScmd) 
    { 
     if (ModelState.IsValid) 
     { 
      //String script = @"C:\TEMP\test.ps1"; 
      String script = @"D:\a-espinoza\Scripts\wsa.ps1"; 

      PowerShell ps = PowerShell.Create(); 
      Runspace runspace = RunspaceFactory.CreateRunspace(); 
      ps.Runspace = runspace; 

      ps.Runspace.Open(); 

      using (var impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate()) 
      { 

       ps.AddScript(script); 
       ps.AddParameter(null, PScmd.cmd); 

       // Execute the script 
       var results = ps.Invoke(); 

       runspace.Close(); 

       if (results.Count > 0) 
       { 
        // We use a string builder ton create our result text 
        var builder = new StringBuilder(); 

        foreach (var psObject in results) 
        { 
         // Convert the Base Object to a string and append it to the string builder. 
         // Add \r\n for line breaks 
         builder.Append(psObject + "\r\n"); 
        } 

        // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < > 
        PScmd.result = Server.HtmlEncode(builder.ToString()); 
       } 
      } 
      //impersonationContext.Undo(); 
     } 

     return View(PScmd); 
    } 

überall dort, wo ein Benutzer ohne Login-Rechte auf dem IIS-Server, wo meine Anwendung bereitgestellt wird, um den Balg Fehler bekam:

Server Error in '/' Application.

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Requested registry access is not allowed.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SecurityException: Requested registry access is not allowed.]
Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +14302727 System.Environment.GetEnvironmentVariable(String variable, EnvironmentVariableTarget target) +278
System.Management.Automation.ModuleIntrinsics.GetExpandedEnvironmentVariable(String name, EnvironmentVariableTarget target) +9
System.Management.Automation.ModuleIntrinsics.SetModulePath() +61
System.Management.Automation.ExecutionContext.InitializeCommon(AutomationEngine engine, PSHost hostInterface) +714
System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface, RunspaceConfiguration runspaceConfiguration, InitialSessionState iss) +19187352
System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper() +19188647 System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall) +360
PowerShellExecution.Controllers.PWCmdsController.Index(PowerShellCMD PScmd) +254 lambda_method(Closure , ControllerBase , Object[]) +127 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 parameters) +242
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) +39
System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12
System.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) +139
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +112 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +452 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +37 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +241
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult asyncResult) +111
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34274

Wie können wir den Fehler zu vermeiden, ohne jeden Zugriff erlauben auf dem Server mit IIS?

Der Registrierungsschlüssel hat bereits allen Benutzern Lesezugriff gewährt.

Antwort

0

Können Sie die Sicherheitsprotokolle auf dem Server anzeigen, um festzustellen, welcher Benutzer die SecurityException verursacht?

Ich könnte ein wenig davon abweichen ... aber die Runspace wird außerhalb der Identitätswechsel erstellt, so würde es als Application Pool Identität/Benutzer ausgeführt werden. Ich würde nicht erwarten, dass das ausgeführt wird, wenn ein Benutzer auf die Site zugreift.

Der Benutzer, den Sie imitieren, muss Zugriff auf den Registrierungsschlüssel haben, auf den Ihr Skript verweist. Überprüfen Sie dies, dass erreichen out: https://technet.microsoft.com/en-us/library/cc775454(v=ws.10).aspx

Versuchen verändert sich:

PowerShell ps = PowerShell.Create(); 
Runspace runspace = RunspaceFactory.CreateRunspace(); 
ps.Runspace = runspace; 
ps.Runspace.Open(); 

using (var impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate()) 
{ 
    //...Code 
} 

An:

using (var impersonationContext = ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate()) 
{ 
    PowerShell ps = PowerShell.Create(); 
    Runspace runspace = RunspaceFactory.CreateRunspace(); 
    ps.Runspace = runspace; 
    ps.Runspace.Open(); 

    //...Code 
} 
+0

Nach tun Ihre vorgeschlagene Update auf dem Code kann ich überprüfen, dass Identitätswechsel funktioniert, als Ereignisprotokolle bestätigen der Benutzer (imitiert) Eintrag. Aber immer noch der gleiche Fehler, seine Benutzer haben keinen Zugriff auf die Registrierung zu lesen, wenn ich das Skript mit diesem Benutzer auf dem Server protokolliert ausgeführt wird, funktioniert es – atlus