English
 电子信箱
 加入收藏

  威盾防火墙 >> 新闻中心 >> 威盾新闻 >> 1.5.2 在Global.asax文件里实现通用防SQL注入漏洞程序

 

1.5.2 在Global.asax文件里实现通用防SQL注入漏洞程序

威盾防火墙 2015-01-30

 

1.5.2   在Global.asax文件里实现通用防SQL注入漏洞程序

相信大家都知道,SQL注入是黑客常用的Web攻击方法之一,如何有效地防SQL注入攻击是编写Web程序首先要考虑的。现在就来演示如何在Global.asax文件里实现通用防SQL注入漏洞程序。

首先,需要创建一个SQLInjectionHelper类完成恶意代码的检查,如代码清单1-11所示。

代码清单1-11   SQLInjectionHelper类

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Text.RegularExpressions;  
  5. using System.Web;  
  6. namespace _1_3  
  7. {  
  8.     public class SQLInjectionHelper  
  9.     {  
  10.         /// <summary> 
  11.         /// 获取Post的数据  
  12.         /// </summary> 
  13.         public static bool ValidUrlData(string request)  
  14.         {  
  15.             bool result = false;  
  16.             //获取Post的数据  
  17.             if (request == "POST")  
  18.             {  
  19.                 for (int i = 0; i < HttpContext.
    Current.Request.Form.Count
    ; i++)  
  20.                 {  
  21.                     result = ValidData(HttpContext.
    Current.Request.Form[i].ToString());  
  22.                     if (result)  
  23.                     {  
  24.                         break;  
  25.                     }  
  26.                 }  
  27.             }  
  28.             //获取QueryString中的数据  
  29.             else  
  30.             {  
  31.                 for (int i = 0; i < HttpContext.Current.
    Request.QueryString.Count
    ; i++)  
  32.                 {  
  33.                     result = ValidData(HttpContext.
    Current.Request.QueryString[i].ToString());  
  34.                     if (result)  
  35.                     {  
  36.                         break;  
  37.                     }  
  38.                 }  
  39.             }  
  40.             return result;  
  41.         }  
  42.         /// <summary> 
  43.         /// 验证是否存在注入代码  
  44.         /// </summary> 
  45.         /// <param name="inputData">输入字符</param> 
  46.         private static bool ValidData(string inputData)  
  47.         {  
  48.             //验证inputData是否包含恶意集合  
  49.             if (Regex.IsMatch(inputData, GetRegexString()))  
  50.             {  
  51.                 return true;  
  52.             }  
  53.             else  
  54.             {  
  55.                 return false;  
  56.             }  
  57.         }  
  58.         /// <summary> 
  59.         /// 获取正则表达式  
  60.         /// </summary> 
  61.         private static string GetRegexString()  
  62.         {  
  63.             //构造SQL的注入关键字符  
  64.             string[] strBadChar = {"and"  
  65.             ,"exec"  ,"insert"  ,"select" ,"delete","update"  
  66.             ,"count" ,"from"  ,"drop"    ,"asc"    ,"char"    ,"or"  
  67.             ,"%"    ,";"   ,":"    ,"\'"    ,"\""   ,"-"   ,"chr"  
  68.             ,"mid"   ,"master"    ,"truncate"    ,"char"    ,"declare"  
  69.             ,"SiteName"    ,"net user"    ,"xp_cmdshell"   ,"/add"  
  70.             ,"exec master.dbo.xp_cmdshell"  ,"net 
    localgroup administrators"};  
  71.             //构造正则表达式  
  72.             string str_Regex = ".*(";  
  73.             for (int i = 0; i < strBadChar.Length - 1; i++)  
  74.             {  
  75.                 str_Regex += strBadChar[i] + "|";  
  76.             }  
  77.             str_Regex += strBadChar[strBadChar.Length - 1] + ").*";  
  78.             return str_Regex;  
  79.         }  
  80.     }  

有了这个类之后,就可以使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件来实现表单或URL提交数据的获取,获取之后传给SQLInjectionHelper类public static bool ValidUrlData(string request)方法来完成恶意代码的检查。见代码清单1-12。

代码清单1-12   Global.asax

  1. //在接收到一个应用程序请求时触发。对于一个请求来说,
    它是第一个被触发的事件,请求一般是用户输入的一个页面请求(URL)。  
  2. protected void Application_BeginRequest(object sender, EventArgs e)  
  3. {  
  4.      bool result = false;  
  5.      result = SQLInjectionHelper.ValidUrlData
    (Request.RequestType.ToUpper());  
  6.      if (result)  
  7.      {  
  8.         Response.Write("您提交的数据有恶意字符!");  
  9.         Response.End();  
  10.      }  
  11.  } 

到现在为止,一个通用防SQL注入漏洞程序已经基本完成。现在就来建一个测试页面做一下SQL注入测试,如代码清单1-13所示。

代码清单1-13   Test.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true"
     CodeBehind="Test.aspx.cs" 
  2. Inherits="_1_3.Test" Keywords="sdfsd"%> 
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  5. <html xmlns="http://www.w3.org/1999/xhtml"> 
  6. <head runat="server"> 
  7.     <title></title> 
  8. </head> 
  9. <body> 
  10.     <form id="form1" runat="server"> 
  11.     <div> 
  12.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
  13.     </div> 
  14.   <asp:Button ID="bt_Post" runat="server"
     Text="获取Post数据" onclick="bt_Post_Click" /> 
  15.   <asp:Button ID="bt_Get" runat="server"  
    Text="获取Get数据" onclick="bt_Get_Click" /> 
  16.     </form> 
  17. </body> 
  18. </html> 

如代码清单1-13所示,首先在页面创建一个TextBox来模拟用户的输入,然后分别添加“获取Post数据”和“获取Get数据”这两个Button来模拟Post请求和Get请求,请求事件的代码,如代码清单1-14所示。

代码清单1-14   Test.aspx.cs

  1. protected void bt_Post_Click(object sender, EventArgs e)  
  2. {  
  3. }  
  4. protected void bt_Get_Click(object sender, EventArgs e)  
  5. {  
  6.   Response.Redirect("Test.aspx?a=1&b=2&c=3");  

创建完测试程序后,运行结果如图1-34所示。

如图1-34所示,只要在文本框中输入所定义的非法字符串,不论Post请求还是Get请求,都会被防SQL注入程序所截获,弹出如图1-35所示页面。

 
【责任编辑:云霞 TEL:(010)68476606】

相关内容: 最新内容:
浅析C#启动停止SQL数据库服务之方法[2015-01-30]
网站安全性:C#防SQL注入代码的实现方法[2015-01-30]
1.6.1 防SQL注入技术[2015-01-30]
ASP防SQL注入攻击程序[2015-01-30]
SQL CONVERT转化函数使用方法小结[2015-01-29]
SQL Server下几个危险的扩展存储过程[2015-01-29]
JSP防注入代码[2015-01-30]
浅析C#启动停止SQL数据库服务之方法[2015-01-30]
网站安全性:C#防SQL注入代码的实现方法[2015-01-30]
1.6.1 防SQL注入技术[2015-01-30]
ASP防SQL注入攻击程序[2015-01-30]
为什么要部署Web应用防火墙?[2015-01-30]