Query Details

Rule : MySQL General-Log Poisoning via Process-Visible SQL Strings

Persistence My SQL JSP Log Poisoning

Query

DeviceProcessEvents
| where Timestamp > ago(7d)
| where ProcessCommandLine contains "general_log_file" or ProcessCommandLine contains "SET GLOBAL general_log" or (ProcessCommandLine contains "SELECT" and ProcessCommandLine contains "<%")
| project Timestamp, DeviceId, DeviceName, AccountName, FileName, ProcessCommandLine, InitiatingProcessFileName
| order by Timestamp desc

About this query

Explanation

This query is designed to detect potentially malicious activity involving MySQL databases. Specifically, it looks for SQL commands that might be used to redirect MySQL logs to a file or to insert JSP (JavaServer Pages) tags into the database, which could indicate an attempt to create a webshell—a type of malware that allows remote control over a web server.

Here's a simple breakdown of the query:

  1. Purpose: The query aims to identify suspicious SQL commands that could be used to compromise a server by altering MySQL logs or inserting harmful code.

  2. What it Looks For:

    • SQL commands that change where MySQL logs are stored (general_log_file or SET GLOBAL general_log).
    • SQL SELECT statements that include JSP tags (<%), which might be used to inject malicious code.
  3. Data Source: It searches through the DeviceProcessEvents table, which contains information about processes running on devices.

  4. Time Frame: The query examines events from the past 7 days.

  5. Output: It lists details such as the time of the event, device information, user account, file names, and the command line that was executed, sorted by the most recent events.

  6. False Positives: It acknowledges that legitimate database administrators (DBAs) might trigger these alerts during troubleshooting, so these cases should be excluded from alerts.

  7. Investigation Steps:

    • Check if the log file was redirected to a web-accessible location, which could indicate an attack.
    • Look for any resulting .jsp files that might have been created as part of the attack.
  8. Context: This detection method is based on documented behavior from a threat group known as ARTEX, which uses these techniques to maintain persistence on compromised systems.

Overall, this query helps security teams identify and investigate potential security incidents involving MySQL log manipulation and webshell deployment.