asp.net 怎么防止SQL 注入 和  脚本注入

解决方案 »

  1.   

    SQL字符过滤吧,不是太懂ASP.NET的安全不止这些,还有很多。
    学浅,愧疚的UP
      

  2.   


    也太笼统了吧
    跟"怎么才能学好ASP.NET差不多"嘛
    这方面知识有好多呢
      

  3.   

    用参数形式构造SQL语句,或者存储过程。
    过滤危险字符''''
      

  4.   

    SQL防注入我觉得用过滤器支处理。但脚本嘛。我也不是很楚了。以前没听说过这个问题。
      

  5.   

    SQL注入我也知道一些,但是不太清楚脚本注入,各位能不能多说一些有关脚本注入的问题
      

  6.   

    1将sql中使用的一些特殊符号,如' -- /* ; %等用Replace()过滤
    2.最好用存储过程和具有参数的SQL语句
     第一:替换单引号,即把所有单独出现的单引号改成两个单引号,防止攻击者修改SQL命令的含义。再来看前面的例子,“SELECT * from Users WHERE login = ''' or ''1''=''1' AND password = ''' or ''1''=''1'”显然会得到与“SELECT * from Users WHERE login = '' or '1'='1' AND password = '' or '1'='1'”不同的结果。  第二:删除用户输入内容中的所有连字符,防止攻击者构造出类如“SELECT * from Users WHERE login = 'mas' -- AND password =''”之类的查询,因为这类查询的后半部分已经被注释掉,不再有效,攻击者只要知道一个合法的用户登录名称,根本不需要知道用户的密码就可以顺利获得访问权限。
      

  7.   

    防sql注入的方法最好是以存储过程参数化的形式
    也可以用字符串过滤的方式    /// 转换成SQL安全字符
        /// </summary>
        /// <param name="Str">待处理字符串</param>
        public static string ToSql(string Str)
        {
            if (Str.Length == 0)
            {
                return "";
            }
            Str = Str.Replace("'", "");
            Str = Str.Replace("select", "sel&#101;ct");
            Str = Str.Replace("join", "jo&#105;n");
            Str = Str.Replace("union", "un&#105;on");
            Str = Str.Replace("where", "wh&#101;re");
            Str = Str.Replace("insert", "ins&#101;rt");
            Str = Str.Replace("delete", "del&#101;te");
            Str = Str.Replace("update", "up&#100;ate");
            Str = Str.Replace("like", "lik&#101;");
            Str = Str.Replace("drop", "dro&#112;");
            Str = Str.Replace("create", "cr&#101;ate");
            Str = Str.Replace("modify", "mod&#105;fy");
            Str = Str.Replace("rename", "ren&#097;me");
            Str = Str.Replace("alter", "alt&#101;r");
            Str = Str.Replace("cast", "ca&#115;t");
            Str = Str.Replace("and", "an&#100;");
            Str = Str.Replace("or", "o&#114");
            Str = Str.Replace("%", "&#37;");
            Str = Str.Replace("<", "&lt;");
            Str = Str.Replace(">", "&gt;");
            return Str;
        }至于角本注入的话,你就要有用户交互的地方(即文本输入的地方)
    用正则过滤<script>..</script>
    或javascript:...
      

  8.   

    1,使用存储过程
    2.使用SqlParameter传递参数
    3.利用参数替换,将可疑的参数replace掉
      

  9.   

    在web项目的根目录下面的Global.asax文件中进行处理就行.
    没有Global.asax就建一个
     void Application_BeginRequest(Object sender, EventArgs e)
        {
            StartProcessRequest();
        }  private void StartProcessRequest()
        {
            try
            {
                string getkeys = "";
                
                string sqlErrorPage = "/action/error.aspx";//转向的错误提示页面 
                if (System.Web.HttpContext.Current.Request.QueryString != null)
                {                for (int i = 0; i < System.Web.HttpContext.Current.Request.QueryString.Count; i++)
                    {
                        getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];
                        if (!ProcessSqlPara(System.Web.HttpContext.Current.Request.QueryString[getkeys]))
                        {
                            System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
                            System.Web.HttpContext.Current.Response.End();
                        }
                    }
                }//下面是验证表单数据的,一般不需要处理
                //if (System.Web.HttpContext.Current.Request.Form != null)
                //{
                //    for (int i = 0; i < System.Web.HttpContext.Current.Request.Form.Count; i++)
                //    {
                //        getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];
                //        if (getkeys == "__VIEWSTATE") continue;
                //        if (!ProcessSqlPara(System.Web.HttpContext.Current.Request.Form[getkeys]))
                //        {
                //            System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
                //            System.Web.HttpContext.Current.Response.End();
                //        }
                //    }
                //}
            }
            catch
            {
                // 错误处理: 处理用户提交信息! 
            }
        }
     private bool ProcessSqlPara(string Str)
        {
            bool ReturnValue = true;
            try
            {
                // /w*((\%27)|(’))((\%6F)|o|(\%4F))((\%72)|r|(\%52))/ix 
                //string reg_str = @"((\%3D)|(=))[^\n]*((\%27)|(\')|(\-\-)|(\%3B)|(:))"; //这个规则首先留意 = 号或它的hex值(%3D),然后考虑零个或多个除换行符以外的任意字符,最后检测单引号,双重破折号或分号。
                string reg_str =  @"(exec|and|delete|insert|select|union|update|chr|mid|master|truncate|char|declare)(\s+|\++)";            
                System.Text.RegularExpressions.Regex reg = new Regex(reg_str, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);            Match mMatch = reg.Match(Str);
                if (mMatch.Success)
                {
                    ReturnValue = false;
                }
                if (ReturnValue)
                {
                    reg_str =  @"(exec|and|delete|insert|select|union|update|chr|mid|master|truncate|char|declare)(\s+|\++)"; reg = new Regex(reg_str, System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                    mMatch = reg.Match(Str);
                    if (mMatch.Success)
                    {
                        ReturnValue = false;
                    }
                }        
                //ReturnValue = Creator.Utils.SQLUtil.CheckParams(Str);                
            }
            catch
            {
                ReturnValue = false;
            }
            return ReturnValue;
        }
        #endregion