http://www.vjsdn.com/bbs/bbsTopicDetails.aspx?pid=352
 http://www.vjsdn.com/bbs/bbsTopicDetails.aspx?pid=448 
这里有些解决方案,但还是被hackers找到入口,迫使我关掉其它页面,请高手们献良言,不胜感谢!!!另外Text字段被大量注入如 <script>www.xxx.com/0.js</script> 如何批量替换,请提供C#源代码.我另加分,谢谢!

解决方案 »

  1.   

    代码里?或数据库里?。数据库里,可以用sql替换啊。
      

  2.   

    是否有sql注入漏洞,在global里屏蔽
    void Application_BeginRequest(Object sender, EventArgs e)
        {
            StartProcessRequest();
        }
        private void StartProcessRequest()
        {
            try
            {
                string getkeys = "";
                string sqlErrorPage = "index.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 (!ProcessSqlStr(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 (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys]))
                        {
                            System.Web.HttpContext.Current.Response.Redirect(sqlErrorPage);
                            System.Web.HttpContext.Current.Response.End();
                        }
                    }
                }
            }
            catch
            {
            }
        }
        private bool ProcessSqlStr(string Str)
        {
            bool ReturnValue = true;
            try
            {
                if (Str.Trim() != "")
                {
                    string SqlStr = "exec¦insert¦select¦delete¦master¦update¦truncate¦declare";
                    string[] anySqlStr = SqlStr.Split('¦');
                    foreach (string ss in anySqlStr)
                    {
                       if(!Str.ToLower().Contains("updatepanel"))
                       {
                        if (Str.ToLower().IndexOf(ss) >= 0)
                        {
                            ReturnValue = false;
                            break;
                        }
                       }
                    }
                }
            }
            catch
            {
                ReturnValue = false;
            }
            return ReturnValue;
        }
    操作数据库用存储过程参考
    参考
      

  3.   

    传参过来的变量过滤一下。    {
            string Temp = StrText;
            if (StrText == "" || StrText == null)
            {
                Temp = "";
            }
            else
            {
                Temp = Temp.ToLower();
                Temp = Temp.Replace("(", "(");
                Temp = Temp.Replace(")", ")");
                Temp = Temp.Replace("--", "");
                Temp = Temp.Replace("/*", "");
                Temp = Temp.Replace("*/", "");
                Temp = Temp.Replace("delete", "");
                Temp = Temp.Replace("drop", "");
                Temp = Temp.Replace("create", "");
                Temp = Temp.Replace("cast", "");
                Temp = Temp.Replace("exec", "");
                Temp = Temp.Replace("insert", "");
                Temp = Temp.Replace("select", "");
                Temp = Temp.Replace("update", "");
                Temp = Temp.Replace("grant", "");
                Temp = Temp.Replace("alter", "");
                Temp = Temp.Replace("len", "");
                Temp = Temp.Replace("and", "");
                Temp = Temp.Replace("%", "%");
                Temp = Temp.Replace("'", " ’");        }        return Temp;
        }
    Sql 里面,如果有规律的注入字符,你就写个replace替换,要是没有规律的,呵呵,用数据库备份吧---例如: 
    update TableName set a=a.Replace('<script>www.xxx.com/0.js </script>','')
    祝你好运!
      

  4.   

    上面的方法名为:public string checkStr(string StrText)   //校验代码
        {
            string Temp = StrText; 
            //......
        }
      

  5.   

    回3楼及所有使用Application_BeginRequest方法进行处理的好友,当发出一个Url请求Application_BeginRequest会重复执行n遍,效率低。不可取!!
      

  6.   

    谢6楼 wangjun8868, 你提供的资料可以替换text字段的js脚本,但我还没测试,如测试通过,得分.
      

  7.   

    回wangjun8868,你提供的存储过程不能替换大文本字段,如text,ntext字段文本大于8060个字符
      

  8.   


    DECLARE @fieldtype sysname
    SET @fieldtype='varchar'
    --删除处理
    DECLARE hCForEach CURSOR GLOBAL
    FOR
    SELECT N'update '+QUOTENAME(o.name)
        +N' set '+ QUOTENAME(c.name) + N' = replace(' + QUOTENAME(c.name) + ',''<script_src=http://ucmal.com/0.js> </script>'','''')'
    FROM sysobjects o,syscolumns c,systypes t
    WHERE o.id=c.id
        AND OBJECTPROPERTY(o.id,N'IsUserTable')=1
        AND c.xusertype=t.xusertype
        AND t.name=@fieldtype
    EXEC sp_MSforeach_Worker @command1=N'?'先执行SET @fieldtype='varchar'
    再SET @fieldtype='nvarchar'再SET @fieldtype='ntext'
      

  9.   

    3ks to wangjun8868
    ----------------------------------
    当SET @fieldtype='ntext' 时运行出错.ntext字段能用replace方法?????????????
    ???????????????????????Server: Msg 8116, Level 16, State 1, Line 1
    函数 replace 的参数 1 的数据类型 ntext 无效。
      

  10.   

    攻击者在update数据库字段时,是采用先截取再加他自己字符的方法,所以对于text/ntext类型,如果你原来的字段内的字符>8000的话,已经无法全部还原成最初状态了. 
      

  11.   

    对特殊字符的显示,HTML Encode/Decode "<script>www.xxx.com/0.js </script>" 这类字符串,你显示的时候应该用var HtmlStr = Server.HtmlEncode("<script>www.xxx.com/0.js </script>"); 
      

  12.   

    http://topic.csdn.net/u/20090729/14/26381958-0d6e-4b90-bc90-d275e9621f93.html
      

  13.   

    没有想象的那么简单,很多细节要注意的,而且并非写好了SQL防注入语句就可以了,有些比如所用的第三方插件等都可能存在漏洞。
      

  14.   

    给你推荐一个人的帖子,他里面又讲到这方面,还有一些工具的介绍:http://topic.csdn.net/u/20090729/14/26381958-0d6e-4b90-bc90-d275e9621f93.html?48788
    希望对你有用! O(∩_∩)O~
      

  15.   

    据说SQL有触发器,你可以设置一下,遇到有<script>字样就不写入.
      

  16.   

    Application_BeginRequest 不可取,这代码copy到其它地方使用是行的
      

  17.   

    回复 27 楼
    这个方法适合于过滤要保存到数据库的参数的方法。
    如果SQL存在漏洞,黑客们会利用操作数据库的语句,例如添加语句在字段的值里面附带 cast 或 create ,当数据库在执行的期间会创建相应的过程或方法,以便他们后续的操控。
    多以过滤掉这个对sql敏感的关键字。对好使用 SqlParameter 操作数据库。 呵呵