濮阳杆衣贸易有限公司

主頁 > 知識庫 > 在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)

在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)

熱門標簽:南陽外呼系統(tǒng)定制化 百度ai地圖標注 申請400電話手續(xù) 同安公安400電話怎么申請流程 電話機器人軟件銷售工作 預(yù)測式外呼系統(tǒng)使用說明 合肥電銷外呼系統(tǒng)哪家公司做的好 玉林市機器人外呼系統(tǒng)哪家好 蘋果手機凱立德地圖標注
首先,創(chuàng)建一個SQLInjectionHelper類完成惡意代碼的檢查
代碼如下
復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
/// summary>
///SQLInjectionHelper 的摘要說明
/// /summary>
public class SQLInjectionHelper
{
/// summary>
/// 獲取Post的數(shù)據(jù)
/// /summary>
/// param name="request">/param>
/// returns>/returns>
public static bool ValidUrlData(string request)
{
bool result = false;
if (request == "POST")
{
for (int i = 0; i HttpContext.Current.Request.Form.Count; i++)
{
result = ValidData(HttpContext.Current.Request.Form[i].ToString());
if (result)
{
break;
}
}
}
else
{
for (int i = 0; i HttpContext.Current.Request.QueryString.Count; i++)
{
result = ValidData(HttpContext.Current.Request.QueryString[i].ToString());
if (result)
{
break;
}
}
}
return result;
}
/// summary>
/// 驗證是否存在注入代碼
/// /summary>
/// param name="inputData">/param>
/// returns>/returns>
private static bool ValidData(string inputData)
{
//驗證inputData是否包含惡意集合
if (Regex.IsMatch(inputData, GetRegexString()))
{
return true;
}
else
{
return false;
}
}
/// summary>
/// 獲取正則表達式
/// /summary>
/// returns>/returns>
private static string GetRegexString()
{
//構(gòu)造SQL的注入關(guān)鍵字符
string[] strChar = { "and", "exec", "insert", "select", "update", "delete", "count", "from", "drop", "asc", "or", "char", "%", ";", ":", "\'", """, "-", "chr", "master", "mid", "truncate", "declare", "char", "SiteName", "/add", "xp_cmdshell", "net user", "net localgroup administrators", "exec master.dbo.xp_cmdshell" };
string str_Regex = ".*(";
for (int i = 0; i strChar.Length - 1; i++)
{
str_Regex += strChar[i] + "|";
}
str_Regex += strChar[strChar.Length - 1] + ").*";
return str_Regex;
}
}

有此類后即可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件來實現(xiàn)表單或者URL提交數(shù)據(jù)的獲取,獲取后傳給SQLInjectionHelper類ValidUrlData方法來完成檢查
代碼如下
復制代碼 代碼如下:

protected void Application_BeginRequest(object sender, EventArgs e)
{
bool result = false;
result = SQLInjectionHelper.ValidUrlData(Request.RequestType.ToUpper());
if (result)
{
Response.Write("您提交的數(shù)據(jù)有惡意字符");
Response.End();
}
}

下面以一個小程序測試
創(chuàng)建一個頁面,如下
復制代碼 代碼如下:

%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
!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>
form id="form1" runat="server">
div>
asp:TextBox ID="TextBox1" runat="server">/asp:TextBox>
br />
asp:Button ID="btnPost" runat="server" Text="獲取Post數(shù)據(jù)"
onclick="btnPost_Click" />
/div>
asp:Button ID="btnGet" runat="server" Text="獲取Get數(shù)據(jù)" onclick="btnGet_Click" />
/form>
/body>
/html>

分別添加單擊事件,如下
復制代碼 代碼如下:

protected void btnPost_Click(object sender, EventArgs e)
{
}
protected void btnGet_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx?a=1b=2c=3");
}

在文本框中輸入非法字符串,無論post請求還是get請求,都會被防SQL注入程序所截獲

                      圖1 測試防SQL注入程序的頁面

                               圖2 錯誤信息

您可能感興趣的文章:
  • PHPCMS2008廣告模板SQL注入漏洞修復
  • Discuz7.2版的faq.php SQL注入漏洞分析
  • 對于ThinkPHP框架早期版本的一個SQL注入漏洞詳細分析
  • php is_numberic函數(shù)造成的SQL注入漏洞
  • php中sql注入漏洞示例 sql注入漏洞修復
  • PHP代碼網(wǎng)站如何防范SQL注入漏洞攻擊建議分享
  • 利用SQL注入漏洞拖庫的方法
  • 利用SQL注入漏洞登錄后臺的實現(xiàn)方法
  • SQL注入漏洞過程實例及解決方案

標簽:南京 臺州 南昌 南京 揚州 淄博 海南 嘉興

巨人網(wǎng)絡(luò)通訊聲明:本文標題《在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)》,本文關(guān)鍵詞  在,Global.asax,文件,里,實現(xiàn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)》相關(guān)的同類信息!
  • 本頁收集關(guān)于在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    哈尔滨市| 汉川市| 弥渡县| 军事| 西青区| 皋兰县| 湟中县| 隆昌县| 台州市| 鄂托克旗| 兰坪| 孝昌县| 建昌县| 江川县| 镇赉县| 独山县| 探索| 保康县| 谷城县| 高台县| 宜兰县| 宁安市| 新沂市| 上饶县| 清远市| 茂名市| 新兴县| 雅安市| 高要市| 嘉祥县| 福泉市| 江门市| 东阿县| 侯马市| 米脂县| 邳州市| 图片| 万载县| 罗山县| 镇安县| 武乡县|