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 descAbout 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:
- Purpose: To identify suspicious file activities that might indicate a webshell being placed on a server.
- Targeted Files: The query looks for files with extensions
.jsp,.jspx,.aspx,.ashx, and.php. - Monitored Actions: It focuses on file creation, modification, and renaming events, ignoring deletions.
- 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, andsqlservr. - Locations of Interest: It also checks if the files are placed in typical web root directories such as
wwwroot,webapps,htdocs,inetpub, andpublic_html. - Data Source: The query uses the
DeviceFileEventstable to gather relevant data. - Output: It lists details like the timestamp, device ID, file name, folder path, action type, and the process that initiated the action.
- False Positives: The query advises tuning to reduce false positives, such as excluding legitimate deployment tools and focusing on server processes as writers.
- 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.