r/PowerShell • • Jul 18 '18

Remotely enable PSRemoting when PSRemoting is disabled.

I'm just gonna leave this here:

Invoke-WmiMethod -ComputerName $TargetMachine -Namespace root\cimv2 -Class Win32_Process -Name Create -Credential $cred -Impersonation 3 -EnableAllPrivileges -ArgumentList "powershell Start-Process powershell -Verb runAs -ArgumentList 'Enable-PSRemoting –force'"
168 Upvotes

47 comments sorted by

41

u/neogohan Jul 18 '18

I've also used PSExec for this purpose.

psexec.exe \\computername -h -s powershell.exe Enable-PSRemoting -Force

Sorta the same idea, but a bit easier to remember off the top of your head.

20

u/yorii Jul 18 '18

Yep, my method is extremely convoluted, but PsExec is a downloaded executable, so my long-as-fuck line has at least some uses :)

40

u/get-postanote Jul 18 '18 edited Jul 18 '18

but...but...but..

You don't have to donwload psexec or the other SyInternals tools to use them.

You just use them from SysInternals live.

Sysinternals Live is a service that enables you to execute Sysinternals tools directly from the Web without hunting for and manually downloading them. Simply enter a tool's Sysinternals Live path into Windows Explorer or a command prompt as http://live.sysinternals.com/<toolname> or \live.sysinternals.com\tools<toolname> https://docs.microsoft.com/en-us/sysinternals

You can even add that live site to your path. See this article: https://www.zdnet.com/article/tip-sysinternals-live-on-your-path

Well, you have to be comfortable with this as well, but if you are willing to download and use... well, you know... 8}

Yet, for offline, air-gapped systems, yeppers, use what's on the box..

And oh yea, splatting woudl make this easier to read. 8^}.

4

u/yorii Jul 18 '18

Hey now that's pretty neat! I didn't even know this, thanks :)

3

u/get-postanote Jul 19 '18

No worries.

I look at this in general jsut like Implicit PSRemoting. The cmdlets live on anohter server, but I can create a connection and proxy them to my machine for a given session need.

3

u/beforesunris3 Jul 18 '18

could you please tell me what do I do wrong? https://i.imgur.com/FvdJK9k.png

2

u/get-postanote Jul 19 '18

Something is blocking you from doing this. If you are at an employer wiht outbound restrictions, I get that, but if you are at home; my assumption is that, there are potential environmental issues you need to look at. I am in the US, so if your are not, that might be a thing for you.

0

u/[deleted] Jul 19 '18

[deleted]

2

u/get-postanote Jul 19 '18

Nope, not really.

I've done it with and without drive mapping in cmd.exe and in the PS ConsoleHost / ISE / VSCode.

I can post pics as proof.

8

u/omogai Jul 18 '18

Yes, but does your method fall afoul malware definitions? psexec.exe is constantly flagged by most as malicious remote control software due to the predilection for past intrusions uploading that to remotely target local pcs from a compromised machine.

2

u/get-postanote Jul 19 '18

I am a sec guy, have been for years (Eng / Dev / Mgmt&Ops).

This is not somehting I dreamed up, but something MS direclty allows via a site recorce controlled by them.

Now, all the being said. if you don't trust MS / SysInternals, in there downloaded state, then there is little reason to trust them regardless of origin. If you trust the, as a download , form hte same site wher eyou can run them direclty, well, you know.

And how is the approach MS allows, and as describved in teh article, any different than code you'd get from GitHub, codeplex, sourceforge, powershellgallery.com, scripts posted here, etc, that also are suspect to the same rational.

Now, don't get me wrong. You shoulf not be doing anything, from anyone, regardless of vendor / source, if you have trust issues with the origin / source / repo, etc.

7

u/mannydr3 Jul 18 '18

You're a madman and a gentleman

1

u/RetroGames59 Oct 16 '21 edited Sep 09 '25

truck possessive selective nutty husky work hospital file crown sheet

This post was mass deleted and anonymized with Redact

3

