sql注入漏洞

來源:互聯網
上載者:User

標籤:

資料庫驗證使用者的功能

//處理介面package com.xidian;import javax.servlet.http.*;import java.io.*;import java.sql.*;public class Logincl extends HttpServlet {public void doGet(HttpServletRequest req,HttpServletResponse res){Connection ct=null;Statement sm=null;ResultSet rs=null;try{   //接收使用者名稱和密碼    String u=req.getParameter("username");    String p=req.getParameter("password");   //串連資料庫   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");   //得到串連   ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","dba");   //建立Statement   sm=ct.createStatement();   //sql注入漏洞   rs=sm.executeQuery("select top 1* from users where username= ‘"+u+"‘and passwd=‘"+p+"‘");  //驗證  if(rs.next()){  //合法  //將驗證成功的資訊,寫入session  //1.得到session  HttpSession hs=req.getSession(true);  //修改session的存在時間  hs.setMaxInactiveInterval(10);   //2.向session添加屬性  hs.setAttribute("pass","ok");  res.sendRedirect("Welcome?uname="+u+"&upass="+p);}else{//不合法res.sendRedirect("login");//要到的servlet的那個url  }
}
catch(Exception ex){ex.printStackTrace();}finally{try {if(rs!=null){rs.close(); }if(rs!=null){sm.close(); }if(rs!=null){ct.close(); }}catch (Exception ex) {ex.printStackTrace(); } } }public void doPost(HttpServletRequest req,HttpServletResponse res){this.doGet(req,res); }}

如果在sqlserver輸入命令:select * from users where username=‘任一字元‘ and passwd=‘任一字元‘ or 1=‘1‘
都能尋找出結果,因為or 1=‘1‘語句使得條件都成立。
如果使用者在輸入密碼:任一字元‘ or 1=‘1 都能通過驗證。

修改後代碼:

rs=sm.executeQuery("select top 1 passwd from users where username= ‘"+u+"‘");if(rs.next()){//說明使用者名稱是存在String dbPasswd=rs.getString(1);if(dbPasswd.equals(p)){//使用者合法HttpSession hs=req.getSession(true);//修改session的存在時間hs.setMaxInactiveInterval(10);//2.向session添加屬性hs.setAttribute("pass","ok");res.sendRedirect("Welcome?uname="+u+"&upass="+p);  }}

不要在sm.executeQuery()中同時查詢使用者名稱和密碼來驗證以帶來注入風險,應該通過輸入的使用者名稱在資料庫中尋找密碼,
然後將資料庫中對應的使用者密碼與使用者輸入的密碼進行比對。

 

sql注入漏洞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.