找回密码
 开放注册

QQ登录

只需一步,快速开始

微信登录

微信扫码,快速开始

搜索
查看: 641|回复: 0

如何防止整站SQL注入方式入侵

[复制链接]

735

主题

1102

回帖

3076

牛毛

二级牛人

积分
3096
QQ
发表于 2009-7-4 08:32:35 | 显示全部楼层 |阅读模式 来自 山东省威海市
如何防止整站SQL注入方式入侵

防止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文件中,在下面增加一个标签,如下:

<appSettings>
    <add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-
USzip" />
</appSettings>


  其中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.Split('-')[0];
        String parameterType = safeParameters.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");
    }
}


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

您需要登录后才可以回帖 登录 | 开放注册

本版积分规则

帮助|Archiver|小黑屋|通信管理局专项备案号:[2008]238号|NB5社区 ( 皖ICP备08004151号;皖公网安备34010402700514号 )

GMT+8, 2025-4-26 03:09 , Processed in 0.166909 second(s), 32 queries .

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表