php global.asax 病毒,在Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)...

首先,创建一个SQLInjectionHelper类完成恶意代码的检查

代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Text.RegularExpressions;

///

///SQLInjectionHelper 的摘要说明

///

public class SQLInjectionHelper

{

///

/// 获取Post的数据

///

///

///

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;

}

///

/// 验证是否存在注入代码

///

///

///

private static bool ValidData(string inputData)

{

//验证inputData是否包含恶意集合

if (Regex.IsMatch(inputData, GetRegexString()))

{

return true;

}

else

{

return false;

}

}

///

/// 获取正则表达式

///

///

private static string GetRegexString()

{

//构造SQL的注入关键字符

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)事件来实现表单或者URL提交数据的获取,获取后传给SQLInjectionHelper类ValidUrlData方法来完成检查

代码如下

protected void Application_BeginRequest(object sender, EventArgs e)

{

bool result = false;

result = SQLInjectionHelper.ValidUrlData(Request.RequestType.ToUpper());

if (result)

{

Response.Write("您提交的数据有恶意字符");

Response.End();

}

}

下面以一个小程序测试:

创建一个页面,如下

οnclick="btnPost_Click" />

分别添加单击事件,如下

protected void btnPost_Click(object sender, EventArgs e)

{

}

protected void btnGet_Click(object sender, EventArgs e)

{

Response.Redirect("Default.aspx?a=1&b=2&c=3");

}

在文本框中输入非法字符串,无论post请求还是get请求,都会被防SQL注入程序所截获

91635e2b9185df455d492a6da3bcfdc1.png

图1 测试防SQL注入程序的页面

ef8ee6468c5822378aab947ce0ec3e63.png

图2 错误信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值