u/zapoklu Jul 19 '18

This kinda stuff can be done through Group Policy as well i'm pretty sure? Maybe a better solution, but can't argue with results :D

3

u/yorii Jul 19 '18

Almost everything can and should be done through Group Policy! But sometimes you just gotta get in, right fucking now.

3

u/albus_fulger Jul 19 '18

I'm looking over this and trying to run it, but it gives me this error:

The string is missing the terminator: '.

Any ideas? It all looks right!

3

u/yorii Jul 19 '18

I think it might be due to me adding the double commands at the end which I haven't actually tested, ; probably breaks the string.

Just replace the final quotation with 'Enable-PSRemoting -Force' and skip the second bit, add it later if needed.

2

u/albus_fulger Jul 19 '18

That did it! Thanks!

3

u/yorii Jul 19 '18

Cool, glad to help :)
I've edited the original post now because apparently the second part there shouldn't even be needed.

4

u/[deleted] Jul 18 '18 edited Jul 18 '18

I have a (possibly dumb) question about the trusted hosts value. In a production environment, would you want to replace the asterisk with a unique hostname? What is the risk, if any, of using the wildcard?

5

u/davesbrown Jul 18 '18

I had a trusted host issue just yesterday and found this site

https://github.com/devops-collective-inc/secrets-of-powershell-remoting/blob/master/manuscript/accessing-remote-computers.md

And way down near the bottom, there is a blurb

Figure 2.29: Adding the remote machine to our TrustedHosts list

This is another case where many blogs will advise just putting "*" in the TrustedHosts list. Really? There's no chance any computer, ever, anywhere, could be impersonated or spoofed? We prefer adding a limited, controlled set of host names or IP addresses. Use a comma-separated list; it's okay to use wildcards along with some other characters (like a domain name, such as *.COMPANY.loc), to allow a wide, but not unlimited, range of hosts. Figure 2.30 shows the successful connection.

Tip: Use the -Concatenate parameter of Set-Item to add your new value to any existing ones, rather than overwriting them.

2

u/[deleted] Jul 18 '18

Thanks! I assumed that "TrustedHosts" was supposed to a fairly limited list, but I wasn't exactly sure how limited it should be.

Thank you for providing some additional context.

1

u/yorii Jul 18 '18

Well, by default it is entirely empty, so that's about how limited it is.

I've personally never set it to a pure wildcard like the example in my original post, but remember that whatever you put there will hold less of a meaning if there is a possibility that another admin could change it later.

And even if you do set it to a wildcard, you can always add another layer of security through firewalling.

2

u/[deleted] Jul 18 '18

Yes, that's exactly what triggered my question actually. I figured this would need to be secured in some fashion, but which method is best in a production environment. I think that having multiple layers of security is a great approach in case one layer is compromised.

2

u/yorii Jul 18 '18 edited Jul 18 '18

Well, if we're counting layers let's just say that port 135 and an admin account is all you require to edit this setting, and port 135 is required to be open against a domain controller from the clients in a functional windows domain network.

Thus the primary and only important security layer to speak of in a majority of windows domain controller machines is securing the administrative rights at all costs.

But adding extra layers of security by for example blocking port 135 against any other server which is not a DC (can be important if you have production servers running applications where a third party also has some form of access), making sure the only admins on a DC are the domain admins and nobody else, and manually listing your administrative ip ranges in the trustedhosts are just some of the things you can do for added security :)

EDIT: I see I listed the admin rights on domain controllers twice, so I might as well mention it a third time, remove anyone who is not a maximum tier superuser from having admin rights on a DC.

2

u/TheIncorrigible1 Jul 19 '18

Never set it to wildcard. You open yourself up to exploits.

2

u/KevMar Community Blogger Jul 19 '18

Also worth pointing out that this is a client setting. You only need to set it on the machine you are using to create the PSSession from.

If all your systems are domain joined, then you also don't need to use it if you use fully qualified names for your pssessions. Trusted hosts come into play when connecting to systems off domain, cross-domain, or via IP address.

