Linux防OS注入过滤特殊字符,过滤网址和输入框中的特殊字符,防止sql注入

该博客文章介绍了一个ASP.NET IHttpModule实现,用于检查请求参数中是否存在潜在的SQL注入攻击字符串。通过检查请求的QueryStrings和Form变量,并对比配置文件中的过滤字符串列表,来阻止含有特定关键字的请求,从而增强应用程序的安全性。当检测到可能的SQL注入尝试时,系统会返回错误消息并终止响应。
摘要由CSDN通过智能技术生成

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

/// 

///cedar 的摘要说明

/// 

public class cedar:IHttpModule

{

public cedar()

{

//

//TODO: 在此处添加构造函数逻辑

//

}

public void Dispose()

{

}

public void Init(HttpApplication application)

{

application.AcquireRequestState += new EventHandler(application_AcquireRequestState);

}

private void application_AcquireRequestState(object sender, EventArgs e)

{

HttpContext content = ((HttpApplication)sender).Context;

try

{

string sqlErrorPage = "default.html";//转到默认页面

string keyValue = string.Empty;

string requestUrl = content.Request.Path.ToString();

if (content.Request.QueryString != null)

{

foreach (string val in content.Request.QueryString)

{

keyValue= content.Server.UrlDecode(content.Request.QueryString[val]);

if (!processSqlStr(keyValue))

{

content.Response.Write("您访问的页面发生错误,此问题我们已经记录并尽快改善,请稍后再试。转到首页");

content.Response.End();

break;

}

}

}

if (content.Request.Form != null)

{

foreach(string val in content.Request.Form)

{

keyValue = content.Server.HtmlDecode(content.Request.Form[val]);

if (keyValue == "_ViEWSTATE") continue;

if (!processSqlStr(keyValue))

{

content.Response.Write("您访问的页面发生错误,此问题我们已经记录并尽快改善,请稍后再试。");

content.Response.End();

break;

}

}

}

}

catch (Exception ex)

{

}

}

private bool processSqlStr(string str)

{

bool returnValue = true;

try

{

if (str.Trim() != "")

{

//取得webconfig中过滤字符串

string sqlStr = ConfigurationManager.AppSettings["FilterSql"].Trim();

//string sqlStr = "declare |exec|varchar |cursor |begin |open |drop |creat |select |truncate";

string[] sqlStrs = sqlStr.Split('|');

foreach (string ss in sqlStrs)

{

if (str.ToLower().IndexOf(ss) >= 0)

{

sqlStr = ss;

returnValue = false;

break;

}

}

}

}

catch

{

returnValue = false;

}

return returnValue;

}

}

在web.config中添加以下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值