首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 网站被连续注入,疯了,求两个正则一个50分!
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 18:00:44 楼主

    我疯了,现在程序员真痛苦啊,好不容易搞好了网站,还要整天提心吊胆的被黑

    我的网站从五一之前就天天被sql注入,数据库被改的一塌糊涂,fk!

    虽然使用了存储过程加参数,但是有几个查询不可避免的要拼接字符串

    我现在考虑加上过滤非法字符的问题,但是对正则不是太熟悉,特求两个正则,其实是一个

    “单引号 横杠 百分号 逗号 update insert delete table ” (除了这些还有什么危险的,大家给提提)

    一个正则是判断字符串中是否有这些字符或单词,我用来提示用户输入了非法字符
    一个正则是直接替换掉这些字符或单词,我在提交时先把字符串整理一下

    因为是用虚拟主机,所以系统安全不归我管,尽可能的使用存储过程和参数,然后把用户输入能屏蔽的尽量屏蔽,除此之外,还有什么嘛?请指点。

    另:这些垃圾黑客去死,呜呜,我的数据啊~~ 这年头真不容易,弄个网站还得顾及着他们,靠!


    100  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 18:20:041楼 得分:0
    同情中,我还没有网站,关注中
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 18:29:002楼 得分:0
    关注中。。。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 18:43:293楼 得分:0
    帮顶! 没着方面的经验!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 18:50:174楼 得分:0
    VB.NET code
    function strleach(str)'过滤非法字符函数 dim tempstr if str="" then exit function tempstr=replace(str,chr(34),"")' " tempstr=replace(tempstr,chr(39),"")' ' tempstr=replace(tempstr,chr(60),"")' < tempstr=replace(tempstr,chr(62),"")' > tempstr=replace(tempstr,chr(37),"")' % tempstr=replace(tempstr,chr(38),"")' & tempstr=replace(tempstr,chr(40),"")' ( tempstr=replace(tempstr,chr(41),"")' ) tempstr=replace(tempstr,chr(59),"")' ; tempstr=replace(tempstr,chr(43),"")' + tempstr=replace(tempstr,chr(45),"")' - tempstr=replace(tempstr,chr(91),"")' [ tempstr=replace(tempstr,chr(93),"")' ] tempstr=replace(tempstr,chr(123),"")' { tempstr=replace(tempstr,chr(125),"")' } strleach=tempstr end function
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 18:54:555楼 得分:0
    http://www.51aspx.com/CV/HouseSpiderSystem/InsApp/word/CreateCode.cs.html
    值得一看

    C# code
    #region检测非法字符,防止sql注入 /**//// <summary> ///检测非法字符,防止sql注入 71/// 如果参数是空,返回false /// 如果参数中包含非法字符,返回false ///// 否则返回 true /// </summary> /// <param name="thePara"></param> /// <returns></returns> public bool toFilter(string thePara) { string[] BadCode = new string[]{ "'", "\"", "exec", "cmd", ">", "<", "and", "=", "\\", ";" }; try { if (CheckNullstr(thePara) == false) //如果参数是空值,返回false { throw new Exception("参数为空"); } else { for (int i = 0; i < BadCode.Length; i++) { if (thePara.IndexOf(BadCode[i]) > 0) { throw new Exception("包含非法字符"); } } } return true; } catch { return false; } } #endregion
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 19:16:016楼 得分:0
    帮顶,不懂。呵呵。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 19:22:597楼 得分:0
    楼主提醒的好,以后我们在开发要求安全性高的软件时得注意了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 20:00:108楼 得分:0
    同情楼主,好在我的网站全是Procedure写的.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 20:12:099楼 得分:0
    最好用参数.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 20:13:0910楼 得分:0
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Teng_s2000
    • 等级:
    发表于:2008-05-08 20:50:1511楼 得分:0
    同情
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-08 20:57:2712楼 得分:0
    注意 <iframe>病毒,有时间服务器的问题。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 08:19:4313楼 得分:0
    同情ing....
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 08:30:2014楼 得分:0
    同情...
    四五樓參考不錯
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 08:31:3515楼 得分:0
    sql都用存储过程!还有就是权限配置!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 08:35:3816楼 得分:0
    轻松对应特殊字符之我见:

    string Title=HttpUtility.HtmlEncode(this.TextBox.Text.Trim().ToString());//对输入内容进行编码
    Title=Title.Replace("'","''");//对应SQL中的单引号


    详细参考
    http://hi.baidu.com/charryong/blog/item/e7e4acedb31de04b78f055f9.html
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 09:11:0217楼 得分:0
    为什么不用Parameter传参的方式?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 09:22:0218楼 得分:0
    我是楼主,我是用了parameter传参数的形式,能用的地方已经都用了

    另:
    tempstr=replace(tempstr,chr(40),"")' ( 
    tempstr=replace(tempstr,chr(41),"")' ) 
    tempstr=replace(tempstr,chr(59),"")' ; 
    tempstr=replace(tempstr,chr(43),"")' + 
    tempstr=replace(tempstr,chr(45),"")' - 
    这种方式效率不是很高,如果敏感词多的话,效率就更低了,应该用正则判断是最优的,请高手给个正则的写法吧,谢谢。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 09:38:1619楼 得分:0
    C# code
    Function IsInteger(Para)//设置判断只允许数字,如果不是数字返回假 IsInteger=false If Not (IsNull(Para) Or Trim(Para)="" Or Not IsNumeric(Para)) Then IsInteger=true End If End Function

    下面是调用,参数=false说明参数不是数字或不单纯只是数字,=真说明参数正确
    C# code
    (if IsIntger("参数")!=false) { A=IsIntger("参数");//参数正确就调用 }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 09:43:4620楼 得分:0
    有点乱.
    改一下
    C# code
    Function IsInteger(Para)//设置判断只允许数字,如果不是数字返回假 IsInteger=false If (Not IsNull(Para) || Trim(Para)="" || Not IsNumeric(Para))) { IsInteger=true; } End Function //-------------------------------------------------------------- if (IsIntger("参数")!=false) { A=IsIntger("参数");//参数正确就调用 }


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 09:56:3421楼 得分:0
    public bool toFilter(string thePara)
             {
                 string[] BadCode = new string[]{ "'", "\"", "exec", "cmd", ">", " <", "and", "=", "\\", ";" };
                 try
                 {
                     if (CheckNullstr(thePara) == false) //如果参数是空值,返回false
                     {
                         throw new Exception("参数为空");
                     }
                     else
                     {
                         for (int i = 0; i  < BadCode.Length; i++)
                         {
                             if (thePara.IndexOf(BadCode[i]) > 0)
                             {
                                 throw new Exception("包含非法字符");
                             }
                         }
                     }
                     return true;
                 }
                 catch 
                {
                  return false;
                }


             }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 10:23:4622楼 得分:0
    C# code
    ['-,%]|(update)|(insert)|(delete)|(table)

    需要替换成什么??
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 10:27:5723楼 得分:0
    代码资料挺全,推荐一下:http://www.ciitc.com
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 11:05:0224楼 得分:0


    插入 ,修改 ,删除 ,查询:

    Default 中的查询...
     1. WebUtility.SearchRedirect(txtSearch.Text);

    namespace PetShop.Web {
        
        public static class WebUtility {

            private const string REDIRECT_URL = "~/Search.aspx?keywords={0}";
           

            public static string InputText(string text, int maxLength) {
                text = text.Trim();
                if (string.IsNullOrEmpty(text))
                    return string.Empty;
                if (text.Length > maxLength)
                    text = text.Substring(0, maxLength);
                text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
                text = Regex.Replace(text, "( <[b ¦B][r ¦R]/*>)+ ¦( <[p ¦P](. ¦\\n)*?>)", "\n"); // <br>
                text = Regex.Replace(text, "(\\s*&[n ¦N][b ¦B][s ¦S][p ¦P];\\s*)+", " "); //&nbsp;
                text = Regex.Replace(text, " <(. ¦\\n)*?>", string.Empty); //any other tags
                text = text.Replace("'", "''");
                return text;
            }

    public static void SearchRedirect(string key) {
                HttpContext.Current.Response.Redirect(string.Format(REDIRECT_URL, InputText(key, 255)));
            }
      
        }


     ///  <summary>
            ///  移除HTMl 标记
            ///  </summary>
            ///  <param name="Html"> </param>
            ///  <param name="RegStr"> </param>
            ///  <returns> </returns>
            public static string Remove(string Html)
            {
                string regesstr = " <.*?>";
                return Regex.Replace(Html, regesstr, string.Empty, RegexOptions.IgnoreCase);
            }
            public static string FilterScript(string content)
            {
                string regexstr = @" <(script)[^>]*>(\s* ¦.)* </\1>";
                return Regex.Replace(content,regexstr,string.Empty,RegexOptions.IgnoreCase);

            }
            ///  <summary>
            /// 过略所有的 危险标记
            ///  </summary>
            ///  <param name="html"> </param>
            ///  <returns> </returns>
            public string wipeScript(string html)
            {
                System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"( <script){1,}[^ <>]*>[^\0]*( <\/script>){1,}", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@"href   *=   *[\s\S]*script   *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@"on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@" <iframe[\s\S]+ </iframe*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@" <frameset[\s\S]+ </frameset*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                html = regex1.Replace(html, "");   //过滤 <script> </script>标记   
                html = regex2.Replace(html, "");   //过滤href=javascript:   ( <A>)   属性   
                html = regex3.Replace(html, "   _disibledevent=");   //过滤其它控件的on...事件   
                html = regex4.Replace(html, "");   //过滤iframe   
                html = regex5.Replace(html, "");   //过滤frameset   
                return html;
            }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 11:14:4725楼 得分:0
    回22楼 替换成空

    string resultString = null;
    try {
        Regex regexObj = new Regex(就是这里不会写);
        resultString = regexObj.Replace(subjectString, "");
    } catch (ArgumentException ex) {
        // Syntax error in the regular expression
    }


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 11:22:0926楼 得分:0
    引用 4 楼 maco_wang 的回复:
    VB.NET code
    function strleach(str)'过滤非法字符函数 
    dim tempstr 
    if str="" then exit function 
    tempstr=replace(str,chr(34),"")' " 
    tempstr=replace(tempstr,chr(39),"")' ' 
    tempstr=replace(tempstr,chr(60),"")'  < 
    tempstr=replace(tempstr,chr(62),"")' > 
    tempstr=replace(tempstr,chr(37),"")' % 
    tempstr=replace(tempstr,chr(38),"")' & 
    tempstr=replace(tempstr,chr(40),"")' ( 
    tempstr=replace…


    学習
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-09 11:28:3827楼 得分:0
    mark!