Rule : Multiple Distinct Scripted Command Variations in a Short Window
Execution Script Retry Variations
Query
DeviceProcessEvents
| where Timestamp > ago(24h)
| where FileName in~ ("powershell.exe","pwsh.exe","python.exe","python3","cmd.exe","bash","sh")
| where strlen(ProcessCommandLine) > 50
| summarize Attempts=count(), Variants=dcount(ProcessCommandLine), Samples=make_set(substring(ProcessCommandLine,0,200),10), FirstSeen=min(Timestamp), LastSeen=max(Timestamp) by DeviceId, DeviceName, AccountName, FileName, bin(Timestamp,2m)
| where Attempts >= 10 and Variants >= 8
| order by Variants descAbout this query
Explanation
This query is designed to detect suspicious activity on a device where a script or command-line interpreter (like PowerShell, Python, or Bash) is used to run many slightly different long commands in a short period. This pattern can indicate an automated process, such as a script or malware, trying to execute commands repeatedly with slight variations, which is unusual for human behavior.
Here's a simplified breakdown of the query:
-
Purpose: To identify when a single interpreter on a device runs many different long command lines within a 2-minute window. This could suggest automated retries, possibly due to command failures.
-
How it Works:
- The query looks at events from the
DeviceProcessEventstable. - It focuses on specific interpreters like PowerShell, Python, and Bash.
- It filters for command lines longer than 50 characters.
- It counts the number of command attempts and the number of distinct command variations within a 2-minute window for each device/account/interpreter.
- It triggers an alert if there are 10 or more attempts with at least 8 different command variations.
- The query looks at events from the
-
Use Case: This is useful for detecting potential malicious activity where an attacker might be trying to execute a command repeatedly with slight modifications, possibly to bypass security measures.
-
Considerations:
- The rule is initially set to a medium severity level and should be fine-tuned before being used for alerting.
- It might generate false positives in environments where developers frequently retry commands during software development or deployment processes.
- The query is meant to be used as a hunting tool to identify patterns of retries and variations, not to detect specific errors or AI activity.
-
Next Steps:
- If an alert is triggered, investigate the command samples to see if they appear to be iterative fixes or adjustments.
- Check the parent process to understand what is driving the interpreter's activity.
Overall, this query helps in identifying unusual patterns of command execution that could indicate automated or malicious behavior.