๐Ÿง  Why This Matters

Modern attackers don't guess โ€” they scan, fingerprint, and exploit known weaknesses. A default or misconfigured Apache Tomcat server can be compromised in minutes using:

  • Public CVEs
  • Automated scanners
  • Exploit frameworks

๐Ÿ‘‰ below article shows how to move from:

"Exposed Tomcat instance" โ†’ "Hardened, resilient production server"

๐Ÿงฑ Architecture Overview

None
None
None

๐Ÿ” Components

  • Client / Attacker Machine
  • Nmap / Nikto โ†’ Reconnaissance
  • Firewall (UFW / iptables) โ†’ Access control
  • Reverse Proxy (Recommended: Caddy/Nginx) โ†’ TLS + filtering
  • Apache Tomcat (Ubuntu 22.04) โ†’ Application server

โš™๏ธ Step 1: Secure Tomcat Setup (Baseline)

Install Tomcat

sudo apt update
sudo apt install tomcat9 -y

๐Ÿ”ฅ Immediately Remove Default Attack Surface

sudo rm -rf /var/lib/tomcat9/webapps/examples
sudo rm -rf /var/lib/tomcat9/webapps/docs
sudo rm -rf /var/lib/tomcat9/webapps/ROOT
sudo rm -rf /var/lib/tomcat9/webapps/manager
sudo rm -rf /var/lib/tomcat9/webapps/host-manager

๐Ÿ‘‰ These apps are frequently exploited for RCE.

Deploy Minimal Test App

sudo mkdir -p /var/lib/tomcat9/webapps/ROOT
echo "<h1>Secure Tomcat Running</h1>" | sudo tee /var/lib/tomcat9/webapps/ROOT/index.html

Start Service

sudo systemctl enable tomcat9
sudo systemctl start tomcat9

๐Ÿ” Step 2: Reconnaissance (Attacker View)

None
None

Run Scan

nmap -sV -O -Pn <server-ip>

What Attackers See

  • Open Port: 8080
  • Service: Apache Tomcat
  • Version Info: Visible
  • OS Fingerprint: Linux

โš ๏ธ Risk

๐Ÿ‘‰ Version exposure allows:

  • Direct CVE mapping
  • Automated exploit usage

๐Ÿ’ฃ Step 3: Exploit Mapping

searchsploit tomcat

Real-World Attack Vectors

  • ๐Ÿ”ฅ WAR file upload โ†’ Remote Code Execution
  • ๐Ÿ“‚ Path traversal โ†’ Read sensitive files
  • ๐Ÿ’ฅ DoS โ†’ Crash server
  • ๐Ÿง  Deserialization attacks

โš”๏ธ How Attackers Actually Break Tomcat

None
None
None
  1. Scan โ†’ Identify open ports
  2. Fingerprint โ†’ Detect Tomcat version
  3. Map โ†’ Find CVEs
  4. Exploit โ†’ Upload payload / trigger bug

๐Ÿ›ก๏ธ Step 4: Foolproof Tomcat Hardening

โœ… 1. Keep Tomcat Updated

sudo apt update && sudo apt upgrade -y

โœ” Patch frequently โœ” Track CVEs

๐Ÿ” 2. Enforce Least Privilege

sudo chown -R tomcat:tomcat /var/lib/tomcat9
sudo chmod -R 750 /var/lib/tomcat9

๐Ÿ”’ 3. Disable Auto Deployment (CRITICAL)

Prevents WAR-based attacks:

Edit

/etc/tomcat9/server.xml
<Host autoDeploy="false" deployOnStartup="false">

๐Ÿ” 4. Enable HTTPS (Correct Tomcat Way)

None

Configure SSL Connector

<Connector port="8443"
           protocol="org.apache.coyote.http11.Http11NioProtocol"
           SSLEnabled="true">
    <SSLHostConfig>
        <Certificate certificateKeystoreFile="conf/keystore.jks"
                     certificateKeystorePassword="changeit" />
    </SSLHostConfig>
</Connector>

Force HTTPS (Tomcat Equivalent of RewriteRule)

<security-constraint>
    <web-resource-collection>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

Link HTTP โ†’ HTTPS

<Connector port="8080" redirectPort="8443" /

๐Ÿงฑ 5. Hide Server Identity

<Connector port="8080" server="SecureServer" />

โœ” Removes version leaks

๐Ÿง  6. Add Security Headers (Tomcat Filter)

<filter>
  <filter-name>securityHeadersFilter</filter-name>
  <filter-class>org.apache.catalina.filters.HttpHeaderSecurityFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>securityHeadersFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

๐Ÿšง 7. Firewall + Rate Limiting

sudo ufw allow 22
sudo ufw allow 443
sudo ufw deny 8080
sudo ufw enable

๐Ÿ‘‰ Never expose 8080 publicly in production.

๐Ÿ”ฅ 8. Reverse Proxy (BEST PRACTICE)

None
None
None

Why?

  • Handles HTTPS
  • Hides backend ports
  • Enables rate limiting

Example (Caddy)

yourdomain.com {
    reverse_proxy localhost:8080
}

Fix Tomcat Behind Proxy

<Connector port="8080"
           proxyName="yourdomain.com"
           proxyPort="443"
           scheme="https"
           secure="true" />

๐Ÿ“Š 9. Monitoring & Detection

Logs:

  • /var/log/tomcat9/catalina.out

Tools:

  • Nmap
  • Nikto
  • OpenVAS

๐Ÿ”ฅ Final Secure Architecture

Client (HTTPS)
   โ†“
Reverse Proxy (Caddy/Nginx)
   โ†“
Tomcat (Internal only, 8080)
   โ†“
Web Application

๐Ÿ“Œ Key Takeaways

โœ” Remove default apps โœ” Never expose port 8080 publicly โœ” Always use HTTPS โœ” Disable auto-deploy โœ” Hide server version โœ” Use reverse proxy โœ” Monitor continuously

๐Ÿง  Final Thought

"A default Tomcat server is a target. A hardened Tomcat server is a fortress."

Security is not a one-time setup โ€” it's a continuous lifecycle of:

  • Scanning
  • Fixing
  • Monitoring