1、在Global.asax.cs中写入:

protected void Application_BeginRequest(Object sender,EventArgs e)
{
      SqlInject myCheck = new SqlInject(this.Request);

myCheck.CheckSqlInject();
}

 
2、新建一个SqlInject防注入类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SC_LuceneNet
{
    public class SqlInject : System.Web.UI.Page
    {
        //检测到注入后的处理方式:   0:仅警告;1:警告+记录;2:警告+自定义错误页面;3:警告+记录+自定义错误页面
        private const int _type = 3;
        private const string errRedirectPage = "/err.aspx";

//过滤特征字符
        private const string StrKeyWord = @"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec|master|net local group administrators|net user|or|and";
        private const string StrRegex = @"-|;|,|/|(|)|[|]|{|}|%|@|*|'|!";

private HttpRequest request;

public SqlInject(System.Web.HttpRequest _request)
        {
            this.request = _request;
        }

public void CheckSqlInject()
        {
            bool isInject = false;
            if (CheckRequestQuery() || CheckRequestForm())
            {
                isInject = true;
            }
            else
            {
                return;
            }

switch (_type)
            {
                case 0:
                    ShowErr();
                    break;
                case 1:
                    ShowErr();
                    break;
                case 2:
                    ShowErr();
                    string temp;
                    System.Web.HttpContext.Current.Response.Write("<script>setTimeout(\"" + "location.href='" + errRedirectPage + "'" + "\",5000)</script>");
                    break;
                case 3:
                    ShowErr();
                    System.Web.HttpContext.Current.Response.Write("<script>setTimeout(\"" + "location.href='" + errRedirectPage + "'" + "\",5000)</script>");
                    break;
                default:
                    break;
            }
            System.Web.HttpContext.Current.Response.End();

}
        private string RelaceSingleQuotes(string _url)
        {
            string URL = _url.Replace("'", "单引号");
            return URL;
        }
        private void ShowErr()
        {
            string msg = @"<font color=red>请不要尝试未授权之入侵检测!</font>" + @"<br><br>";
            msg += @"操作IP:" + request.ServerVariables["REMOTE_ADDR"] + @"<br>";
            msg += @"操作时间:" + DateTime.Now + @"<br>";
            msg += @"页面:" + request.ServerVariables["URL"].ToLower() + request.QueryString.ToString() + @"<br>";
            msg += @"<a href='err.aspx' onclick='javascript:window.close()'>关闭</a>";
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Write(msg);
        }
        ///<summary>
        /// 特征字符
        ///</summary>
        public static string KeyWord
        {
            get
            {
                return StrKeyWord;
            }
        }
        ///<summary>
        /// 特征符号
        ///</summary>
        public static string RegexString
        {
            get
            {
                return StrRegex;
            }
        }

///<summary>
        ///检查字符串中是否包含Sql注入关键字
        /// <param name="_key">被检查的字符串</param>
        /// <returns>如果包含注入true;否则返回false</returns>
        ///</summary>
        private static bool CheckKeyWord(string _key)
        {
            string[] pattenString = StrKeyWord.Split('|');
            string[] pattenRegex = StrRegex.Split('|');
            foreach (string sqlParam in pattenString)
            {
                if (_key.Contains(sqlParam + " ") || _key.Contains(" " + sqlParam))
                {
                    return true;
                }
            }
            foreach (string sqlParam in pattenRegex)
            {
                if (_key.Contains(sqlParam))
                {
                    return true;
                }
            }
            return false;

}
        ///<summary>
        ///检查URL中是否包含Sql注入
        /// <param name="_request">当前HttpRequest对象</param>
        /// <returns>如果包含注入true;否则返回false</returns>
        ///</summary>
        public bool CheckRequestQuery()
        {
            if (request.QueryString.Count > 0)
            {
                foreach (string sqlParam in this.request.QueryString)
                {
                    if (sqlParam == "__VIEWSTATE") continue;
                    if (sqlParam == "__EVENTVALIDATION") continue;
                    if (CheckKeyWord(request.QueryString[sqlParam].ToLower()))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        ///<summary>
        ///检查提交的表单中是否包含Sql注入
        /// <param name="_request">当前HttpRequest对象</param>
        /// <returns>如果包含注入true;否则返回false</returns>
        ///</summary>
        public bool CheckRequestForm()
        {
            if (request.Form.Count > 0)
            {
                foreach (string sqlParam in this.request.Form)
                {
                    if (sqlParam == "__VIEWSTATE") continue;
                    if (sqlParam == "__EVENTVALIDATION") continue;
                    if (CheckKeyWord(request.Form[sqlParam]))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

}
}

 
3、新建err.aspx错误文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="err.aspx.cs" Inherits="SC_LuceneNet.err" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <script type='text/javascript'>
         if (confirm('是否重新登陆~~~~~~')) {
             location = "login.aspx";
         }
    </script>
</body>
</html>

SQL防注入程序的更多相关文章

  1. SQL防注入程序 v1.0

    /// ***************C#版SQL防注入程序 v1.0************ /// *使用方法: /// 一.整站防注入(推荐) /// 在Global.asax.cs中查找App ...

  2. sql 防注入 维基百科

    http://zh.wikipedia.org/wiki/SQL%E8%B3%87%E6%96%99%E9%9A%B1%E7%A2%BC%E6%94%BB%E6%93%8A SQL攻击(SQL inj ...

  3. PHP之SQL防注入代码集合(建站常用)

    SQL防注入代码一 <?php if (!function_exists (quote)) { function quote($var) { if (strlen($var)) { $var=! ...

  4. php之防注入程序绕过浅谈

    <?php/*判断传递的变量是否含有非法字符如:$_POST/$_GET功能:SQL防注入系统*/ //屏蔽错误提示error_reporting(7); //需要过滤的字符 $ArrFiltr ...

  5. 特殊字符的过滤方法,防sql防注入代码的过滤方法

    特殊字符的过滤方法 function strFilter($str){ //特殊字符的过滤方法 $str = str_replace('`', '', $str); $str = str_replac ...

  6. PHP SQL防注入

    过年前后在做一个抽奖的东西,需要用户填写中奖信息,为了防止非法用户对数据库进行入侵神马的,于是写下基本的防注入语句,需要用的可以自己封装成一个function. $str = str_replace( ...

  7. PHP安全、Sql防注入安全汇总

    利用Mysqli和PDO 产生原因 主要就是一些数据没有经过严格的验证,然后直接拼接 SQL 去查询.导致漏洞产生,比如: $id = $_GET['id']; $sql = "SELECT ...

  8. 360提供的SQL防注入

    <?php class sqlsafe { private $getfilter = "'|(and|or)\\b.+?(>|<|=|in|like)|\\/\\*.+?\ ...

  9. .net sql 防注入 httpmodule

    1 新建一个类,实现IHttpModule接口 using System; using System.Collections.Generic; using System.Linq; using Sys ...

随机推荐

  1. OSGEARTH三维地形开源项目

    第一章   OSGEarth介绍 第二章   OSGEarth编译环境配置 OSGEarth的编译环境配置随着版本的不同.运行平台的不同,也有很大的差异.本章主要以Windows XP SP3(x86 ...

  2. “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题

    “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题 最近做一个前端的项目,项目需要引用VLC浏览器插件,javascript在IE.Firefox等浏览器 ...

  3. 逆向最大匹配分词算法C#

    逆向顺序 句子:大家好我叫XX我是一名程序员 程序员 -> 序员 -> 员 名程序 -> 程序 -> 序 一名程 -> 名程 -> 程 是一名 -> 一名 - ...

  4. matplotlib 的几种风格 练习

    〇.准备数据 import numpy as np x = np.linspace(0, 5, 10) y = x ** 2 一.matlab风格的API 1.单图 from pylab import ...

  5. [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树

    4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...

  6. 20135220谈愈敏Blog7_可执行程序的装载

    可执行程序的装载 谈愈敏 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029000 一. ...

  7. warning: LF will be replaced by CRLF

    一. Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符.这个功能的选项如下: false - 不做任何检查warn - 在提交时检查并警告t ...

  8. WiFi QC 自动测试:Qt控制无线路由器

    在测试wifi的时候,测试人员一般要使用很多不同型号的AP,并且需要不断地切换Chariot的配置. 这里的思路是致力于提供一个友好的GUI界面来自动控制AP,并且自动控制Chariot进行Throu ...

  9. 秒杀9种排序算法(JavaScript版)

    一:你必须知道的 1> JS原型 2> 排序中的有序区和无序区 3> 二叉树的基本知识 如果你不知道上面三个东西,还是去复习一下吧,否则,看下面的东西有点吃力. 二:封装丑陋的原型方 ...

  10. [整理] ES5 词法约定文档树状图

    将ES5 词法说明整理为了树状图,方便查阅,请自行点开小图看大图: