Clear Command History
Adversaries clear command history to conceal actions taken during an intrusion. On Windows, PowerShell maintains two history stores: the in-session history (cleared by Clear-History) and the PSReadLine history file at %APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt. Attackers may delete or truncate this file or use Remove-Item (Get-PSReadlineOption).HistorySavePath — the method used by Medusa Group ransomware. On Linux/macOS, history -c clears the in-memory history, HISTFILE= unsets the history file, and rm ~/.bash_history deletes the persistent record. Setting HISTSIZE=0 or HISTFILESIZE=0 prevents future history recording. TeamTNT, Aquatic Panda, APT41, Kobalos, and APT5 (ESXi) have all been observed clearing command history as post-exploitation cleanup.
DeviceProcessEvents
| where Timestamp > ago(24h)
| where (
// PowerShell history file deletion or manipulation
(FileName in~ ("powershell.exe", "pwsh.exe")
and ProcessCommandLine has_any ("ConsoleHost_history", "PSReadLine", "Get-PSReadlineOption", "Clear-History", "Remove-Item"))
or
// Linux/macOS bash history clearing via shell
(FileName in~ ("bash", "sh", "zsh", "fish")
and ProcessCommandLine has_any ("history -c", "history -w", "HISTFILE=", "HISTSIZE=0", "HISTFILESIZE=0"))
or
// Direct deletion of history files
(ProcessCommandLine has_any ("bash_history", ".zsh_history", ".ash_history", ".fish_history")
and ProcessCommandLine has_any ("rm ", "del ", "Remove-Item", "unlink"))
)
| project Timestamp, DeviceName, AccountName, FileName, ProcessCommandLine,
InitiatingProcessFileName, InitiatingProcessCommandLine
| sort by Timestamp desc
| union (
DeviceFileEvents
| where Timestamp > ago(24h)
| where ActionType in ("FileDeleted", "FileModified")
| where FolderPath has_any ("PSReadLine", "ConsoleHost_history")
or FileName in~ (".bash_history", ".zsh_history", ".ash_history")
| project Timestamp, DeviceName, InitiatingProcessAccountName,
FileName, FolderPath, ActionType, InitiatingProcessFileName
)
| sort by Timestamp desc Data Sources
Required Tables
False Positives
- Administrators running Clear-History during interactive PowerShell sessions for legitimate housekeeping
- Shell profile scripts that set HISTSIZE=0 for service accounts that should not record history
- Backup or rotation scripts that delete and recreate .bash_history files
- Security tools that sanitize history files after removing credentials accidentally typed at the command line
References (5)
- https://attack.mitre.org/techniques/T1070/003/
- https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_history
- https://community.sophos.com/sophos-labs/b/blog/posts/powershell-command-history-forensics
- https://knowledge.broadcom.com/external/article/321910/auditing-esxi-shell-logins-and-commands.html
- https://github.com/redcanaryco/atomic-red-team/blob/master/atomics/T1070.003/T1070.003.md
Unlock Pro Content
Get the full detection package for T1070.003 including response playbook, investigation guide, and atomic red team tests.