13

u/Lee_Dailey [grin] Jul 18 '18

howdy yorii,

that is really pretty neato! [grin] i can't test it with my solitary win7 setup, but it reads like it otta work.

it IS kinda long, tho, so here is a try at making it more readable ...

$ArgList = @(
    "powershell"
    "Start-Process powershell"
    "-Verb runAs"
    "-ArgumentList 'Enable-PSRemoting –force;"
    "Set-Item WSMan:localhost\client\trustedhosts -value *'"
    ) -join ' '

$IWM_Params = @{
    ComputerName = $TargetMachine
    Namespace = 'root\cimv2'
    Class = 'Win32_Process'
    Name = 'Create'
    Credential = $Cred
    # the next value may need to be quoted if it needs to be [string] instead of [int]
    Impersonation = 3
    EnableAllPrivileges = $True
    ArgumentList = $ArgList
    }
Invoke-WmiMethod @IWM_Params

this aint been tested! try it out on a known setup before you use it in production. [grin]

take care,
lee

5

u/get-postanote Jul 18 '18

What he said...

6

u/Lee_Dailey [grin] Jul 18 '18

[grin]

2

u/[deleted] Jul 20 '18

Just tried this - worked beautifully! Thanks Lee.

1

u/Lee_Dailey [grin] Jul 20 '18

howdy TheHappyBusman,

you are most welcome ... and thanks for letting me know that it works! [grin]

take care,
lee

7

u/Ta11ow Jul 18 '18

Pretty sure the `Enable-PSRemoting` command sets the trustedhosts value on its own... handy, though, thank you! :D

5

u/[deleted] Jul 18 '18

I've had it "forget" to do it a time or two. But honestly, we configure remoting with Group Policy. Quick and easy.

0

u/[deleted] Jul 18 '18

[deleted]

2

u/[deleted] Jul 18 '18

Yeah. We limit it to within our subnet, and only admins can remote. It works pretty well.

0

u/[deleted] Jul 18 '18

[deleted]

2

u/[deleted] Jul 18 '18

No we have a security group we add to the administrator group on the workstation. That group has the ability to remote.

1

u/RetroGames59 Oct 16 '21 edited Sep 09 '25

live slap crown towering friendly serious hard-to-find teeny desert spectacular

This post was mass deleted and anonymized with Redact

1

u/yorii Oct 16 '21

Do you have a remote admin account saved to $cred with $cred = get-credentials?

1

u/RetroGames59 Oct 16 '21 edited Sep 09 '25

merciful serious toothbrush reminiscent sheet rob office shelter shy hard-to-find

This post was mass deleted and anonymized with Redact

2

u/yorii Oct 16 '21

There could be firewall restrictions, this relies on RPC using several ports, primarily
135 and a secondary set of dynamic ports at the end of the range (49152-65535 for W7 and W10).

The account saved to $cred also needs to have full admin rights over the remote machine, I've only tried the script within a domain using domain admin credentials.

1

u/[deleted] Oct 16 '21 edited Sep 09 '25

[removed] — view removed comment

2

u/yorii Oct 16 '21

Nothing more should be needed within a domain, but I take it you're trying this without a domain? Then yeah it's probably something missing with regards to UAC.

1

u/RetroGames59 Oct 16 '21 edited Sep 09 '25

snatch grandfather slim depend capable fuel unique march zephyr governor

This post was mass deleted and anonymized with Redact

2

u/[deleted] Jul 19 '18

I have a little hack as well, I stick a .cmd file in shell:startup that invokes powershell from a batch file. From there, I run Enable-PSRemoting -Force and Start-Service WinRM (or whatever it's called).

Sadly, it only works on Win7/S08 machines for me. :(

2

u/KevMar Community Blogger Jul 19 '18

I just mentioned this in another comment, but trusted hosts is a client setting for your local system to use when connecting to a remote system. Super common misconception.

1

u/Deeds4u Jul 18 '18

my god...I'd as your hand in marriage. Yes!

*great find