使用Asp.Net Web应用程序中的sysinternals PSExec执行脚本

PSL*_*PSL 8 c# asp.net process processstartinfo psexec

我试图PSExec从我的Asp.Net Web应用程序执行连接到远程服务器.它"Access Denied Error -5"以某种方式提供没有凭据设置,并通过设置它给出的PSEXEC命令中的凭据 "2250 Network connection could not be found".我是服务器上的管理员,我已Windows authentication and Asp.Net Impersonation启用(IIS7.5).更有趣的是,当我尝试从一个console application或甚至只是使用command prompt它执行它只是工作正常.我试图做一个ping操作作为测试.

这是我的代码片段: -

            var startInfo = new ProcessStartInfo{
                CreateNoWindow = true,
                UseShellExecute = false,
                FileName = FilePath,
                Arguments = CommandArgs
            }

            Process vsCommandProcess = Process.Start(startInfo);

            vsCommandProcess.WaitForExit();
            var exitCode = vsCommandProcess.ExitCode;
            if (vsCommandProcess.ExitCode != 0)
            {
                ...rest of the code
Run Code Online (Sandbox Code Playgroud)

这里:-

FilePath --> C:\pstools\psexec.exe
Arguments --> \\servername -accepteula -u domain\userName -p password ipconfig (1)
               \\servername -accepteula ipconfig (2)         

(1) Gives Error 2250 (2) gives Error 5
Run Code Online (Sandbox Code Playgroud)

相同的命令和代码适用于控制台应用程序.所以我相信它肯定与Asp.net应用程序有关,该应用程序无法将凭证转移到远程机器.我的事件尝试startInfo.LoadUserProfile但无济于事.

感谢您的帮助.我试图查找类似的问题,但找不到我面临的问题的解决方案.

Mit*_*tch 2

考虑退出psexec该流程。直接访问 WMI 可能会更深入地了解问题所在:

var connOpts = new ConnectionOptions()
{
// Optional, default is to use current identity
    Username = "username",
    Password = "password"
};

// create a handle to the Win32_Process object on the remote computer
ManagementScope mgmtScope = new ManagementScope(@"\\servername\root\cimv2", connOpts);
ManagementClass w32Process = new ManagementClass(mgmtScope, 
    new ManagementPath("Win32_Process"), new ObjectGetOptions());

// create the process itself
object[] createArgs = new object[] {
    // [in]   string CommandLine,
    "notepad.exe", 
    // [in]   string CurrentDirectory,
    null, 
    // [in]   Win32_ProcessStartup ProcessStartupInformation,
    null, 
    // [out]  uint32 ProcessId
    0};

var result = (int)w32Process.InvokeMethod("Create", createArgs);

switch (result)
{
    case 0: /* no-op, successful start */ break;
    case 2: throw new Exception("Access Denied");
    case 3: throw new Exception("Insufficient Privilege");
    case 8: throw new Exception("Unknown failure");
    case 9: throw new Exception("Path not found");
    case 21: throw new InvalidOperationException("Invalid Parameter");
}
Run Code Online (Sandbox Code Playgroud)

如果您仍然遇到模拟问题,转储内容HttpContext.Current.User.Identity以验证 IIS 是否配置正确可能会有所帮助。此外,如果您使用 Kerberos(通过 Negotiate/SPNEGO),您可能需要允许计算机委托身份。如果您知道计算机将连接到的 SPN,则可以使用约束委派,但在许多情况下,如果提前未知目标,则有必要允许非约束委派。


注意:如果您远程连接到计算机只是为了运行 ipconfig,则可以通过 WMI 获取相同的信息,而不必费力尝试将输出返回STDOUT到调用计算机。看一下Win32_NetworkAdapterConfiguration类。