English
 电子信箱
 加入收藏

  威盾防火墙 >> 支持与下载 >> 技术文章 >> 网站安全:整站如何防止SQL注入方式入侵

 

网站安全:整站如何防止SQL注入方式入侵

威盾防火墙 2009-05-07

 

防止SQL注入,通常一个一个文件修改不仅麻烦而且还有漏掉的危险,下面我说一下如何从整个系统防止注入。

做到以下三步,相信你的程序就会比较安全了,而且对整个网站的维护也将变的简单。

一、数据验证类

parameterCheck.cs 

 

  

 

public class parameterCheck{

 

    public static bool isEmail(string emailString){

 

        return System.Text.RegularExpressions.Regex.IsMatch(emailString, "['\\w_-]+(\\.

 

['\\w_-]+)*@['\\w_-]+(\\.['\\w_-]+)*\\.[a-zA-Z]{2,4}");

 

    }

 

    public static bool isInt(string intString){

 

        return System.Text.RegularExpressions.Regex.IsMatch(intString ,"^(\\d{5}-\\d{4})|

 

(\\d{5})$");

 

    }

 

    public static bool isUSZip(string zipString){

 

        return System.Text.RegularExpressions.Regex.IsMatch(zipString ,"^-[0-9]+$|^[0-9]

 

+$");

 

    }

 

}

二、Web.config

在你的Web.config文件中,在下面增加一个标签,如下:

 

    USzip" />

 

其中key是后面的值为“OrderId-int32”等,其中“-”前面表示参数的名称比如:OrderId,后面的int32表示数据类型。

三、Global.asax

在Global.asax中增加下面一段:

protected void Application_BeginRequest(Object sender, EventArgs e){

 

    String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings

 

["safeParameters"].ToString().Split(',');

 

    for(int i= 0 ;i < safeParameters.Length; i++){

 

        String parameterName = safeParameters[i].Split('-')[0];

 

        String parameterType = safeParameters[i].Split('-')[1];

 

        isValidParameter(parameterName, parameterType);

 

    }

 

} 

 

 

public void isValidParameter(string parameterName, string parameterType){

 

    string parameterValue = Request.QueryString[parameterName];

 

    if(parameterValue == null) return;

 

 

    if(parameterType.Equals("int32")){

 

        if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");

 

    }

 

    else if (parameterType.Equals("double")){

 

        if(!parameterCheck.isDouble(parameterValue)) Response.Redirect("parameterError.aspx");

 

    }

 

    else if (parameterType.Equals("USzip")){

 

        if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");

 

    }

 

    else if (parameterType.Equals("email")){

 

        if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");

 

    }

 

}

以后需要修改的时候我们只需要修改以上三个文件,对整个系统的维护将会大大提高效率,当然你可以根据自己的需要增加其它的变量参数和数据类型。


相关内容: 最新内容:
网管员日记:网站SQL注入防御实战[2009-05-02]
从端口下手 企业防范SQL蠕虫病毒的侵袭[2009-05-02]
SQL Server中保护数据的安全选项[2009-04-30]
web安全---防范SQL注入式攻击[2009-04-30]
SQL注入技术和跨站脚本攻击的检测[2009-04-24]
安全知识:数据安全之SQL数据库服务器的安全策略[2009-03-27]
了解CSS挂马及相应防范方法[2009-05-07]
为抢客户雇黑客入侵电脑 40万用户遭上网难[2009-05-07]
黑客盗取美国弗州830万人病例 勒索1000万美元[2009-05-07]
多个政府网站被植入木马 可窃取网民个人资料[2009-05-07]
网店竟遭黑客攻击 账号被窃险成“黑店”[2009-05-06]
做精明的“赶马人” 木马查找清除攻略[2009-05-06]