Clearing Windows Console History

Identifies when a user attempts to clear console history. An adversary may clear the command history of a compromised account to conceal the actions undertaken during an intrusion.

Elastic rule (View on GitHub)

  1[metadata]
  2creation_date = "2021/11/22"
  3integration = ["endpoint", "windows", "system"]
  4maturity = "production"
  5min_stack_comments = "New fields added: required_fields, related_integrations, setup"
  6min_stack_version = "8.3.0"
  7updated_date = "2024/03/28"
  8
  9[rule]
 10author = ["Austin Songer"]
 11description = """
 12Identifies when a user attempts to clear console history. An adversary may clear the command history of a compromised
 13account to conceal the actions undertaken during an intrusion.
 14"""
 15from = "now-9m"
 16index = ["winlogbeat-*", "logs-endpoint.events.process-*", "logs-windows.*", "endgame-*",  "logs-system.security*"]
 17language = "eql"
 18license = "Elastic License v2"
 19name = "Clearing Windows Console History"
 20note = """## Triage and analysis
 21
 22### Investigating Clearing Windows Console History
 23
 24PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks. This makes it available for use in various environments, and creates an attractive way for attackers to execute code.
 25
 26Attackers can try to cover their tracks by clearing PowerShell console history. PowerShell has two different ways of logging commands: the built-in history and the command history managed by the PSReadLine module. This rule looks for the execution of commands that can clear the built-in PowerShell logs or delete the `ConsoleHost_history.txt` file.
 27
 28#### Possible investigation steps
 29
 30- Investigate the process execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
 31- Identify the user account that performed the action and whether it should perform this kind of action.
 32- Contact the account owner and confirm whether they are aware of this activity.
 33- Investigate other alerts associated with the user/host during the past 48 hours.
 34  - Verify if any other anti-forensics behaviors were observed.
 35- Investigate the PowerShell logs on the SIEM to determine if there was suspicious behavior that an attacker may be trying to cover up.
 36
 37### False positive analysis
 38
 39- This activity is unlikely to happen legitimately. Benign true positives (B-TPs) can be added as exceptions if necessary.
 40
 41### Response and remediation
 42
 43- Initiate the incident response process based on the outcome of the triage.
 44- Isolate the involved host to prevent further post-compromise behavior.
 45- Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
 46- Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
 47- Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
 48- Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
 49  - Ensure that PowerShell auditing policies and log collection are in place to grant future visibility.
 50"""
 51references = [
 52    "https://stefanos.cloud/kb/how-to-clear-the-powershell-command-history/",
 53    "https://www.shellhacks.com/clear-history-powershell/",
 54    "https://community.sophos.com/sophos-labs/b/blog/posts/powershell-command-history-forensics",
 55]
 56risk_score = 47
 57rule_id = "b5877334-677f-4fb9-86d5-a9721274223b"
 58setup = """## Setup
 59
 60If enabling an EQL rule on a non-elastic-agent index (such as beats) for versions <8.2,
 61events will not define `event.ingested` and default fallback for EQL rules was not added until version 8.2.
 62Hence for this rule to work effectively, users will need to add a custom ingest pipeline to populate
 63`event.ingested` to @timestamp.
 64For more details on adding a custom ingest pipeline refer - https://www.elastic.co/guide/en/fleet/current/data-streams-pipeline-tutorial.html
 65"""
 66severity = "medium"
 67tags = [
 68    "Domain: Endpoint",
 69    "OS: Windows",
 70    "Use Case: Threat Detection",
 71    "Tactic: Defense Evasion",
 72    "Tactic: Execution",
 73    "Resources: Investigation Guide",
 74    "Data Source: Elastic Endgame",
 75    "Data Source: Elastic Defend"
 76]
 77timestamp_override = "event.ingested"
 78type = "eql"
 79
 80query = '''
 81process where host.os.type == "windows" and event.type == "start" and
 82  (process.name : ("powershell.exe", "pwsh.exe", "powershell_ise.exe") or ?process.pe.original_file_name == "PowerShell.EXE") and
 83     (process.args : "*Clear-History*" or
 84     (process.args : ("*Remove-Item*", "rm") and process.args : ("*ConsoleHost_history.txt*", "*(Get-PSReadlineOption).HistorySavePath*")) or
 85     (process.args : "*Set-PSReadlineOption*" and process.args : "*SaveNothing*"))
 86'''
 87
 88
 89[[rule.threat]]
 90framework = "MITRE ATT&CK"
 91[[rule.threat.technique]]
 92id = "T1070"
 93name = "Indicator Removal"
 94reference = "https://attack.mitre.org/techniques/T1070/"
 95[[rule.threat.technique.subtechnique]]
 96id = "T1070.003"
 97name = "Clear Command History"
 98reference = "https://attack.mitre.org/techniques/T1070/003/"
 99
100
101
102[rule.threat.tactic]
103id = "TA0005"
104name = "Defense Evasion"
105reference = "https://attack.mitre.org/tactics/TA0005/"
106
107
108[[rule.threat]]
109framework = "MITRE ATT&CK"
110[[rule.threat.technique]]
111id = "T1059"
112name = "Command and Scripting Interpreter"
113reference = "https://attack.mitre.org/techniques/T1059/"
114[[rule.threat.technique.subtechnique]]
115id = "T1059.001"
116name = "PowerShell"
117reference = "https://attack.mitre.org/techniques/T1059/001/"
118
119
120
121[rule.threat.tactic]
122id = "TA0002"
123name = "Execution"
124reference = "https://attack.mitre.org/tactics/TA0002/"

Triage and analysis

Investigating Clearing Windows Console History

PowerShell is one of the main tools system administrators use for automation, report routines, and other tasks. This makes it available for use in various environments, and creates an attractive way for attackers to execute code.

Attackers can try to cover their tracks by clearing PowerShell console history. PowerShell has two different ways of logging commands: the built-in history and the command history managed by the PSReadLine module. This rule looks for the execution of commands that can clear the built-in PowerShell logs or delete the ConsoleHost_history.txt file.

Possible investigation steps

  • Investigate the process execution chain (parent process tree) for unknown processes. Examine their executable files for prevalence, whether they are located in expected locations, and if they are signed with valid digital signatures.
  • Identify the user account that performed the action and whether it should perform this kind of action.
  • Contact the account owner and confirm whether they are aware of this activity.
  • Investigate other alerts associated with the user/host during the past 48 hours.
    • Verify if any other anti-forensics behaviors were observed.
  • Investigate the PowerShell logs on the SIEM to determine if there was suspicious behavior that an attacker may be trying to cover up.

False positive analysis

  • This activity is unlikely to happen legitimately. Benign true positives (B-TPs) can be added as exceptions if necessary.

Response and remediation

  • Initiate the incident response process based on the outcome of the triage.
  • Isolate the involved host to prevent further post-compromise behavior.
  • Investigate credential exposure on systems compromised or used by the attacker to ensure all compromised accounts are identified. Reset passwords for these accounts and other potentially compromised credentials, such as email, business systems, and web services.
  • Run a full antimalware scan. This may reveal additional artifacts left in the system, persistence mechanisms, and malware components.
  • Determine the initial vector abused by the attacker and take action to prevent reinfection through the same vector.
  • Using the incident response data, update logging and audit policies to improve the mean time to detect (MTTD) and the mean time to respond (MTTR).
    • Ensure that PowerShell auditing policies and log collection are in place to grant future visibility.

References

Related rules

to-top