Query Details

Rule : Web Executable File Written by Server and Database Process

Persistence Webshell File Drop

Query

let serverParents=dynamic(["w3wp.exe","java.exe","javaw.exe","tomcat.exe","httpd.exe","apache2","nginx","php-fpm","mysqld.exe","mysqld","sqlservr.exe"]);
DeviceFileEvents
| where Timestamp > ago(7d)
| where ActionType in ("FileCreated","FileModified","FileRenamed")
| where FileName matches regex @"(?i)\.(jsp|jspx|aspx|ashx|php)$"
| where set_has_element(serverParents, tolower(InitiatingProcessFileName)) or FolderPath has_any ("wwwroot","webapps","htdocs","inetpub","public_html")
| project Timestamp, DeviceId, DeviceName, FileName, FolderPath, ActionType, InitiatingProcessFileName, InitiatingProcessCommandLine, InitiatingProcessAccountName
| order by Timestamp desc

About this query

Explanation

This query is designed to detect potentially malicious activities where certain types of web-executable files (like .jsp, .aspx, and .php) are created, modified, or renamed by web server or database processes, or are placed in web root directories. This behavior can indicate a webshell drop, which is a method attackers use to gain persistent access to a server.

Here's a simplified breakdown of the query:

  1. Purpose: To identify suspicious file activities that might indicate a webshell being placed on a server.
  2. Targeted Files: The query looks for files with extensions .jsp, .jspx, .aspx, .ashx, and .php.
  3. Monitored Actions: It focuses on file creation, modification, and renaming events, ignoring deletions.
  4. Processes of Interest: The query checks if these file actions are initiated by common web or database server processes like w3wp, java, tomcat, httpd, nginx, php-fpm, mysqld, and sqlservr.
  5. Locations of Interest: It also checks if the files are placed in typical web root directories such as wwwroot, webapps, htdocs, inetpub, and public_html.
  6. Data Source: The query uses the DeviceFileEvents table to gather relevant data.
  7. Output: It lists details like the timestamp, device ID, file name, folder path, action type, and the process that initiated the action.
  8. False Positives: The query advises tuning to reduce false positives, such as excluding legitimate deployment tools and focusing on server processes as writers.
  9. Triage Steps: It suggests reviewing the file content, checking for any child processes spawned by the web server, and correlating with web server logs to confirm suspicious activity.

Overall, this query is a proactive measure to detect and investigate potential security threats related to unauthorized webshell deployments on servers.