Windows Privilege escalation via Powershell History
PowerShell.exe terminal stores all the PS commands history in a text file. When an administrator has used hard-coded credentials to perform any operation on the regular user i.e student user environment using PowerShell then, it would become necessary to clean the PowerShell command history. If an administrator forgets to clean up the history, then the admin user has exposed some sensitive information like credentials, configuration settings, etc.
The default location for the PowerShell command history:
Checking PowerShell History.
We can notice, the ConsoleHost_history.txt file contains all the PS executed commands. We could easily go through it line by line or we can run filters using the Select-String cmdlet. In this case, we will be looking at the file manually.
Hunting for credentials.
Logging as administrator
Setting up Metasploit in order to gain remote access.
Setting up hta_server.
“This module hosts an HTML Application (HTA) that when opened will run a payload via Powershell.”
Executing the payload.
Shelling the target.
MITRE ATT&CK Technique: ID: T1070.003
Indicator Removal on Host: Clear Command History (Other sub-techniques of Indicator Removal on Host).
On Windows hosts, PowerShell has two different command history providers: the built-in history and the command history managed by the
PSReadLine module. The built-in history only tracks the commands used in the current session. This command history is not available to other sessions and is deleted when the session ends.
PSReadLine command history tracks the commands used in all PowerShell sessions and writes them to a file (
$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt by default). This history file is available to all sessions and contains all past history since the file is not deleted when the session ends.
Adversaries may run the PowerShell command
Clear-History to flush the entire command history from a current PowerShell session. This, however, will not delete/flush the
Adversaries may also delete the
ConsoleHost_history.txt file or edit its contents to hide the PowerShell commands they have run.