Query Details

Webshell Detection

Query

# Possible webshell on the endpoint

#### MITRE ATT&CK Technique(s)

| Technique ID | Title    | Link    |
| ---  | --- | --- |
| T1505.003 | Web Shell | https://attack.mitre.org/techniques/T1505/003 |

#### Description 
Attackers install web shells on servers by taking advantage of security gaps, typically vulnerabilities in web applications, in internet-facing servers. These attackers scan the internet, often using public scanning interfaces like shodan.io, to locate servers to target. They may use previously fixed vulnerabilities that unfortunately remain unpatched in many servers, but they are also known to quickly take advantage of newly disclosed vulnerabilities.

#### Risk
Attackers can run arbitrary code on a server by exploiting a vulnerable web application

#### References
- https://www.microsoft.com/en-us/security/blog/2021/02/11/web-shell-attacks-continue-to-rise/

#### Author
- **Name: Babak Mahmoodizadeh**
- **Github: https://github.com/babakmhz**
- **LinkedIn: https://www.linkedin.com/in/babak-mhz/**

#### Scenario 1
Look for suspicious process that IIS worker process (w3wp.exe), nginx, Apache HTTP server processes (httpd.exe, visualsvnserver.exe), etc. do not typically initiate (e.g., cmd.exe, powershell.exe and /bin/bash)

#### Scenario 2 
Look for suspicious web shell execution, this can identify processes that are associated with remote execution and reconnaissance activity (example: “arp”, “certutil”, “cmd”, “echo”, “ipconfig”, “gpresult”, “hostname”, “net”, “netstat”, “nltest”, “nslookup”, “ping”, “powershell”, “psexec”, “qwinsta”, “route”, “systeminfo”, “tasklist”, “wget”, “whoami”, “wmic”, etc.)


## Defender For Endpoint
```KQL
let webservers = dynamic(["beasvc.exe", "coldfusion.exe", "httpd.exe", "owstimer.exe", "visualsvnserver.exe", "w3wp.exe", "tomcat", "apache2", "nginx"]);
let linuxShells = dynamic(["/bin/bash", "/bin/sh", "python", "python3"]);
let windowsShells = dynamic(["powershell.exe", "powershell_ise.exe", "cmd.exe"]);
let exclusions = dynamic(["csc.exe", "php-cgi.exe", "vbc.exe", "conhost.exe"]);
DeviceProcessEvents
| where (InitiatingProcessParentFileName in~(webservers) or InitiatingProcessCommandLine in~(webservers))
| where (InitiatingProcessFileName in~(windowsShells) or InitiatingProcessCommandLine has_any(linuxShells))
| where FileName !in~ (exclusions)
| extend Reason = iff(InitiatingProcessParentFileName in~ (webservers), "Suspicious web shell execution", "Suspicious webserver process")
| summarize by FileName, DeviceName, Reason, InitiatingProcessParentFileName, InitiatingProcessCommandLine
```
## Sentinel
```KQL
let webservers = dynamic(["beasvc.exe", "coldfusion.exe", "httpd.exe", "owstimer.exe", "visualsvnserver.exe", "w3wp.exe", "tomcat", "apache2", "nginx"]);
let linuxShells = dynamic(["/bin/bash", "/bin/sh", "python", "python3"]);
let windowsShells = dynamic(["powershell.exe", "powershell_ise.exe", "cmd.exe"]);
let exclusions = dynamic(["csc.exe", "php-cgi.exe", "vbc.exe", "conhost.exe"]);
DeviceProcessEvents
| where (InitiatingProcessParentFileName in~(webservers) or InitiatingProcessCommandLine in~(webservers))
| where (InitiatingProcessFileName in~(windowsShells) or InitiatingProcessCommandLine has_any(linuxShells))
| where FileName !in~ (exclusions)
| extend Reason = iff(InitiatingProcessParentFileName in~ (webservers), "Suspicious web shell execution", "Suspicious webserver process")
| summarize by FileName, DeviceName, Reason, InitiatingProcessParentFileName, InitiatingProcessCommandLine
```

Explanation

The query is looking for possible webshells on endpoints. It checks for suspicious processes that are not typically initiated by web servers like IIS, nginx, or Apache HTTP server. It also looks for suspicious web shell execution, identifying processes associated with remote execution and reconnaissance activity. The query filters out certain processes and summarizes the results by file name, device name, reason, initiating process parent file name, and initiating process command line.

Details

Bert-Jan Pals profile picture

Bert-Jan Pals

Released: January 8, 2024

Tables

DeviceProcessEvents

Keywords

Devices,Intune,User,KQL,MITREATT&CK,Technique,WebShell,Endpoint,IIS,nginx,ApacheHTTPserver,cmd.exe,powershell.exe,/bin/bash,arp,certutil,echo,ipconfig,gpresult,hostname,net,netstat,nltest,nslookup,ping,psexec,qwinsta,route,systeminfo,tasklist,wget,whoami,wmic,DefenderForEndpoint,Sentinel

Operators

letdynamicwherein~has_any!in~extendiffsummarizeby

Actions