zoukankan      html  css  js  c++  java
  • xss漏洞修复,待完善

    1.防止sql注入

     /// <summary>
            /// 分析用户请求是否正常
            /// </summary>
            /// <param name="Str">传入用户提交数据</param>
            /// <returns>返回是否含有SQL注入式攻击代码</returns>
            /// 
            private bool ProcessSqlStr(string Str)
            {
                bool ReturnValue = true;
                try
                {
                    if (!string.IsNullOrWhiteSpace(Str))
                    {
                        Str = Str.Replace("/*", "");
                        Str = Str.Replace("*/", "");
                        Str = Str.ToLower();
                        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.IndexOf(ss) >= 0)
                            {
                                ReturnValue = false;
                            }
                        }
                    }
                }
                catch
                {
                    ReturnValue = false;
                }
                return ReturnValue;
            }

    2.防止xss注入

       private bool ProcessXSSStr(string Str)
            {
                bool ReturnValue = true;
                try
                {
                    if (!string.IsNullOrWhiteSpace(Str))
                    {
                        Str = Str.Replace("/*", "");
                        Str = Str.Replace("*/", "");
                        Str = Str.ToLower();
                        string[] anyXSSStr = {"javascript", "vbscript", "script","alert(","expression("
            ,"onabort", "onactivate", "onafterprint", "onafterupdate", "onbeforeactivate", "onbeforecopy", "onbeforecut", "onbeforedeactivate", "onbeforeeditfocus", "onbeforepaste", "onbeforeprint", "onbeforeunload", "onbeforeupdate", "onblur",
            "onbounce", "oncellchange", "onchange", "onclick", "oncontextmenu", "oncontrolselect", "oncopy", "oncut", "ondataavailable", "ondatasetchanged", "ondatasetcomplete", "ondblclick", "ondeactivate", "ondrag", "ondragend", "ondragenter", 
            "ondragleave", "ondragover", "ondragstart", "ondrop", "onerror", "onerrorupdate", "onfilterchange", "onfinish", "onfocus", "onfocusin", "onfocusout", "onhelp", "onkeydown", "onkeypress", "onkeyup", "onlayoutcomplete", "onload",
            "onlosecapture", "onmousedown", "onmouseenter", "onmouseleave", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onmousewheel", "onmove", "onmoveend", "onmovestart", "onpaste", "onpropertychange", "onreadystatechange", 
            "onreset", "onresize", "onresizeend", "onresizestart", "onrowenter", "onrowexit", "onrowsdelete", "onrowsinserted", "onscroll", "onselect", "onselectionchange", "onselectstart", "onstart", "onstop", "onsubmit", "onunload"};
                        foreach (string ss in anyXSSStr)
                        {
                            if (Str.IndexOf(ss) >= 0)
                            {
                                ReturnValue = false;
                            }
                        }
                    }
                }
                catch
                {
                    ReturnValue = false;
                }
                return ReturnValue;
            }

    3.对http请求进行拦截处理,上下文根据程序进行修改

     public System.Web.Mvc.ActionResult Execute(Page_Context pageViewContext, PagePositionContext positionContext)
            {
                if (pageViewContext.ControllerContext.HttpContext.Request.Form != null)
                {
                    for (int i = 0; i < pageViewContext.ControllerContext.HttpContext.Request.Form.Keys.Count; i++)
                    {
                        string getkeys = pageViewContext.ControllerContext.HttpContext.Request.Form.Keys[i];
                        string str = pageViewContext.ControllerContext.HttpContext.Request.Form[getkeys];
                        if (!ProcessSqlStr(str))
                        {
                            pageViewContext.ControllerContext.HttpContext.Response.Redirect("~/safe_error");
                            pageViewContext.ControllerContext.HttpContext.Response.End();                        
                        }
                    }
                }
    
                if (pageViewContext.ControllerContext.HttpContext.Request.QueryString != null)
                {
                    string url = pageViewContext.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
    
                    if (!ProcessXSSStr(url))
                    {
                        pageViewContext.ControllerContext.HttpContext.Response.Redirect("~/safe_error");
                        pageViewContext.ControllerContext.HttpContext.Response.End();
                    }
    
                    for (int i = 0; i < pageViewContext.ControllerContext.HttpContext.Request.QueryString.Count; i++)
                    {
                        string getkeys = pageViewContext.ControllerContext.HttpContext.Request.QueryString.Keys[i];
                        
                        string str = pageViewContext.ControllerContext.HttpContext.Request.Form[getkeys];
    
                        if (!ProcessXSSStr(getkeys))
                        {
                            pageViewContext.ControllerContext.HttpContext.Response.Redirect("~/safe_error");
                            pageViewContext.ControllerContext.HttpContext.Response.End();
                        }
    
    
                        if (!ProcessSqlStr(str))
                        {
                            pageViewContext.ControllerContext.HttpContext.Response.Redirect("~/safe_error");
                            pageViewContext.ControllerContext.HttpContext.Response.End();   
                        }
    
                     
                    }
                }
                return null;
            }

    其他方法:

    antixss:      http://www.cnblogs.com/coderzh/archive/2010/06/24/1764725.html

                     https://msdn.microsoft.com/en-us/library/aa973813.aspx

  • 相关阅读:
    【Github】github图片显示不出
    【Linux】docker安装FastDFS
    【Github】问题解决:Failed to connect to github.com port 443: Operation timed out
    python生成1000w的mysql测试数据
    python 瀑布流
    django使用url路径组合搜索
    将规定的文件以及文件夹,压缩打包
    定期清理iis_log日志文件
    自己开发的python分页插件
    使用IO多路复用selectors模块写上传下载功能
  • 原文地址:https://www.cnblogs.com/ldybyz/p/5948680.html
Copyright © 2011-2022 走看看