1. 程式人生 > 實用技巧 >SQL Server手注之延時型時間盲注

SQL Server手注之延時型時間盲注

近來處理安全問題遇到的sql注入:

延時函式 WAITFOR DELAY

WAITFOR是SQL Server中Transact-SQL提供的⼀個流程控制語句。它的作⽤就是等待特定時間,然後繼續執⾏後 續的語句。它包含⼀個引數DELAY,⽤來指定等待的時間。

如果將該語句成功注⼊後,會造成資料庫返回記錄和 Web請求也會響應延遲特定的時間。由於該語句不涉及條件判斷等情況,所以容易注⼊成功。根據Web請求是否有延遲,滲透測試⼈員就可以判斷⽹站是否存在注⼊漏洞。同時,由於該語句並不返回特定內容,所以它也是盲注的重要檢測⽅法。

語法:

WAITFOR DELAY '0:0:n'

⽰例:

WAITFOR DELAY '0:0:4' --  表⽰延遲4秒

IF exists ()⼦句

語法:

IF exists () WAITFOR DELAY '0:0:5'

手工延時注入

1.判斷是否存在注⼊

WAITFOR DELAY '0:0:4'

2.猜測資料庫名

猜測資料庫名是否存在

if ((select count(*) from master.dbo.sysdatabases where dbid=5)=1) waitfor delay '0:0:3'--

這條語句的意思呢是判斷dibd=6的資料庫是否存在!如果存在那麼就延遲3秒返回!

根據dbid猜庫名,先猜出長度

if ((select count(*) from master.dbo.sysdatabases where dbid=5 and len(name)=4)=1) waitfor delay '0:0:3'--


為了避免使用者進行這個注入攻擊系統,c#可以採用如下方法將引數過濾即可

public static string ReplaceSQLChar(string str)
{
if (str == String.Empty)
return String.Empty;
str = str.Replace("'", "");
str = str.Replace(";", "");
str = str.Replace(",", "");
str = str.Replace("?", "");
str = str.Replace("<", "");

str = str.Replace(">", "");
str = str.Replace("(", "");
str = str.Replace(")", "");
str = str.Replace("@", "");
str = str.Replace("=", "");
str = str.Replace("+", "");
str = str.Replace("*", "");
str = str.Replace("&", "");
str = str.Replace("#", "");
str = str.Replace("%", "");
str = str.Replace("$", "");

//刪除與資料庫相關的詞
str = Regex.Replace(str, "select", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "insert", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "delete from", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "count", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "drop table", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "asc", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "char", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "xp_cmdshell", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "exec master", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net localgroup administrators", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net user", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "or", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "net", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "-", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "delete", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "drop", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "script", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "update", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "and", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "chr", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "master", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "truncate", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "declare", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "mid", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "waitfor", "", RegexOptions.IgnoreCase);
str = Regex.Replace(str, "delay", "", RegexOptions.IgnoreCase);
return str;
}