Windows privilege escalation via PowerShell History

Michael Koczwara
3 min readMar 14, 2021

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:

%userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

i.e

C:\Users\student\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history.txt

Checking PowerShell History.

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.

obtained creds:

administrator: alita_123321

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.

Meterpreter/C2 channel.

Shell access.

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.

The 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 ConsoleHost_history.txt file.

Adversaries may also delete the ConsoleHost_history.txt file or edit its contents to hide the PowerShell commands they have run.

--

--