SQL注入式攻擊是利用是指利用設計上的漏洞,在目標伺服器上執行Sql指令以及進行其他方式的攻擊動態產生Sql指令時沒有對使用者輸入的資料進行驗證是Sql注入攻擊得逞的主要原因。
比如:
如果你的查詢語句是select * from admin where username=''"&user&"'' and password=''"&pwd&"''"
那麼,如果我的用戶名是:1'' 或 ''1''=''1
那麼,你的查詢語句將會變成:
select * from admin where username=''1 or ''1''=''1'' and password=''"&pwd&"''"
這樣你的查詢語句就通過了,這樣你就可以進入你的管理介面。
所以防範的時候需要對使用者的輸入進行檢查。特別式一些特殊字符,例如單引號,雙引號,分號,逗號,冒號,連接號等進行轉換或過濾。
需要過濾的特殊字元及字串有:
net user
xp_cmdshell
/add
exec master.dbo.xp_cmdshell
net localgroup administrators
select
count
Asc
char
mid
''
:
"
insert
delete from
drop table
update
truncate
from
%
下面是我寫的兩種關於解決注入式攻擊的防範程式碼,供大家學習參考!
js版的防範SQL注入式攻擊程式碼:
<script language="****">
<!--
var url = location.search;
var re=/^?(.*)(select%20|insert%20|delete%20from%20|count(|drop%20table|update%20truncate%20|asc(|mid(|char (|xp_cmdshell|exec%20master|net%20localgroup%20administrators|"|../../image/bbs3000/whatchutalkingabout_smile.gifnet%20user|''|%20or%20)(.*)$/gi;
var e = re.test(url);
if(e) {
alert("地址中含有非法字元~");
location.href="error.asp";
}
//-->
<script>
[CODE END]
asp版的防範SQL注入式攻擊程式碼~:
[CODE START]
<%
On Error Resume Next
Dim strTemp
If LCase(Request.ServerVariables("HTTPS")) = "off" Then
strTemp = "http://"
Else
strTemp = "https://"
End If
strTemp = strTemp & Request.ServerVariables("SERVER_NAME")
If Request.ServerVariables("SERVER_PORT") <> 80 Then strTemp = strTemp & ":" & Request.ServerVariables("SERVER_PORT")
strTemp = strTemp & Request.ServerVariables("URL")
If Trim(Request.QueryString) < "" Then strTemp = strTemp & "?" & Trim(Request.QueryString)
strTemp = LCase(strTemp)
If Instr(strTemp,"select%20") or Instr(strTemp,"insert%20") or Instr(strTemp, "delete%20from") 或 Instr(strTemp,"count(") 或 Instr(strTemp,"drop%20table") 或 Instr(strTemp,"update%20") 或 Instr(strTemp,"truncate%20") 或Instr(strTemp,"asc(") 或 Instr(strTemp,"mid(") 或 Instr(strTemp,"char(") 或 Instr(strTemp,"xp_cmdshell") 或 Instr(strTemp,"exec%20master") 或Instr(strTemp,"net%20localgroup%20administrators") 或 Instr(strTemp,":") or Instr(strTemp,"net%20user") or Instr(strTemp,"''") 或 Instr(strTemp,"%20or %20") then
Response.Write "<script language=''****''>"
Response.Write "alert(''非法位址!!'');"
Response.Write "location.href=''error.asp'';"
Response.Write "<script>"
End If
%>
以下是較簡單的防範方法,這些都是大家比較熟悉的方法,我就是轉帖過來。希望能給你一點幫助~
主要是針對數字型的變數傳遞:
id = Request.QueryString("id")
If Not(isNumeric(id)) Then
Response.Write "非法位址~"
Response.End
End If