r/PowerShell • u/PinkDraconian • 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=16319
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
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 yTrying 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).PasswordPoint 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.
→ More replies (1)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
→ More replies (2)
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
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
4
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
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
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
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
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.