SQL注入式攻击是利用是指利用设计上的漏洞,在目标服务器上运行Sql命令以及进行其他方式的攻击动态生成Sql命令时没有对用户输入的数据进行验证是Sql注入攻击得逞的主要原因。

比如:
如果你的查询语句是
1 select * from admin where username="&user&and password="&pwd&""
2 
 那么,如果我的用户名是:1 or 1=1
 那么,你的查询语句将会变成:
1 select * from admin where username=1 or 1=1 and password="&pwd&""
2 
这样你的查询语句就通过了,从而就可以进入你的管理界面。

所以防范的时候需要对用户的输入进行检查。特别式一些特殊字符,比如单引号,双引号,分号,逗号,冒号,连接号等进行转换或者过滤。

需要过滤的特殊字符及字符串有:

【转载】防范SQL注入式攻击net user
【转载】防范SQL注入式攻击   xp_cmdshell
【转载】防范SQL注入式攻击   /add
【转载】防范SQL注入式攻击   exec master.dbo.xp_cmdshell
【转载】防范SQL注入式攻击   net localgroup administrators
【转载】防范SQL注入式攻击   select
【转载】防范SQL注入式攻击   count
【转载】防范SQL注入式攻击   Asc
【转载】防范SQL注入式攻击   char
【转载】防范SQL注入式攻击   mid
【转载】防范SQL注入式攻击   
【转载】防范SQL注入式攻击   :
【转载】防范SQL注入式攻击   
"
【转载】防范SQL注入式攻击   insert
【转载】防范SQL注入式攻击   delete from
【转载】防范SQL注入式攻击   drop table
【转载】防范SQL注入式攻击   update
【转载】防范SQL注入式攻击   truncate
【转载】防范SQL注入式攻击   from
【转载】防范SQL注入式攻击   %
【转载】防范SQL注入式攻击

下面关于解决注入式攻击的防范代码,供大家学习参考!

js版的防范SQL注入式攻击代码:

【转载】防范SQL注入式攻击<script language="javascript">
【转载】防范SQL注入式攻击
<!--
【转载】防范SQL注入式攻击 
var url = location.search;
【转载】防范SQL注入式攻击 
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;
【转载】防范SQL注入式攻击 var e = re.test(url);
【转载】防范SQL注入式攻击 if(e) {
【转载】防范SQL注入式攻击  alert(
"地址中含有非法字符~");
【转载】防范SQL注入式攻击  location.href=
"error.asp";
【转载】防范SQL注入式攻击 }
【转载】防范SQL注入式攻击//-->
【转载】防范SQL注入式攻击<script> 
【转载】防范SQL注入式攻击
【转载】防范SQL注入式攻击

 1 bool CheckParams(params object[] args)
 2 {
 3     string[] Lawlesses={"=",""};
 4     if(Lawlesses==null||Lawlesses.Length<=0)return true;
 5     //构造正则表达式,例:Lawlesses是=号和号,则正则表达式为 .*[=}].*  (正则表达式相关内容请见MSDN)
 6     //另外,由于我是想做通用而且容易修改的函数,所以多了一步由字符数组到正则表达式,实际使用中,直接写正则表达式亦可;
 7 
 8 
 9     string str_Regex=".*[";
10     for(int i=0;i< Lawlesses.Length-1;i++)
11         str_Regex+=Lawlesses[i]+"|";
12     str_Regex+=Lawlesses[Lawlesses.Length-1]+"].*";
13     //
14     foreach(object arg in args)
15     {
16         if(arg is string)//如果是字符串,直接检查
17         {
18             if(Regex.Matches(arg.ToString(),str_Regex).Count>0)
19             return false;
20         }
21         else if(arg is ICollection)//如果是一个集合,则检查集合内元素是否字符串,是字符串,就进行检查
22         {
23             foreach(object obj in (ICollection)arg)
24             {
25                 if(obj is string)
26                 {
27                     if(Regex.Matches(obj.ToString(),str_Regex).Count>0)
28                     return false;
29                 }
30             }
31         }
32     }
33     return true;
34 
35 

相关文章:

  • 2021-09-07
  • 2021-11-25
  • 2021-07-15
  • 2022-02-03
  • 2021-05-25
  • 2021-12-07
  • 2021-06-28
  • 2022-12-23
猜你喜欢
  • 2022-02-13
  • 2021-07-07
  • 2021-12-13
  • 2022-02-17
  • 2021-06-07
  • 2021-04-01
相关资源
相似解决方案