Detect Potential Cleartext Credentials in Command Line

KQL Queries

KQL (Kusto Query Language) query to identify potential cleartext credentials in command lines, leveraging Microsoft Defender for Endpoint or other platforms like Azure Monitor Logs:

DeviceProcessEvents
| where Timestamp > ago(7d)  // Adjust the time frame as needed
| where ProcessCommandLine has_any ("password", "pwd", "pass", "secret", "key", "credential", "login")
| extend SuspiciousWords = extract_all(@"(?i)(password\s*[:=]\s*\S+|pwd\s*[:=]\s*\S+|pass\s*[:=]\s*\S+|secret\s*[:=]\s*\S+|key\s*[:=]\s*\S+|credential\s*[:=]\s*\S+|login\s*[:=]\s*\S+)", ProcessCommandLine)
| where array_length(SuspiciousWords) > 0
| project Timestamp, DeviceName, AccountName, FileName, FolderPath, ProcessCommandLine, SuspiciousWords
| extend AccountDomain = tostring(split(AccountName, "\\", 0)), Username = tostring(split(AccountName, "\\", 1))
| summarize Count = count(), Commands = make_set(ProcessCommandLine) by Timestamp, DeviceName, AccountName, FileName, FolderPath, ProcessCommandLine
| order by Count desc

Key Features of the Query:

  1. Filters Suspicious Command Lines:

    • Targets command lines with keywords commonly associated with credentials like password, pwd, secret, etc.

  2. Extracts Potential Credentials:

    • Uses regex to extract possible key-value pairs (e.g., password=1234).

  3. Aggregation for Context:

    • Groups occurrences by DeviceName, AccountDomain, and Username to provide context.

  4. Summarization and Ordering:

    • Highlights accounts and devices with the highest occurrences of potential issues.

How It Works:

  • Extract_all Function: This regex extracts any matching patterns from the command line that indicate potential cleartext credentials.

  • Dynamic Analysis: Produces a dynamic array of potential matches, ensuring flexibility in parsing varying formats.

  • Adjustable Time Frame: Allows tuning for recent or historical analysis.

Use Case Scenarios:

  • Detect accidental or intentional exposure of credentials in scripts or commands.

  • Investigate potential misuse by attackers or internal personnel.

Last updated