r/PowerShell • • Nov 24 '20

When hackers gain powershell access to a machine, they will check the powershell history. Here's a practical example:

https://youtu.be/CndMDvjX8dg?t=1631
232 Upvotes

43 comments sorted by

8

u/Fallingdamage Nov 24 '20

Wait.. people actually use plaintext passwords for interactive accounts in powershell? really?? I always write in a prompt; Read-Host -AsSecureString. I never enter plain text passwords in in a string.

6

u/PinkDraconian Nov 24 '20

You'd be surprised what pentesters find on the daily in big companies! But then again, there's a million ways that a system could be misconfigured or have a mistake that causes security issues, a sysadmin may know all 999 thousand of them but miss one, and it's a pentesters job to find that one they missed!

4

u/Fallingdamage Nov 25 '20

And you know there are 999 thousand issues and the pentesters will find all but that one.

Last time we had a third party do our testing - afterward I sent them a list of list of things they missed.. since I wanted to make sure the report was accurate.

4

u/PinkDraconian Nov 25 '20

This is also true! A pentester should aim to find everything but it's obvious that we aren't perfect either and will miss stuff (Hence why regular checks are needed). Sorry you had a bad experience with those people!

2

u/Fallingdamage Nov 25 '20

👍

In my case, they missing things like SMB1 on server shares.

2

u/PinkDraconian Nov 25 '20

Yea that is really bad. I think you got scammed. Hopefully that didn't stop you (or your company) to keep on doing regular tests (with other, decent people). I always hate it when I have to quit on a client because of time constraints, when I know that I can find more still. I also always let them know in my reports so they are aware

3

u/Fallingdamage Nov 25 '20

Oh, we know they're bad now. We use other companies for our internal testing now. :)

Some companies just rebrand some slick tool, run it on your network to generate reports and sell those reports to you with a tool to give your company a letter grade. The guys I worked with on that pen test... I wondered how they managed at all.

5

u/MobileWriter Nov 24 '20

Personally for me this helped not necessarily with passwords, but for specific keys. When accessing REST API instances, I often use a key in plains-text per their Powershell modules.

2

u/ApricotPenguin Nov 24 '20

But... But... It makes copying and pasting commands so much faster!

9

u/[deleted] Nov 24 '20

Yeah this is a good lesson in how to setup a secure environment for sure

7

u/MyOtherSide1984 Nov 24 '20

As someone completely out of the loop for security, is this something setup at an organizational level or something that should be done personally in my own devices? What steps should be taken for a home network to protect myself?

20

u/PinkDraconian Nov 24 '20

This is something that will most likely also be the case on your devices. Try running ` Get-Content C:\Users\<USERNAME>\AppData\Roaming\Microsoft\Windows\Powershell\PSReadline\ConsoleHost_history.txt` and you'll see your history.

How to protect yourself? Don't put cleartext credentials in your commands, and if you do, clear the history.

Not that this is something where the attacker already needs code execution on a user's account and it's used by attackers to escalate privileges or laterally move to other users.

5

u/bozho Nov 24 '20

PSReadline also allows you to control (via custom handler) how and if a command is stored to history. There are three levels: do not store command to history, store only to memory for the current session, store to history file.

2

u/PinkDraconian Nov 24 '20

Thanks! That's very helpful for me to include in reports to clients as well!

4

u/MyOtherSide1984 Nov 24 '20

Solid, yeh I always use get-credentials and store them as secure strings and such. I'm curious, a script I run in my home network requires a password be passed to a Selenium browser window. I have it setup where I exported a securestring to a txt file and import that for the password, is this not a safe method of passing credentials?

2

u/PinkDraconian Nov 24 '20 edited Nov 25 '20

That might be secure. However, I am no blue teaming expert and merely work as a red teamer. So if I made a mistake here, sorry! (just covering myself)

2

u/MyOtherSide1984 Nov 24 '20

I'd never heard of those terms before! Learned something new :) Red team opinions sound more helpful here when attempting to avoid external attacks.

3

u/not_rholliday Nov 24 '20

We had to do something like this for some automated Exchange Online scripts, it was very annoying it wouldn’t just run under current credentials. But as I recall, secure strings can be reversed by the account that created them. So they would be vulnerable to an attacker that gains access to the account.

1

u/MyOtherSide1984 Nov 24 '20

Yeh that makes sense, but if they have access to the account, there's more issues that I should be concerned with. Ultimately, the password isn't crazy important since it's unique, but I wouldn't use this in production really.

1

u/PinkDraconian Nov 25 '20

Exactly this. You just have to watch out with entering passwords that might give an attacker an escalation of privileges. However the first step is making sure that an attacker can't get into your account. A password for a simple script will most likely not be your worries (Unless you reuse passwords, but let's not start about that)

9

u/[deleted] Nov 24 '20 edited Nov 24 '20

If you export it using ConvertFrom-SecureString it means the output is encrypted for your user and machine, so only your user on that machine can retrieve it with ConvertTo-SecureString again. For entire data structures like credentials you can use Export-Clixml and Import-Clixml to safely and easily rebuild encrypted objects. Both of these two sets of cmdlets use DPAPI for encryption.

Get-Credential -UserName UsrTst -Message test | Export-Clixml cred.entials
Get-Content cred.entials
$c = Import-Clixml cred.entials
Write-Host ([System.Net.NetworkCredential]$c).Password -f y

Trying via WSL2 Ubuntu I could not retrieve the password, I got gibberish.

I'm not sure how that works with remoting, but I think that if someone already has access to your computer, as you, then it's trivial to retrieve the password:

$pw = Read-Host -AsSecureString
#type onetwothree
$pw | ConvertFrom-SecureString > pw.txt

#hacker:
$importedPw = Get-Content pw.txt | ConvertTo-SecureString
[System.Net.NetworkCredential]::new("", $importedPw).Password

