# 8009 - Pentesting Apache JServ Protocol (AJP)

<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 的其他方式：

* 如果您想看到您的**公司在 HackTricks 中做广告**或**下载 PDF 版本的 HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方 PEASS & HackTricks 商品**](https://peass.creator-spring.com)
* 探索[**PEASS 家族**](https://opensea.io/collection/the-peass-family)，我们独家的[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品
* **加入** 💬 [**Discord 群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或在 **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)\*\* 上关注\*\*我们。
* 通过向 [**HackTricks**](https://github.com/carlospolop/hacktricks) 和 [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github 仓库提交 PR 来分享您的黑客技巧。

</details>

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

加入 [**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy) 服务器，与经验丰富的黑客和赏金猎人交流！

**黑客见解**\
参与深入探讨黑客的刺激和挑战的内容

**实时黑客新闻**\
通过实时新闻和见解及时了解快节奏的黑客世界

**最新公告**\
随时了解最新的赏金任务发布和重要平台更新

**加入我们的** [**Discord**](https://discord.com/invite/N3FrSbmwdy)，立即与顶尖黑客合作！

## 基本信息

来源：<https://diablohorn.com/2011/10/19/8009-the-forgotten-tomcat-port/>

> AJP 是一种线路协议。它是 HTTP 协议的优化版本，允许独立的 Web 服务器（如 [Apache](http://httpd.apache.org/)）与 Tomcat 进行通信。从历史上看，Apache 在提供静态内容方面比 Tomcat 快得多。其想法是让 Apache 在可能的情况下提供静态内容，但将请求代理到 Tomcat 处理与 Tomcat 相关的内容。

还有有趣的信息：

> ajp13 协议是基于数据包的。出于性能考虑，二进制格式显然优于更易读的纯文本。Web 服务器通过 TCP 连接与 Servlet 容器通信。为了减少昂贵的套接字创建过程，Web 服务器将尝试维护持久的 TCP 连接到 Servlet 容器，并重用连接进行多个请求/响应周期

**默认端口：** 8009

```
PORT     STATE SERVICE
8009/tcp open  ajp13
```

## CVE-2020-1938 ['Ghostcat'](https://www.chaitin.cn/en/ghostcat)

如果AJP端口暴露，Tomcat可能会受到Ghostcat漏洞的影响。这里有一个[exploit](https://www.exploit-db.com/exploits/48143)可用于利用此问题。

Ghostcat是一个LFI漏洞，但受到一定限制：只能提取特定路径下的文件。尽管如此，这可能包括像`WEB-INF/web.xml`这样的文件，根据服务器设置，这些文件可能泄露像Tomcat接口凭据这样的重要信息。

修补版本为9.0.31或更高版本，8.5.51和7.0.100已解决了此问题。

## 枚举

### 自动化

```bash
nmap -sV --script ajp-auth,ajp-headers,ajp-methods,ajp-request -n -p 8009 <IP>
```

### [**暴力破解**](/generic-methodologies-and-resources/brute-force.md#ajp)

## AJP 代理

### Nginx 反向代理 & AJP

[查看 Docker 化版本](#Dockerized-version)

当我们遇到一个开放的 AJP 代理端口（8009 TCP）时，我们可以使用 Nginx 与 `ajp_module` 来访问“隐藏”的 Tomcat 管理器。这可以通过编译 Nginx 源代码并添加所需的模块来实现，具体步骤如下：

* 下载 Nginx 源代码
* 下载所需的模块
* 使用 `ajp_module` 编译 Nginx 源代码。
* 创建一个指向 AJP 端口的配置文件

```bash
# Download Nginx code
wget https://nginx.org/download/nginx-1.21.3.tar.gz
tar -xzvf nginx-1.21.3.tar.gz

# Compile Nginx source code with the ajp module
git clone https://github.com/dvershinin/nginx_ajp_module.git
cd nginx-1.21.3
sudo apt install libpcre3-dev
./configure --add-module=`pwd`/../nginx_ajp_module --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules
make
sudo make install
nginx -V
```

在`/etc/nginx/conf/nginx.conf`文件中的`http`块内，将整个`server`块注释掉，并添加以下行：

```shell-session
upstream tomcats {
server <TARGET_SERVER>:8009;
keepalive 10;
}
server {
listen 80;
location / {
ajp_keep_conn on;
ajp_pass tomcats;
}
}
```

启动Nginx，并通过向本地主机发出cURL请求来检查一切是否正常工作。

```html
sudo nginx
curl http://127.0.0.1:80

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Apache Tomcat/X.X.XX</title>
<link href="favicon.ico" rel="icon" type="image/x-icon" />
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="tomcat.css" rel="stylesheet" type="text/css" />
</headas
<body>
<div id="wrapper">
<div id="navigation" class="curved container">
<span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span>
<span id="nav-hosts"><a href="/docs/">Documentation</a></span>
<span id="nav-config"><a href="/docs/config/">Configuration</a></span>
<span id="nav-examples"><a href="/examples/">Examples</a></span>
<span id="nav-wiki"><a href="https://wiki.apache.org/tomcat/FrontPage">Wiki</a></span>
<span id="nav-lists"><a href="https://tomcat.apache.org/lists.html">Mailing Lists</a></span>
<span id="nav-help"><a href="https://tomcat.apache.org/findhelp.html">Find Help</a></span>
<br class="separator" />
</div>
<div id="asf-box">
<h1>Apache Tomcat/X.X.XX</h1>
</div>
<div id="upper" class="curved container">
<div id="congrats" class="curved container">
<h2>If you're seeing this, you've successfully installed Tomcat. Congratulations!</h2>
<SNIP>
```

### Nginx Docker化版本

```bash
git clone https://github.com/ScribblerCoder/nginx-ajp-docker
cd nginx-ajp-docker
```

将`nginx.conf`中的`TARGET-IP`替换为AJP IP，然后构建并运行。

```bash
docker build . -t nginx-ajp-proxy
docker run -it --rm -p 80:80 nginx-ajp-proxy
```

### Apache AJP 代理

发现只有端口8009是开放的而没有其他可访问的web端口是罕见的。然而，仍然可以利用**Metasploit**来利用它。通过利用**Apache**作为代理，请求可以被重定向到端口8009上的**Tomcat**。

```bash
sudo apt-get install libapache2-mod-jk
sudo vim /etc/apache2/apache2.conf # append the following line to the config
Include ajp.conf
sudo vim /etc/apache2/ajp.conf     # create the following file, change HOST to the target address
ProxyRequests Off
<Proxy *>
Order deny,allow
Deny from all
Allow from localhost
</Proxy>
ProxyPass       / ajp://HOST:8009/
ProxyPassReverse    / ajp://HOST:8009/
sudo a2enmod proxy_http
sudo a2enmod proxy_ajp
sudo systemctl restart apache2
```

这种设置具有绕过入侵检测和防范系统（IDS/IPS）的潜力，因为**AJP协议的二进制特性**，尽管这种能力尚未经过验证。通过将常规的Metasploit Tomcat漏洞利用指向`127.0.0.1:80`，您可以有效地控制目标系统。

```bash
msf  exploit(tomcat_mgr_deploy) > show options
```

## 参考资料

* <https://github.com/yaoweibin/nginx_ajp_module>
* <https://academy.hackthebox.com/module/145/section/1295>

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

加入[**HackenProof Discord**](https://discord.com/invite/N3FrSbmwdy)服务器，与经验丰富的黑客和赏金猎人交流！

**黑客见解**\
参与深入探讨黑客的刺激和挑战的内容

**实时黑客新闻**\
通过实时新闻和见解及时了解快节奏的黑客世界

**最新公告**\
随时了解最新的赏金任务发布和重要平台更新

**加入我们的** [**Discord**](https://discord.com/invite/N3FrSbmwdy) 并开始与顶尖黑客合作！

<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的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群**](https://discord.gg/hRep4RUj7f) 或 [**电报群**](https://t.me/peass) 或在**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)\*\* 上\*\*关注我们。
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hacktricks.xsx.tw/network-services-pentesting/8009-pentesting-apache-jserv-protocol-ajp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
