JS和C#分別防注入代碼

來源:互聯網
上載者:User

如果你的查詢語句是select * from admin where username=''''"&user&"'''' and password=''''"&pwd&"''''"
那麼,如果我的使用者名稱是:1'''' or ''''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注入式攻擊的兩段代碼~:
[CODE START]  
<script language="javascript">
<!--
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|\"|:|net%20user|\''''|%20or%20)(.*)$/gi;
var e = re.test(url);
if(e) {
alert("地址中含有非法字元~");
location-href="error.asp";
}
//-->
<script>
登陸和密碼輸入判斷
//防止非法字串注入
function checkuseravoid(str){
var inj_str="‘|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,";
var sarray=new Array();
sarray=inj_str.split('|');
for (var i=0 ;i <inj_stra.length ;i++ ) {
if (str.indexOf(inj_stra)>=0)
return true;
}
return false;
}
[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") or Instr(strTemp,"count(") or Instr(strTemp,"drop%20table") or Instr(strTemp,"update%20") or Instr(strTemp,"truncate%20") or Instr(strTemp,"asc(") or Instr(strTemp,"mid(") or Instr(strTemp,"char(") or Instr(strTemp,"xp_cmdshell") or Instr(strTemp,"exec%20master") or Instr(strTemp,"net%20localgroup%20administrators") or Instr(strTemp,":") or Instr(strTemp,"net%20user") or Instr(strTemp,"''''") or Instr(strTemp,"%20or%20") then
Response.Write "<script language=''''javascript''''>"
Response.Write "alert(''''非法地址!!'''');"
Response.Write "location-href=''''error.asp'''';"
Response.Write "<script>"
End If
%>
[CODE END]
C# 檢查字串,防SQL注入攻擊
這個例子裡暫訂為=號和''''號
bool CheckParams(params object[] args)
{
string[] Lawlesses={"=","''''"};
if(Lawlesses==null||Lawlesses.Length<=0)return true;
//構造Regex,例:Lawlesses是=號和''''號,則Regex為 .*[=}''''].* (Regex相關內容請見MSDN)
//另外,由於我是想做通用而且容易修改的函數,所以多了一步由字元數組到Regex,實際使用中,直接寫Regex亦可;
string str_Regex=".*[";
for(int i=0;i< Lawlesses.Length-1;i++)
str_Regex+=Lawlesses+"|";
str_Regex+=Lawlesses[Lawlesses.Length-1]+"].*";
//
foreach(object arg in args)
{
if(arg is string)//如果是字串,直接檢查
{
if(Regex.Matches(arg.ToString(),str_Regex).Count>0)
return false;
}
else if(arg is ICollection)//如果是一個集合,則檢查集合內元素是否字串,是字串,就進行檢查
{
foreach(object obj in (ICollection)arg)
{
if(obj is string)
{
if(Regex.Matches(obj.ToString(),str_Regex).Count>0)
return false;
}
}
}
}
return true;
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.