Detecting Webshell
Detecting Webshell
IR
Web shell
https://hellodigi.ir/profile/razipoor
razipoorabolfazl@yahoo.com
Abolfazl razipour
Below is a simple example of a Web Shell intrusion process:
Web Site
Attacker
Server
Backdoor
رضی پورrazipour
Abolfazl ابوالفضل
رضی پور
Abolfazl ابوالفضل
razipour
The most famous webshell used
30%
C99 WebShell
25%
China Chopper WebShell
20%
JFolder WebShell
15%
Tux WebShell
10%
B374K WebShell / RootShell Family
hell o digi
Apache web server log
Apache HTTP Server, commonly referred to simply as Apache, is an
open source web server developed by the Apache Software
Foundation.
Apache was one of the first web servers that were provided for web
services, and with its help, web sites can be hosted on the Internet.
"Mozilla/4.08 [en] (Win98; I ;Nav) :This field is related to browser and system information
Abolfazl razipour
The Webshell log on the Apache web server
Apache log example
Access to files with suspicious names These example logs
192.168.1.102 - - [12/May/2023:15:45:32 +0000] "GET /c99.php HTTP/1.1" 200 4523 "http://hellodigi.ir/" "Mozilla/5.0 (Windows NT 10.0; Win64; may indicate
x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/53.3"
suspicious activity
Use of questionable functions in request parameters related to webshells.
However, to confirm
192.168.1.105 - - [12/May/2023:16:21:15 +0000] "GET /index.php?eval=base64_decode('c29tZSBjb2RlIGhlcmU=') HTTP/1.1" 200 1287
"http://hellodigi.ir/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/ (KHTML, like Gecko) Chrome" the reality of these
cases, more
Access to files with suspicious extensions
research and a more
192.168.1.108 - - [12/May/2023:17:54:21 +0000] "GET /uploads/r57.jsp HTTP/1.1" 200 2516 "http://hellodigi.ir/" "Mozilla/5.0 (Windows NT 10.0; detailed
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
investigation is
Execute system commands in a PHP file needed, which we
will do together
192.168.1.108 - - [12/May/2023:17:54:21 +0000] "GET /uploads/r57.jsp HTTP/1.1" 200 2516 "http://hellodigi.ir/" "Mozilla/5.0 (Windows NT 10.0;
Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
187 : This field indicates the number of bytes sent to the client in response. This value does not include HTTP
.headers. In this example, 187 bytes are sent Abolfazl razipour
The webshell log on the IIS web server
Sample IIS log
Access to files with suspicious names These example logs
may indicate
2023-05-08 19:35:46 192.168.1.101 GET /cmd.jsp - 80 - 198.51.100.2 suspicious activity
Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/58.0.3029.110+Safari/537.36 200 0 0 15 related to webshells.
However, to confirm
the reality of these
Access to files with suspicious extensions
cases, more research
2023-05-10 10:16:45 192.168.0.2 POST /uploads/shell.asp - 443 - 10.0.0.1 Mozilla/5.0 - 200 0 0 2345
and a more detailed
investigation is
needed, which we
Execute system commands
will do together
2023-05-10 10:17:30 192.168.0.2 POST /uploads/shell.asp cmd=dir 443 - 10.0.0.1 Mozilla/5.0 - 200 0 0 3456
Abolfazl razipour
Webshell detection in Linux
There are four key steps to detecting webshells on Linux:
Using the find command to find files with suspicious names: detect webshells on
find /var/www -type f \( -iname "*.php" -o -iname "*.jsp" -o -iname "*.asp" -o -iname "*.aspx" \) -exec grep -l -E 'c99|r57|wso' {} \; Linux systems, you
Using the grep command to search for suspicious functions in files:
can use various
grep -r -E --include='*.php' 'eval|shell_exec|system|passthru|exec|popen' /var/www available tools and
commands. Here are
Using the Clam AV tool to scan files:
sudo clamscan -r -i /var/www
some methods and
commands you can
Using the awk command to check the number of suspicious functions in the files:
find /var/www -type f -iname "*.php" -exec awk '/eval|shell_exec|system|passthru|exec|popen/ { count++ } END { if(count > 0) { print FILENAME ": " count } }' {} \;
use to find webshells
on Linux:
Using the find command to find very small files:
This command looks for PHP files smaller than 2000 bytes in the /var/www directory. Many web shells are small in size to load quickly.
find /var/www -type f -size -2000c -iname "*.php“
Abolfazl razipour
Webshell detection in Windows
There are several key steps to detect webshells in Windows:
1. Check files: Check system files for signs of webshells, such as files with
suspicious extensions or files with unusual PHP, ASP, JSP, or PERL code.
2. Check the logs: The logs of the IIS web server or any other web server
used can contain information about web shell attacks. Therefore, they
must be examined and analyzed.
3. Using specialized tools: Tools like PowerShell to check files and logs, or
specialized tools like IDS/IPS... and forensic tools can help detect
webshells.
Abolfazl razipour
Detect in Splunk Webshell
To detect webshells using Splunk, the following key steps can be
followed:
1. Data collection: To detect webshell, you first need to send web server logs to
Splunk. These logs may contain data from Apache, IIS, or any other web server.
2. Data analysis: Using Splunk's analytical capabilities, you can review the
collected data. You can use SPL (Splunk Processing Language) queries to find
suspicious patterns in the data.
3. Use rules and alarms: You can set up rules and alarms in Splunk to alert you
when it detects suspicious patterns or webshell-specific symbols.
4. Using Splunk ES or UBA For more advanced detection, you can use Splunk
security products such as Splunk Enterprise Security (ES) or Splunk User
Behavior Analytics (UBA). These tools recognize more complex patterns and
increase the possibility of detecting webshells.
Webshell detection in Splunk
One of the queries you can use in Splunk to identify webshells and suspicious activity in the Apache sensor is as follows:
To detect web
index=your_webserver_index sourcetype=access_combined shells, you can use
| eval suspicious_extensions = "php|asp|aspx|pl|cgi" the Splunk tool and
| rex field=uri_path ".*\.(?P<file_extension>(?i)php|asp|aspx|pl|cgi)" query because to
| search file_extension!="" http_status>=400 create an alert or
| stats count by src_ip, uri_path, http_status, file_extension create a
| sort - count dashboard:
index=your_webserver_index sourcetype=access_combined: This section searches for web server logs of the type
"access_combined" returns at the specified index. Replace "your_webserver_index" with your appropriate index name.
eval suspicious_extensions: This section introduces a variable to store suspicious extensions.
rex field=uri_path: This field creates a regex vector to extract the extension of the requested files.
search file_extension!="" http_status>=400: This field only selects records with suspicious extensions and HTTP status
codes higher than 400.stats count by src_ip, uri_path, http_status, file_extension: It calculates requests based on source
IP address, URI path, HTTP status code, and file extension.
sort - count: This section sorts the results by count in descending order.
.With this query, you can identify requests with suspicious extensions and HTTP status codes higher than 400.
Abolfazl razipour
Webshell detection in Splunk
Here are some more queries in Splunk that you can use to identify suspicious activity and various attacks:
Repeated failed login attempts: To detect web
index=your_webserver_index sourcetype=access_combined shells, you can
| search http_status=401 use the Splunk
| stats count by src_ip tool and query
| where count > THRESHOLD because to
| sort - count create an alert
Multiple requests to non-existent files (404): or create a
index=your_webserver_index sourcetype=access_combined dashboard:
| search http_status=404
| stats count by uri_path
| where count > THRESHOLD
| sort - count
Find suspicious activity by browser:
index=your_webserver_index sourcetype=access_combined
| rex field=user_agent "(?i)(?P<suspicious_user_agent>curl|wget|nmap|python|perl|nikto)"
| search suspicious_user_agent!=""
| stats count by src_ip, uri_path, http_status, suspicious_user_agent
| sort - count
Abolfazl razipour
Webshell detection in Splunk
Here are some more queries in Splunk that you can use to identify suspicious activity and various attacks:
Access to files with suspicious extensions To detect web
shells, you can
index=your_webserver_index sourcetype=access_combined use the Splunk
| rex field=uri_path "\.(?P<suspicious_extension>php[345]?|jsp|jspx|asp|aspx|cgi|pl|sh)$" tool and query
| stats count by src_ip, uri_path, http_status, suspicious_extension
because to
| sort - count
create an alert
Identifying suspicious activities using specific keywords: or create a
index=your_webserver_index sourcetype=access_combined dashboard:
| rex field=uri_query"(?i)(?P<suspicious_keywords>(c99|c100|r57|wso|shell_exec|passthru|eval|assert|base64_decode|str_rot13|gzinflate))(\W|$)"
| search suspicious_keywords!=""
| stats count by src_ip, uri_path, http_status, suspicious_keywords
| sort - count
Detection of access to files with suspicious names
index=your_webserver_index sourcetype=access_combined
| rex field=uri_path "(?i)(?P<suspicious_filenames>(c99|r57|wso)\.(php[345]?|jsp|jspx|asp|aspx|cgi|pl|sh))$"
| search suspicious_filenames!=""
| stats count by src_ip, uri_path, http_status, suspicious_filenames
| sort - count
Abolfazl razipour
Webshell detection in Splunk
Here are some more queries in Splunk that you can use to identify suspicious activity and various attacks:
Detect Directory Traversal Attempts:
To detect web
index=your_webserver_index sourcetype=access_combined shells, you can
| rex field=uri_path "(?i)(?P<dir_traversal>\.\./|\.\.\\)" use the Splunk
| search dir_traversal!="" tool and query
| stats count by src_ip, uri_path, http_status because to
| sort - count create an alert
Identifying Brute Force Attempts: or create a
index=your_webserver_index sourcetype=access_combined dashboard:
| eval time=strftime(_time, "%Y-%m-%d %H:%M:%S")
| transaction src_ip maxspan=10m maxpause=2s
| where eventcount > THRESHOLD
| table time, src_ip, uri_path, http_status, eventcount
| sort - eventcount
Abolfazl razipour
Webshell detection in Splunk
Here are some more queries in Splunk that you can use to identify suspicious activity and various attacks:
To detect web
HTTP requests with unusual paths: shells, you can
use the Splunk
index=iis_logs cs_uri_stem=*cmd* OR cs_uri_stem=*shell* OR cs_uri_stem=*upload* OR cs_uri_stem=*exec* tool and query
OR cs_uri_stem=*admin* because to
| table _time, c_ip, cs_uri_stem, cs(User_Agent), sc_status
create an alert
Searching for high volume POST requests: or create a
dashboard:
index=iis_logs method=POST
| stats sum(sc_bytes) as total_bytes by cs_uri_stem
| where total_bytes > SOME_THRESHOLD
Abolfazl razipour
2.تشخیص فعالیتهای مشکوک با استفاده از کلمات کلیدی مشخص:
Abolfazl razipour
2.تشخیص فعالیتهای مشکوک با استفاده از کلمات کلیدی مشخص:
Abolfazl razipour
ELK detection in Webshell
To use ELK (Elasticsearch, Logstash, Kibana) to detect Webshell attacks, you must first
send log information from your web server (whether from Apache, IIS, or another web
server) to Elasticsearch. This can be done using Logstash or Beats.
After sending data to Elasticsearch, you can analyze this data using Kibana. To find signs
of a Webshell attack, you can use the following methods:
1. Checking requests to files with suspicious extensions (eg .php, .asp, .aspx, .jsp and ...).
2. Check queries that have suspicious keywords such as cmd.exe, bash, sh, python, etc.
3. Check requests that go to files with suspicious names such as shell, hack, exploit, etc.
4. Investigate a large number of 404 (not found) requests, which could indicate an
attempt to find vulnerable files.
5. Checking User-Agents that have suspicious names such as python, perl, curl, wget, etc.
Webshell detection in ELK
Here are some more queries in ELK that you can use to identify suspicious activity and various attacks:
Identifying suspicious activities using specific keywords and patterns: To detect web
GET filebeat-*/_search shells, you can use
{ the ELK tool and
"query": {
query because to
"regexp": {
"request": create an alert or
".*((cmd|passthru|eval|exec|assert|create_function|include|require)(_once)?|system|popen|show_source|phpinfo|shell_exe create a
c|base64_decode|gzinflate|chmod|mkdir|fopen|fclose|readfile)\\(.*\\).*" dashboard:
}
}
}
Check for attempts to manipulate files (such as changing access levels, creating new files, opening and closing files):
GET filebeat-*/_search
{
"query": {
"regexp": {
"request": ".*((chmod|mkdir|fopen|fclose)\\(.*\\)).*"
}
}
}
Abolfazl razipour
Webshell detection in ELK
Here are some more queries in ELK that you can use to identify suspicious activity and various attacks:
Review of strange commands used in Webshell attacks:
To detect web
GET filebeat-*/_search shells, you can use
{
the ELK tool and
"query": {
"regexp": { query because to
"request": ".*((wget|curl)\\s.*\\s-o\\s.*|php\\s.*\\s-r\\s.*).*" create an alert or
} create a
}
} dashboard:
GET filebeat-*/_search
{
"query": {
"regexp": {
"request": ".*(&\\s.*|;\\s.*).*"
}
}
}
Abolfazl razipour
Snort detection in Webshell
Snort is an open source IDS intrusion detection system that can be used
to detect web shell attacks. This tool can help detect malicious
activities by analyzing network traffic and detecting suspicious patterns.
You can use different rules to detect web shell attacks with Snort. These
rules are designed depending on the specific patterns used in web shell
attacks. Below is an example of these rules:
Webshell detection in Snort
You can use
Detection of access to files with suspicious names: different rules to
detect web shell
alert tcp $EXTERNAL_NET any -> $HTTP_SERVERS $HTTP_PORTS (msg:"WEB-MISC access to potential webshell"; flow:to_server,established;
content:".php"; http_uri; nocase; pcre:"/\/(shell|upload|admin|c99|r57|wso)\.php/Ui"; classtype:web-application-attack; sid:1000002; rev:1;) attacks with Snort.
These rules
Detect suspicious command line commands in HTTP request parameters:
depend on the
specific patterns
alert tcp $EXTERNAL_NET any -> $HTTP_SERVERS $HTTP_PORTS (msg:"WEB-MISC potential webshell command execution"; flow:to_server,established; used in webshell
content:"cmd="; http_uri; nocase; pcre:"/cmd=(id|uname|ls|cat|whoami|wget|curl|nc|netcat|ping|ifconfig|ipconfig)/Ui"; classtype:web-application-attack;
sid:1000003; rev:1;)
attacks
Abolfazl razipour
Suricata detection in Webshell
Abolfazl razipour
Wireshark detection in Webshell
To detect suspicious activity based on the browser, we can use the http.user_agent
field in Wireshark. The following filter filters out HTTP requests whose User-Agent
matches a specified value. For example, if we want to see all requests whose User-
Agent is "Mozilla/5.0", we can use the following filter:
Abolfazl razipour
Modsecurity in Webshell detection
For example, specifically for webshell detection, one of the rules we can use in ModSecurity is as To detect web
follows: shell attacks with
modsecurity, you
SecRule REQUEST_FILENAME "@endsWith .php" \ can use different
"id:1000000,phase:1,t:none,log,deny,msg:'PHP file requested'" rules. These rules
SecRule ARGS_POST|ARGS_GET "@rx (eval\(|base64_decode|gzinflate|str_rot13|convert_uudecode)\s*\(.*\)" \
"id:1000001,phase:2,t:none,log,deny,msg:'Potential web shell activity'" depend on the
specific patterns
used in webshell
The first rule checks if the requested file name ends with .php. If so, it logs an event and attacks
rejects the request.
The second rule checks if any of the POST or GET parameters match patterns that can
indicate webshell activity, including the eval, base64_decode, gzinflate, str_rot13, and
convert_uudecode functions. If so, it logs an event and rejects the request.
Abolfazl razipour
THANK YOU
Abolfazl razipour