惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
B
Blog RSS Feed
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
D
Docker
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
C
Check Point Blog
The Cloudflare Blog
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
量子位
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
T
The Blog of Author Tim Ferriss
博客园 - 【当耐特】
Vercel News
Vercel News
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
博客园 - 司徒正美

博客园 - 小鱼儿

毕业五年后同学之间的差距 - 小鱼儿 - 博客园 产品设计与用户体验之“小便池上的苍蝇” 深入浅出讲解什么是面向对象思想 企业没有忠诚的员工,只有真实的需求(听曾仕强讲座一句话有感) 成为一名优秀程序员的三步曲 快速解决网站挂马问题 js直接关闭窗口不提示ie6.0,ie7.0通用 总结设置sql2002或者2005计划任务 - 小鱼儿 - 博客园 一个大学毕业生的感慨,从招聘中看中国软件业(转载) 上善若水,厚德载物 职场黑锅你背还是不背 陪客点菜技巧 十道职场护身符 .NET牛人应该知道些什么(转) VS 2008 和 .NET 3.5 Beta 2 发布了 .net工程师必懂题(笔试题目)转 C#基础概念二十五问(转) 个人网站刷IP流量的小工具 C#与Flash ActionScript 2.0通过xml操作数据库(二)
最近sql注入数据库被更改泛滥,以下提供一个.net程序防止sql注...
小鱼儿 · 2008-05-07 · via 博客园 - 小鱼儿

Posted on 2008-05-07 15:55  小鱼儿  阅读(2380)  评论()    收藏  举报

最近sql注入数据库被更改泛滥:状况如下:“ </title> </pre>> <script src=http://sb.5252.ws:88/107/1.js> </script> <”,
以下提供一个.net程序防止sql注入的方法(过滤敏感语句的仅供参考)方式如下:在Global.asax文件下面加入如下代码:
 void Application_BeginRequest(Object sender, EventArgs e)
    {
        StartProcessRequest();

    }

    #region SQL注入式攻击代码分析
    ///  <summary>
    /// 处理用户提交的请求
    ///  </summary>
    private void StartProcessRequest()
    {
        try
        {
            string getkeys = "";
            string sqlErrorPage = "../default.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
        {
            // 错误处理: 处理用户提交信息!
        }
    }
    ///  <summary>
    /// 分析用户请求是否正常
    ///  </summary>
    ///  <param name="Str">传入用户提交数据 </param>
    ///  <returns>返回是否含有SQL注入式攻击代码 </returns>
    private bool ProcessSqlStr(string Str)
    {
        bool ReturnValue = true;
        try
        {
            if (Str.Trim() != "")
            {
                string SqlStr = "and ¦exec ¦insert ¦select ¦delete ¦update ¦count ¦* ¦chr ¦mid ¦master ¦truncate ¦char ¦declare";

                string[] anySqlStr = SqlStr.Split(' ¦');
                foreach (string ss in anySqlStr)
                {
                    if (Str.ToLower().IndexOf(ss) >= 0)
                    {
                        ReturnValue = false;
                        break;
                    }
                }
            }
        }
        catch
        {
            ReturnValue = false;
        }
        return ReturnValue;
    }
    #endregion