最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

asp.net下檢測(cè)SQL注入式攻擊代碼

 更新時(shí)間:2009年09月18日 13:27:03   作者:  
防網(wǎng)站被攻擊代碼
兩個(gè)類:
(頁面數(shù)據(jù)校驗(yàn)類)PageValidate.cs 基本通用。
代碼如下:
復(fù)制代碼 代碼如下:

using System;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace Common
{
    /// <summary>
    /// 頁面數(shù)據(jù)校驗(yàn)類
    /// </summary>
    public class PageValidate
    {
        private static Regex RegNumber = new Regex("^[0-9]+$");
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等價(jià)于^[+-]?\d+[.]?\d+$
        private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或數(shù)字的字符串,和 [a-zA-Z0-9] 語法一樣
        private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");

        public PageValidate()
        {
        }


        #region 數(shù)字字符串檢查        

        /// <summary>
        /// 檢查Request查詢字符串的鍵值,是否是數(shù)字,最大長(zhǎng)度限制
        /// </summary>
        /// <param name="req">Request</param>
        /// <param name="inputKey">Request的鍵值</param>
        /// <param name="maxLen">最大長(zhǎng)度</param>
        /// <returns>返回Request查詢字符串</returns>
        public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
        {
            string retVal = string.Empty;
            if(inputKey != null && inputKey != string.Empty)
            {
                retVal = req.QueryString[inputKey];
                if(null == retVal)
                    retVal = req.Form[inputKey];
                if(null != retVal)
                {
                    retVal = SqlText(retVal, maxLen);
                    if(!IsNumber(retVal))
                        retVal = string.Empty;
                }
            }
            if(retVal == null)
                retVal = string.Empty;
            return retVal;
        }        
        /// <summary>
        /// 是否數(shù)字字符串
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            Match m = RegNumber.Match(inputData);
            return m.Success;
        }        
        /// <summary>
        /// 是否數(shù)字字符串 可帶正負(fù)號(hào)
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(string inputData)
        {
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }        
        /// <summary>
        /// 是否是浮點(diǎn)數(shù)
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            Match m = RegDecimal.Match(inputData);
            return m.Success;
        }        
        /// <summary>
        /// 是否是浮點(diǎn)數(shù) 可帶正負(fù)號(hào)
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(string inputData)
        {
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }        

        #endregion

        #region 中文檢測(cè)

        /// <summary>
        /// 檢測(cè)是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasCHZN(string inputData)
        {
            Match m = RegCHZN.Match(inputData);
            return m.Success;
        }    

        #endregion

        #region 郵件地址
        /// <summary>
        /// 是否是浮點(diǎn)數(shù) 可帶正負(fù)號(hào)
        /// </summary>
        /// <param name="inputData">輸入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }        

        #endregion

        #region 其他

        /// <summary>
        /// 檢查字符串最大長(zhǎng)度,返回指定長(zhǎng)度的串
        /// </summary>
        /// <param name="sqlInput">輸入字符串</param>
        /// <param name="maxLength">最大長(zhǎng)度</param>
        /// <returns></returns>            
        public static string SqlText(string sqlInput, int maxLength)
        {            
            if(sqlInput != null && sqlInput != string.Empty)
            {
                sqlInput = sqlInput.Trim();                            
                if(sqlInput.Length > maxLength)//按最大長(zhǎng)度截取字符串
                    sqlInput = sqlInput.Substring(0, maxLength);
            }
            return sqlInput;
        }        
        /// <summary>
        /// 字符串編碼
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static string HtmlEncode(string inputData)
        {
            return HttpUtility.HtmlEncode(inputData);
        }
        /// <summary>
        /// 設(shè)置Label顯示Encode的字符串
        /// </summary>
        /// <param name="lbl"></param>
        /// <param name="txtInput"></param>
        public static void SetLabel(Label lbl, string txtInput)
        {
            lbl.Text = HtmlEncode(txtInput);
        }
        public static void SetLabel(Label lbl, object inputObj)
        {
            SetLabel(lbl, inputObj.ToString());
        }        
        //字符串清理
        public static string InputText(string inputString, int maxLength)
        {            
            StringBuilder retVal = new StringBuilder();

            // 檢查是否為空
            if ((inputString != null) && (inputString != String.Empty))
            {
                inputString = inputString.Trim();

                //檢查長(zhǎng)度
                if (inputString.Length > maxLength)
                    inputString = inputString.Substring(0, maxLength);

                //替換危險(xiǎn)字符
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append(""");
                            break;
                        case '<':
                            retVal.Append("<");
                            break;
                        case '>':
                            retVal.Append(">");
                            break;
                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }                
                retVal.Replace("'", " ");// 替換單引號(hào)
            }
            return retVal.ToString();

        }
        /// <summary>
        /// 轉(zhuǎn)換成 HTML code
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Encode(string str)
        {            
            str = str.Replace("&","&");
            str = str.Replace("'","''");
            str = str.Replace("\"",""");
            str = str.Replace(" ","&nbsp;");
            str = str.Replace("<","<");
            str = str.Replace(">",">");
            str = str.Replace("\n","<br>");
            return str;
        }
        /// <summary>
        ///解析html成 普通文本
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Decode(string str)
        {            
            str = str.Replace("<br>","\n");
            str = str.Replace(">",">");
            str = str.Replace("<","<");
            str = str.Replace("&nbsp;"," ");
            str = str.Replace(""","\"");
            return str;
        }

        #endregion 

    }
}

通用文件(Global.asax),保存為Global.asax文件名 放到網(wǎng)站根木馬下即可。(其他功能自行補(bǔ)上)
復(fù)制代碼 代碼如下:

<script language="C#" runat="server"><!--
    protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            StartProcessRequest();
        }


/// <summary>
/// 處理用戶提交的請(qǐng)求
/// </summary>
private void StartProcessRequest()
{
try
{
string getkeys = "";

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.Write("Get,出現(xiàn)錯(cuò)誤,包含非法字符串");
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.Write("Post,出現(xiàn)錯(cuò)誤,包含非法字符串");
System.Web.HttpContext.Current.Response.End();
}
}
}
            if(System.Web.HttpContext.Current.Request.Cookies!=null)
            {
             for (int i = 0; i < System.Web.HttpContext.Current.Request.Cookies.Count; i++)
{
getkeys = System.Web.HttpContext.Current.Request.Cookies.Keys[i];
if (getkeys == "__VIEWSTATE") continue;
if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Cookies[getkeys].Value))
{
System.Web.HttpContext.Current.Response.Write("Cookies,出現(xiàn)錯(cuò)誤,包含非法字符串");
System.Web.HttpContext.Current.Response.End();
}
}
            }

}
catch
{
// 錯(cuò)誤處理: 處理用戶提交信息!
}
}
/// <summary>
/// 分析用戶請(qǐng)求是否正常
/// </summary>
/// <param name="Str">傳入用戶提交數(shù)據(jù) </param>
/// <returns>返回是否含有SQL注入式攻擊代碼 </returns>
private bool ProcessSqlStr(string Str)
{
bool ReturnValue = true;
try
{
if (Str.Trim() != "")
{
                string SqlStr = "select¦insert¦delete¦update¦declare¦sysobjects¦syscolumns¦cast¦truncate¦master¦mid¦exec";

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

// --></script>

相關(guān)文章

  • ASP.NET服務(wù)器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值用法

    ASP.NET服務(wù)器端控件RadioButtonList,DropDownList,CheckBoxList的取值、賦值

    這三個(gè)控件都有一個(gè)Items集合,可以用 RepeatLayout 和 RepeatDirection 屬性來控制列表的呈現(xiàn)形式
    2013-10-10
  • Asp.Net 音頻文件上傳和播放代碼

    Asp.Net 音頻文件上傳和播放代碼

    在網(wǎng)上找到一個(gè)名叫AspNetPager的第三方控件,將AspNetPager.dll文件引用到項(xiàng)目的Bin中。在網(wǎng)頁中可直接調(diào)用。
    2010-05-05
  • .NET 某券商論壇系統(tǒng)卡死問題分析

    .NET 某券商論壇系統(tǒng)卡死問題分析

    前幾個(gè)月有位朋友找到我,說他們的的web程序沒有響應(yīng)了,而且監(jiān)控發(fā)現(xiàn)線程數(shù)特別高,內(nèi)存也特別大,讓我?guī)兔匆幌略趺椿厥?現(xiàn)在回過頭來幾經(jīng)波折,回味價(jià)值太濃了,本文給大家分享.NET 某券商論壇系統(tǒng)卡死問題分析,感興趣的朋友跟隨小編一起看看吧
    2024-11-11
  • asp.net中調(diào)用Office來制作3D統(tǒng)計(jì)圖的實(shí)例代碼

    asp.net中調(diào)用Office來制作3D統(tǒng)計(jì)圖的實(shí)例代碼

    這篇文章介紹了asp.net中調(diào)用Office來制作3D統(tǒng)計(jì)圖的實(shí)例代碼,有需要的朋友可以參考一下
    2013-11-11
  • .NET8 依賴注入

    .NET8 依賴注入

    依賴注入是一種設(shè)計(jì)模式,用于解耦組件(服務(wù))之間的依賴關(guān)系,它通過將依賴關(guān)系的創(chuàng)建和管理交給外部容器來實(shí)現(xiàn),而不是在組件(服務(wù))內(nèi)部直接創(chuàng)建依賴對(duì)象,本文介紹.NET8 依賴注入的相關(guān)知識(shí),感興趣的朋友一起看看吧
    2023-12-12
  • asp.net core 屬性路由和約定路由的實(shí)現(xiàn)

    asp.net core 屬性路由和約定路由的實(shí)現(xiàn)

    ASP.NET Core中的WebAPI路由用于映射客戶端請(qǐng)求的URL到服務(wù)器端的控制器和操作方法,路由分為基于屬性的路由和約定路由兩種方式,本文就來介紹一下,感興趣的可以了解一下
    2025-01-01
  • asp.net 8 服務(wù)器爆滿的解決過程

    asp.net 8 服務(wù)器爆滿的解決過程

    如果遇到"服務(wù)器爆滿"的問題,通常是指服務(wù)器無法處理更多的請(qǐng)求,可能是因?yàn)橘Y源限制、并發(fā)連接數(shù)太多或者服務(wù)器配置不當(dāng),檢查服務(wù)器資源:確保服務(wù)器有足夠的CPU、內(nèi)存和帶寬資源來處理請(qǐng)求,調(diào)整Kestrel配置,可以在Program.cs或Startup.cs中配置最大并發(fā)連接數(shù)
    2024-05-05
  • ASP.NET AJAX 1.0 RC開發(fā)10分鐘圖解

    ASP.NET AJAX 1.0 RC開發(fā)10分鐘圖解

    12月15日,ASP.NET AJAX 1.0 RC版發(fā)布,我下載安裝試用了一下,沒有寫一行代碼,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的AJAX應(yīng)用,以下為截圖說明。
    2008-03-03
  • ASP.NET緩存 方法和最佳實(shí)踐

    ASP.NET緩存 方法和最佳實(shí)踐

    在 ASP.NET 提供的許多特性中,緩存支持無疑是我最欣賞的特性,我這樣說當(dāng)然是有充分理由的。
    2010-06-06
  • ASP.NET中repeater嵌套實(shí)現(xiàn)代碼(附源碼)

    ASP.NET中repeater嵌套實(shí)現(xiàn)代碼(附源碼)

    repeater嵌套經(jīng)常會(huì)在一些特殊效果顯示下會(huì)用到,新手朋友們可以詳細(xì)看下本文,希望對(duì)你有所幫助,代碼很整潔同時(shí)附有源碼
    2013-03-03

最新評(píng)論

达拉特旗| 肇源县| 新沂市| 余江县| 盖州市| 年辖:市辖区| 贺州市| 泸定县| 朝阳市| 马关县| 永吉县| 寻乌县| 长寿区| 安塞县| 乌拉特后旗| 南投县| 铜山县| 长兴县| 岳阳县| 茶陵县| 申扎县| 东方市| 永州市| 青岛市| 丹江口市| 仁怀市| 宾阳县| 九龙坡区| 德格县| 衡阳市| 利辛县| 建德市| 云和县| 从化市| 苏尼特右旗| 新巴尔虎右旗| 上栗县| 孝昌县| 沧州市| 石泉县| 九江县|