0

Is it possible to impersonate using Powershell script. Like C# straight forward functions

   dwSessionId = WTSGetActiveConsoleSessionId()
    WTSQueryUserToken(dwSessionId, ref hUserToken)

are there any functions in powershell doing same tasks as above.

I want to get token of current user who is "logged in and have desktop interactive session running".

Eduard Uta
  • 2,477
  • 5
  • 26
  • 36
Kaustubh_Kharche
  • 725
  • 3
  • 13
  • 34

2 Answers2

1

Seems like its not possible out of the box.

The Windows API will not expose the information you need, which is why Powershell can't get to them. Its an intentional feature of the security subsystem. The only way for this to work is for the Linux machines to trust the calling machine, such as joining them to an Active Directory (or any kerberos setup really).

Aside from that, you'd need to store and pass this information somehow.

You could store the RSA key in the user's keystore and extract it at runtime (using the .NET Crypto/Keystore libs), so you aren't storing the key around with the code. That way the key itself would be protected by the OS and available only when the calling user was authenticated. You'd have one more thing to install, but may be the only way to achieve what you are aiming for.

-Quoted from here

Using this method + the qwinsta command to get the active sessions will somehow get you the needed information you want.

Community
  • 1
  • 1
Totem
  • 1,030
  • 7
  • 16
0

This is of course possible via pinvoke and embedded C# code. But the main hurdle is the fact, that you can call WTSQueryUserToken only as "local system". You could grab the process-token from the "SamSs" service for instance to make this call work.

Carsten
  • 1,612
  • 14
  • 21