2

I'm automating the testing of the installation, detection, and uninstallation of some Windows applications. In order to run most of those installers silently, they must be run as nt authority\system. That is easy enough to accomplish on a local machine by invoking psexec something like this:

psexec -s setup.exe /S

I need to be able to automatically roll back the test target computer to known-good states, so I'm using another computer to orchestrate all this. Ideally I could use PowerShell remoting to start the installer on the target computer. I haven't yet found a way to achieve that.

Attempt 1: psexec from a Remote Session

The most obvious thing to do is to connect to the target computer using remoting and invoke psexec -s. Here's what that looks like:

[target.ad.example.com]: PS C:\Users\un1\Documents> C:\PsTools\PsExec.exe -s whoami
C:\PsTools\PsExec.exe :
    + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com

The problem is, the process just hangs at that point.

Attempt 2: Start-Process with -Verb RunAs

Using the RunAs verb with Start-Process may well run a process elevated, but it doesn't seem to run it as nt authority\system:

whoami-to-file.bat

whoami > out.txt

PowerShell Session

[target.ad.example.com]: PS C:\> Start-Process .\whoami-to-file.bat -Verb RunAs -WorkingDirectory
[target.ad.example.com]: PS C:\> Get-Contents out.txt
example\un1

The process is not started as nt authority\system.

The Question

Is it possible to start a process as nt authority\system over PowerShell remoting? If so, how?

alx9r
  • 3,675
  • 4
  • 26
  • 55
  • 1
    Did you try `C:\PsTools\PsExec.exe -s -accepteula whoami`? – Lieven Keersmaekers Nov 06 '15 at 10:20
  • @LievenKeersmaekers Nice suggestion. I just tried that and it no longer hangs. I'm still seeing an error, but now there's hope. Thanks for the help. – alx9r Nov 06 '15 at 16:39
  • Run procmon on the remote system, execute your command, stop procmon and most likely you'll see something like `whoami not found`in the trace. – Lieven Keersmaekers Nov 06 '15 at 20:31
  • @LievenKeersmaekers `psexec -s` wasn't even getting that far. It couldn't launch its helper service which makes sense because remoting+impersonation probably adds up to a double-hop. See [my answer](https://stackoverflow.com/a/33572899/1404637). – alx9r Nov 06 '15 at 20:48

2 Answers2

2

Note: I'm no expert at Windows security and credentials, so I don't understand the exact security implications of this technique. In my case the only credentials in question are those a temporary test computer, so there isn't much risk. I doubt this technique is a good idea for production.

It's a Double-Hop (I think)

clymb3r's article about CredSSP I think explains why psexec -s fails over PowerShell remoting. I think that PowerShell remoting counts as one hop and invoking psexec -s counts as a second hop. If that's the case we have a manifestation of the double-hop authentication problem.

Use CredSSP

I suppose there are a variety of ways to overcome the double-hop problem. This being a testing scenario, CredSSP seems appropriate (beware the security risk). Here's the proof of concept.

First you have to enable CredSSP on both computers:

PS C:\> Enable-WSManCredSSP Client -DelegateComputer target.ad.example.com
PS C:\> Invoke-Command { Enable-WSManCredSSP Server} -ComputerName target.ad.example.com

Then you can remote to the target using CredSSP:

PS C:\> $credential = Get-Credential example\target-admin
PS C:\> Enter-PSSession target.ad.example.com -Credential $credential -Authentication CredSSP
[target.ad.example.com]: PS C:\> 

And psexec -s works:

[target.ad.example.com]: PS C:\> psexec -s whoami
C:\PsTools\PsExec.exe :
+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
Connecting to local system...Starting PSEXESVC service on local system...Connecting with PsExec service on
target...Starting whoami on target...
whoami exited on target with error code 0.
nt authority\system    
alx9r
  • 3,675
  • 4
  • 26
  • 55
0

https://github.com/mkellerman/Invoke-CommandAs

Made a function to Invoke-Command as SYSTEM, or provided credential, against local/remote computer. Returns PSObjects, handles network interruptions and resolves any Double-Hop issues.

Try it out let me know if this resolves your issues.

Marc Kellerman
  • 466
  • 3
  • 10