ASPX防注入程序 V1.0

虽然说ASP.NET属于安全性高的脚本语言,但是也经常看到ASP.NET网站由于过滤不严造成注射.由于ASP.NET基本上配合MMSQL数据库架设 如果权限过大的话很容易被攻击. 再者在网络上找不到好的ASP.NET防注射脚本,所以就自己写了个. 在这里共享出来旨在让程序员免除SQL注入的困扰.

我写了两个版本,VB.NET和C#版本方便不同程序间使用.


描述:
       1. XP + IIS5.1 + Access + MSSQL2000 下测试通过。
       2. 由于考虑到ASPX大多数和MMSQL数据库配合使用,在此增加了MMSQL关键字。
      3. 放到数据库连接代码处即可,和ASP用法类似。


C#Code:

    public void JK1986_CheckSql()
    {
        string jk1986_sql = "exec��select��drop��alter��exists��union��and��or��xor��order��mid��asc��execute��xp_cmdshell��insert��update��delete��join��declare��char��sp_oacreate��wscript.shell��xp_regwrite��'��;��--��%";
        string[] jk_sql = jk1986_sql.Split('��');
        foreach (string jk in jk_sql)
        {
            // -----------------------防 Post 注入-----------------------
            if (Page.Request.Form != null)
            {
                for (int k = 0; k < Page.Request.Form.Count; k++)
                {
                    string getsqlkey = Page.Request.Form.Keys[k];
                    string getip;
                    if (Page.Request.Form[getsqlkey].ToLower().Contains(jk) == true)
                    {
                        Response.Write("<script Language=JavaScript>alert('ASP.NET( C#版本 )防注入程序提示您,请勿提交非法字符!↓\\n\\nBlog: http://hi.baidu.com/ahhacker86 \\n\\nBy:�艋谩锝?� && JK1986');</" + "script>");
                        Response.Write("非法操作!系统做了如下记录 ↓" + "<br>");
                        if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null )
                        {
                            getip = this.Page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                        }
                        else
                        {
                            getip = Page.Request.ServerVariables["REMOTE_ADDR"];
                        }
                        Response.Write("操 作 I P :" + getip + "<br>");
                        Response.Write("操 作 时 间:" + DateTime.Now.ToString() + "<br>");
                        Response.Write("操 作 页 面:" + Page.Request.ServerVariables["URL"] + "<br>");
                        Response.Write("提 交 方 式:P O S T " + "<br>");
                        Response.Write("提 交 参 数:" + jk + "<br>");
                        Response.Write("提 交 数 据:" + Page.Request.Form[getsqlkey].ToLower() + "<br>");
                        Response.End();
                    }
                }
            }
            // -----------------------防 GET 注入-----------------------
            if (Page.Request.QueryString != null)
            {
                for (int k = 0; k < Page.Request.QueryString.Count; k++)
                {
                    string getsqlkey = Page.Request.QueryString.Keys[k];
                    string getip;
                    if (Page.Request.QueryString[getsqlkey].ToLower().Contains(jk) == true)
                    {
                        Response.Write("<script Language=JavaScript>alert('ASP.NET( C#版本 )防注入程序提示您,请勿提交非法字符!↓\\n\\nBlog: http://hi.baidu.com/ahhacker86 \\n\\nBy:�艋谩锝?� && JK1986');</" + "script>");
                        Response.Write("非法操作!系统做了如下记录 ↓" + "<br>");
                        if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null )
                        {
                            getip = this.Page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                        }
                        else
                        {
                            getip = Page.Request.ServerVariables["REMOTE_ADDR"];
                        }
                        Response.Write("操 作 I P :" + getip + "<br>");
                        Response.Write("操 作 时 间:" + DateTime.Now.ToString() + "<br>");
                        Response.Write("操 作 页 面:" + Page.Request.ServerVariables["URL"] + "<br>");
                        Response.Write("提 交 方 式:G E T " + "<br>");
                        Response.Write("提 交 参 数:" + jk + "<br>");
                        Response.Write("提 交 数 据:" + Page.Request.QueryString[getsqlkey].ToLower() + "<br>");
                        Response.End();
                    }
                }
            }
            // -----------------------防 Cookies 注入-----------------------
            if (Page.Request.Cookies != null)
            {
                for (int k = 0; k < Page.Request.Cookies.Count; k++)
                {
                    string getsqlkey = Page.Request.Cookies.Keys[k];
                    string getip;
                    if (Page.Request.Cookies[getsqlkey].Value.ToLower().Contains(jk) == true)
                    {
                        Response.Write("<script Language=JavaScript>alert('ASP.NET( C#版本 )防注入程序提示您,请勿提交非法字符!↓\\n\\nBlog: http://hi.baidu.com/ahhacker86 \\n\\nBy:�艋谩锝?� && JK1986');</" + "script>");
                        Response.Write("非法操作!系统做了如下记录 ↓" + "<br>");
                        if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null )
                        {
                            getip = this.Page.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                        }
                        else
                        {
                            getip = Page.Request.ServerVariables["REMOTE_ADDR"];
                        }
                        Response.Write("操 作 I P :" + getip + "<br>");
                        Response.Write("操 作 时 间:" + DateTime.Now.ToString() + "<br>");
                        Response.Write("操 作 页 面:" + Page.Request.ServerVariables["URL"] + "<br>");
                        Response.Write("提 交 方 式: Cookies " + "<br>");
                        Response.Write("提 交 参 数:" + jk + "<br>");
                        Response.Write("提 交 数 据:" + Page.Request.Cookies[getsqlkey].Value.ToLower() + "<br>");
                        Response.End();
                    }
                }
            }

        }    
    }    
 
ASP.NET( C#版本 )防注入程序 下载地址: http://www.ahiec.net//eWebEditor/UploadFile/ASPXSQL.rar

ASP.NET( VB.NET版本 )防注入程序 下载地址: http://www.ahiec.net/eWebEditor/UploadFile/ASPXSQLVB.rar

什么 问题请及时反馈给我 期待与你交流.

你可能感兴趣的:(职场,休闲)