我现在想写一个PageBase 页面,所有的页面都继承 PageBase 。 我现在接手的一个项目是已经运行的项目,代码测试发现有很多SQL注入,我想通过这个PageBase 来替换所有的 Request.QueryString 变量,但是 Request.QueryString 变量是可读的,不能更改值变量。请问大家有啥好方法能解决此问题吗?注明:我在.NET版区已经发帖,但是没人回答。特此来SQL区提问,希望大家能给出解决方案。

解决方案 »

  1.   

    数据库被注入攻击   所有文本型字下段数据都被加了     <script_src=http://ucmal.com/0.js> </script> 
    怎么删掉?
    --sql 2000解决方法
    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'?'
    --sql 2005 解决方法
    declare  @t  varchar(255),@c  varchar(255)  
    declare  table_cursor  cursor  for  
    select  a.name,b.name  from  sysobjects  a,syscolumns  b  
    where  a.iD=b.iD  AnD  a.xtype='u'  
    AnD  (b.xtype=99  or  b.xtype=35  or  b.xtype=231  or  b.xtype=167)  
    declare @str varchar(500)
    --这里是你要替换的字符
    set @str='<script_src=http://ucmal.com/0.js> </script>'
    open  table_cursor  fetch  next  from  table_cursor  
    into  @t,@c  while(@@fetch_status=0)  
    begin      
        exec('update  [' + @t + ']  set  [' + @c + ']=replace(cast([' + @c + '] as varchar(8000)),'''+@str+''','''')')      
        fetch  next  from  table_cursor  into  @t,@c  
    end  
    close  table_cursor  deallocate  table_cursor; 
      

  2.   

    SQL注入专题
    http://topic.csdn.net/u/20081205/09/3dd06076-bcbe-45d4-998c-8999fdbe6fae.html
      

  3.   

    乌龟 我要的是.NET解决方法 。。
      

  4.   

    在pagebase中定义一个全局静态变量来保存这个Request.QueryString的值可以么?
    然后根据你的判断来修改这个全局静态变量就是了啥...
    在你的其他页面中读取这个值就行了.
      

  5.   

    和尚  我也是这么想的。 public class PageBase: System.Web.UI.Page
        {
            #region 事件        protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                Response.Expires = 0;
                if (HttpContext.Current.Request.QueryString.Count > 0)
                {
                    foreach (string queryName in HttpContext.Current.Request.Params)
                    {
                        if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                            continue;
                        //替换字符串,防止SQL注入
                        HttpContext.Current.Request.Params[queryName] = SqlCheckHelper.TranslateSqlChar(HttpContext.Current.Request.QueryString[queryName]);
                        //HttpContext.Current.Request.QueryString[queryName] = SqlCheckHelper.TranslateSqlChar(HttpContext.Current.Request.QueryString[queryName]); 
                    }
                }        }        #endregion
        }就是不能更改HttpContext.Current.Request.Params[queryName]的值,它只是可读的,我现在想用重定向试试看。  不知和尚有啥好方法。
      

  6.   

    不懂。要么换个思路,防注入:
    http://topic.csdn.net/u/20090729/14/26381958-0d6e-4b90-bc90-d275e9621f93.html
    http://topic.csdn.net/u/20091014/19/8d93909b-88a4-47ba-9956-f6716f761cbc.html
      

  7.   


    public class PageBase: System.Web.UI.Page
        {
           public static string QueryStringValue = string.Empty();
            #region 事件
            
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                Response.Expires = 0;
                if (HttpContext.Current.Request.QueryString.Count > 0)
                {
                    foreach (string queryName in HttpContext.Current.Request.Params)
                    {
                        if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                            continue;
                        //替换字符串,防止SQL注入
                        QueryStringValue  = SqlCheckHelper.TranslateSqlChar(HttpContext.Current.Request.QueryString[queryName]);
                        //HttpContext.Current.Request.QueryString[queryName] = SqlCheckHelper.TranslateSqlChar(HttpContext.Current.Request.QueryString[queryName]); 
                    }
                }        }        #endregion
        }
    然后在你的其他页面引用QueryStringValue这个试试,但是这个有一个严重的问题是,并发,多人同时使用这个pagebase的时候会导致值被重写,产生并发错误.
    另外一种方式是放在Session里面,保存Session的值,读取Session的值即可.其实由于是page request过来的,直接在页面里面处理了就是了,不一定非要提到PageBase里面啦..
      

  8.   

    或者说在你需要拼接和使用SQL的时候,在讲那些需要的值替换掉就是了,不一定非要在PageBase里面实现.
      

  9.   

    是修改已有的代码?如果一处处改,工作量有点大.想简单点也可以,写一个继承于Request的类 比如xRequest, 提供一个字段,值就是 对Request.QueryString进行过滤后的值, 然后批量替换 Request为xRequest. 虽然这个代价有点大,不过可以简化工作量。
    最好的还是手工去改代码,以传参方式执行sql语句。