Point is really just to avoid having plaintext passwords floating around.

4

u/MobileWriter Nov 24 '20

u/MyOtherSide1984You can also convert secure string to plain text if you wish to pass it through the console with:

[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))

1

u/PMental Nov 24 '20

Why the need to export to file? Why not just pass it directly to Selenium securely?

Or are you using Selenium outside of PowerShell?

2

u/MyOtherSide1984 Nov 24 '20

I'm using Selenium as part of powershell, but I don't know a way to store the password for later user besides just using the secure string. I exported it once and haven't had to go back ever. This is part of a scheduled task script that logs into my Cox account and reports my data usage.

1

u/PMental Nov 24 '20

Ah yes, I misunderstood you, that should work. I tend to use Get-Credentialand exporting the resulting object with Export-CliXml but the end result is similar (apart from storing a username as well).

1

u/shaniftvm Nov 25 '20

My selenium script doesn't work in that way. When I send the password to selenium browser window, it is being sent in the encrypted form. May I know how did u setup this?

1

u/MyOtherSide1984 Nov 25 '20

It's not I don't think. It's converted to plain text as it types it. Here's what I have. Let me know if this is different than expected. Ultimately, the console and script never sees my password, you'd have to interrupt the script and get it to type it elsewhere or copy/paste it in the split second window when the password is there, I think

$User = "username" $File = "C:\temp\Selenium\scriptsencrypted_password1_cox.txt" $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential ` -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

Sorry, on mobile

1

u/shaniftvm Nov 25 '20

It doesn't work for me. When I send $mycredential.password to selenium browser window using sendkeys, it sends the encrypted form of the password. Means, in browser, the typed password is system.security.securestring and not the actual password. Am I missing anything here?

1

u/MyOtherSide1984 Nov 25 '20

Ahh, yeh. So, I don't know why I had to do this, but I couldn't just send a normal PSCredential or the $variable.password. This is what I'm sending to the Selenium password field:

$Firefoxdriver.findelementbyid('okta-signin-password').sendkeys($CoxCredentials.getnetworkcredential().password)}

So you need the object and then utilize the getnetworkcredential().password property to output the plaintext password. Regrettably, this can also be done directly in the console. So this:

$CoxCredentials.getnetworkcredential().password

outputs the password directly into the console for all to read.

Here's my above comment in an easier to read format:

$User = "username"
$Coxpass = "C:\temp\Selenium\scriptsencrypted_password1_cox.txt"
$CoxCredentials=New-Object -TypeName System.Management.Automation.PSCredential `
 -ArgumentList $User, (Get-Content $coxpass | ConvertTo-SecureString)

#do some things
#here's the sendkeys password:

$Firefoxdriver.findelementbyid('okta-signin-password').sendkeys($CoxCredentials.getnetworkcredential().password)}

42

u/crypticedge Nov 24 '20

Yep. Bash history is also looked at on Linux. Both super useful to admins, and super useful to hackers

29

u/apatt0384 Nov 24 '20

Hacker: “These are the worst scripts I’ve ever seen”

5

u/Bits_Not_Bytes Nov 25 '20

A few people in this thread seem to getting the wrong idea from this video.

This is not a reason for why you should not use PS history.
This is a reason why you should not enter in cleartext passwords or embed them in scripts. Use Get-Credential or run things as service accounts.

Disabling PS history because of this will help but keep in mind this is only possible after your account has been compromised. Don't fall into the logic trap that because hackers use powershell features, powershell (or it's features) should be disabled. Protecting your account should be the focus, though having multiple layers of protection is never a had idea though.

On another note it looks like there is discussion about addressing this risk:
https://github.com/PowerShell/PSReadLine/pull/1058
But doesn't seem to work from my testing.

3

u/ypwu Nov 25 '20

Agreed but what are the alternatives? SecureString is getting phased out, its recommend not to use it. I feel like there should be a cross platform SecureString. Only cmdlets where I have to use plain text creds are vmware oscustomization ones. So I just do Read-Host and cast that to a variable instead of using the password in command directly.

2

u/Flasharn Nov 24 '20

I did not know this! Thanks!

4

u/[deleted] Nov 24 '20

Throw this in a task scheduler:

Remove-Item (Get-PSReadlineOption).HistorySavePath

6

u/nostril_spiders Nov 24 '20

Questionable.

If that task stops working, how are you going to know?

It's like setting up automation to lock your front door every half an hour, so you can go out without your keys.

Just set PSReadline to not save history.

But even that is not a decision I could commend. History is valuable. Here are two better options:

  • set a handler in PSReadline to filter history items that look secret

  • don't enter secrets in plaintext at the PS CLI.

-1

u/[deleted] Nov 25 '20

It just erases your history so you don't have to. It's not that complicated.

6

u/nostril_spiders Nov 25 '20

When I go to sleep, I turn the light off. I don't paint the lightbulb black.

Why have history enabled and create a task to delete it?

0

u/[deleted] Nov 25 '20

[deleted]

3

u/nostril_spiders Nov 25 '20

History is incredibly valuable. Machiavelli wrote, "A wise ruler invokes bck-i-search often, for what worked once will work again".

He also wrote "PSReadline 2.1 has right-arrow completion from history", although this is often misattributed to FDR, who used PSReadline to great effect by quickly creating many aeroplanes.

1

u/yukee2018 Nov 24 '20

Very interesting stuff, tnx!

1

u/DigitalBassLV Dec 09 '20

And THAT is why Administrator should NEVER be allowed access to your databases if you can help it.

Jesus he made that look easy. And the tool is on Github. Holy smokes

0

u/RolleRND Nov 24 '20 edited Nov 24 '20

Can you output/ln file to null as in Linux?